From 6819bc56a4b7975b172cd7e086e04ed9626cfccb Mon Sep 17 00:00:00 2001 From: Carl Johnsen Date: Tue, 14 May 2024 08:23:51 +0200 Subject: [PATCH] #24 Added (missing) namespaces to the morphology C++ implementations --- src/lib/cpp/cpu/morphology.cc | 5 ++++- src/lib/cpp/cpu_seq/morphology.cc | 6 +++++- src/lib/cpp/gpu/morphology.cc | 6 +++++- src/lib/cpp/include/morphology.hh | 4 ++++ src/pybind/morphology-pybind.cc | 32 +++++++++++++++++++++++++++---- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/lib/cpp/cpu/morphology.cc b/src/lib/cpp/cpu/morphology.cc index a180aa5..4fe79b8 100644 --- a/src/lib/cpp/cpu/morphology.cc +++ b/src/lib/cpp/cpu/morphology.cc @@ -1,6 +1,8 @@ #include "morphology.hh" #include "datatypes.hh" +namespace cpu_par { + template void morphology_3d_sphere( const mask_type *voxels, @@ -43,4 +45,5 @@ void morphology_3d_sphere( } } } -} \ No newline at end of file +} +} // namespace cpu_par \ No newline at end of file diff --git a/src/lib/cpp/cpu_seq/morphology.cc b/src/lib/cpp/cpu_seq/morphology.cc index 50c16bf..3744689 100644 --- a/src/lib/cpp/cpu_seq/morphology.cc +++ b/src/lib/cpp/cpu_seq/morphology.cc @@ -1,6 +1,8 @@ #include "morphology.hh" #include "datatypes.hh" +namespace cpu_seq { + template void morphology_3d_sphere( const mask_type *voxels, @@ -41,4 +43,6 @@ void morphology_3d_sphere( } } } -} \ No newline at end of file +} + +} // namespace cpu_seq \ No newline at end of file diff --git a/src/lib/cpp/gpu/morphology.cc b/src/lib/cpp/gpu/morphology.cc index 5125354..b8e1945 100644 --- a/src/lib/cpp/gpu/morphology.cc +++ b/src/lib/cpp/gpu/morphology.cc @@ -1,6 +1,8 @@ #include "morphology.hh" #include "datatypes.hh" +namespace gpu { + template void morphology_3d_sphere( const mask_type *voxels, @@ -107,4 +109,6 @@ void morphology_3d_sphere_r16( #else throw runtime_error("Library wasn't compiled with OpenACC."); #endif -} \ No newline at end of file +} + +} // namespace gpu \ No newline at end of file diff --git a/src/lib/cpp/include/morphology.hh b/src/lib/cpp/include/morphology.hh index 66a28e4..e33cadc 100644 --- a/src/lib/cpp/include/morphology.hh +++ b/src/lib/cpp/include/morphology.hh @@ -3,6 +3,8 @@ #include "datatypes.hh" +namespace NS { + template void morphology_3d_sphere( const mask_type *voxels, @@ -11,4 +13,6 @@ void morphology_3d_sphere( const int64_t strides[3], mask_type *result); + +} // namespace NS #endif \ No newline at end of file diff --git a/src/pybind/morphology-pybind.cc b/src/pybind/morphology-pybind.cc index f208e89..78d4af5 100644 --- a/src/pybind/morphology-pybind.cc +++ b/src/pybind/morphology-pybind.cc @@ -7,6 +7,8 @@ namespace py = pybind11; #include "morphology.cc" #include "datatypes.hh" +namespace python_api { + template void morphology_3d_sphere_wrapper( const py::array_t &np_voxels, @@ -24,15 +26,37 @@ void morphology_3d_sphere_wrapper( mask_type *result = static_cast(result_info.ptr); if (radius == (int64_t) 16) { - morphology_3d_sphere_r16(voxels, N, strides, result); + NS::morphology_3d_sphere_r16(voxels, N, strides, result); } else { - morphology_3d_sphere(voxels, radius, N, strides, result); + NS::morphology_3d_sphere(voxels, radius, N, strides, result); } } + +template +void morphology_3d_sphere_wrapper_alt( + const py::array_t &np_voxels, + const int64_t radius, + py::array_t np_result) { + auto + voxels_info = np_voxels.request(), + result_info = np_result.request(); + + int64_t Nz = voxels_info.shape[0], Ny = voxels_info.shape[1], Nx = voxels_info.shape[2]; + int64_t N[3] = {Nz, Ny, Nx}; + int64_t strides[3] = {Ny*Nx, Nx, 1}; + + const mask_type *voxels = static_cast(voxels_info.ptr); + mask_type *result = static_cast(result_info.ptr); + + NS::morphology_3d_sphere_alt(voxels, radius, N, strides, result); } +} // namespace python_api + PYBIND11_MODULE(morphology, m) { m.doc() = "Morphology operations."; // optional module docstring - m.def("dilate_3d_sphere", &morphology_3d_sphere_wrapper, false>, py::arg("np_voxels"), py::arg("radius"), py::arg("np_result").noconvert()); - m.def("erode_3d_sphere", &morphology_3d_sphere_wrapper, true>, py::arg("np_voxels"), py::arg("radius"), py::arg("np_result").noconvert()); + m.def("dilate_3d_sphere", &python_api::morphology_3d_sphere_wrapper, false>, py::arg("np_voxels"), py::arg("radius"), py::arg("np_result").noconvert()); + m.def("dilate_3d_sphere_alt", &python_api::morphology_3d_sphere_wrapper_alt, false>, py::arg("np_voxels"), py::arg("radius"), py::arg("np_result").noconvert()); + m.def("erode_3d_sphere", &python_api::morphology_3d_sphere_wrapper, true>, py::arg("np_voxels"), py::arg("radius"), py::arg("np_result").noconvert()); + m.def("erode_3d_sphere_alt", &python_api::morphology_3d_sphere_wrapper_alt, true>, py::arg("np_voxels"), py::arg("radius"), py::arg("np_result").noconvert()); } \ No newline at end of file