Skip to content

Commit

Permalink
Python bindings
Browse files Browse the repository at this point in the history
* added backend support to python bindings. It is untested.

* made requested changes to first part of project

* implemented all critical bindings. Started writing tests but they are not all passing yet.

* fixed test 2

* reverted dGrid back to using Neon::int32_3d instead of 3 integers

* used max's fix for py_neon.Index_3d

* mostly finished implementing and debugging dense tests

* continued fixing issues with dense/block/multires grids

* multires now passes tests

* sparsity_pattern is implemented for dense/block/multires but probably isn't implemented correctly
  • Loading branch information
mattloulou authored Jul 12, 2024
1 parent febef72 commit 50c500d
Show file tree
Hide file tree
Showing 35 changed files with 953 additions and 568 deletions.
20 changes: 20 additions & 0 deletions libNeonDomain/include/Neon/domain/details/bGrid/bPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ class bPartition
helpGetNghIdx(const Idx& idx, const typename Idx::DataBlockIdx* blockConnectivity)
const -> Idx;

#if !defined(NEON_WARP_COMPILATION)
inline static void getOffsets(size_t* offsets, size_t* length) {
static std::vector<size_t> cpp_offsets = {
offsetof(bPartition, mCardinality),
offsetof(bPartition, mMem),
offsetof(bPartition, mStencilNghIndex),
offsetof(bPartition, mBlockConnectivity),
offsetof(bPartition, mMask),
offsetof(bPartition, mOrigin),
offsetof(bPartition, mSetIdx),
offsetof(bPartition, mMultiResDiscreteIdxSpacing),
offsetof(bPartition, mDomainSize),
};

*length = cpp_offsets.size();
for (size_t i = 0; i < cpp_offsets.size(); ++i) {
offsets[i] = cpp_offsets[i];
}
}
#endif

int mCardinality;
T* mMem;
Expand Down
6 changes: 5 additions & 1 deletion libNeonDomain/include/Neon/domain/details/bGrid/bSpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ class bSpan
NEON_CUDA_HOST_DEVICE inline auto setAndValidateGPUDevice(
Idx& bidx) const -> bool;


#if !defined(NEON_WARP_COMPILATION)
// Function to get offsets of member variables
static void getOffsets(size_t* offsets, size_t* length);
#endif

// We don't need to have a count on active blocks
typename Idx::DataBlockCount mFirstDataBlockOffset;
typename SBlock::BitMask const* NEON_RESTRICT mActiveMask;
Expand Down
50 changes: 16 additions & 34 deletions libNeonDomain/include/Neon/domain/details/bGrid/bSpan_imp.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#include "Neon/domain/details/bGrid/bSpan.h"

namespace Neon::domain::details::bGrid {
Expand Down Expand Up @@ -29,7 +31,7 @@ bSpan<SBlock>::setAndValidateCPUDevice(Idx& bidx,
uint32_t const& z) const -> bool
{

bidx.mDataBlockIdx = dataBlockIdx + mFirstDataBlockOffset;
bidx.mDataBlockIdx = dataBlockIdx;
bidx.mInDataBlockIdx.x = static_cast<typename Idx::InDataBlockIdx::Integer>(x);
bidx.mInDataBlockIdx.y = static_cast<typename Idx::InDataBlockIdx::Integer>(y);
bidx.mInDataBlockIdx.z = static_cast<typename Idx::InDataBlockIdx::Integer>(z);
Expand All @@ -47,42 +49,22 @@ bSpan<SBlock>::bSpan(typename Idx::DataBlockCount firstDataBloc
{
}


// #if !defined(NEON_WARP_COMPILATION)
// template <typename SBlock>
// // std::vector<size_t> bSpan<SBlock>::getOffsets() {
// // return {
// // // can't use the `offsetof` macro here since bSpan is a template type
// // 0,
// // sizeof(Idx::DataBlockCount),
// // sizeof(Idx::DataBlockCount) + sizeof(SBlock::BitMask*)
// // };
// // }
// std::vector<size_t> bSpan<SBlock>::getOffsets() {
// std::vector<size_t> offsets;
// bSpan temp(typename Idx::DataBlockCount(), nullptr, Neon::DataView());

// offsets.push_back(reinterpret_cast<char*>(&(temp.mFirstDataBlockOffset)) - reinterpret_cast<char*>(&temp));
// offsets.push_back(reinterpret_cast<char*>(&(temp.mActiveMask)) - reinterpret_cast<char*>(&temp));
// offsets.push_back(reinterpret_cast<char*>(&(temp.mDataView)) - reinterpret_cast<char*>(&temp));

// return offsets;
// }
// #endif

#if !defined(NEON_WARP_COMPILATION)
template <typename SBlock>
inline std::vector<size_t> bSpan<SBlock>::getOffsets() {
bSpan temp({0}, nullptr, {});

// Calculate offsets directly
std::vector<size_t> offsets;
offsets.push_back(reinterpret_cast<size_t>(&(temp.mFirstDataBlockOffset)) - reinterpret_cast<size_t>(&temp));
offsets.push_back(reinterpret_cast<size_t>(&(temp.mActiveMask)) - reinterpret_cast<size_t>(&temp));
offsets.push_back(reinterpret_cast<size_t>(&(temp.mDataView)) - reinterpret_cast<size_t>(&temp));
inline void bSpan<SBlock>::getOffsets(size_t* offsets, size_t* length) {
static std::vector<size_t> cpp_offsets = {
offsetof(bSpan, mFirstDataBlockOffset),
offsetof(bSpan, mActiveMask),
offsetof(bSpan, mDataView)
};

return offsets;
*length = cpp_offsets.size();
for (size_t i = 0; i < cpp_offsets.size(); ++i) {
offsets[i] = cpp_offsets[i];
}
}
#endif

} // namespace Neon::domain::details::bGrid
} // namespace Neon::domain::details::bGrid

#pragma GCC diagnostic pop
28 changes: 28 additions & 0 deletions libNeonDomain/include/Neon/domain/details/dGrid/dPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,34 @@ class dPartition
return s.str();
}

#if !defined(NEON_WARP_COMPILATION)
inline static void getOffsets(size_t* offsets, size_t* length) {
// std::cout << "dGrid_dField_dPartition cpp offsets: " << offsetof(dSpan, mDataView) << " " << offsetof(dSpan, mZghostRadius) << " " << offsetof(dSpan, mZboundaryRadius) << " " << offsetof(dSpan, mMaxZInDomain) << " " << offsetof(dSpan, mSpanDim) << " " << std::endl;
static std::vector<size_t> cpp_offsets = {
offsetof(dPartition, mDataView),
offsetof(dPartition, mDim),
offsetof(dPartition, mMem),
offsetof(dPartition, mZHaloRadius),
offsetof(dPartition, mZBoundaryRadius),
offsetof(dPartition, mPitch.x),
offsetof(dPartition, mPitch.y),
offsetof(dPartition, mPitch.z),
offsetof(dPartition, mPitch.w),
offsetof(dPartition, mPrtID),
offsetof(dPartition, mOrigin),
offsetof(dPartition, mCardinality),
offsetof(dPartition, mFullGridSize),
offsetof(dPartition, mPeriodicZ),
offsetof(dPartition, mStencil),
};

*length = cpp_offsets.size();
for (size_t i = 0; i < cpp_offsets.size(); ++i) {
offsets[i] = cpp_offsets[i];
}
}
#endif

private:
Neon::DataView mDataView;
Neon::index_3d mDim;
Expand Down
36 changes: 35 additions & 1 deletion libNeonDomain/include/Neon/domain/details/mGrid/mPartition.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#pragma once


Expand Down Expand Up @@ -184,6 +186,36 @@ class mPartition : public Neon::domain::details::bGrid::bPartition<T, C, kStatic
*/
NEON_CUDA_HOST_DEVICE inline Neon::index_3d getGlobalIndex(Idx gidx) const;

#if !defined(NEON_WARP_COMPILATION)
inline static void getOffsets(size_t* offsets, size_t* length) {
static std::vector<size_t> cpp_offsets = {
offsetof(mPartition, mCardinality),
offsetof(mPartition, mMem),
offsetof(mPartition, mStencilNghIndex),
offsetof(mPartition, mBlockConnectivity),
offsetof(mPartition, mMask),
offsetof(mPartition, mOrigin),
offsetof(mPartition, mSetIdx),
offsetof(mPartition, mMultiResDiscreteIdxSpacing),
offsetof(mPartition, mDomainSize),
offsetof(mPartition, mLevel),
offsetof(mPartition, mMemParent),
offsetof(mPartition, mMemChild),
offsetof(mPartition, mParentBlockID),
offsetof(mPartition, mMaskLowerLevel),
offsetof(mPartition, mMaskUpperLevel),
offsetof(mPartition, mChildBlockID),
offsetof(mPartition, mParentNeighbourBlocks),
offsetof(mPartition, mRefFactors),
offsetof(mPartition, mSpacing),
};

*length = cpp_offsets.size();
for (size_t i = 0; i < cpp_offsets.size(); ++i) {
offsets[i] = cpp_offsets[i];
}
}
#endif

private:
inline NEON_CUDA_HOST_DEVICE auto childID(const Idx& gidx) const -> uint32_t;
Expand All @@ -202,4 +234,6 @@ class mPartition : public Neon::domain::details::bGrid::bPartition<T, C, kStatic
};
} // namespace Neon::domain::details::mGrid

#include "Neon/domain/details/mGrid/mPartition_imp.h"
#include "Neon/domain/details/mGrid/mPartition_imp.h"

#pragma GCC diagnostic pop
1 change: 0 additions & 1 deletion libNeonDomain/src/domain/details/dGrid/dGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ auto dGrid::getSpan(Neon::Execution execution,
return mData->spanTable.getSpan(execution, setIdx, dataView);
}


auto dGrid::helpGetPartitionDim()
const -> const Neon::set::DataSet<index_3d>
{
Expand Down
37 changes: 24 additions & 13 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,
Neon::Backend* backendPtr,
Neon::index_3d dim)
uint64_t& backendPtr,
const Neon::int32_3d* dim,
int* sparsity_pattern)
-> int;

/**
Expand All @@ -19,12 +20,18 @@ extern "C" auto bGrid_delete(
uint64_t& handle)
-> int;

extern "C" auto bGrid_get_dimensions(
uint64_t& gridHandle,
Neon::index_3d* dim)
-> int;

/**
* Generates a new field object on the heap.
*/
extern "C" auto bGrid_bField_new(
uint64_t& handle,
uint64_t& grid)
uint64_t& fieldHandle,
uint64_t& gridHandle,
int cardinality)
-> int;

/**
Expand Down Expand Up @@ -60,24 +67,24 @@ extern "C" auto bGrid_bField_partition_size(

extern "C" auto bGrid_get_properties( /* TODOMATT verify what the return of this method should be */
uint64_t& gridHandle,
const Neon::index_3d& idx)
const Neon::index_3d* idx)
-> int;

extern "C" auto bGrid_is_inside_domain(
uint64_t& gridHandle,
const Neon::index_3d& idx)
const Neon::index_3d* const idx)
-> bool;

extern "C" auto bGrid_bField_read(
uint64_t& fieldHandle,
const Neon::index_3d& idx,
const int& cardinality)
const Neon::index_3d* idx,
const int cardinality)
-> int;

extern "C" auto bGrid_bField_write(
uint64_t& fieldHandle,
const Neon::index_3d& idx,
const int& cardinality,
const Neon::index_3d* idx,
const int cardinality,
int newValue)
-> int;

Expand All @@ -91,5 +98,9 @@ extern "C" auto bGrid_bField_update_device_data(
int streamSetId)
-> int;

extern "C" auto dGrid_dSpan_get_member_field_offsets(std::size_t* length)
-> std::size_t*;
extern "C" auto bGrid_bSpan_get_member_field_offsets(size_t* offsets, size_t* length)
-> void;


extern "C" auto bGrid_bField_bPartition_get_member_field_offsets(size_t* offsets, size_t* length)
-> void;
27 changes: 4 additions & 23 deletions libNeonPy/include/Neon/py/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,23 @@
#include <cstdint>
#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;

/**
*
*/
extern "C" auto dBackend_new(
uint64_t& handle,
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 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.
*/
extern "C" auto dBackend_delete(
uint64_t& handle)
-> int;


extern "C" auto dBackend_get_string(uint64_t& handle) -> const char*;

extern "C" auto dBackend_sync(uint64_t& handle) -> int;
Loading

0 comments on commit 50c500d

Please sign in to comment.