From efc91fc8c76b090624b13cadfeb2d865ca01aa44 Mon Sep 17 00:00:00 2001 From: massimim <57805133+massimim@users.noreply.github.com> Date: Sat, 25 May 2024 00:09:14 +0200 Subject: [PATCH] Fixing layout issue with python bindings (#49) --- .../Neon/domain/details/dGrid/dPartition.h | 23 +++++++++++-- libNeonPy/include/Neon/py/grid.h | 12 +++++-- libNeonPy/src/Neon/py/grid.cpp | 33 ++++++++++++++----- py_neon/dataview.py | 6 ++-- py_neon/dense/field.py | 13 ++++++++ py_neon/dense/grid.py | 11 +++++++ py_neon/dense/partition.py | 4 +-- py_neon/execution.py | 8 ++--- 8 files changed, 89 insertions(+), 21 deletions(-) diff --git a/libNeonDomain/include/Neon/domain/details/dGrid/dPartition.h b/libNeonDomain/include/Neon/domain/details/dGrid/dPartition.h index 99a21e3c..789e7a69 100644 --- a/libNeonDomain/include/Neon/domain/details/dGrid/dPartition.h +++ b/libNeonDomain/include/Neon/domain/details/dGrid/dPartition.h @@ -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), @@ -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; diff --git a/libNeonPy/include/Neon/py/grid.h b/libNeonPy/include/Neon/py/grid.h index 70ade962..bc225242 100644 --- a/libNeonPy/include/Neon/py/grid.h +++ b/libNeonPy/include/Neon/py/grid.h @@ -35,9 +35,9 @@ extern "C" auto dGrid_dField_delete( extern "C" auto dGrid_dField_get_partition( uint64_t& field_handle, Neon::dGrid::Partition* partition_handle, - int execution, + Neon::Execution execution, int device, - int data_view) + Neon::DataView data_view) -> int; extern "C" auto dGrid_get_span( @@ -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* partitionPtr) -> int; \ No newline at end of file diff --git a/libNeonPy/src/Neon/py/grid.cpp b/libNeonPy/src/Neon/py/grid.cpp index a737f5bc..7bb867e2 100644 --- a/libNeonPy/src/Neon/py/grid.cpp +++ b/libNeonPy/src/Neon/py/grid.cpp @@ -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) { @@ -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; @@ -111,17 +112,17 @@ auto dGrid_dField_new( auto dGrid_dField_get_partition( uint64_t& field_handle, [[maybe_unused]] Neon::dGrid::Partition* 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; @@ -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; @@ -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* partitionPtr) + -> int +{ + return sizeof(*partitionPtr); } \ No newline at end of file diff --git a/py_neon/dataview.py b/py_neon/dataview.py index e290b9d9..895177fd 100644 --- a/py_neon/dataview.py +++ b/py_neon/dataview.py @@ -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): diff --git a/py_neon/dense/field.py b/py_neon/dense/field.py index 109296b7..9b3ce651 100644 --- a/py_neon/dense/field.py +++ b/py_neon/dense/field.py @@ -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, @@ -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: @@ -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 diff --git a/py_neon/dense/grid.py b/py_neon/dense/grid.py index 86ae4222..deefa844 100644 --- a/py_neon/dense/grid.py +++ b/py_neon/dense/grid.py @@ -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') @@ -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 diff --git a/py_neon/dense/partition.py b/py_neon/dense/partition.py index 3a255e2e..da604dce 100644 --- a/py_neon/dense/partition.py +++ b/py_neon/dense/partition.py @@ -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), diff --git a/py_neon/execution.py b/py_neon/execution.py index 3d461893..51cfeb7a 100644 --- a/py_neon/execution.py +++ b/py_neon/execution.py @@ -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