diff --git a/libNeonPy/include/Neon/py/bGrid.h b/libNeonPy/include/Neon/py/bGrid.h index 2689923a..148f5c2a 100644 --- a/libNeonPy/include/Neon/py/bGrid.h +++ b/libNeonPy/include/Neon/py/bGrid.h @@ -5,11 +5,12 @@ /** * Initialize a new grid object on the heap. * NOTE: some parameters are still not exposed - */ /* TODOMATT fix the constructor to have correct arguments */ + */ extern "C" auto bGrid_new( uint64_t& handle, uint64_t& backendPtr, - const Neon::index_3d* dim) + const Neon::int32_3d* dim, + int* sparsity_pattern) -> int; /** diff --git a/libNeonPy/include/Neon/py/mGrid.h b/libNeonPy/include/Neon/py/mGrid.h index fd69d90b..fb0c1eeb 100644 --- a/libNeonPy/include/Neon/py/mGrid.h +++ b/libNeonPy/include/Neon/py/mGrid.h @@ -10,9 +10,11 @@ extern "C" auto mGrid_new( uint64_t& handle, uint64_t& backendPtr, const Neon::index_3d* dim, + int* sparsity_pattern, uint32_t depth) -> int; + /** * Delete a grid object on the heap. */ diff --git a/libNeonPy/src/Neon/py/bGrid.cpp b/libNeonPy/src/Neon/py/bGrid.cpp index ff006ed1..b1425d05 100644 --- a/libNeonPy/src/Neon/py/bGrid.cpp +++ b/libNeonPy/src/Neon/py/bGrid.cpp @@ -6,7 +6,8 @@ auto bGrid_new( uint64_t& handle, uint64_t& backendPtr, - const Neon::index_3d* dim) + const Neon::index_3d* dim, + int* sparsity_pattern) -> int { std::cout << "bGrid_new - BEGIN" << std::endl; @@ -23,7 +24,7 @@ auto bGrid_new( } Neon::domain::Stencil d3q19 = Neon::domain::Stencil::s19_t(false); - Grid g(*backend, *dim, [](Neon::index_3d const& /*idx*/) { return true; }, d3q19); + Grid g(*backend, *dim, [=](Neon::index_3d const& idx) { return sparsity_pattern[idx.x * (dim->x * dim->y) + idx.y * dim->z + idx.z ]; }, d3q19); auto gridPtr = new (std::nothrow) Grid(g); AllocationCounter::Allocation(); diff --git a/libNeonPy/src/Neon/py/mGrid.cpp b/libNeonPy/src/Neon/py/mGrid.cpp index 3d2ed43b..b4521e8a 100644 --- a/libNeonPy/src/Neon/py/mGrid.cpp +++ b/libNeonPy/src/Neon/py/mGrid.cpp @@ -6,6 +6,7 @@ auto mGrid_new( uint64_t& handle, uint64_t& backendPtr, const Neon::index_3d* dim, + int* sparsity_pattern, uint32_t depth) -> int { @@ -24,7 +25,7 @@ auto mGrid_new( Neon::domain::Stencil d3q19 = Neon::domain::Stencil::s19_t(false); // @TODOMATT define/use a multiresolution constructor for Grid g (talk to max about this) - Grid g(*backend, *dim, std::vector>{[](Neon::index_3d const& /*idx*/) { return true; }}, d3q19, Grid::Descriptor(depth)); + Grid g(*backend, *dim, std::vector>{[=](Neon::index_3d const& idx) { return sparsity_pattern[idx.x * (dim->x * dim->y) + idx.y * dim->z + idx.z ]; }}, d3q19, Grid::Descriptor(depth)); auto gridPtr = new (std::nothrow) Grid(g); AllocationCounter::Allocation(); diff --git a/py_neon/block/bGrid.py b/py_neon/block/bGrid.py index ac95c7b1..b2ab1b2a 100644 --- a/py_neon/block/bGrid.py +++ b/py_neon/block/bGrid.py @@ -10,19 +10,32 @@ from .bSpan import bSpan from ..backend import Backend from py_neon.index_3d import Index_3d +import numpy as np class bGrid(object): - def __init__(self, backend = None, dim = None): + def __init__(self, backend = None, dim = None, sparsity_pattern: np.ndarray = None): + if sparsity_pattern is None: + sparsity_pattern = np.ones((dim.x,dim.y,dim.z)) + if backend is None: + # raise exception + raise Exception('dGrid: backend pamrameter is missing') + if sparsity_pattern.shape[0] != dim.x or sparsity_pattern.shape[1] != dim.y or sparsity_pattern.shape[2] != dim.z: + raise Exception('dGrid: sparsity_pattern\'s shape does not match the dim') + self.handle: ctypes.c_uint64 = ctypes.c_uint64(0) self.backend = backend self.dim = dim + self.sparsity_pattern = sparsity_pattern + + 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() @@ -36,7 +49,8 @@ def _help_load_api(self): # grid_new self.py_neon.lib.bGrid_new.argtypes = [self.py_neon.handle_type, self.py_neon.handle_type, - ctypes.POINTER(py_neon.Index_3d)] + ctypes.POINTER(py_neon.Index_3d), + ctypes.POINTER(ctypes.c_int)] self.py_neon.lib.bGrid_new.restype = ctypes.c_int # grid_delete @@ -69,17 +83,17 @@ def _help_load_api(self): def _help_grid_new(self): - if self.handle == 0: - raise Exception('bGrid: Invalid handle') + if self.backend.handle.value == 0: # Check backend handle validity + raise Exception('bGrid: Invalid backend handle') - if self.backend is None: - self.backend = Backend() - if self.dim is None: - self.dim = py_neon.Index_3d(10,10,10) - - res = self.py_neon.lib.bGrid_new(ctypes.byref(self.handle), ctypes.byref(self.backend.handle), self.dim) + if self.handle.value != 0: # Ensure the grid handle is uninitialized + raise Exception('bGrid: Grid handle already initialized') + + sparsity_pattern_array = self.sparsity_pattern.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) + res = self.py_neon.lib.bGrid_new(ctypes.byref(self.handle), ctypes.byref(self.backend.handle), self.dim, sparsity_pattern_array) if res != 0: raise Exception('bGrid: Failed to initialize grid') + print(f"bGrid initialized with handle {self.handle.value}") def _help_grid_delete(self): if self.py_neon.lib.bGrid_delete(ctypes.byref(self.handle)) != 0: diff --git a/py_neon/dense/dGrid.py b/py_neon/dense/dGrid.py index b6d5e051..709028ee 100644 --- a/py_neon/dense/dGrid.py +++ b/py_neon/dense/dGrid.py @@ -16,7 +16,7 @@ class dGrid(object): - def __init__(self, backend: Backend = None, dim: Index_3d = Index_3d(10,10,10), sparsity_pattern: np.ndarray = None): # @TODOMATT implement psarsity pattern + def __init__(self, backend: Backend = None, dim: Index_3d = Index_3d(10,10,10), sparsity_pattern: np.ndarray = None): if sparsity_pattern is None: sparsity_pattern = np.ones((dim.x,dim.y,dim.z)) if backend is None: @@ -46,10 +46,6 @@ def __del__(self): def _help_load_api(self): - # grid_new - # self.py_neon.lib.dGrid_new.argtypes = [self.py_neon.handle_type, - # self.py_neon.handle_type, - # py_neon.Index_3d] self.py_neon.lib.dGrid_new.argtypes = [self.py_neon.handle_type, self.py_neon.handle_type, ctypes.POINTER(py_neon.Index_3d), @@ -87,19 +83,16 @@ def _help_load_api(self): def _help_grid_new(self): if self.backend.handle.value == 0: # Check backend handle validity - raise Exception('DGrid: Invalid backend handle') + raise Exception('dGrid: Invalid backend handle') if self.handle.value != 0: # Ensure the grid handle is uninitialized - raise Exception('DGrid: Grid handle already initialized') + raise Exception('dGrid: Grid handle already initialized') - print(f"Initializing grid with handle {self.handle.value} and backend handle {self.backend.handle.value}") - sys.stdout.flush() # Ensure the print statement is flushed to the console - sparsity_pattern_array = self.sparsity_pattern.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) res = self.py_neon.lib.dGrid_new(ctypes.byref(self.handle), ctypes.byref(self.backend.handle), self.dim, sparsity_pattern_array) if res != 0: - raise Exception('DGrid: Failed to initialize grid') - print(f"Grid initialized with handle {self.handle.value}") + raise Exception('dGrid: Failed to initialize grid') + print(f"dGrid initialized with handle {self.handle.value}") def _help_grid_delete(self): if self.py_neon.lib.dGrid_delete(ctypes.byref(self.handle)) != 0: diff --git a/py_neon/multires/mGrid.py b/py_neon/multires/mGrid.py index bc2e2f09..642230fd 100644 --- a/py_neon/multires/mGrid.py +++ b/py_neon/multires/mGrid.py @@ -10,20 +10,33 @@ from ..block.bSpan import bSpan from ..backend import Backend from py_neon.index_3d import Index_3d +import numpy as np class mGrid(object): - def __init__(self, backend = None, dim = None, depth = 1): + def __init__(self, backend = None, dim = None, depth = 1, sparsity_pattern: np.ndarray = None): + if sparsity_pattern is None: + sparsity_pattern = np.ones((dim.x,dim.y,dim.z)) + if backend is None: + # raise exception + raise Exception('dGrid: backend pamrameter is missing') + if sparsity_pattern.shape[0] != dim.x or sparsity_pattern.shape[1] != dim.y or sparsity_pattern.shape[2] != dim.z: + raise Exception('dGrid: sparsity_pattern\'s shape does not match the dim') + self.handle: ctypes.c_uint64 = ctypes.c_uint64(0) self.backend = backend self.dim = dim + self.sparsity_pattern = sparsity_pattern self.depth = depth + + 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() @@ -38,6 +51,7 @@ def _help_load_api(self): self.py_neon.lib.mGrid_new.argtypes = [self.py_neon.handle_type, self.py_neon.handle_type, ctypes.POINTER(py_neon.Index_3d), + ctypes.POINTER(ctypes.c_int), ctypes.c_int] self.py_neon.lib.mGrid_new.restype = ctypes.c_int @@ -75,17 +89,17 @@ def _help_load_api(self): def _help_grid_new(self): - if self.handle == 0: - raise Exception('mGrid: Invalid handle') + if self.backend.handle.value == 0: # Check backend handle validity + raise Exception('mGrid: Invalid backend handle') - if self.backend is None: - self.backend = Backend() - if self.dim is None: - self.dim = py_neon.Index_3d(10,10,10) - - res = self.py_neon.lib.mGrid_new(ctypes.byref(self.handle), ctypes.byref(self.backend.handle), self.dim, self.depth) + if self.handle.value != 0: # Ensure the grid handle is uninitialized + raise Exception('mGrid: Grid handle already initialized') + + sparsity_pattern_array = self.sparsity_pattern.ctypes.data_as(ctypes.POINTER(ctypes.c_int)) + res = self.py_neon.lib.mGrid_new(ctypes.byref(self.handle), ctypes.byref(self.backend.handle), self.dim, sparsity_pattern_array, self.depth) if res != 0: raise Exception('mGrid: Failed to initialize grid') + print(f"mGrid initialized with handle {self.handle.value}") def _help_grid_delete(self): if self.py_neon.lib.mGrid_delete(ctypes.byref(self.handle)) != 0: