Tinker9 70bd052 (Thu Nov 9 12:11:35 2023 -0800)
Loading...
Searching...
No Matches
genunit.h
1#pragma once
2#include "tool/darray.h"
3#include <cassert>
4#include <memory>
5
6namespace tinker {
7inline namespace v1 {
9{
12};
13
14template <GenericUnitVersion VERS>
16
17template <>
19{
20 static void deallocate(void*) {}
21 static void allocate(void**, size_t) {}
22 static void copyin(void*, const void*, size_t, int) {}
23};
24
25template <>
27{
28 static void deallocate(void* p)
29 {
31 }
32
33 static void allocate(void** pp, size_t nb)
34 {
36 }
37
38 static void copyin(void* d, const void* s, size_t nb, int queue)
39 {
40 deviceMemoryCopyinBytesAsync(d, s, nb, queue);
41 }
42};
43}
44
48template <class T,
49 GenericUnitVersion VERSION = GenericUnitVersion::DISABLE_ON_DEVICE>
51{
52private:
53 static constexpr bool USE_DPTR = (VERSION ==
54 GenericUnitVersion::DISABLE_ON_DEVICE
55 ? false
56 : true);
57
58 int unit;
59
61 using hostptr_vec = std::vector<std::unique_ptr<T>>;
62 using deviceptr_vec = std::vector<
63 std::unique_ptr<T, decltype(&mem_op::deallocate)>>;
64
65 static hostptr_vec& hostptrs()
66 {
67 static hostptr_vec o;
68 return o;
69 }
70
71 static deviceptr_vec& deviceptrs()
72 {
73 assert(USE_DPTR);
74 static deviceptr_vec o;
75 return o;
76 }
77
78 const T& obj() const
79 {
80 assert(0 <= unit && unit < (int)hostptrs().size() &&
81 "const T& GenericUnit::obj() const");
82 return *hostptrs()[unit];
83 }
84
85 T& obj()
86 {
87 assert(0 <= unit && unit < (int)hostptrs().size() &&
88 "T& GenericUnit::obj()");
89 return *hostptrs()[unit];
90 }
91
92public:
94 static int size()
95 {
96 if CONSTEXPR (USE_DPTR) assert(hostptrs().size() == deviceptrs().size());
97 return hostptrs().size();
98 }
99
101 static void clear()
102 {
103 hostptrs().clear(); // call ~T() on host
104 if CONSTEXPR (USE_DPTR)
105 deviceptrs().clear(); // call deallocate(T*) on device
106 }
107
110 template <class DT = T>
111 static void resize(int s)
112 {
113 static_assert(std::is_base_of<T, DT>::value, "");
114 assert(!USE_DPTR);
115 for (int i = size(); i < s; ++i)
116 hostptrs().emplace_back(new DT);
117 }
118
121 {
122 hostptrs().emplace_back(new T);
123 if CONSTEXPR (USE_DPTR) {
124 T* ptr;
125 mem_op::allocate(reinterpret_cast<void**>(&ptr), sizeof(T));
126 deviceptrs().emplace_back(ptr, mem_op::deallocate);
127 }
128 return size() - 1;
129 }
130
132 : unit(-1)
133 {}
134
136 : unit(u)
137 {}
138
139 operator int() const
140 {
141 return unit;
142 }
143
145 bool valid() const
146 {
147 return unit >= 0;
148 }
149
152 void close()
153 {
154 unit = -1;
155 }
156
159 const T& operator*() const
160 {
161 return obj();
162 }
163
165 {
166 return obj();
167 }
169
172 const T* operator->() const
173 {
174 return &obj();
175 }
176
178 {
179 return &obj();
180 }
182
185 const T* deviceptr() const
186 {
187 assert(0 <= unit && (size_t)unit < deviceptrs().size() &&
188 "const T* GenericUnit::deviceptr() const");
189 return deviceptrs()[unit].get();
190 }
191
193 {
194 assert(0 <= unit && unit < (int)deviceptrs().size() &&
195 "T* GenericUnit::deviceptr()");
196 return deviceptrs()[unit].get();
197 }
199
204 void deviceptrUpdate(const T& hobj, int queue)
205 {
206 assert(&hobj == &this->obj());
207 mem_op::copyin(this->deviceptr(), &hobj, sizeof(T), queue);
208 }
209};
210}
#define CONSTEXPR
Definition: macro.h:61
T * operator->()
Gets the (const) pointer to the object on host.
Definition: genunit.h:177
T * deviceptr()
Gets (const) device pointer to the object.
Definition: genunit.h:192
GenericUnit(int u)
Definition: genunit.h:135
GenericUnit()
Definition: genunit.h:131
static GenericUnit open()
Returns a new unit, similar to opening a new Fortran i/o unit.
Definition: genunit.h:120
bool valid() const
Whether the current unit is open.
Definition: genunit.h:145
void deviceptrUpdate(const T &hobj, int queue)
Updates the object on device by an object on host.
Definition: genunit.h:204
const T * operator->() const
Gets the (const) pointer to the object on host.
Definition: genunit.h:172
T & operator*()
Gets the (const) reference to the object on host.
Definition: genunit.h:164
static void clear()
Releases all of the resources and reset size() to 0.
Definition: genunit.h:101
const T & operator*() const
Gets the (const) reference to the object on host.
Definition: genunit.h:159
void close()
Closes the current unit.
Definition: genunit.h:152
static int size()
Gets the number of open units.
Definition: genunit.h:94
const T * deviceptr() const
Gets (const) device pointer to the object.
Definition: genunit.h:185
static void resize(int s)
Resizes the capacity for the objects on host.
Definition: genunit.h:111
Resource handle. Analogous to Fortran i/o unit represented by a signed integer.
Definition: genunit.h:51
void deviceMemoryAllocateBytes(void **pptr, size_t nbytes)
Allocates device pointer.
void deviceMemoryCopyinBytesAsync(void *dst, const void *src, size_t nbytes, int queue)
Similar to OpenACC async copyin, copies data from host to device.
void deviceMemoryDeallocate(void *ptr)
Deallocates device pointer.
GenericUnitVersion
Definition: genunit.h:9
Definition: testrt.h:9
static void copyin(void *, const void *, size_t, int)
Definition: genunit.h:22
static void deallocate(void *)
Definition: genunit.h:20
static void allocate(void **, size_t)
Definition: genunit.h:21
static void copyin(void *d, const void *s, size_t nb, int queue)
Definition: genunit.h:38
static void deallocate(void *p)
Definition: genunit.h:28
static void allocate(void **pp, size_t nb)
Definition: genunit.h:33
Definition: genunit.h:15