Skip to content

Commit

Permalink
Merge pull request iraikov#10 from iraikov/feature/shared_array
Browse files Browse the repository at this point in the history
Support for shared memory arrays
  • Loading branch information
iraikov authored Jan 9, 2025
2 parents d06e26a + 21d74be commit 80f284e
Show file tree
Hide file tree
Showing 13 changed files with 611 additions and 349 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWER)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

enable_language(CXX)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD 17)

if ( CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -DMPICH_SKIP_MPICXX=1 -DOMPI_SKIP_MPICXX=1 -DH5_USE_110_API")
endif()

include(${PROJECT_SOURCE_DIR}/cmake/neuroh5_utils.cmake)

set(NEUROH5_VERSION 0.1.9)
set(NEUROH5_VERSION 0.1.15)

cmake_policy(SET CMP0074 NEW) # enables use of HDF5_ROOT variable

Expand Down
5 changes: 4 additions & 1 deletion include/data/attr_val.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ namespace neuroh5
void resize (size_t size);

template<class T>
const std::vector<T>& attr_vec (size_t i) const;
const std::vector<T>& const_attr_vec (size_t i) const;

template<class T>
std::vector<T>& attr_vec (size_t i);

template<class T>
size_t size_attr (size_t i) const;
Expand Down
4 changes: 2 additions & 2 deletions include/graph/edge_attributes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ namespace neuroh5
{
size_t i = attr_index.attr_index<T>(attr_name);
string path = hdf5::edge_attribute_path(src_pop_name, dst_pop_name, attr_namespace, attr_name);
graph::write_edge_attribute<T>(comm, file, path, edge_attr_values.attr_vec<T>(i));
graph::write_edge_attribute<T>(comm, file, path, edge_attr_values.const_attr_vec<T>(i));
}
}
else
Expand Down Expand Up @@ -368,7 +368,7 @@ namespace neuroh5
size_t i = attr_index.attr_index<T>(attr_name);
graph::append_edge_attribute<T>(comm, file, src_pop_name, dst_pop_name,
attr_namespace, attr_name,
edge_attr_values.attr_vec<T>(i),
edge_attr_values.const_attr_vec<T>(i),
chunk_size);
}
}
Expand Down
6 changes: 4 additions & 2 deletions include/neuroh5_types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ namespace neuroh5

typedef std::map<NODE_IDX_T, edge_tuple_t> edge_map_t;

typedef edge_map_t::const_iterator edge_map_iter_t;

typedef edge_map_t::const_iterator edge_map_const_iter_t;

typedef edge_map_t::iterator edge_map_iter_t;

typedef std::map<rank_t, edge_map_t> rank_edge_map_t;

typedef rank_edge_map_t::const_iterator rank_edge_map_iter_t;
Expand Down
2 changes: 1 addition & 1 deletion python/neuroh5/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

set(NEUROH5_IO_SRCS
"iomodule.cc"
"iomodule.cc"
)

include_directories(${MPI_C_INCLUDE_DIRS})
Expand Down
424 changes: 97 additions & 327 deletions python/neuroh5/iomodule.cc

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions python/neuroh5/shared_array.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

#include "shared_array.hh"

// Function to create shared numpy array
static PyObject* create_shared_array(PyObject* self, PyObject* args)
{
Py_ssize_t size;
const char* dtype_str;

// Parse size and dtype arguments
if (!PyArg_ParseTuple(args, "ns", &size, &dtype_str)) {
return nullptr;
}

// Map dtype string to appropriate creation function
std::string dtype(dtype_str);
if (dtype == "float32" || dtype == "float") {
return create_typed_shared_array<float>(size);
}
else if (dtype == "int32") {
return create_typed_shared_array<int32_t>(size);
}
else if (dtype == "int16") {
return create_typed_shared_array<int16_t>(size);
}
else if (dtype == "int8") {
return create_typed_shared_array<int8_t>(size);
}
else if (dtype == "uint32") {
return create_typed_shared_array<uint32_t>(size);
}
else if (dtype == "uint16") {
return create_typed_shared_array<uint16_t>(size);
}
else if (dtype == "uint8") {
return create_typed_shared_array<uint8_t>(size);
}

PyErr_SetString(PyExc_ValueError, "Unsupported dtype");
return nullptr;
}

// Module method definition
static PyMethodDef ModuleMethods[] = {
{"create_shared_array", create_shared_array, METH_VARARGS,
"Create a numpy array that shares memory with C++ array. Args: size, dtype"},
{nullptr, nullptr, 0, nullptr}
};

// Module definition structure
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"shared_array_module",
"Module that provides shared memory array between C++ and Python",
-1,
ModuleMethods
};

// Module initialization function
PyMODINIT_FUNC PyInit_shared_array_module(void) {
import_array(); // Initialize numpy C-API

PyObject* module = PyModule_Create(&moduledef);
if (!module) {
return nullptr;
}

return module;
}
Loading

0 comments on commit 80f284e

Please sign in to comment.