Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring for warp #52

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions libNeonPy/include/Neon/py/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,34 @@
#include "Neon/set/Backend.h"


/**
* Empty constructor
*/
extern "C" auto dBackend_new1(
uint64_t& handle)
-> int;

/**
* Creating a Backend object with the first nGpus devices.
*/
extern "C" auto dBackend_new2(
uint64_t& handle,
int nGpus /*! Number of devices. The devices are selected in the order specifies by CUDA */,
int runtime /*! Type of runtime to use */)
-> int;
///**
// * Empty constructor
// */
//extern "C" auto dBackend_new1(
// uint64_t& handle)
// -> int;
//
///**
// * Creating a Backend object with the first nGpus devices.
// */
//extern "C" auto dBackend_new2(
// uint64_t& handle,
// int nGpus /*! Number of devices. The devices are selected in the order specifies by CUDA */,
// int runtime /*! Type of runtime to use */)
// -> int;

/**
*
*/
extern "C" auto dBackend_new3(
extern "C" auto dBackend_new(
uint64_t& handle,
const int* devIds /*! Vectors of device ids. There are CUDA device ids */,
int runtime /*! Type of runtime to use */)
int runtime /*! Type of runtime to use */,
int numDecices /*! Number of devices */,
const int* devIds /*! Vectors of device ids. There are CUDA device ids */
)
-> int;





/**
* Delete a backend object on the heap.
*/
Expand Down
72 changes: 37 additions & 35 deletions libNeonPy/src/Neon/py/backend.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Neon/py/backend.h"
#include "Neon/set/Backend.h"
#include "Neon/py/AllocationCounter.h"

#include "Neon/Neon.h"
void backend_constructor_prologue(uint64_t& handle) {
std::cout << "dBackend_new - BEGIN" << std::endl;
std::cout << "dBackend handle" << handle << std::endl;
Expand All @@ -18,46 +18,48 @@ int backend_constructor_epilogue(uint64_t& handle, Neon::Backend* backendPtr) {
return 0;
}

auto dBackend_new1(
uint64_t& handle)
-> int
{
std::cout << "first constructor" << std::endl;
AllocationCounter::Allocation();

backend_constructor_prologue(handle);

auto backendPtr = new (std::nothrow) Neon::Backend();

return backend_constructor_epilogue(handle, backendPtr);
}

auto dBackend_new2(
//auto dBackend_new1(
// uint64_t& handle)
// -> int
//{
// std::cout << "first constructor" << std::endl;
// AllocationCounter::Allocation();
//
// backend_constructor_prologue(handle);
//
// auto backendPtr = new (std::nothrow) Neon::Backend();
//
// return backend_constructor_epilogue(handle, backendPtr);
//}
//
//auto dBackend_new2(
// uint64_t& handle,
// int nGpus,
// int runtime)
// -> int
//{
// std::cout << "second constructor" << std::endl;
// AllocationCounter::Allocation();
// backend_constructor_prologue(handle);
//
// auto backendPtr = new (std::nothrow) Neon::Backend(nGpus, Neon::Runtime(runtime));
//
// return backend_constructor_epilogue(handle, backendPtr);
//}

auto dBackend_new(
uint64_t& handle,
int nGpus,
int runtime)
int runtime,
int numDevices,
const int* devIds)
-> int
{
std::cout << "second constructor" << std::endl;
AllocationCounter::Allocation();
Neon::init();
backend_constructor_prologue(handle);

auto backendPtr = new (std::nothrow) Neon::Backend(nGpus, Neon::Runtime(runtime));

return backend_constructor_epilogue(handle, backendPtr);
}

auto dBackend_new3(
uint64_t& handle,
const int* devIds,
int runtime)
-> int
{
std::cout << "third constructor" << std::endl;

backend_constructor_prologue(handle);
std::vector<int> vec(devIds, devIds + numDevices);

auto backendPtr = new (std::nothrow) Neon::Backend(std::vector<int>(*devIds), Neon::Runtime(runtime));
auto backendPtr = new (std::nothrow) Neon::Backend(vec, Neon::Runtime(runtime));
AllocationCounter::Allocation();

return backend_constructor_epilogue(handle, backendPtr);
Expand Down
5 changes: 5 additions & 0 deletions libNeonSet/src/set/DevSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ auto DevSet::set(const Neon::DeviceType& devType,
for (auto&& invalidID : invilidIdsVec) {
exp << invalidID << " ";
}
exp << "valid ids are:\n";
for (auto&& invalidID : invilidIdsVec) {
exp << invalidID << " ";
}
NEON_THROW(exp);
}
}
Expand Down Expand Up @@ -168,6 +172,7 @@ auto DevSet::validateIds()
if (m_devType == Neon::DeviceType::CUDA) {
for (auto&& gpuId : this->m_devIds) {
if (gpuId.idx() >= Neon::sys::globalSpace::gpuSysObj().numDevs()) {
std:: cout << "Neon::sys::globalSpace::gpuSysObj().numDevs() "<< Neon::sys::globalSpace::gpuSysObj().numDevs()<<std::endl;
invalidIds.push_back(gpuId);
}
}
Expand Down
File renamed without changes.
97 changes: 97 additions & 0 deletions py_neon/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import ctypes
from enum import Enum
from typing import List

import numpy as np

from py_neon import Py_neon


class Backend(object):
class Runtime(Enum):
none = 0
system = 0
stream = 1
openmp = 2

def __init__(self,
runtime: Runtime = Runtime.openmp,
n_dev: int = 1,
dev_idx_list: List[int] = [0]):

self.handle: ctypes.c_uint64 = ctypes.c_uint64(0)
try:
self.py_neon: Py_neon = Py_neon()
except Exception as e:
self.handle: ctypes.c_uint64 = ctypes.c_uint64(0)
raise Exception('Failed to initialize PyNeon: ' + str(e))
self.help_load_api()
self.help_backend_new(runtime, n_dev, dev_idx_list)

def __del__(self):
if self.handle == 0:
return
self.help_backend_delete()

def help_load_api(self):

# # backend_new
# self.py_neon.lib.dBackend_new1.argtypes = [self.py_neon.handle_type]
# self.py_neon.lib.dBackend_new1.restype = ctypes.c_int
#
# # backend_new
# self.py_neon.lib.dBackend_new2.argtypes = [self.py_neon.handle_type,
# ctypes.c_int,
# ctypes.c_int]
# self.py_neon.lib.dBackend_new2.restype = ctypes.c_int

# backend_new
self.py_neon.lib.dBackend_new.argtypes = [self.py_neon.handle_type,
ctypes.c_int,
ctypes.c_int,
ctypes.POINTER(ctypes.c_int)]

self.py_neon.lib.dBackend_new.restype = ctypes.c_int

# backend_delete
self.py_neon.lib.dBackend_delete.argtypes = [self.py_neon.handle_type]
self.py_neon.lib.dBackend_delete.restype = ctypes.c_int

# backend_get_string
self.py_neon.lib.dBackend_get_string.argtypes = [self.py_neon.handle_type]
self.py_neon.lib.dBackend_get_string.restype = ctypes.c_char_p

# TODOMATT get num devices
# TODOMATT get device type

def help_backend_new(self,
runtime: Runtime,
n_dev: int,
dev_idx_list: List[int]):
if self.handle.value != ctypes.c_uint64(0).value:
raise Exception(f'DBackend: Invalid handle {self.handle}')

if n_dev > len(dev_idx_list):
dev_idx_list = list(range(n_dev))
else:
n_dev = len(dev_idx_list)

dev_idx_np = np.array(dev_idx_list, dtype=int)
dev_idx_ptr = dev_idx_np.ctypes.data_as(ctypes.POINTER(ctypes.c_int))

res = self.py_neon.lib.dBackend_new(ctypes.byref(self.handle),
runtime.value,
n_dev,
dev_idx_ptr)
if res != 0:
raise Exception('DBackend: Failed to initialize backend')

def help_backend_delete(self):
if self.handle == 0:
return
res = self.py_neon.lib.dBackend_delete(self.handle)
if res != 0:
raise Exception('Failed to delete backend')

def __str__(self):
return ctypes.cast(self.py_neon.lib.get_string(self.handle), ctypes.c_char_p).value.decode('utf-8')
Empty file added py_neon/block/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions py_neon/dense/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from .backend import Backend
from py_neon.backend import Backend
from .dGrid import dGrid
from .dField import dField
from .dSpan import dSpan
from .dPartition import dPartitionInt
from .bGrid import bGrid
from .bField import bField
from .bSpan import bSpan
from .bPartition import bPartitionInt
from .mGrid import mGrid
from .mField import mField
from .mPartition import mPartitionInt
from .allocationCounter import allocationCounter
# from .bGrid import bGrid
# from .bField import bField
# from .bSpan import bSpan
# from .bPartition import bPartitionInt
# from .mGrid import mGrid
# from .mField import mField
# from .mPartition import mPartitionInt
from py_neon.allocationCounter import allocationCounter
# from .partition import PartitionInt
98 changes: 0 additions & 98 deletions py_neon/dense/backend.py

This file was deleted.

Loading
Loading