23#include "../global/global.h"
24#include "../global/global_cuda.h"
25#include "../utils/gpu.hpp"
30namespace cuda_utilities
46 static_assert(std::is_trivially_copyable_v<T>,
47 "DeviceVector can only be used with trivially_copyable types due to the internal "
48 "usage of functions like cudaMemcpy, cudaMemcpyPeer, cudaMemset");
69 : _size(std::exchange(other._size, 0)), _ptr(std::exchange(other._ptr,
nullptr))
76 std::swap(_size, other._size);
77 std::swap(_ptr, other._ptr);
106 size_t size() {
return _size; }
127 T
at(
size_t const index);
137 void assign(T
const &hostValue,
size_t const &index = 0);
209 void _allocate(
size_t const size)
215 GPU_Error_Check(cudaMalloc(&_ptr, _size *
sizeof(T)));
225 if (_ptr !=
nullptr) GPU_Error_Check(cudaFree(_ptr));
236namespace cuda_utilities
250 GPU_Error_Check(cudaMemset(_ptr, 0, _size *
sizeof(T)));
264 size_t const count = std::min(_size, newSize) *
sizeof(T);
269 if (oldDevPtr !=
nullptr) {
271 GPU_Error_Check(cudaMemcpyPeer(_ptr, 0, oldDevPtr, 0, count));
274 GPU_Error_Check(cudaFree(oldDevPtr));
293 GPU_Error_Check(cudaMemcpy(&hostValue, &(_ptr[index]),
sizeof(T), cudaMemcpyDeviceToHost));
305 return (*
this)[index];
307 throw std::out_of_range(
308 "Warning: DeviceVector.at() detected an"
309 " out of bounds memory access. Tried to"
311 std::to_string(index) +
" of " + std::to_string(_size));
320 GPU_Error_Check(cudaMemcpy(&(_ptr[index]),
322 sizeof(T), cudaMemcpyHostToDevice));
330 if (arrSize <= _size) {
331 GPU_Error_Check(cudaMemcpy(_ptr, arrIn, arrSize *
sizeof(T), cudaMemcpyHostToDevice));
333 throw std::out_of_range(
334 "Warning: Couldn't copy array to device,"
335 " device array is too small. Host array"
337 std::to_string(arrSize) +
", device array size=" + std::to_string(arrSize));
346 if (_size <= arrSize) {
347 GPU_Error_Check(cudaMemcpy(arrOut, _ptr, _size *
sizeof(T), cudaMemcpyDeviceToHost));
349 throw std::out_of_range(
350 "Warning: Couldn't copy array to host, "
351 "host array is too small. Host array "
353 std::to_string(arrSize) +
", device array size=" + std::to_string(arrSize));
A templatized class to encapsulate a device global memory pointer in a std::vector like interface com...
Definition DeviceVector.h:45
void assign(T const &hostValue, size_t const &index=0)
Assign a single value in the array. Should generally only be used when the pointer points to a scalar...
Definition DeviceVector.h:318
void cpyDeviceToHost(T *arrOut, size_t const &arrSize)
Copy the array from the device to a host array. Checks if the host array is large enough based on the...
Definition DeviceVector.h:344
~DeviceVector()
Destroy the Device Vector object by calling the _deAllocate private method.
Definition DeviceVector.h:86
void cpyHostToDevice(const T *arrIn, size_t const &arrSize)
Copy the first arrSize elements of arrIn to the device.
Definition DeviceVector.h:328
void cpyHostToDevice(std::vector< T > const &vecIn)
Copy the contents of a std::vector to the device.
Definition DeviceVector.h:178
T at(size_t const index)
Return a value from device memory. This method performs a cudaMemcpy to copy the desired element to t...
Definition DeviceVector.h:300
void reset(size_t const newSize)
Reset the size of the array. This frees the old array and allocates a new one; all values in the arra...
Definition DeviceVector.h:281
DeviceVector(DeviceVector< T > &&other) noexcept
Definition DeviceVector.h:68
T * data()
Get the raw device pointer.
Definition DeviceVector.h:99
void resize(size_t const newSize)
Resize the device container to contain newSize elements. If newSize is greater than the current size ...
Definition DeviceVector.h:258
DeviceVector() noexcept
Definition DeviceVector.h:54
T operator[](size_t const &index)
Overload the [] operator to return a value from device memory. This method performs a cudaMemcpy to c...
Definition DeviceVector.h:290
DeviceVector(size_t const size, bool const initialize=false)
Construct a new Device Vector object by calling the _allocate private method.
Definition DeviceVector.h:244
void cpyDeviceToHost(std::vector< T > &vecOut)
Copy the array from the device to a host std::vector. Checks if the host array is large enough.
Definition DeviceVector.h:195
DeviceVector & operator=(DeviceVector< T > &&other) noexcept
Definition DeviceVector.h:74
size_t size()
Get the number of elements in the array.
Definition DeviceVector.h:106