PeTar
N-body code for collisional gravitational systems
cuda_pointer.h
Go to the documentation of this file.
1 #pragma once
2 #include <assert.h>
3 #include <cuda.h>
4 #include <cuda_runtime.h>
5 //#include <helper_cuda.h>
6 //#define CUDA_SAFE_CALL checkCudaErrors
7 #define CUDA_SAFE_CALL(val) val
8 
9 template <typename T>
10 struct cudaPointer{
13  size_t size;
15  dev_pointer = NULL;
16  host_pointer = NULL;
17  size = 0;
18  }
19 // ~cudaPointer(){
20 // free();
21 // }
22  void allocate(int _size){
23  size = _size;
24  void *p;
25  CUDA_SAFE_CALL(cudaMalloc(&p, size * sizeof(T)));
26  assert(p);
27  dev_pointer = (T*)p;
28  CUDA_SAFE_CALL(cudaMallocHost(&p, size * sizeof(T)));
29  assert(p);
30  host_pointer = (T*)p;
31  }
32  void free(){
33  CUDA_SAFE_CALL(cudaFree(dev_pointer));
34  CUDA_SAFE_CALL(cudaFreeHost(host_pointer));
35  dev_pointer = NULL;
36  host_pointer = NULL;
37  size = 0;
38  }
39  void htod(int count){
40  CUDA_SAFE_CALL(cudaMemcpy(dev_pointer, host_pointer, count * sizeof(T), cudaMemcpyHostToDevice));
41  }
42  void htod(){
43  this->htod(size);
44  }
45  void dtoh(int count){
46  CUDA_SAFE_CALL(cudaMemcpy(host_pointer, dev_pointer, count * sizeof(T), cudaMemcpyDeviceToHost));
47  }
48  void dtoh(){
49  this->dtoh(size);
50  }
51  T &operator[] (int i){
52  return host_pointer[i];
53  }
54  operator T* (){
55  return dev_pointer;
56  }
57 };
cudaPointer::host_pointer
T * host_pointer
Definition: cuda_pointer.h:12
cudaPointer::dtoh
void dtoh(int count)
Definition: cuda_pointer.h:45
cudaPointer
Definition: cuda_pointer.h:10
cudaPointer::allocate
void allocate(int _size)
Definition: cuda_pointer.h:22
cudaPointer::dev_pointer
T * dev_pointer
Definition: cuda_pointer.h:11
cudaPointer::htod
void htod(int count)
Definition: cuda_pointer.h:39
cudaPointer::free
void free()
Definition: cuda_pointer.h:32
cudaPointer::dtoh
void dtoh()
Definition: cuda_pointer.h:48
cudaPointer::cudaPointer
cudaPointer()
Definition: cuda_pointer.h:14
cudaPointer::htod
void htod()
Definition: cuda_pointer.h:42
CUDA_SAFE_CALL
#define CUDA_SAFE_CALL(val)
Definition: cuda_pointer.h:7
cudaPointer::size
size_t size
Definition: cuda_pointer.h:13
cudaPointer::operator[]
T & operator[](int i)
Definition: cuda_pointer.h:51