public
Last active

Cuda Memory wrapper class

  • Download Gist
CudaMemory.h
C
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/*
* Cuda memory wrapper
*/
 
#ifndef CUDAMEMORY_H
#define CUDAMEMORY_H
 
#ifndef NDEBUG
#include <iostream>
#include <stdlib.h>
#endif
 
#include <vector>
#include <cuda_runtime.h>
 
namespace gpu {
// assert macro
#ifndef NDEBUG
#define gpu__assert(condition, message) \
do { \
if (! (condition)) { \
std::cerr << "Assertion `" #condition "` failed in " << __FILE__ \
<< " line " << __LINE__ << ": " << message << std::endl; \
/*std::exit(EXIT_FAILURE); */ throw "Assertion error!"; \
} \
} while (false)
#else
#define gpu__assert(condition, message) do { } while (false)
#endif
 
// general types
typedef unsigned int ref_index;
typedef unsigned int memcount_t;
typedef unsigned int index_t;
typedef long count_t;
 
namespace internal {
std::vector<count_t> refCounts;
static std::vector<index_t> freeIndexes;
 
index_t newReference() {
if (!freeIndexes.empty()) {
index_t r = freeIndexes[freeIndexes.size() - 1];
freeIndexes.pop_back();
return r;
}
index_t ref_id = refCounts.size();
refCounts.push_back(1);
return ref_id;
}
 
void freeReference(index_t ref_id, void* ptr) {
count_t newCount = --refCounts[ref_id];
gpu__assert(newCount >= 0, "Count is negative!");
if (newCount == 0) {
std::cout << "Freeing ref#" << ref_id << std::endl;
// we should free it now
cudaFree(ptr);
// and make the reference index available
freeIndexes.push_back(ref_id);
}
}
}
 
template <class S>
class CudaMemory {
public:
// template types
typedef CudaMemory<S> this_type;
typedef S scalar_type;
 
// constructors
 
CudaMemory() : count(0), d_ptr(0), ref_id(0) {
}
 
CudaMemory(memcount_t c) : count(0), d_ptr(0), ref_id(0) {
// trying to allocate the data
if (cudaMalloc((void**) &d_ptr, sizeof (scalar_type) * c) == cudaSuccess) {
// now we can do something
count = c;
ref_id = internal::newReference();
}
}
// copy
 
CudaMemory(const CudaMemory::this_type& orig) : count(orig.count), d_ptr(orig.d_ptr), ref_id(orig.ref_id) {
// update reference count
if (count > 0) {
// let's increment the count
++internal::refCounts[ref_id];
}
}
 
this_type& operator =(const this_type& other) {
// free current content
if (count > 0) {
internal::freeReference(ref_id, (void*) d_ptr);
count = 0;
}
// copy new content
count = other.count;
d_ptr = other.d_ptr;
ref_id = other.ref_id;
// update reference count
if (count > 0) {
++internal::refCounts[ref_id];
}
}
 
// free
 
~CudaMemory() {
// free content
if (count > 0) {
internal::freeReference(ref_id, (void*) d_ptr);
}
}
 
// transfers
 
void copyFrom(S* host_ptr) {
gpu__assert(count > 0, "Nothing to copy!");
cudaMemcpy(d_ptr, host_ptr, sizeof (scalar_type) * count, cudaMemcpyHostToDevice);
}
 
void copyTo(S* host_ptr) {
gpu__assert(count > 0, "Nothing to copy!");
cudaMemcpy(host_ptr, d_ptr, sizeof (scalar_type) * count, cudaMemcpyDeviceToHost);
}
 
// overloading i/o operators
template<typename T>
friend this_type& operator <<(this_type& mem, const T&);
template<typename T>
friend this_type& operator >>(this_type& mem, T&);
 
// getters
 
bool empty() const {
return count == 0;
}
 
scalar_type* get() {
return d_ptr;
}
 
count_t ref_count() const {
if (count > 0) return internal::refCounts[ref_id];
else return 0;
}
memcount_t size() const {
return count;
}
 
// implicit conversion
 
/* operator scalar_type*() {
return d_ptr;
} */
private:
memcount_t count;
scalar_type* d_ptr;
ref_index ref_id;
};
 
// names
typedef CudaMemory<char> CudaCharMemory;
typedef CudaMemory<int> CudaIntMemory;
typedef CudaMemory<long> CudaLongMemory;
typedef CudaMemory<float> CudaFloatMemory;
typedef CudaMemory<double> CudaDoubleMemory;
 
}
 
#endif /* CUDAMEMORY_H */

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.