-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCudaSurface.cuh
44 lines (34 loc) · 1.16 KB
/
CudaSurface.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
#pragma once
#include "CudaArray.cuh"
template <class T>
struct CudaSurfaceAccessor {
cudaSurfaceObject_t m_cuSuf;
template <cudaSurfaceBoundaryMode mode = cudaBoundaryModeTrap>
__device__ __forceinline__ T read(int x, int y, int z) const {
return surf3Dread<T>(m_cuSuf, x * sizeof(T), y, z, mode);
}
template <cudaSurfaceBoundaryMode mode = cudaBoundaryModeTrap>
__device__ __forceinline__ void write(T val, int x, int y, int z) const {
surf3Dwrite<T>(val, m_cuSuf, x * sizeof(T), y, z, mode);
}
};
template <class T>
struct CudaSurface : CudaArray<T> {
cudaSurfaceObject_t m_cuSuf{};
explicit CudaSurface(uint3 const &_dim)
: CudaArray<T>(_dim) {
cudaResourceDesc resDesc{};
resDesc.resType = cudaResourceTypeArray;
resDesc.res.array.array = CudaArray<T>::getArray();
checkCudaErrors(cudaCreateSurfaceObject(&m_cuSuf, &resDesc));
}
cudaSurfaceObject_t getSurface() const {
return m_cuSuf;
}
CudaSurfaceAccessor<T> accessSurface() const {
return {m_cuSuf};
}
~CudaSurface() {
checkCudaErrors(cudaDestroySurfaceObject(m_cuSuf));
}
};