Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
pyShambackends.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
15
21#include "shamcomm/logs.hpp"
23#include <pybind11/complex.h>
24
25template<class T>
26inline void register_DeviceBuffer(py::module &m, const char *class_name) {
27
28 shamcomm::logs::debug_ln("[Py]", "registering shamrock.backends." + std::string(class_name));
29 py::class_<sham::DeviceBuffer<T>>(m, class_name)
30 .def(py::init([]() {
31 return std::make_unique<sham::DeviceBuffer<T>>(
32 sham::DeviceBuffer<T>{0, shamsys::instance::get_compute_scheduler_ptr()});
33 }))
34 .def(
35 "get_size",
36 [](sham::DeviceBuffer<T> &self) {
37 return self.get_size();
38 })
39 .def(
40 "resize",
41 [](sham::DeviceBuffer<T> &self, u32 sz) {
42 self.resize(sz);
43 })
44 .def(
45 "get_val_at_idx",
46 [](sham::DeviceBuffer<T> &self, u32 idx) {
47 return self.get_val_at_idx(idx);
48 })
49 .def(
50 "set_val_at_idx",
51 [](sham::DeviceBuffer<T> &self, u32 idx, T val) {
52 self.set_val_at_idx(idx, val);
53 })
54 .def(
55 "fill",
56 [](sham::DeviceBuffer<T> &self, T val) {
57 self.fill(val);
58 })
59 .def(
60 "copy_from_stdvec",
61 [](sham::DeviceBuffer<T> &self, const std::vector<T> &v) {
62 self.copy_from_stdvec(v);
63 })
64 .def("copy_to_stdvec", [](sham::DeviceBuffer<T> &self) {
65 return self.copy_to_stdvec();
66 });
67}
68
70 auto &m = root_module;
71
72 py::module shambackends_module = m.def_submodule("backends", "backend library");
73
74 register_DeviceBuffer<u8>(shambackends_module, "DeviceBuffer_u8");
75 register_DeviceBuffer<u32>(shambackends_module, "DeviceBuffer_u32");
76 register_DeviceBuffer<f32>(shambackends_module, "DeviceBuffer_f32");
77 register_DeviceBuffer<f64>(shambackends_module, "DeviceBuffer_f64");
78 register_DeviceBuffer<f64_2>(shambackends_module, "DeviceBuffer_f64_2");
79 register_DeviceBuffer<f64_3>(shambackends_module, "DeviceBuffer_f64_3");
80
81 shambackends_module.def("reset_mem_info_max", []() {
83 });
84
85 py::class_<sham::MemPerfInfos>(shambackends_module, "MemPerfInfos")
86 .def(py::init([]() {
87 return sham::MemPerfInfos{};
88 }))
89 .def_readwrite(
90 "time_alloc_host",
92 py::return_value_policy::reference_internal)
93 .def_readwrite(
94 "time_alloc_device",
96 py::return_value_policy::reference_internal)
97 .def_readwrite(
98 "time_alloc_shared",
100 py::return_value_policy::reference_internal)
101 .def_readwrite(
102 "time_free_host",
104 py::return_value_policy::reference_internal)
105 .def_readwrite(
106 "time_free_device",
108 py::return_value_policy::reference_internal)
109 .def_readwrite(
110 "time_free_shared",
112 py::return_value_policy::reference_internal)
113 .def_readwrite(
114 "allocated_byte_host",
116 py::return_value_policy::reference_internal)
117 .def_readwrite(
118 "allocated_byte_device",
120 py::return_value_policy::reference_internal)
121 .def_readwrite(
122 "allocated_byte_shared",
124 py::return_value_policy::reference_internal)
125 .def_readwrite(
126 "max_allocated_byte_host",
128 py::return_value_policy::reference_internal)
129 .def_readwrite(
130 "max_allocated_byte_device",
132 py::return_value_policy::reference_internal)
133 .def_readwrite(
134 "max_allocated_byte_shared",
136 py::return_value_policy::reference_internal)
137 .def(
138 "__str__",
139 [](const sham::MemPerfInfos &mem_perf_infos) {
140 return shambase::format(
141 "MemPerfInfos(\n"
142 " time_alloc_host=\"{0:}\",\n"
143 " time_alloc_device=\"{1:}\",\n"
144 " time_alloc_shared=\"{2:}\",\n"
145 " time_free_host=\"{3:}\",\n"
146 " time_free_device=\"{4:}\",\n"
147 " time_free_shared=\"{5:}\",\n"
148 " allocated_byte_host=\"{6:}\",\n"
149 " allocated_byte_device=\"{7:}\",\n"
150 " allocated_byte_shared=\"{8:}\",\n"
151 " max_allocated_byte_host=\"{9:}\",\n"
152 " max_allocated_byte_device=\"{10:}\",\n"
153 " max_allocated_byte_shared=\"{11:}\")",
154 mem_perf_infos.time_alloc_host,
155 mem_perf_infos.time_alloc_device,
156 mem_perf_infos.time_alloc_shared,
157 mem_perf_infos.time_free_host,
158 mem_perf_infos.time_free_device,
159 mem_perf_infos.time_free_shared,
160 mem_perf_infos.allocated_byte_host,
161 mem_perf_infos.allocated_byte_device,
162 mem_perf_infos.allocated_byte_shared,
163 mem_perf_infos.max_allocated_byte_host,
164 mem_perf_infos.max_allocated_byte_device,
165 mem_perf_infos.max_allocated_byte_shared);
166 })
167 .def("__repr__", [](const sham::MemPerfInfos &mem_perf_infos) {
168 return shambase::format(
169 "MemPerfInfos(\n"
170 " time_alloc_host=\"{0:}\",\n"
171 " time_alloc_device=\"{1:}\",\n"
172 " time_alloc_shared=\"{2:}\",\n"
173 " time_free_host=\"{3:}\",\n"
174 " time_free_device=\"{4:}\",\n"
175 " time_free_shared=\"{5:}\",\n"
176 " allocated_byte_host=\"{6:}\",\n"
177 " allocated_byte_device=\"{7:}\",\n"
178 " allocated_byte_shared=\"{8:}\",\n"
179 " max_allocated_byte_host=\"{9:}\",\n"
180 " max_allocated_byte_device=\"{10:}\",\n"
181 " max_allocated_byte_shared=\"{11:}\")",
182 mem_perf_infos.time_alloc_host,
183 mem_perf_infos.time_alloc_device,
184 mem_perf_infos.time_alloc_shared,
185 mem_perf_infos.time_free_host,
186 mem_perf_infos.time_free_device,
187 mem_perf_infos.time_free_shared,
188 mem_perf_infos.allocated_byte_host,
189 mem_perf_infos.allocated_byte_device,
190 mem_perf_infos.allocated_byte_shared,
191 mem_perf_infos.max_allocated_byte_host,
192 mem_perf_infos.max_allocated_byte_device,
193 mem_perf_infos.max_allocated_byte_shared);
194 });
195
196 shambackends_module.def("get_mem_perf_info", []() {
198 });
199}
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
MemPerfInfos get_mem_perf_info()
Retrieve the memory performance information.
void reset_mem_info_max()
Reset the memory information for the maximum allocated bytes.
Pybind11 include and definitions.
#define ON_PYTHON_INIT
Register a Python module init function using static initialization.
void debug_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:133
Structure to store the performance informations about memory allocation and deallocation.
f64 time_alloc_host
Time spent allocating memory on the host.
size_t max_allocated_byte_host
max bytes allocated on the host
size_t allocated_byte_shared
Bytes allocated in shared memory.
f64 time_free_device
Time spent deallocating memory on the device.
f64 time_free_shared
Time spent deallocating memory in shared memory.
size_t max_allocated_byte_device
max bytes allocated on the device
f64 time_alloc_device
Time spent allocating memory on the device.
size_t allocated_byte_device
Bytes allocated on the device.
size_t allocated_byte_host
Bytes allocated on the host.
f64 time_free_host
Time spent deallocating memory on the host.
f64 time_alloc_shared
Time spent allocating memory in shared memory.
size_t max_allocated_byte_shared
max bytes allocated in shared memory