Skip to content

Commit

Permalink
sparsity_pattern is implemented for dense/block/multires but probably…
Browse files Browse the repository at this point in the history
… isn't implemented correctly
  • Loading branch information
mattloulou committed Jul 10, 2024
1 parent 7f5571d commit 4498ab2
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 36 deletions.
5 changes: 3 additions & 2 deletions libNeonPy/include/Neon/py/bGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 2 additions & 0 deletions libNeonPy/include/Neon/py/mGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
5 changes: 3 additions & 2 deletions libNeonPy/src/Neon/py/bGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion libNeonPy/src/Neon/py/mGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<std::function<bool(const Neon::index_3d&)>>{[](Neon::index_3d const& /*idx*/) { return true; }}, d3q19, Grid::Descriptor(depth));
Grid g(*backend, *dim, std::vector<std::function<bool(const Neon::index_3d&)>>{[=](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();

Expand Down
34 changes: 24 additions & 10 deletions py_neon/block/bGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
17 changes: 5 additions & 12 deletions py_neon/dense/dGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 23 additions & 9 deletions py_neon/multires/mGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4498ab2

Please sign in to comment.