-
Notifications
You must be signed in to change notification settings - Fork 0
/
cg_usm.cpp
502 lines (326 loc) · 13.3 KB
/
cg_usm.cpp
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include <iostream>
#include <math.h>
#include <numeric>
#include <vector>
#include <CL/sycl.hpp>
#include <getopt.h>
#include <assert.h>
#include <sys/time.h>
#include <chrono>
#include <iomanip>
#include "./functions.cpp"
typedef std::chrono::duration<unsigned long long> my_duration;
using namespace cl;
static struct option long_options[] = {
/* name, has_arg, flag, val */
{"block size", 1, NULL, 'b'},
{"size", 1, NULL, 's'},
{"iterations", 1, NULL, 'i'},
{"atomic", 1, NULL, 'a'},
{0,0,0,0}
};
int main(int argc, char* argv[]) {
int n_row, n_col;
n_row = n_col = 128; // deafult matrix size
int opt, option_index=0;
int block_size = 16;
double * A;
double * b;
int iterations = 5;
func_ret_t ret, ret1, ret2;
bool atomics = true;
const char * atom = NULL;
while ((opt = getopt_long(argc, argv, "::s:b:i:a:",
long_options, &option_index)) != -1 ) {
switch(opt){
case 'b':
block_size = atoi(optarg);
break;
case 's':
n_col=n_row= atoi(optarg);
break;
case 'i':
iterations = atoi(optarg);
break;
case 'a':
atom = optarg;
break;
case '?':
fprintf(stderr, "invalid option\n");
break;
case ':':
fprintf(stderr, "missing argument\n");
break;
default:
std::cout<<"Usage: "<< argv[0]<< "[-s matrix_size|-b blocksize <optional>| -a yes or no (for atomics)<optional>] \n" << std::endl;
exit(EXIT_FAILURE);
}
}
if ((optind < argc) || (optind == 1))
{
std::cout<<"Usage: "<< argv[0]<< "[-s matrix_size|-b blocksize <optional>]\n" << std::endl;
exit(EXIT_FAILURE);
}
if (n_row)
{
printf("Creating matrix internally of size = %d\n", n_row);
ret = create_matrix(&A, n_row);
ret1 = create_vector(&b, n_row);
if (ret != RET_SUCCESS && ret1 != RET_SUCCESS)
{
A = NULL;
std::cout<< stderr << "error creating matrix internally of size = "<< n_row << std::endl;
exit(EXIT_FAILURE);
}
}
else
{
printf("No input for matrix sise specified!\n");
exit(EXIT_FAILURE);
}
if (atom)
{
if (std::strcmp(atom, "no")) atomics = false;
}
std::cout << "Matrix size: [" << n_row << "," << n_col << "]" <<std::endl;
int k = 0;
{ // SYCL scope
sycl::queue Q{};
std::cout << "running on ..."<< std::endl;
std::cout << Q.get_device().get_info<sycl::info::device::name>()<<"\n"<<std::endl;
auto r = sycl::malloc_shared<double>(n_row*sizeof(double),Q);
auto rp = sycl::malloc_shared<double>(n_row*sizeof(double),Q);
auto p = sycl::malloc_shared<double>(n_row*sizeof(double),Q);
auto alpha = sycl::malloc_shared<double>(1*sizeof(double),Q);
alpha[0]=0.0;
auto beta = sycl::malloc_shared<double>(1*sizeof(double),Q);
beta[0]=0.0;
auto num = sycl::malloc_shared<double>(1*sizeof(double),Q);
num[0] = 0.0;
auto den = sycl::malloc_shared<double>(1*sizeof(double),Q);
den[0] = 0.0;
auto x0 = sycl::malloc_shared<double>(n_row*sizeof(double),Q);
std::fill(x0,x0+n_row,0.0);
auto A_shared = static_cast<double *>(sycl::malloc_shared(n_row*n_row*sizeof(double), Q));
auto b_shared = static_cast<double *>(sycl::malloc_shared(n_row*sizeof(double), Q));
//Q.memcpy(A_shared,A,sizeof(double)*n_row*n_row);
//Q.memcpy(b_shared,b,sizeof(double)*n_row*n_row);
for (size_t i = 0; i < n_row; i++)
{
b_shared[i] = b[i];
for (size_t j = 0; j < n_row; j++)
{
A_shared[i*n_row+j] = A[i*n_row+j];
}
}
Q.wait();
auto N = static_cast<size_t>(n_row);
sycl::range<1> global1{N};
sycl::range<2> global2{N,N};
auto N_b = static_cast<size_t>(block_size);
if (block_size > n_row)
{
std::cout << "Given input block size is greater than the matrix size changing block size to matrix size \n" << std::endl;
N_b = N;
}
sycl::range<1> local1{N_b};
sycl::range<2> local2{N_b,N_b};
auto kernel_duration1 = std::chrono::microseconds(0) ;
auto kernel_duration2 = std::chrono::microseconds(0) ;
auto kernel_duration3 = std::chrono::microseconds(0) ;
auto kernel_duration4 = std::chrono::microseconds(0) ;
auto kernel_duration5 = std::chrono::microseconds(0) ;
auto kernel_duration6 = std::chrono::microseconds(0) ;
auto kernel_duration7 = std::chrono::microseconds(0) ;
auto kernel_start_time = std::chrono::high_resolution_clock::now();
auto kernel_start1 = std::chrono::high_resolution_clock::now();
Q.parallel_for<class stream>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
auto temp = 0.0;
for (size_t j = 0; j < N; j++)
{
temp += A_shared[i*N+j]*x0[j];
}
r[i] = b_shared[i] - temp ;
});
Q.wait();
auto kernel_end1 = std::chrono::high_resolution_clock::now();
kernel_duration1 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end1 - kernel_start1);
for (size_t i = 0; i < N; i++)
{
p[i] = r[i];
}
for (size_t i = 0; i < n_row; i++)
{
rp[i] = r[i];
}
double err = 0.0;
for (size_t i = 0; i < N; i++)
{
err += r[i]*r[i];
}
err = std::sqrt(err);
auto accum = sycl::malloc_shared<double>(n_row*sizeof(double),Q);
auto tolerance = 1E-5 ;
while(err > tolerance)
{
std::fill(accum,accum+n_row,0.0);
num[0] = 0.0;
den[0] = 0.0;
//##########
auto kernel_start2 = std::chrono::high_resolution_clock::now();
if(atomics)
{
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
auto v = sycl::atomic_ref<double, sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::global_space>(
num[0]);
v.fetch_add(r[i]*r[i]);
});
}
else
{
auto accum_shared = sycl::malloc_shared<double>(n_row/block_size,Q); Q.wait();
auto tile = static_cast<size_t>(block_size);
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto j = it.get_id(0);
for (size_t k = 0; k < tile; k++)
{
accum_shared[j] += r[j*tile + k]*r[j*tile + k];
}
});
std::accumulate(accum_shared, accum_shared+(n_row/block_size), num[0]);
}
auto kernel_end2 = std::chrono::high_resolution_clock::now();
kernel_duration2 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end2 - kernel_start2);
//##########
auto kernel_start3 = std::chrono::high_resolution_clock::now();
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
for (size_t j = 0; j < N; j++)
{
accum[i] += p[i]*A_shared[i*N+j]*p[j] ;
}
});
Q.wait();
auto kernel_end3 = std::chrono::high_resolution_clock::now();
kernel_duration3 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end3 - kernel_start3);
//##########
den[0] = std::accumulate(accum, accum+n_row,0.0);
alpha[0] = num[0] / den[0];
//##########
auto kernel_start4 = std::chrono::high_resolution_clock::now();
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
x0[i] = alpha[0]*p[i];
});
Q.wait();
auto kernel_end4 = std::chrono::high_resolution_clock::now();
kernel_duration4 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end4 - kernel_start4);
//##########
auto kernel_start5 = std::chrono::high_resolution_clock::now();
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
double temp = 0.0;
for (size_t j = 0; j < N; j++)
{
temp+= alpha[0]*A_shared[i*N+j]*p[j];
}
r[i] = r[i] - temp;
});
Q.wait();
auto kernel_end5 = std::chrono::high_resolution_clock::now();
kernel_duration5 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end5 - kernel_start5);
//std::cout << kernel_duration4.count()/(k*iterations*1E6) << std::endl;
//##########
err = 0.0;
for (size_t i = 0; i < N; i++)
{
err += r[i]*r[i];
}
err = std::sqrt(err);
if (err < tolerance)
{
break;
}
num[0] = 0.0;
den[0] = 0.0;
//##########
auto kernel_start6 = std::chrono::high_resolution_clock::now();if(atomics)
{
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
auto v = sycl::atomic_ref<double, sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::global_space>(
num[0]);
v.fetch_add(r[i]*r[i]);
auto v1 = sycl::atomic_ref<double, sycl::memory_order::relaxed,
sycl::memory_scope::device,
sycl::access::address_space::global_space>(
den[0]);
v1.fetch_add(rp[i]*rp[i]);
});
}
else
{
auto accum_shared_num = sycl::malloc_shared<double>(n_row/block_size,Q); Q.wait();
auto accum_shared_den = sycl::malloc_shared<double>(n_row/block_size,Q); Q.wait();
auto tile = static_cast<size_t>(block_size);
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto j = it.get_id(0);
for (size_t k = 0; k < tile; k++)
{
accum_shared_num[j] += r[j*tile+k]*r[j*tile+k];
accum_shared_den[j] += rp[j*tile+k]*rp[j*tile+k];
}
});
std::accumulate(accum_shared_num, accum_shared_num+(n_row/block_size), num[0]);
std::accumulate(accum_shared_den, accum_shared_den+(n_row/block_size), den[0]);
}
Q.wait()
auto kernel_end6 = std::chrono::high_resolution_clock::now();
kernel_duration6 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end6 - kernel_start6);
//##########
beta[0] = num[0]/den[0];
//##########
auto kernel_start7 = std::chrono::high_resolution_clock::now();
Q.parallel_for<>(sycl::range<1>(global1), [=](sycl::item<1>it){
auto i = it.get_id(0);
p[i] = r[i] + beta[0]*p[i];
});
Q.wait();
auto kernel_end7 = std::chrono::high_resolution_clock::now();
kernel_duration7 += std::chrono::duration_cast<std::chrono::microseconds>(kernel_end7 - kernel_start7);
//##########
for (size_t i = 0; i < n_row; i++)
{
rp[i] = r[i];
}
k++;
}
auto kernel_end_time = std::chrono::high_resolution_clock::now();
auto kernel_duration = std::chrono::duration_cast<std::chrono::microseconds>(kernel_end_time - kernel_start_time);
auto appl_duration = std::chrono::microseconds(0);
appl_duration = kernel_duration2 + kernel_duration3 + kernel_duration4 + kernel_duration5 + kernel_duration6 + kernel_duration7;
std::cout << "Average total time taken to execute application : "<< (kernel_duration.count()/(1E6)) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel1 : "<< kernel_duration1.count()/(1E6) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel2 : "<< kernel_duration2.count()/(1E6) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel3 : "<< kernel_duration3.count()/(1E6) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel4 : "<< kernel_duration4.count()/(1E6) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel5 : "<< kernel_duration5.count()/(1E6) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel6 : "<< kernel_duration6.count()/(1E6) <<" seconds" <<std::endl;
std::cout << "\n";
std::cout << "Average time taken to execute kernel7 : "<< kernel_duration7.count()/(iterations*1E6) <<" seconds" <<std::endl;
std::cout << "\n";
}
return 0;
}