-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.cu
406 lines (318 loc) · 11.7 KB
/
reduce.cu
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "kernels/reducevoxelbycoord.cuh"
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <thrust/system_error.h>
#include <thrust/sort.h>
#include <cuda.h>
#include <iostream>
#include <chrono>
#include <cstdlib>
using std::chrono::steady_clock;
using std::chrono::steady_clock;
using namespace mgpu;
/////////////////////////////////////////////////////////////////
#define BATCH_SIZE 136
#define VERTICES 8
const int imgsize = 500 * (500 / 2 + 1);
#define cudaCheckErrors(msg) \
do { \
cudaError_t __err = cudaGetLastError(); \
if (__err != cudaSuccess) { \
fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
msg, cudaGetErrorString(__err), \
__FILE__, __LINE__); \
fprintf(stderr, "*** FAILED - ABORTING\n"); \
exit(1); \
} \
} while (0)
/////////////////////////////////////////////////////////////////
typedef int CRSCoord;
//typedef double Voxel;
class Voxel {
public:
__host__ __device__ __forceinline__
Voxel(double r = 0, double i = 0, double w = 0) {
real = r; imag = i; weit = w;
}
__host__ __device__ __forceinline__
Voxel operator+(const Voxel& that) const {
return Voxel(real + that.real,
imag + that.imag,
weit + that.weit);
}
__host__ __device__ __forceinline__
Voxel& operator+=(const Voxel& that) {
real += that.real;
imag += that.imag;
weit += that.weit;
return *this;
}
public:
double real;
double imag;
double weit;
};
/////////////////////////////////////////////////////////////////
int read_data_stream(CRSCoord *coords, Voxel *voxels)
{
FILE *fcor = fopen("../../data/stream/coords.dat", "rb");
FILE *fvxl = fopen("../../data/stream/voxels.dat", "rb");
const int total_size = BATCH_SIZE * imgsize * VERTICES;
std::cout << "Data reading begin..." << std::endl;
steady_clock::time_point begin = steady_clock::now();
if (fcor && fvxl) {
if (fread(coords,
sizeof(CRSCoord),
BATCH_SIZE * imgsize * VERTICES,
fcor)
== total_size) {
fclose(fcor);
} else {
std::cout << "coords.dat read error!\n";
exit(1);
}
if (fread(voxels,
sizeof(Voxel),
BATCH_SIZE * imgsize * VERTICES,
fvxl)
== total_size) {
fclose(fvxl);
} else {
std::cout << "voxels.dat read error!\n";
exit(1);
}
} else {
std::cout << "file open error!\n";
exit(2);
}
//for (int i = 0; i < total_size; ++i)
// printf("coord:%10d, voxel:%15.6f\n", coords[i], voxels[i]);
steady_clock::time_point end = steady_clock::now();
std::cout << "Data reading done with time consumed: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count()
<< "ms" << std::endl;
return total_size;
}
void voxels_reshape(Voxel *voxels)
{
const int total_size = BATCH_SIZE * imgsize * VERTICES;
std::cout << "Data reshaping begin..." << std::endl;
steady_clock::time_point begin = steady_clock::now();
double *temp = (double*)malloc(total_size * sizeof(Voxel));
for (int i = 0; i < total_size; ++i) {
temp[i] = voxels[i].real;
temp[total_size + i] = voxels[i].imag;
temp[total_size * 2 + i] = voxels[i].weit;
}
memcpy(voxels, temp, total_size * sizeof(Voxel));
free(temp);
steady_clock::time_point end = steady_clock::now();
std::cout << "Data reshaped done with time consumed: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count()
<< "ms" << std::endl;
}
void record(CRSCoord *coords, CRSCoord *dev_coords,
Voxel *voxels, Voxel *dev_voxels,
const int length, const char *filename,
bool transpose = false)
{
const int total_size = BATCH_SIZE * imgsize * VERTICES;
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("File %s open error!\n", filename);
exit(1);
}
cudaMemcpy(coords,
dev_coords,
length * sizeof(CRSCoord),
cudaMemcpyDeviceToHost);
cudaMemcpy(voxels,
dev_voxels,
total_size * sizeof(Voxel),
cudaMemcpyDeviceToHost);
cudaCheckErrors("offload voxel stream");
if (!transpose) {
for (int i = 0; i < length; ++i) {
fprintf(fp, "coord:%12d, voxel:(%15.6f,%15.6f,%15.6f)\n",
coords[i], voxels[i].real, voxels[i].imag, voxels[i].weit);
}
}else {
double *temp = (double*)voxels;
for (int i = 0; i < length; ++i) {
fprintf(fp, "coord:%12d, voxel:(%15.6f,%15.6f,%15.6f)\n",
coords[i], temp[i], temp[total_size + i],
temp[total_size * 2 + i]);
}
}
fclose(fp);
}
void statistic(CRSCoord *coords, Voxel *voxels, int length)
{
int segment = 0, counter = 1;
int current = 0, next;
while (current < length) {
next = current < length - 1 ? current + 1 : current;
if (next != current && coords[next] == coords[current]) {
counter++;
} else {
printf("Segment No.%8d: length %8d\n", segment, counter);
segment++;
counter = 1;
}
current++;
}
}
/////////////////////////////////////////////////////////////////
__global__ void kernel_reduce(CRSCoord *dev_cors,
Voxel *dev_vxls,
CRSCoord *dev_r_cors,
Voxel *dev_r_vxls)
{
;
}
/////////////////////////////////////////////////////////////////
void partial_reduce(CRSCoord *raw_dev_cors, Voxel *raw_dev_vxls,
CRSCoord *raw_r_dev_cors, Voxel *raw_r_dev_vxls)
{
;
}
int mgpu_reduce(CRSCoord *raw_dev_cors, double *raw_dev_vxls,
CRSCoord *raw_r_dev_cors, double *raw_r_dev_vxls,
CudaContext& context)
{
int *counts;
cudaMalloc((void**)&counts, sizeof(int));
context.Start();
ReduceVoxelByCoord(raw_dev_cors,
raw_dev_vxls,
BATCH_SIZE * imgsize * VERTICES,
(double)0,
mgpu::plus<double>(),
mgpu::equal_to<CRSCoord>(),
raw_r_dev_cors,
raw_r_dev_vxls,
(int*)0,
counts,
context);
double milliseconds = context.Split();
std::cout << "MGPU reduce done with time consumed: "
<< milliseconds << "ms" << std::endl;
int retrieval = 0;
cudaMemcpy(&retrieval, counts, sizeof(int), cudaMemcpyDeviceToHost);
return retrieval;
}
int thrust_reduce(CRSCoord *raw_dev_cors, Voxel *raw_dev_vxls,
CRSCoord *raw_r_dev_cors, Voxel *raw_r_dev_vxls)
{
thrust::device_ptr<CRSCoord> dev_cors(raw_dev_cors);
thrust::device_ptr<Voxel> dev_vxls(raw_dev_vxls);
thrust::device_ptr<CRSCoord> reduced_cors(raw_r_dev_cors);
thrust::device_ptr<Voxel> reduced_vxls(raw_r_dev_vxls);
thrust::pair<thrust::device_ptr<CRSCoord>,
thrust::device_ptr<Voxel> > reduce_end;
reduce_end.first = reduced_cors;
reduce_end.second = reduced_vxls;
cudaEvent_t time_start, time_end;
cudaEventCreate(&time_start);
cudaEventCreate(&time_end);
try {
cudaEventRecord(time_start);
reduce_end = thrust::reduce_by_key(dev_cors,
dev_cors + BATCH_SIZE * imgsize * VERTICES,
dev_vxls,
reduced_cors,
reduced_vxls);
cudaEventRecord(time_end);
} catch (thrust::system_error e) {
std::cout << "Error detected in reduce by key: "
<< e.what() << std::endl;
exit(1);
}
cudaEventSynchronize(time_end);
cudaCheckErrors("event sync");
float milliseconds = 0;
cudaEventElapsedTime(&milliseconds, time_start, time_end);
std::cout << "Thrust reduce done with time consumed: "
<< milliseconds << "ms" << std::endl;
return thrust::distance(reduced_cors, reduce_end.first);
}
/////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
CRSCoord *coords, *r_coords, *raw_dev_cors, *raw_r_dev_cors;
Voxel *voxels, *r_voxels, *raw_dev_vxls, *raw_r_dev_vxls;
const int size = BATCH_SIZE * imgsize * VERTICES;
/* host buffer */
cudaHostAlloc((void**)&coords, size * sizeof(CRSCoord),
cudaHostAllocDefault);
cudaHostAlloc((void**)&voxels, size * sizeof(Voxel),
cudaHostAllocDefault);
/* reduction result for verification */
cudaHostAlloc((void**)&r_coords, size * sizeof(CRSCoord),
cudaHostAllocDefault);
cudaHostAlloc((void**)&r_voxels, size * sizeof(Voxel),
cudaHostAllocDefault);
/* on device buffer */
cudaMalloc((void**)&raw_dev_cors, size * sizeof(CRSCoord));
cudaMalloc((void**)&raw_dev_vxls, size * sizeof(Voxel));
cudaMalloc((void**)&raw_r_dev_cors, size * sizeof(CRSCoord));
cudaMalloc((void**)&raw_r_dev_vxls, size * sizeof(Voxel));
cudaCheckErrors("Memory allocation");
/* data reading */
int length = read_data_stream(coords, voxels);
/* analysis */
//statistic(coords, voxels, length);
/* upload data */
cudaMemcpy(raw_dev_cors,
coords,
length * sizeof(CRSCoord),
cudaMemcpyHostToDevice);
cudaMemcpy(raw_dev_vxls,
voxels,
length * sizeof(Voxel),
cudaMemcpyHostToDevice);
cudaCheckErrors("Memory copy H2D");
/* ---------------------------------------------------- *
* thrust reduce
* ---------------------------------------------------- */
int tlen = thrust_reduce(raw_dev_cors, raw_dev_vxls,
raw_r_dev_cors, raw_r_dev_vxls);
//record(r_coords, raw_r_dev_cors,
// r_voxels, raw_r_dev_vxls,
// tlen, "thrust-result.txt");
/* ---------------------------------------------------- *
* mgpu reduce
* ---------------------------------------------------- */
ContextPtr context = CreateCudaDevice(argc, argv, true);
/* reshape and re-upload */
voxels_reshape(voxels);
cudaMemcpy(raw_dev_vxls,
voxels,
length * sizeof(Voxel),
cudaMemcpyHostToDevice);
cudaCheckErrors("Memory copy H2D after reshape");
int mlen = mgpu_reduce(raw_dev_cors,
(double*)raw_dev_vxls,
raw_r_dev_cors,
(double*)raw_r_dev_vxls,
*context);
//record(r_coords, raw_r_dev_cors,
// r_voxels, raw_r_dev_vxls,
// mlen, "mgpu-result.txt", true);
if (tlen != mlen)
printf("reduced length not equal, thrust: %d, mgpu: %d\n", tlen, mlen);
/* ---------------------------------------------------- *
* partial reduce
* ---------------------------------------------------- */
// TODO
/* clean up */
cudaFreeHost(coords);
cudaFreeHost(voxels);
cudaFreeHost(r_coords);
cudaFreeHost(r_voxels);
cudaFree(raw_dev_cors);
cudaFree(raw_dev_vxls);
cudaFree(raw_r_dev_cors);
cudaFree(raw_r_dev_vxls);
return 0;
}