2#include "tool/darray.h"
14template <GenericUnitVersion VERS>
22 static void copyin(
void*,
const void*,
size_t,
int) {}
38 static void copyin(
void* d,
const void* s,
size_t nb,
int queue)
53 static constexpr bool USE_DPTR = (VERSION ==
54 GenericUnitVersion::DISABLE_ON_DEVICE
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)>>;
65 static hostptr_vec& hostptrs()
71 static deviceptr_vec& deviceptrs()
74 static deviceptr_vec o;
80 assert(0 <= unit && unit < (
int)hostptrs().
size() &&
81 "const T& GenericUnit::obj() const");
82 return *hostptrs()[unit];
87 assert(0 <= unit && unit < (
int)hostptrs().
size() &&
88 "T& GenericUnit::obj()");
89 return *hostptrs()[unit];
97 return hostptrs().size();
105 deviceptrs().clear();
110 template <
class DT = T>
113 static_assert(std::is_base_of<T, DT>::value,
"");
115 for (
int i =
size(); i < s; ++i)
116 hostptrs().emplace_back(
new DT);
122 hostptrs().emplace_back(
new T);
125 mem_op::allocate(
reinterpret_cast<void**
>(&ptr),
sizeof(T));
126 deviceptrs().emplace_back(ptr, mem_op::deallocate);
187 assert(0 <= unit && (
size_t)unit < deviceptrs().
size() &&
188 "const T* GenericUnit::deviceptr() const");
189 return deviceptrs()[unit].get();
194 assert(0 <= unit && unit < (
int)deviceptrs().
size() &&
195 "T* GenericUnit::deviceptr()");
196 return deviceptrs()[unit].get();
206 assert(&hobj == &this->obj());
207 mem_op::copyin(this->
deviceptr(), &hobj,
sizeof(T), queue);
#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
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