Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
LazyScratchBuf.h
Go to the documentation of this file.
1
5#include <type_traits>
6#include <vector>
7
8#include "../utils/DeviceVector.h"
9#include "../utils/error_handling.h"
10
11namespace io
12{
13
14namespace io_detail
15{
16
17template <class T>
18struct TChecker {
19 static_assert(!(std::is_pointer_v<T> || std::is_reference_v<T>), "T should not be a pointer or reference");
20 static_assert(!(std::is_volatile_v<T> || std::is_const_v<T>), "T should not be volatile or const");
21 typedef T type;
22};
23
24template <class T>
25using CheckedT = typename TChecker<T>::type;
26
27template <typename VecType>
28typename VecType::value_type* resize_and_get_(VecType& buf, std::size_t buf_size)
29{
30 CHOLLA_ASSERT(buf_size > 0, "buf_size must be positive");
31 if (buf.size() < buf_size) {
32 buf.resize(buf_size);
33 }
34 return buf.data();
35}
36
37} // namespace io_detail
38
45{
48 std::vector<float> h_f32_buf;
49 std::vector<double> h_f64_buf;
50
51 public:
52 // prevent accidental deep copies (I can't imagine ever wanting this)
53
54 LazyScratchBuf() = default;
55 LazyScratchBuf(const LazyScratchBuf&) = delete;
56 LazyScratchBuf& operator=(const LazyScratchBuf&) = delete;
57
58 template <typename T>
59 io_detail::CheckedT<T>* get_buf_dev(std::size_t buf_size)
60 {
61 if constexpr (std::is_same_v<T, float>) {
62 return io_detail::resize_and_get_(this->d_f32_buf, buf_size);
63 } else if constexpr (std::is_same_v<T, double>) {
64 return io_detail::resize_and_get_(this->d_f64_buf, buf_size);
65 } else {
66 CHOLLA_ERROR("unrecognized type");
67 }
68 }
69
70 template <typename T>
71 io_detail::CheckedT<T>* get_buf_host(std::size_t buf_size)
72 {
73 if constexpr (std::is_same_v<T, float>) {
74 return io_detail::resize_and_get_(this->h_f32_buf, buf_size);
75 } else if constexpr (std::is_same_v<T, double>) {
76 return io_detail::resize_and_get_(this->h_f64_buf, buf_size);
77 } else {
78 CHOLLA_ERROR("unrecognized type");
79 }
80 }
81};
82
83} // namespace io
A templatized class to encapsulate a device global memory pointer in a std::vector like interface com...
Definition DeviceVector.h:45
Definition LazyScratchBuf.h:45
Definition LazyScratchBuf.h:18