DNDSR 0.2.1
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
DeviceStorage.cpp
Go to the documentation of this file.
1/// @file DeviceStorage.cpp
2/// @brief Host-backend implementation of @ref DNDS::DeviceStorage "DeviceStorage" and the
3/// #deviceStorageBase_deleter helper.
4
5#include "DeviceStorage.hpp"
6#include "DeviceStorage.hxx"
7
8namespace DNDS
9{
11
13 {
14 // Deleter callback passed to `std::shared_ptr`; the caller guarantees
15 // `p` was allocated with `new`. Smart-pointer ownership is tracked
16 // upstream.
17 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
18 delete p;
19 }
20
21 // DNDS_DEVICE_STORAGE_BASE_DELETER_INST(int, )
22 // DNDS_DEVICE_STORAGE_BASE_DELETER_INST(double, )
23 // DNDS_DEVICE_STORAGE_BASE_DELETER_INST(rowsize, )
24 // DNDS_DEVICE_STORAGE_BASE_DELETER_INST(real, )
25 // DNDS_DEVICE_STORAGE_BASE_DELETER_INST(index, )
26
27 // * explicit instantiation of device storage
28
29 // DNDS_DEVICE_STORAGE_INST(int, DeviceBackend::Host, )
30 // DNDS_DEVICE_STORAGE_INST(double, DeviceBackend::Host, )
31 //! how to resolve int-rowsize duplicate?
32 // DNDS_DEVICE_STORAGE_INST(rowsize, DeviceBackend::Host, )
33 // DNDS_DEVICE_STORAGE_INST(real, DeviceBackend::Host, )
34 // DNDS_DEVICE_STORAGE_INST(index, DeviceBackend::Host, )
35
36 // 1. specialize DeviceStorage
37 template <>
39 {
41 // std::vector<T> data;
42 size_t _size = 0;
43 uint8_t *data = nullptr;
44
45 public:
46 explicit DeviceStorage(size_t n) : // data(n),
47 _size(n)
48 {
49 }
50
51 uint8_t *raw_ptr() override
52 {
53 // return reinterpret_cast<void *>(data.data());
54 // ! point-back design:
55 return data;
56 }
57 void copy_host_to_device(uint8_t *host_ptr, size_t n_bytes) override
58 {
59 DNDS_assert_info(n_bytes == bytes(), "bytes size mismatch");
60 auto *host_T_ptr = host_ptr;
61 // std::copy(host_T_ptr, host_T_ptr + data.size(), data.begin());
62 // ! point-back design:
63 data = host_T_ptr;
64 }
65 void copy_device_to_host(uint8_t *host_ptr, size_t n_bytes) override
66 {
67 DNDS_assert_info(n_bytes == bytes(), "bytes size mismatch");
68 auto *host_T_ptr = host_ptr;
69 // std::copy(data.begin(), data.end(), host_T_ptr);
70 // ! point-back design:
71 // do nothing
72 }
73 void copy_to_device(uint8_t *device_ptr_dst, size_t n_bytes) override
74 {
75 DNDS_assert_info(n_bytes == bytes(), "bytes size mismatch");
76 auto *device_T_ptr_dst = device_ptr_dst;
77 // ! point-back design:
78 // do nothing
79 }
80
81 [[nodiscard]] size_t bytes() const override
82 {
83 // return data.size() * sizeof(T);
84 // ! point-back design:
85 return _size;
86 }
87 [[nodiscard]] DeviceBackend backend() const override
88 {
90 }
91 };
92
94 {
95 // return std::make_unique<DeviceStorage<T, DeviceBackend::CUDA>>(n_elem);
97 }
99 {
100 // return std::make_shared<DeviceStorage<T, DeviceBackend::CUDA>>(n_elem);
102 }
103}
Device memory abstraction layer with backend-specific storage and factory creation.
Reserved for template implementations of #DeviceStorage primitives.
#define DNDS_assert_info(expr, info)
Debug-only assertion with an extra std::string info message.
Definition Errors.hpp:117
Abstract interface to a byte buffer owned by a specific backend.
how to resolve int-rowsize duplicate?
DeviceBackend backend() const override
Which backend the buffer lives on.
uint8_t * raw_ptr() override
Raw byte pointer to the underlying storage.
void copy_to_device(uint8_t *device_ptr_dst, size_t n_bytes) override
Device-to-device copy of n_bytes into device_ptr_dst.
size_t bytes() const override
Buffer size in bytes.
void copy_device_to_host(uint8_t *host_ptr, size_t n_bytes) override
Copy n_bytes from this device buffer into host_ptr.
void copy_host_to_device(uint8_t *host_ptr, size_t n_bytes) override
Copy n_bytes from host_ptr into this device buffer.
Compile-time-specialised storage class; one definition per DeviceBackend.
the host side operators are provided as implemented
void deviceStorageBase_deleter(DeviceStorageBase *p)
Stateless deleter for DeviceStorageBase that works across shared-library boundaries where the vtable ...
DeviceBackend
Enumerates the backends a DeviceStorage / Array can live on.
@ Host
Plain CPU memory.
std::unique_ptr< DeviceStorageBase, std::function< void(DeviceStorageBase *)> > t_supDeviceStorageBase
Owning unique pointer to a DeviceStorageBase with cross-DLL-safe deleter.
std::shared_ptr< DeviceStorageBase > t_sspDeviceStorageBase
Shared pointer equivalent of t_supDeviceStorageBase.
static t_supDeviceStorageBase device_storage_create_unique(DeviceBackend backend, size_t n_bytes)
static t_sspDeviceStorageBase device_storage_create_shared(DeviceBackend backend, size_t n_bytes)
const tPoint const tPoint const tPoint & p
Eigen::Vector3d n(1.0, 0.0, 0.0)