QTrk
Public Member Functions | Public Attributes | List of all members
device_vec< T > Class Template Reference

#include <gpu_utils.h>

Public Member Functions

 device_vec ()
 
 device_vec (size_t N)
 
 device_vec (const device_vec< T > &src)
 
 device_vec (const std::vector< T > &src)
 
 ~device_vec ()
 
void init (size_t s)
 
void free ()
 
 operator std::vector< T > () const
 
device_vec< T > & operator= (const std::vector< T > &src)
 
device_vec< T > & operator= (const device_vec< T > &src)
 
void copyToHost (T *dst, bool async, cudaStream_t s=0)
 
void copyToHost (std::vector< T > &dst, bool async, cudaStream_t s=0)
 
void copyToDevice (const std::vector< T > &src, bool async=false, cudaStream_t s=0)
 
void copyToDevice (const T *first, size_t size, bool async=false, cudaStream_t s=0)
 
std::vector< T > toVector ()
 
size_t memsize ()
 

Public Attributes

size_t size
 
T * data
 

Detailed Description

template<typename T>
class device_vec< T >

Todo:
Delete or free() not called explicitly anywhere. Needed?

Definition at line 68 of file gpu_utils.h.

Constructor & Destructor Documentation

§ device_vec() [1/4]

template<typename T>
device_vec< T >::device_vec ( )
inline

Definition at line 70 of file gpu_utils.h.

70  {
71  data = 0;
72  size = 0;
73  }
size_t size
Definition: gpu_utils.h:155

§ device_vec() [2/4]

template<typename T>
device_vec< T >::device_vec ( size_t  N)
inline

Definition at line 75 of file gpu_utils.h.

75  {
76  data = 0;
77  size = 0;
78  init(N);
79  }
void init(size_t s)
Definition: gpu_utils.h:93
size_t size
Definition: gpu_utils.h:155

§ device_vec() [3/4]

template<typename T>
device_vec< T >::device_vec ( const device_vec< T > &  src)
inline

Definition at line 80 of file gpu_utils.h.

80  {
81  data = 0; size = 0;
82  init(src.size);
83  dbgCUDAErrorCheck(cudaMemcpy(data, src.data, sizeof(T)*size, cudaMemcpyDeviceToDevice));
84  }
void init(size_t s)
Definition: gpu_utils.h:93
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ device_vec() [4/4]

template<typename T>
device_vec< T >::device_vec ( const std::vector< T > &  src)
inline

Definition at line 85 of file gpu_utils.h.

85  {
86  data=0; size=0;
87  init(src.size());
88  dbgCUDAErrorCheck(cudaMemcpy(data, &src[0], sizeof(T)*size, cudaMemcpyHostToDevice));
89  }
void init(size_t s)
Definition: gpu_utils.h:93
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ ~device_vec()

template<typename T>
device_vec< T >::~device_vec ( )
inline

Definition at line 90 of file gpu_utils.h.

90  {
91  free();
92  }
void free()
Definition: gpu_utils.h:104

Member Function Documentation

§ copyToDevice() [1/2]

template<typename T>
void device_vec< T >::copyToDevice ( const std::vector< T > &  src,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 137 of file gpu_utils.h.

137  {
138  copyToDevice(&src[0], src.size(), async, s);
139  }
void copyToDevice(const std::vector< T > &src, bool async=false, cudaStream_t s=0)
Definition: gpu_utils.h:137

§ copyToDevice() [2/2]

template<typename T>
void device_vec< T >::copyToDevice ( const T *  first,
size_t  size,
bool  async = false,
cudaStream_t  s = 0 
)
inline

Definition at line 140 of file gpu_utils.h.

140  {
141  if (this->size < size)
142  init(size);
143  if (async)
144  dbgCUDAErrorCheck(cudaMemcpyAsync(data, first, sizeof(T) * size, cudaMemcpyHostToDevice, s));
145  else
146  dbgCUDAErrorCheck(cudaMemcpy(data, first, sizeof(T) * size, cudaMemcpyHostToDevice));
147  }
void init(size_t s)
Definition: gpu_utils.h:93
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ copyToHost() [1/2]

template<typename T>
void device_vec< T >::copyToHost ( T *  dst,
bool  async,
cudaStream_t  s = 0 
)
inline

Definition at line 126 of file gpu_utils.h.

126  {
127  if (async)
128  dbgCUDAErrorCheck(cudaMemcpyAsync(dst, data, sizeof(T) * size, cudaMemcpyDeviceToHost, s));
129  else
130  dbgCUDAErrorCheck(cudaMemcpy(dst, data, sizeof(T) * size, cudaMemcpyDeviceToHost));
131  }
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ copyToHost() [2/2]

template<typename T>
void device_vec< T >::copyToHost ( std::vector< T > &  dst,
bool  async,
cudaStream_t  s = 0 
)
inline

Definition at line 132 of file gpu_utils.h.

132  {
133  if (dst.size() != size)
134  dst.resize(size);
135  copyToHost(&dst[0], async, s);
136  }
void copyToHost(T *dst, bool async, cudaStream_t s=0)
Definition: gpu_utils.h:126
size_t size
Definition: gpu_utils.h:155

§ free()

template<typename T>
void device_vec< T >::free ( )
inline

Definition at line 104 of file gpu_utils.h.

104  {
105  if (data) {
106  dbgCUDAErrorCheck(cudaFree(data));
107  data=0;
108  }
109  }
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ init()

template<typename T>
void device_vec< T >::init ( size_t  s)
inline

Definition at line 93 of file gpu_utils.h.

93  {
94  if(size != s) {
95  free();
96  }
97  if (s!=0) {
98  if (cudaMalloc(&data, sizeof(T)*s) != cudaSuccess) {
99  throw std::bad_alloc(SPrintf("device_vec<%s> init %d elements failed", typeid(T).name(), s).c_str());
100  }
101  size = s;
102  }
103  }
void free()
Definition: gpu_utils.h:104
size_t size
Definition: gpu_utils.h:155
std::string SPrintf(const char *fmt,...)
Definition: utils.cpp:132

§ memsize()

template<typename T>
size_t device_vec< T >::memsize ( )
inline

Definition at line 154 of file gpu_utils.h.

154 { return size*sizeof(T); }
size_t size
Definition: gpu_utils.h:155

§ operator std::vector< T >()

template<typename T>
device_vec< T >::operator std::vector< T > ( ) const
inline

Definition at line 110 of file gpu_utils.h.

110  {
111  std::vector<T> dst(size);
112  dbgCUDAErrorCheck(cudaMemcpy(&dst[0], data, sizeof(T)*size, cudaMemcpyDeviceToHost));
113  return dst;
114  }
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ operator=() [1/2]

template<typename T>
device_vec<T>& device_vec< T >::operator= ( const std::vector< T > &  src)
inline

Definition at line 115 of file gpu_utils.h.

115  {
116  init(src.size());
117  dbgCUDAErrorCheck(cudaMemcpy(data, &src[0], sizeof(T)*size, cudaMemcpyHostToDevice));
118  return *this;
119  }
void init(size_t s)
Definition: gpu_utils.h:93
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ operator=() [2/2]

template<typename T>
device_vec<T>& device_vec< T >::operator= ( const device_vec< T > &  src)
inline

Definition at line 120 of file gpu_utils.h.

120  {
121  clear();
122  init(src.size);
123  dbgCUDAErrorCheck(cudaMemcpy(data, src.data, sizeof(T)*size, cudaMemcpyDeviceToDevice));
124  return *this;
125  }
void init(size_t s)
Definition: gpu_utils.h:93
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

§ toVector()

template<typename T>
std::vector<T> device_vec< T >::toVector ( )
inline

Definition at line 149 of file gpu_utils.h.

149  {
150  std::vector<T> v (size);
151  dbgCUDAErrorCheck(cudaMemcpy(&v[0], data, sizeof(T)*size, cudaMemcpyDeviceToHost));
152  return v;
153  }
size_t size
Definition: gpu_utils.h:155
void dbgCUDAErrorCheck(cudaError_t e)
Definition: gpu_utils.h:63

Member Data Documentation

§ data

template<typename T>
T* device_vec< T >::data

Definition at line 156 of file gpu_utils.h.

§ size

template<typename T>
size_t device_vec< T >::size

Definition at line 155 of file gpu_utils.h.


The documentation for this class was generated from the following file: