35 template <
typename ArrayType>
40 assert(array_size > 0);
43 size_t max_index = std::numeric_limits<size_t>::max();
44 if (max_index /
sizeof(ArrayType) < array_size)
46 std::cerr <<
"The size of array in bytes exceeds the maximum " \
47 <<
"integer limit, which is: " << max_index <<
". The " \
48 <<
"array size is: " << array_size <<
", and the size of " \
49 <<
"data type is: " <<
sizeof(ArrayType) <<
"-bytes." \
54 ArrayType* device_array;
55 size_t num_bytes =
static_cast<size_t>(array_size) *
sizeof(ArrayType);
56 cudaError_t error =
cudaMalloc(&device_array, num_bytes);
57 assert(error == cudaSuccess);
75 template <
typename ArrayType>
77 ArrayType*& device_array,
82 assert(array_size > 0);
85 size_t max_index = std::numeric_limits<size_t>::max();
86 if (max_index /
sizeof(ArrayType) < array_size)
88 std::cerr <<
"The size of array in bytes exceeds the maximum " \
89 <<
"integer limit, which is: " << max_index <<
". The " \
90 <<
"array size is: " << array_size <<
", and the size of " \
91 <<
"data type is: " <<
sizeof(ArrayType) <<
"-bytes." \
96 size_t num_bytes =
static_cast<size_t>(array_size) *
sizeof(ArrayType);
97 cudaError_t error =
cudaMalloc(&device_array, num_bytes);
98 assert(error == cudaSuccess);
114 template <
typename ArrayType>
117 const size_t num_bytes)
121 assert(num_bytes > 0);
123 cudaError_t error =
cudaMalloc(&device_array, num_bytes);
124 assert(error == cudaSuccess);
141 template <
typename ArrayType>
143 const ArrayType* host_array,
145 ArrayType* device_array)
147 size_t num_bytes =
static_cast<size_t>(array_size) *
sizeof(ArrayType);
148 cudaError_t error =
cudaMemcpy(device_array, host_array, num_bytes,
149 cudaMemcpyHostToDevice);
150 assert(error == cudaSuccess);
165 template <
typename ArrayType>
168 if (device_array != NULL)
170 cudaError_t error =
cudaFree(device_array);
171 assert(error == cudaSuccess);
187 template<
typename ArrayType>
191 assert(error == cudaSuccess);
205 template<
typename ArrayType>
210 assert(error == cudaSuccess);
An interface to CUDA linrary to facilitate working with CUDA, such as memory allocation,...
static int get_device()
Gets the current device in multi-gpu applications.
static void del(void *device_array)
Deletes memory on gpu device if its pointer is not NULL, then sets the pointer to NULL.
static void alloc_bytes(void *&device_array, const size_t num_bytes)
Allocates memory on gpu device. This function uses an existing given pointer.
static ArrayType * alloc(const LongIndexType array_size)
Allocates memory on gpu device. This function creates a pointer and returns it.
static void copy_to_device(const ArrayType *host_array, const LongIndexType array_size, ArrayType *device_array)
Copies memory on host to device memory.
static void set_device(int device_id)
Sets the current device in multi-gpu applications.
cudaError_t cudaGetDevice(int *device)
Definition of CUDA's cudaGetDevice function using dynamically loaded cudart library.
cudaError_t cudaSetDevice(int device)
Definition of CUDA's cudaSetDevice function using dynamically loaded cudart library.
cudaError_t cudaFree(void *devPtr)
Definition of CUDA's cudaFree function using dynamically loaded cudart library.
cudaError_t cudaMemcpy(void *dst, const void *src, size_t count, cudaMemcpyKind kind)
Definition of CUDA's cudaMemcpy function using dynamically loaded cudart library.
cudaError_t cudaMalloc(void **devPtr, size_t size)
Definition of CUDA's cudaMalloc function using dynamically loaded cudart library.