54 lines
1.1 KiB
C++
54 lines
1.1 KiB
C++
#include <assert.h>
|
|
// #include <cutil.h>
|
|
#include <helper_cuda.h>
|
|
|
|
|
|
template <typename T>
|
|
struct cudaPointer{
|
|
T *dev_pointer;
|
|
T *host_pointer;
|
|
int size;
|
|
cudaPointer(){
|
|
dev_pointer = NULL;
|
|
host_pointer = NULL;
|
|
size = 0;
|
|
}
|
|
~cudaPointer(){
|
|
// free();
|
|
}
|
|
void allocate(int _size){
|
|
size = _size;
|
|
void *p;
|
|
checkCudaErrors(cudaMalloc(&p, size * sizeof(T)));
|
|
assert(p);
|
|
dev_pointer = (T*)p;
|
|
checkCudaErrors(cudaMallocHost(&p, size * sizeof(T)));
|
|
assert(p);
|
|
host_pointer = (T*)p;
|
|
}
|
|
void free(){
|
|
checkCudaErrors(cudaFree(dev_pointer));
|
|
checkCudaErrors(cudaFreeHost(host_pointer));
|
|
dev_pointer = NULL;
|
|
host_pointer = NULL;
|
|
size = 0;
|
|
}
|
|
void htod(int count){
|
|
checkCudaErrors(cudaMemcpy(dev_pointer, host_pointer, count * sizeof(T), cudaMemcpyHostToDevice));
|
|
}
|
|
void htod(){
|
|
this->htod(size);
|
|
}
|
|
void dtoh(int count){
|
|
checkCudaErrors(cudaMemcpy(host_pointer, dev_pointer, count * sizeof(T), cudaMemcpyDeviceToHost));
|
|
}
|
|
void dtoh(){
|
|
this->dtoh(size);
|
|
}
|
|
T &operator [] (int i){
|
|
return host_pointer[i];
|
|
}
|
|
operator T* (){
|
|
return dev_pointer;
|
|
}
|
|
};
|