-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcuda_shared_ptr.hpp
More file actions
149 lines (132 loc) · 3.05 KB
/
Copy pathcuda_shared_ptr.hpp
File metadata and controls
149 lines (132 loc) · 3.05 KB
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
/*
* (C) Copyright Karol Dzitkowski 2015
*
*/
#define CUDA_CALL(x) do { assert((x)==cudaSuccess); } while(0)
#ifndef CUDA_SMART_PTR_SHARED_PTR_HPP_
#define CUDA_SMART_PTR_SHARED_PTR_HPP_
#include <cuda_runtime_api.h>
#include <assert.h>
#include <algorithm> // for std::swap
#include <atomic>
#include <cstddef>
template <class T> class cuda_shared_ptr
{
private:
T * ptr;
std::atomic<int> * cnt;
public:
explicit cuda_shared_ptr()
{
init();
CUDA_CALL( cudaMalloc((void**)&ptr, sizeof(T)) );
}
explicit cuda_shared_ptr(size_t size)
{
init();
if (size == 0) ptr = nullptr;
else CUDA_CALL( cudaMalloc((void**)&ptr, size) );
}
explicit cuda_shared_ptr(T * p = 0) : ptr(p) { init(); }
~cuda_shared_ptr()
{
if (decCounter())
{
CUDA_CALL( cudaFree(ptr) );
delete cnt;
}
}
void reset(T * p = 0)
{
assert(p == 0 || p != ptr);
cuda_shared_ptr<T>(p).swap(*this);
}
T & operator*() const
{
assert(ptr != 0);
return *ptr;
}
T * operator->() const
{
assert(ptr != 0);
return ptr;
}
T * get() const { return ptr; }
operator bool() const
{
return ptr != 0;
}
void swap(cuda_shared_ptr & other) noexcept
{
std::swap(ptr, other.ptr);
std::swap(cnt, other.cnt);
}
int use_count()
{
return *cnt;
}
private:
bool decCounter(){ return --(*cnt) == 0; }
void incCounter(){ (*cnt)++; }
void init(){ cnt = new std::atomic<int>(); cnt->store(1); }
public:
cuda_shared_ptr(cuda_shared_ptr const & r) : ptr(r.ptr), cnt(r.cnt)
{
incCounter();
}
/* NOT YET SUPPORTED
cuda_shared_ptr( cuda_shared_ptr && r ) noexcept : ptr( r.ptr ), cnt()
{
swap(cnt, r.cnt);
r.px = 0;
}
*/
cuda_shared_ptr & operator=(cuda_shared_ptr const & r) noexcept
{
cuda_shared_ptr<T>(r).swap(*this);
return *this;
}
template<class Y>
cuda_shared_ptr & operator=(cuda_shared_ptr<Y> const & r) noexcept
{
cuda_shared_ptr<T>(r).swap(*this);
return *this;
}
};
template<class T, class U> inline bool operator==(
cuda_shared_ptr<T> const & a,
cuda_shared_ptr<U> const & b) noexcept
{
return a.get() == b.get();
}
template<class T, class U> inline bool operator!=(
cuda_shared_ptr<T> const & a,
cuda_shared_ptr<U> const & b) noexcept
{
return a.get() != b.get();
}
template<class T> inline bool operator==(
cuda_shared_ptr<T> const & p,
std::nullptr_t ) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator==(
std::nullptr_t,
cuda_shared_ptr<T> const & p) noexcept
{
return p.get() == 0;
}
template<class T> inline bool operator!=(
cuda_shared_ptr<T> const & p,
std::nullptr_t) noexcept
{
return p.get() != 0;
}
template<class T> inline bool operator!=(
std::nullptr_t,
cuda_shared_ptr<T> const & p) noexcept
{
return p.get() != 0;
}
#endif