-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsharedptr.h
210 lines (159 loc) · 3.66 KB
/
sharedptr.h
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
@file
@section License
TODO
@section description
Convenience class for shared CPU/GPU allocations.
Based on the X10 Runtime ideas described in Pai et al. in PACT 2012.
Also see NVIDIA Hemi's array.h at <https://github.com/harrism/hemi>
@author Sreepathi Pai <[email protected]>
*/
#pragma once
#include <cstdlib>
#include <cstdio>
#include <cuda.h>
#include <assert.h>
#include "cutil_subset.h"
template<typename T>
class Shared {
T **ptrs;
bool *owner;
bool *isCPU;
int max_devices;
size_t nmemb;
public:
Shared() {
nmemb = 0;
}
Shared(size_t nmemb) {
this->nmemb = nmemb;
max_devices = 2;
ptrs = (T **) calloc(max_devices, sizeof(T *));
owner = (bool *) calloc(max_devices, sizeof(bool));
isCPU = (bool *) calloc(max_devices, sizeof(bool));
isCPU[0] = true;
for(int i = 0; i < max_devices; i++)
owner[i] = true;
}
void alloc(size_t nmemb) {
assert(this->nmemb == 0);
this->nmemb = nmemb;
max_devices = 2;
ptrs = (T **) calloc(max_devices, sizeof(T *));
owner = (bool *) calloc(max_devices, sizeof(bool));
isCPU = (bool *) calloc(max_devices, sizeof(bool));
isCPU[0] = true;
for(int i = 0; i < max_devices; i++)
owner[i] = true;
}
void free()
{
for(int i = 0; i < max_devices; i++)
free_device(i);
}
bool free_device(int device = 0)
{
assert(device < max_devices);
if(!ptrs[device])
return true;
if(isCPU[device])
::free(ptrs[device]);
else
{
if(cudaFree(ptrs[device]) == cudaSuccess)
ptrs[device] = NULL;
else
return false;
}
return true;
}
bool find_owner(int &o)
{
int i;
for(i = 0; i < max_devices; i++)
if(owner[i]) {
o = i;
break;
}
return i < max_devices;
}
T *cpu_rd_ptr()
{
if(ptrs[0] == NULL)
ptrs[0] = (T *) calloc(nmemb, sizeof(T));
if(!owner[0])
{
int o;
if(find_owner(o))
copy(o, 0);
owner[0] = true;
}
return ptrs[0];
}
T *cpu_wr_ptr(bool overwrite = false)
{
if(ptrs[0] == NULL)
ptrs[0] = (T *) calloc(nmemb, sizeof(T));
if(!owner[0])
{
if(!overwrite)
{
int o;
if(find_owner(o))
copy(o, 0);
}
owner[0] = true;
}
for(int i = 1; i < max_devices; i++)
owner[i] = false;
return ptrs[0];
}
T *gpu_rd_ptr(int device = 1) /* device >= 1 */
{
assert(device >= 1);
if(ptrs[device] == NULL)
CUDA_SAFE_CALL(cudaMalloc(&ptrs[device], nmemb * sizeof(T)));
if(!owner[device])
{
int o;
if(find_owner(o))
copy(o, device);
owner[device] = true;
}
return ptrs[device];
}
T *gpu_wr_ptr(bool overwrite = false, int device = 1)
{
assert(device >= 1);
if(ptrs[device] == NULL)
CUDA_SAFE_CALL(cudaMalloc(&ptrs[device], nmemb * sizeof(T)));
if(!owner[device])
{
if(!overwrite)
{
int o;
if(find_owner(o))
copy(o, device);
}
owner[device] = true;
}
for(int i = 0; i < max_devices; i++)
if(i != device)
owner[i] = false;
return ptrs[device];
}
void copy(int src, int dst)
{
if(!ptrs[src])
return;
assert(ptrs[dst]);
if(isCPU[dst] && !isCPU[src]) {
CUDA_SAFE_CALL(cudaMemcpy(ptrs[dst], ptrs[src], nmemb * sizeof(T), cudaMemcpyDeviceToHost));
} else if (!isCPU[dst] && !isCPU[src]) {
CUDA_SAFE_CALL(cudaMemcpy(ptrs[dst], ptrs[src], nmemb * sizeof(T), cudaMemcpyDeviceToDevice));
} else if (!isCPU[dst] && isCPU[src]) {
CUDA_SAFE_CALL(cudaMemcpy(ptrs[dst], ptrs[src], nmemb * sizeof(T), cudaMemcpyHostToDevice));
} else
abort(); // cpu-to-cpu not implemented
}
};