Skip to content

Commit

Permalink
Bindings for grids.
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
  • Loading branch information
mattloulou authored Jul 3, 2024
1 parent 25536af commit ca3d9ac
Show file tree
Hide file tree
Showing 41 changed files with 2,622 additions and 364 deletions.
1 change: 1 addition & 0 deletions libNeonDomain/include/Neon/domain/Grids.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Neon/domain/aGrid.h"
#include "Neon/domain/eGrid.h"
#include "Neon/domain/bGrid.h"
#include "Neon/domain/mGrid.h"
#include "Neon/domain/dGridSoA.h"
#include "Neon/domain/bGridDisg.h"
#include "Neon/domain/bGridMgpuDisg.h"
Expand Down
3 changes: 3 additions & 0 deletions libNeonDomain/include/Neon/domain/details/bGrid/bSpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class bSpan
typename Idx::DataBlockCount mFirstDataBlockOffset;
typename SBlock::BitMask const* NEON_RESTRICT mActiveMask;
Neon::DataView mDataView;

// Function to get offsets of member variables
static std::vector<size_t> getOffsets();
};
} // namespace Neon::domain::details::bGrid

Expand Down
37 changes: 37 additions & 0 deletions libNeonDomain/include/Neon/domain/details/bGrid/bSpan_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,41 @@ 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));

return offsets;
}
#endif

} // namespace Neon::domain::details::bGrid
6 changes: 6 additions & 0 deletions libNeonDomain/include/Neon/domain/details/dGrid/dSpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Neon/set/DevSet.h"
#include "dIndex.h"
#include "Neon/set/ExecutionThreadSpan.h"
#include <vector>

namespace Neon::domain::details::dGrid {

Expand Down Expand Up @@ -43,6 +44,11 @@ class dSpan
helpGetDim()
const -> Neon::index_3d const&;

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

private:
Neon::DataView mDataView;
int mZghostRadius;
Expand Down
18 changes: 18 additions & 0 deletions libNeonDomain/include/Neon/domain/details/dGrid/dSpan_imp.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ NEON_CUDA_HOST_DEVICE inline auto dSpan::helpGetDim()
return mSpanDim;
}

#if !defined(NEON_WARP_COMPILATION)
inline void dSpan::getOffsets(size_t* offsets, size_t* length) {
std::cout << "dGrid_dSpan 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(dSpan, mDataView),
offsetof(dSpan, mZghostRadius),
offsetof(dSpan, mZboundaryRadius),
offsetof(dSpan, mMaxZInDomain),
offsetof(dSpan, mSpanDim)
};

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

} // namespace Neon::domain::details::dGrid
6 changes: 6 additions & 0 deletions libNeonDomain/include/Neon/domain/details/mGrid/mField.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class mField
using Grid = Neon::domain::details::mGrid::mGrid;
using Partition = Neon::domain::details::mGrid::mPartition<T, C>;
using Idx = typename Partition::Idx;
using Descriptor = mGridDescriptor<1>;

mField() = default;

Expand Down Expand Up @@ -98,6 +99,11 @@ class mField
return mData->grid->getBackend();
}

auto getDescriptor() const -> const Descriptor&
{
return mData->grid->getDescriptor();
}

private:
mField(const std::string& name,
const mGrid& grid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ auto mField<T, C>::getReference(const Neon::index_3d& idx,
const int& cardinality,
const int level) -> T&
{
return mData->fields[level].getReference()(idx, cardinality);
return mData->fields[level].getReference(idx, cardinality);
}

template <typename T, int C>
Expand Down
1 change: 1 addition & 0 deletions libNeonDomain/include/Neon/domain/details/mGrid/mGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class mGrid
auto getOriginBlock3DIndex(const Neon::int32_3d idx, int level) const -> Neon::int32_3d;
auto getDescriptor() const -> const Descriptor&;
auto getRefFactors() const -> const Neon::set::MemSet<int>&;
auto getGridCount() const -> uint32_t;
auto getLevelSpacing() const -> const Neon::set::MemSet<int>&;
auto getBackend() const -> const Backend&;
auto getBackend() -> Backend&;
Expand Down
5 changes: 5 additions & 0 deletions libNeonDomain/src/domain/details/mGrid/mGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ auto mGrid::getRefFactors() const -> const Neon::set::MemSet<int>&
return mData->mRefFactors;
}

auto mGrid::getGridCount() const -> uint32_t
{
return mData->grids.size();
}

auto mGrid::getLevelSpacing() const -> const Neon::set::MemSet<int>&
{
return mData->mSpacing;
Expand Down
26 changes: 26 additions & 0 deletions libNeonPy/include/Neon/py/AllocationCounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <mutex>

class AllocationCounter {
public:
// Static methods to manage allocations and deallocations
static void Allocation();
static void Deallocation();

// Static method to get the current allocation count
static int GetAllocationCount();

private:
// Private constructor to prevent instantiation
AllocationCounter() = default;

// Private static method to get the singleton instance
static AllocationCounter& GetInstance();

// Private static variable to hold the allocation count
static int allocationCount;

// Mutex to protect access to allocationCount
static std::mutex allocationMutex;
};

9 changes: 9 additions & 0 deletions libNeonPy/include/Neon/py/allocationBindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include <cstdint>
#include "AllocationCounter.h"

/**
* Get Counter
*/
extern "C" auto get_allocation_counter()
-> int;
95 changes: 95 additions & 0 deletions libNeonPy/include/Neon/py/bGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once
#include <cstdint>
#include "Neon/domain/Grids.h"

/**
* 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)
-> int;

/**
* Delete a grid object on the heap.
*/
extern "C" auto bGrid_delete(
uint64_t& handle)
-> int;

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

/**
* Delete a field object on the heap.
*/
extern "C" auto bGrid_bField_delete(
uint64_t& handle)
-> int;

extern "C" auto bGrid_bField_get_partition(
uint64_t& field_handle,
Neon::bGrid::Partition<int, 0>* partition_handle,
Neon::Execution execution,
int device,
Neon::DataView data_view)
-> int;

extern "C" auto bGrid_get_span(
uint64_t& gridHandle,
Neon::bGrid::Span* spanRes,
int execution,
int device,
int data_view)
-> int;

extern "C" auto bGrid_span_size(
Neon::bGrid::Span* spanRes)
-> int;

extern "C" auto bGrid_bField_partition_size(
Neon::bGrid::Partition<int, 0>* partitionPtr)
-> int;

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

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

extern "C" auto bGrid_bField_read(
uint64_t& fieldHandle,
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,
int newValue)
-> int;

extern "C" auto bGrid_bField_update_host_data(
uint64_t& fieldHandle,
int streamSetId)
-> int;

extern "C" auto bGrid_bField_update_device_data(
uint64_t& fieldHandle,
int streamSetId)
-> int;

extern "C" auto dGrid_dSpan_get_member_field_offsets(std::size_t* length)
-> std::size_t*;
44 changes: 44 additions & 0 deletions libNeonPy/include/Neon/py/backend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#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_new3(
uint64_t& handle,
const int* devIds /*! Vectors of device ids. There are CUDA device ids */,
int runtime /*! Type of runtime to use */)
-> 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*;

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
* NOTE: some parameters are still not exposed
*/
extern "C" auto dGrid_new(
uint64_t& handle)
uint64_t& handle,
uint64_t& backendPtr,
const Neon::int32_3d* dim)
-> int;
// extern "C" auto dGrid_new(
// uint64_t& handle,
// uint64_t& backendPtr,
// Neon::index_3d dim)
// -> int;

/**
* Delete a grid object on the heap.
Expand Down Expand Up @@ -54,4 +61,40 @@ extern "C" auto dGrid_span_size(

extern "C" auto dGrid_dField_partition_size(
Neon::dGrid::Partition<int, 0>* partitionPtr)
-> int;
-> int;

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

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

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

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

extern "C" auto dGrid_dField_update_host_data(
uint64_t& fieldHandle,
int streamSetId)
-> int;

extern "C" auto dGrid_dField_update_device_data(
uint64_t& fieldHandle,
int streamSetId)
-> int;

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

0 comments on commit ca3d9ac

Please sign in to comment.