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
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
69Register_pymod(shambackendslibinit) {
70
71 py::module shambackends_module = m.def_submodule("backends", "backend library");
72
73 register_DeviceBuffer<u8>(shambackends_module, "DeviceBuffer_u8");
74 register_DeviceBuffer<u32>(shambackends_module, "DeviceBuffer_u32");
75 register_DeviceBuffer<f32>(shambackends_module, "DeviceBuffer_f32");
76 register_DeviceBuffer<f64>(shambackends_module, "DeviceBuffer_f64");
77 register_DeviceBuffer<f64_2>(shambackends_module, "DeviceBuffer_f64_2");
78 register_DeviceBuffer<f64_3>(shambackends_module, "DeviceBuffer_f64_3");
79
80 shambackends_module.def("reset_mem_info_max", []() {
82 });
83
84 py::class_<sham::MemPerfInfos>(shambackends_module, "MemPerfInfos")
85 .def(py::init([]() {
86 return sham::MemPerfInfos{};
87 }))
88 .def_readwrite(
89 "time_alloc_host",
91 py::return_value_policy::reference_internal)
92 .def_readwrite(
93 "time_alloc_device",
95 py::return_value_policy::reference_internal)
96 .def_readwrite(
97 "time_alloc_shared",
99 py::return_value_policy::reference_internal)
100 .def_readwrite(
101 "time_free_host",
103 py::return_value_policy::reference_internal)
104 .def_readwrite(
105 "time_free_device",
107 py::return_value_policy::reference_internal)
108 .def_readwrite(
109 "time_free_shared",
111 py::return_value_policy::reference_internal)
112 .def_readwrite(
113 "allocated_byte_host",
115 py::return_value_policy::reference_internal)
116 .def_readwrite(
117 "allocated_byte_device",
119 py::return_value_policy::reference_internal)
120 .def_readwrite(
121 "allocated_byte_shared",
123 py::return_value_policy::reference_internal)
124 .def_readwrite(
125 "max_allocated_byte_host",
127 py::return_value_policy::reference_internal)
128 .def_readwrite(
129 "max_allocated_byte_device",
131 py::return_value_policy::reference_internal)
132 .def_readwrite(
133 "max_allocated_byte_shared",
135 py::return_value_policy::reference_internal)
136 .def(
137 "__str__",
138 [](const sham::MemPerfInfos &mem_perf_infos) {
139 return shambase::format(
140 "MemPerfInfos(\n"
141 " time_alloc_host=\"{0:}\",\n"
142 " time_alloc_device=\"{1:}\",\n"
143 " time_alloc_shared=\"{2:}\",\n"
144 " time_free_host=\"{3:}\",\n"
145 " time_free_device=\"{4:}\",\n"
146 " time_free_shared=\"{5:}\",\n"
147 " allocated_byte_host=\"{6:}\",\n"
148 " allocated_byte_device=\"{7:}\",\n"
149 " allocated_byte_shared=\"{8:}\",\n"
150 " max_allocated_byte_host=\"{9:}\",\n"
151 " max_allocated_byte_device=\"{10:}\",\n"
152 " max_allocated_byte_shared=\"{11:}\")",
153 mem_perf_infos.time_alloc_host,
154 mem_perf_infos.time_alloc_device,
155 mem_perf_infos.time_alloc_shared,
156 mem_perf_infos.time_free_host,
157 mem_perf_infos.time_free_device,
158 mem_perf_infos.time_free_shared,
159 mem_perf_infos.allocated_byte_host,
160 mem_perf_infos.allocated_byte_device,
161 mem_perf_infos.allocated_byte_shared,
162 mem_perf_infos.max_allocated_byte_host,
163 mem_perf_infos.max_allocated_byte_device,
164 mem_perf_infos.max_allocated_byte_shared);
165 })
166 .def("__repr__", [](const sham::MemPerfInfos &mem_perf_infos) {
167 return shambase::format(
168 "MemPerfInfos(\n"
169 " time_alloc_host=\"{0:}\",\n"
170 " time_alloc_device=\"{1:}\",\n"
171 " time_alloc_shared=\"{2:}\",\n"
172 " time_free_host=\"{3:}\",\n"
173 " time_free_device=\"{4:}\",\n"
174 " time_free_shared=\"{5:}\",\n"
175 " allocated_byte_host=\"{6:}\",\n"
176 " allocated_byte_device=\"{7:}\",\n"
177 " allocated_byte_shared=\"{8:}\",\n"
178 " max_allocated_byte_host=\"{9:}\",\n"
179 " max_allocated_byte_device=\"{10:}\",\n"
180 " max_allocated_byte_shared=\"{11:}\")",
181 mem_perf_infos.time_alloc_host,
182 mem_perf_infos.time_alloc_device,
183 mem_perf_infos.time_alloc_shared,
184 mem_perf_infos.time_free_host,
185 mem_perf_infos.time_free_device,
186 mem_perf_infos.time_free_shared,
187 mem_perf_infos.allocated_byte_host,
188 mem_perf_infos.allocated_byte_device,
189 mem_perf_infos.allocated_byte_shared,
190 mem_perf_infos.max_allocated_byte_host,
191 mem_perf_infos.max_allocated_byte_device,
192 mem_perf_infos.max_allocated_byte_shared);
193 });
194
195 shambackends_module.def("get_mem_perf_info", []() {
197 });
198}
Header file describing a Node Instance.
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
void copy_from_stdvec(const std::vector< T > &vec)
Copy the content of a std::vector into the buffer.
void resize(size_t new_size, bool keep_data=true)
Resizes the buffer to a given size.
void fill(T value, std::array< size_t, 2 > idx_range)
Fill a subpart of the buffer with a given value.
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
T get_val_at_idx(size_t idx) const
Get the value at a given index in the buffer.
size_t get_size() const
Gets the number of elements in the buffer.
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 Register_pymod(placeholdername)
Register a python module init function using static initialisation.
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