-
Notifications
You must be signed in to change notification settings - Fork 0
/
CudaArray.cuh
45 lines (38 loc) · 1.51 KB
/
CudaArray.cuh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#include <cuda_runtime.h>
#include "helper_cuda.h"
#include "Noncopyable.h"
template <class T>
struct CudaArray : Noncopyable {
cudaArray *m_cuArray{};
uint3 m_dim{};
explicit CudaArray(uint3 const &_dim)
: m_dim(_dim) {
cudaExtent extent = make_cudaExtent(m_dim.x, m_dim.y, m_dim.z);
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<T>();
checkCudaErrors(cudaMalloc3DArray(&m_cuArray, &channelDesc, extent, cudaArraySurfaceLoadStore));
}
void copyIn(T const *_data) {
cudaMemcpy3DParms copy3DParams{};
copy3DParams.srcPtr = make_cudaPitchedPtr((void *)_data, m_dim.x * sizeof(T), m_dim.x, m_dim.y);
copy3DParams.dstArray = m_cuArray;
copy3DParams.extent = make_cudaExtent(m_dim.x, m_dim.y, m_dim.z);
copy3DParams.kind = cudaMemcpyHostToDevice;
checkCudaErrors(cudaMemcpy3D(©3DParams));
}
void copyOut(T *_data) {
cudaMemcpy3DParms copy3DParams{};
copy3DParams.srcArray = m_cuArray;
copy3DParams.dstPtr = make_cudaPitchedPtr((void *)_data, m_dim.x * sizeof(T), m_dim.x, m_dim.y);
copy3DParams.extent = make_cudaExtent(m_dim.x, m_dim.y, m_dim.z);
copy3DParams.kind = cudaMemcpyDeviceToHost;
//printf("%p %d %d %d\n", _data, m_dim.x, m_dim.y, m_dim.z);
checkCudaErrors(cudaMemcpy3D(©3DParams));
}
cudaArray *getArray() const {
return m_cuArray;
}
~CudaArray() {
checkCudaErrors(cudaFreeArray(m_cuArray));
}
};