Skip to content

Commit

Permalink
Fixing layout issue with python bindings (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
massimim authored May 24, 2024
1 parent d60aa23 commit efc91fc
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 21 deletions.
23 changes: 21 additions & 2 deletions libNeonDomain/include/Neon/domain/details/dGrid/dPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class dPartition
Neon::index_3d fullGridSize,
NghIdx* stencil = nullptr)
: mDataView(dataView),
mMem(mem),
mDim(dim),
mMem(mem),
mZHaloRadius(zHaloRadius),
mZBoundaryRadius(zBoundaryRadius),
mPitch(pitch),
Expand Down Expand Up @@ -462,10 +462,29 @@ class dPartition
return mStencil;
}

auto to_string()
const -> std::string
{
std::stringstream s;
s << "mDataView " << mDataView << "\n";
s << "mMem " << mMem << "\n";
s << "mDim " << mDim << "\n";
s << "mZHaloRadius " << mZHaloRadius << "\n";
s << "mZBoundaryRadius " << mZBoundaryRadius << "\n";
s << "mPitch " << mPitch << "\n";
s << "mPrtID " << mPrtID << "\n";
s << "mOrigin " << mOrigin << "\n";
s << "mCardinality " << mCardinality << "\n";
s << "mFullGridSize " << mFullGridSize << "\n";
s << "mPeriodicZ " << mPeriodicZ << "\n";
s << "mStencil " << mStencil << "\n";
return s.str();
}

private:
Neon::DataView mDataView;
T* NEON_RESTRICT mMem;
Neon::index_3d mDim;
T* NEON_RESTRICT mMem;
int mZHaloRadius;
int mZBoundaryRadius;
Pitch mPitch;
Expand Down
12 changes: 10 additions & 2 deletions libNeonPy/include/Neon/py/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ extern "C" auto dGrid_dField_delete(
extern "C" auto dGrid_dField_get_partition(
uint64_t& field_handle,
Neon::dGrid::Partition<int, 0>* partition_handle,
int execution,
Neon::Execution execution,
int device,
int data_view)
Neon::DataView data_view)
-> int;

extern "C" auto dGrid_get_span(
Expand All @@ -46,4 +46,12 @@ extern "C" auto dGrid_get_span(
int execution,
int device,
int data_view)
-> int;

extern "C" auto dGrid_span_size(
Neon::dGrid::Span* spanRes)
-> int;

extern "C" auto dGrid_dField_partition_size(
Neon::dGrid::Partition<int, 0>* partitionPtr)
-> int;
33 changes: 25 additions & 8 deletions libNeonPy/src/Neon/py/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ auto dGrid_new(
Neon::Backend bk(1, Neon::Runtime::openmp);
Neon::index_3d dim(10, 10, 10);
Neon::domain::Stencil d3q19 = Neon::domain::Stencil::s19_t(false);
Grid g(bk, dim, [](auto idx) { return true; }, d3q19);
Grid g(bk, dim, [](Neon::index_3d const& /*idx*/) { return true; }, d3q19);
auto gridPtr = new (std::nothrow) Grid(g);

if (gridPtr == nullptr) {
Expand Down Expand Up @@ -59,6 +59,7 @@ auto dGrid_get_span(
std::cout << "dGrid_get_span - execution " << execution << std::endl;
std::cout << "dGrid_get_span - device " << device << std::endl;
std::cout << "dGrid_get_span - data_view " << data_view << std::endl;
std::cout << "dGrid_get_span - Span size " << sizeof(*spanRes) << std::endl;

using Grid = Neon::dGrid;
Grid* gridPtr = (Grid*)gridHandle;
Expand Down Expand Up @@ -111,17 +112,17 @@ auto dGrid_dField_new(
auto dGrid_dField_get_partition(
uint64_t& field_handle,
[[maybe_unused]] Neon::dGrid::Partition<int, 0>* partitionPtr,
int execution,
Neon::Execution execution,
int device,
int data_view)
Neon::DataView data_view)
-> int
{

std::cout << "dGrid_dField_get_partition - BEGIN " << std::endl;
std::cout << "dGrid_dField_get_partition - field_handle " << field_handle << std::endl;
std::cout << "dGrid_dField_get_partition - execution " << execution << std::endl;
std::cout << "dGrid_dField_get_partition - execution " << Neon::ExecutionUtils::toString(execution) << std::endl;
std::cout << "dGrid_dField_get_partition - device " << device << std::endl;
std::cout << "dGrid_dField_get_partition - data_view " << data_view << std::endl;
std::cout << "dGrid_dField_get_partition - data_view " << Neon::DataViewUtil::toString(data_view) << std::endl;

using Grid = Neon::dGrid;
using Field = Grid::Field<int, 0>;
Expand All @@ -130,12 +131,14 @@ auto dGrid_dField_get_partition(
std::cout << fieldPtr->toString() << std::endl;

if (fieldPtr != nullptr) {
auto p = fieldPtr->getPartition(Neon::ExecutionUtils::fromInt(execution),
auto p = fieldPtr->getPartition(execution,
device,
Neon::DataViewUtil::fromInt(data_view));
data_view);
std::cout << p.cardinality() << std::endl;

*partitionPtr = p;
std::cout << "dGrid_dField_get_partition\n"
<< partitionPtr->to_string();

std::cout << "dGrid_dField_get_partition - END" << std::endl;

return 0;
Expand All @@ -161,4 +164,18 @@ auto dGrid_dField_delete(
std::cout << "dGrid_dField_delete - END" << std::endl;

return 0;
}

auto dGrid_span_size(
Neon::dGrid::Span* spanRes)
-> int
{
return sizeof(*spanRes);
}

auto dGrid_dField_partition_size(
Neon::dGrid::Partition<int, 0>* partitionPtr)
-> int
{
return sizeof(*partitionPtr);
}
6 changes: 3 additions & 3 deletions py_neon/dataview.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Values(Enum):

def __init__(self, data_view: Values):
if data_view == DataView.Values.standard:
self.data_view = 1
self.data_view = 0
if data_view == DataView.Values.internal:
self.data_view = 2
self.data_view = 1
if data_view == DataView.Values.boundary:
self.data_view = 3
self.data_view = 2

@property
def value(self):
Expand Down
13 changes: 13 additions & 0 deletions py_neon/dense/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ def help_load_api(self):
## new_field
self.py_neon.lib.dGrid_dField_new.argtypes = [self.handle_type,
self.handle_type]
self.py_neon.lib.dGrid_dField_new.restype = ctypes.c_int

## delete_field
self.py_neon.lib.dGrid_dField_delete.argtypes = [self.handle_type]
self.py_neon.lib.dGrid_dField_delete.restype = ctypes.c_int

## get_partition
self.py_neon.lib.dGrid_dField_get_partition.argtypes = [self.handle_type,
Expand All @@ -43,6 +45,11 @@ def help_load_api(self):
ctypes.c_int, # the device id
NeDataView, # the data view
]
self.py_neon.lib.dGrid_dField_get_partition.restype = ctypes.c_int

# size partition
self.py_neon.lib.dGrid_dField_partition_size.argtypes = [ctypes.POINTER(NePartitionInt)]
self.py_neon.lib.dGrid_dField_partition_size.restype = ctypes.c_int

def help_new(self):
if self.handle == 0:
Expand Down Expand Up @@ -78,6 +85,12 @@ def get_partition(self,
if res != 0:
raise Exception('Failed to get span')

ccp_size = self.py_neon.lib.dGrid_dField_partition_size(partition)
ctypes_size = ctypes.sizeof(partition)

if ccp_size != ctypes_size:
raise Exception(f'Failed to get span: cpp_size {ccp_size} != ctypes_size {ctypes_size}')

print(f"Partition {partition}")
wpne_partition = Wpne_NeonDensePartitionInt(partition)
return wpne_partition
11 changes: 11 additions & 0 deletions py_neon/dense/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def help_load_api(self):
]
self.py_neon.lib.dGrid_get_span.restype = ctypes.c_int

self.py_neon.lib.dGrid_span_size.argtypes = [ctypes.POINTER(Span)]
self.py_neon.lib.dGrid_span_size.restype = ctypes.c_int


def help_grid_new(self):
if self.handle == 0:
raise Exception('DGrid: Invalid handle')
Expand Down Expand Up @@ -74,4 +78,11 @@ def get_span(self,
res = self.py_neon.lib.dGrid_get_span(self.handle, span, execution, c, data_view)
if res != 0:
raise Exception('Failed to get span')

cpp_size = self.py_neon.lib.dGrid_span_size(span)
ctypes_size = ctypes.sizeof(span)

if cpp_size != ctypes_size:
raise Exception(f'Failed to get span: cpp_size {cpp_size} != ctypes_size {ctypes_size}')

return span
4 changes: 2 additions & 2 deletions py_neon/dense/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
class PartitionInt(ctypes.Structure):
_fields_ = [
("mDataView", py_neon.DataView),
("mMem", ctypes.POINTER(ctypes.c_int)),
("mDim", py_neon.Index_3d),
("mMem", ctypes.POINTER(ctypes.c_int)),
("mZHaloRadius", ctypes.c_int),
("mZBoundaryRadius", ctypes.c_int),
("mPitch1", ctypes.c_uint64),
("mPitch2", ctypes.c_uint64),
("mPitch3", ctypes.c_uint64),
("mPitch4", ctypes.c_uint64),
("mPrtID", ctypes.c_uint64),
("mPrtID", ctypes.c_int),
("mOrigin", py_neon.Index_3d),
("mCardinality", ctypes.c_int),
("mFullGridSize", py_neon.Index_3d),
Expand Down
8 changes: 4 additions & 4 deletions py_neon/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class Execution(ctypes.Structure):
_fields_ = [("execution", ctypes.c_uint8)]

class Values(Enum):
device = 1
host = 2
device = 0
host = 1

def __init__(self, execution: Values):
if execution == Execution.Values.device:
self.execution = 1
self.execution = 0
if execution == Execution.Values.host:
self.execution = 2
self.execution = 1

def __int__(self):
return self.execution
Expand Down

0 comments on commit efc91fc

Please sign in to comment.