From febef72a55aa8c119b05820f8c19eb669c8b1000 Mon Sep 17 00:00:00 2001 From: massimim <57805133+massimim@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:26:03 +0200 Subject: [PATCH] Refactoring for warp --- libNeonPy/include/Neon/py/backend.h | 41 +++++----- libNeonPy/src/Neon/py/backend.cpp | 72 +++++++++-------- libNeonSet/src/set/DevSet.cpp | 5 ++ py_neon/{dense => }/allocationCounter.py | 0 py_neon/backend.py | 97 ++++++++++++++++++++++ py_neon/block/__init__.py | 0 py_neon/{dense => block}/bField.py | 0 py_neon/{dense => block}/bGrid.py | 0 py_neon/{dense => block}/bPartition.py | 0 py_neon/{dense => block}/bSpan.py | 0 py_neon/dense/__init__.py | 18 ++--- py_neon/dense/backend.py | 98 ----------------------- py_neon/dense/dGrid.py | 29 ++++--- py_neon/dense/dSpan.py | 2 +- py_neon/multires/__init__.py | 0 py_neon/{dense => multires}/mField.py | 0 py_neon/{dense => multires}/mGrid.py | 0 py_neon/{dense => multires}/mPartition.py | 0 18 files changed, 188 insertions(+), 174 deletions(-) rename py_neon/{dense => }/allocationCounter.py (100%) create mode 100644 py_neon/backend.py create mode 100644 py_neon/block/__init__.py rename py_neon/{dense => block}/bField.py (100%) rename py_neon/{dense => block}/bGrid.py (100%) rename py_neon/{dense => block}/bPartition.py (100%) rename py_neon/{dense => block}/bSpan.py (100%) delete mode 100644 py_neon/dense/backend.py create mode 100644 py_neon/multires/__init__.py rename py_neon/{dense => multires}/mField.py (100%) rename py_neon/{dense => multires}/mGrid.py (100%) rename py_neon/{dense => multires}/mPartition.py (100%) diff --git a/libNeonPy/include/Neon/py/backend.h b/libNeonPy/include/Neon/py/backend.h index 1ee3afbd..09a5a391 100644 --- a/libNeonPy/include/Neon/py/backend.h +++ b/libNeonPy/include/Neon/py/backend.h @@ -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. */ diff --git a/libNeonPy/src/Neon/py/backend.cpp b/libNeonPy/src/Neon/py/backend.cpp index c3593400..351d5caa 100644 --- a/libNeonPy/src/Neon/py/backend.cpp +++ b/libNeonPy/src/Neon/py/backend.cpp @@ -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; @@ -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 vec(devIds, devIds + numDevices); - auto backendPtr = new (std::nothrow) Neon::Backend(std::vector(*devIds), Neon::Runtime(runtime)); + auto backendPtr = new (std::nothrow) Neon::Backend(vec, Neon::Runtime(runtime)); AllocationCounter::Allocation(); return backend_constructor_epilogue(handle, backendPtr); diff --git a/libNeonSet/src/set/DevSet.cpp b/libNeonSet/src/set/DevSet.cpp index ef1c037d..87b6a46b 100644 --- a/libNeonSet/src/set/DevSet.cpp +++ b/libNeonSet/src/set/DevSet.cpp @@ -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); } } @@ -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()< 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') diff --git a/py_neon/block/__init__.py b/py_neon/block/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/py_neon/dense/bField.py b/py_neon/block/bField.py similarity index 100% rename from py_neon/dense/bField.py rename to py_neon/block/bField.py diff --git a/py_neon/dense/bGrid.py b/py_neon/block/bGrid.py similarity index 100% rename from py_neon/dense/bGrid.py rename to py_neon/block/bGrid.py diff --git a/py_neon/dense/bPartition.py b/py_neon/block/bPartition.py similarity index 100% rename from py_neon/dense/bPartition.py rename to py_neon/block/bPartition.py diff --git a/py_neon/dense/bSpan.py b/py_neon/block/bSpan.py similarity index 100% rename from py_neon/dense/bSpan.py rename to py_neon/block/bSpan.py diff --git a/py_neon/dense/__init__.py b/py_neon/dense/__init__.py index 196f94a5..1b0b54d4 100644 --- a/py_neon/dense/__init__.py +++ b/py_neon/dense/__init__.py @@ -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 \ No newline at end of file diff --git a/py_neon/dense/backend.py b/py_neon/dense/backend.py deleted file mode 100644 index ef60dd94..00000000 --- a/py_neon/dense/backend.py +++ /dev/null @@ -1,98 +0,0 @@ -import copy -import ctypes -from enum import Enum - -import py_neon -from py_neon.execution import Execution -from py_neon import Py_neon -from py_neon.dataview import DataView - - -class Backend(object): - - class Runtime(Enum): - none = 0 - system = 0 - stream = 1 - openmp = 2 - - def __init__(self, arg1 = None, arg2 = None): - 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(arg1, arg2) - - 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_new3.argtypes = [self.py_neon.handle_type, - ctypes.POINTER(ctypes.c_int), - ctypes.c_int] - self.py_neon.lib.dBackend_new3.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, arg1=None, arg2=None): - if self.handle == 0: - raise Exception('DBackend: Invalid handle') - - if arg1 is None and arg2 is None: - # Call the empty constructor - res = self.py_neon.lib.dBackend_new1(ctypes.byref(self.handle)) - elif isinstance(arg1, int) and isinstance(arg2, Backend.Runtime): - # Call the constructor with nGpus and runtime - res = self.py_neon.lib.dBackend_new2(ctypes.byref(self.handle), arg1, arg2.value) - elif isinstance(arg1, list) and isinstance(arg2, Backend.Runtime): - # Call the constructor with devIds and runtime - from ctypes import c_int - - # Convert the list to a ctypes array - dev_ids = (c_int * len(arg1))(*arg1) - res = self.py_neon.lib.dBackend_new3(ctypes.byref(self.handle), dev_ids, arg2.value) - else: - raise Exception('DBackend: Invalid arguments provided') - - 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') - \ No newline at end of file diff --git a/py_neon/dense/dGrid.py b/py_neon/dense/dGrid.py index 820cda1f..a1bfa938 100644 --- a/py_neon/dense/dGrid.py +++ b/py_neon/dense/dGrid.py @@ -8,7 +8,7 @@ from py_neon import Py_neon from py_neon.dataview import DataView from .dSpan import dSpan -from .backend import Backend +from py_neon.backend import Backend from py_neon.index_3d import Index_3d import numpy as np @@ -19,21 +19,30 @@ class dGrid(object): def __init__(self, backend = None, dim = None): self.handle: ctypes.c_uint64 = ctypes.c_uint64(0) - self.backend = backend if backend is not None else Backend() # @TODOMATT if the input is not the correct type, throw an error - self.dim = dim if dim is not None else Index_3d(10, 10, 10) # @TODOMATT if the input is not the correct type, throw an error + self.backend = backend + self.dim = dim + + if backend is None: + # rise exception + raise Exception('dGrid: backend pamrameter is missing') + if dim is None: + # raise exception + raise Exception('dGrid: dim parameter is missing') + 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_grid_new() + + self._help_load_api() + self._help_grid_new() def __del__(self): - if self.handle.value != 0: - self.help_grid_delete() + if self.handle != 0: + self._help_grid_delete() - def help_load_api(self): + def _help_load_api(self): # grid_new # self.py_neon.lib.dGrid_new.argtypes = [self.py_neon.handle_type, @@ -69,7 +78,7 @@ def help_load_api(self): self.py_neon.lib.dGrid_is_inside_domain.restype = ctypes.c_bool - def help_grid_new(self): + def _help_grid_new(self): if self.backend.handle.value == 0: # Check backend handle validity raise Exception('DGrid: Invalid backend handle') @@ -86,7 +95,7 @@ def help_grid_new(self): raise Exception('DGrid: Failed to initialize grid') print(f"Grid initialized with handle {self.handle.value}") - def help_grid_delete(self): + def _help_grid_delete(self): if self.py_neon.lib.dGrid_delete(ctypes.byref(self.handle)) != 0: raise Exception('Failed to delete grid') diff --git a/py_neon/dense/dSpan.py b/py_neon/dense/dSpan.py index 5a9772e9..01714dd2 100644 --- a/py_neon/dense/dSpan.py +++ b/py_neon/dense/dSpan.py @@ -37,7 +37,7 @@ def get_member_field_offsets(self): def __str__(self): def get_offset(field_name): - return ctypes.offsetof(dSpan, field_name) + return "ctypes.(dSpan, field_name)" str = f"" str += f"\n\tdataView: {self.dataView} (offset: {get_offset('dataView')})" diff --git a/py_neon/multires/__init__.py b/py_neon/multires/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/py_neon/dense/mField.py b/py_neon/multires/mField.py similarity index 100% rename from py_neon/dense/mField.py rename to py_neon/multires/mField.py diff --git a/py_neon/dense/mGrid.py b/py_neon/multires/mGrid.py similarity index 100% rename from py_neon/dense/mGrid.py rename to py_neon/multires/mGrid.py diff --git a/py_neon/dense/mPartition.py b/py_neon/multires/mPartition.py similarity index 100% rename from py_neon/dense/mPartition.py rename to py_neon/multires/mPartition.py