-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg.rg
628 lines (568 loc) · 15.3 KB
/
cg.rg
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import "regent"
local c = regentlib.c
fspace MatEl {
row : int32,
col : int32,
val : double,
}
fspace CrsEl {
col : int32,
val : double,
}
fspace LAConfig {
p : int8,
p2 : int8,
dim : int32,
m : int32,
tol : double,
}
fspace MatPtr(r : region(ispace(int1d),CrsEl)) {
el : ptr(CrsEl, r),
}
task NormInner(vec : region(ispace(int1d),double)) where reads(vec) do
var r : double = 0
for i in vec.ispace do
r += vec[i]*vec[i]
end
return r
end
task VecAddScaleSelfInner(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double),
a : double)
where reads(vec1,vec2), writes(vec2) do
__demand(__vectorize)
for i in vec2.ispace do
vec2[i] = vec1[i] + a*vec2[i]
end
end
task VecScaleAddSelfInner(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double),
a : double)
where reads(vec1,vec2), writes(vec1) do
__demand(__vectorize)
for i in vec2.ispace do
vec1[i] += a*vec2[i]
end
end
task VecScaleAddInner(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double),
a : double,
result : region(ispace(int1d),double))
where reads(vec1,vec2), writes(result) do
__demand(__vectorize)
for i in result.ispace do
result[i] = vec1[i] + a*vec2[i]
end
end
task VecAddInner(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double),
result : region(ispace(int1d),double))
where reads(vec1,vec2), writes(result) do
__demand(__vectorize)
for i in result.ispace do
result[i] = vec1[i] + vec2[i]
end
end
task VecSubtInner(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double),
result : region(ispace(int1d),double))
where reads(vec1,vec2), writes(result) do
__demand(__vectorize)
for i in result.ispace do
result[i] = vec1[i] - vec2[i]
end
end
task DotInner(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double))
where reads(vec1,vec2) do
var result : double = 0
for i in vec1.ispace do
result += vec1[i] * vec2[i]
end
return result
end
task DotInner2(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double))
where reads(vec1,vec2) do
var result : double = 0
for i in vec1.ispace do
result += vec1[i] * vec2[i]
end
return result
end
task VecAdd(vec1 : region(ispace(int1d),double),
vec2 : region(ispace(int1d),double),
result : region(ispace(int1d),double),
config : LAConfig)
where reads(vec1,vec2), writes(result) do
var res_p_ind = ispace(int1d,config.p)
var res_p = partition(equal,result,res_p_ind)
for i in res_p_ind do
VecAddInner(vec1,vec2,res_p[i])
end
end
task MatVecMulInner(mat : region(ispace(int1d),MatEl),
vec : region(ispace(int1d),double),
result : region(ispace(int1d),double))
where reads(mat,vec), reduces +(result) do
for el in mat do
result[el.row] += el.val * vec[el.col]
end
end
task MatVecCRSInner(mat : region(ispace(int1d),CrsEl),
vec : region(ispace(int1d),double),
result : region(ispace(int1d),double),
rowst : int32)
where reads(mat,vec), reduces+(result) do
var cts : int32 = 0
var col_last : int32 = 0
for el in mat do
if el.col < col_last then
cts+=1
end
result[rowst + cts] += el.val * vec[el.col]
col_last = el.col
--c.printf("rst: %d, count: %d, row: %d, col: %d\n",rowst,cts,el.row,el.col)
end
end
task MatVecCRSInner2(mat : region(ispace(int1d),CrsEl),
vec : region(ispace(int1d),double),
result : region(ispace(int1d),double),
rowptrs : region(ispace(int1d),int32))
where reads relaxed(mat,vec,rowptrs), writes(result) do
--var total : int64 = 0
for i in rowptrs.ispace do
var res : double = 0
for j = rowptrs[i],rowptrs[i+1],1 do
res += mat[j].val * vec[mat[j].col]
-- total += 1
end
result[i] = res;
end
--c.printf("did %d\n", total)
end
task pv(v : region(ispace(int1d),double)) where reads(v) do
c.printf("[ ")
for i in v.ispace do
c.printf("%f, ",v[i],i)
end
c.printf("]\n")
end
task pm(A : region(ispace(int1d),MatEl)) where reads(A) do
c.printf("======= MX =======\n")
for e in A do
c.printf("(%d, %d) : %f \n", e.row, e.col, e.val)
end
c.printf("==================\n")
end
task pmc(A : region(ispace(int1d),CrsEl)) where reads(A) do
c.printf("======= MX =======\n")
for e in A do
c.printf("(%d) : %f \n", e.col, e.val)
end
c.printf("==================\n")
end
task countWraps(mat : region(ispace(int1d),MatEl)) where reads(mat) do
var col_last : int32 = 0
var cts : int32 = 0
for el in mat do
if el.col < col_last then
cts += 1
end
col_last = el.col
end
return cts
end
task MatVecMul(mat : region(ispace(int1d),MatEl),
vec : region(ispace(int1d),double),
result : region(ispace(int1d),double),
config : LAConfig)
where reads(mat,vec,result), writes(result) do
var mat_p_ind = ispace(int1d, config.p)
var mat_p = partition(equal,mat,mat_p_ind)
var rowst = region(mat_p_ind,int32)
for i = 1,config.p,1 do
rowst[i] = countWraps(mat_p[i-1])
var p2 = mat_p[i]
if mat[mat_p[i].ispace.bounds.lo].col < mat[mat_p[i].ispace.bounds.lo-1].col then
rowst[i] += 1
end
end
for i = 1,config.p,1 do
rowst[i] = rowst[i]+rowst[i-1]
--c.printf("%d,%d\n",i,rowst[i])
end
var crsmat = region(ispace(int1d,config.m),CrsEl)
for i in crsmat.ispace do
crsmat[i].val = mat[i].val
crsmat[i].col = mat[i].col
end
var rowptr = region(ispace(int1d,config.dim+1),int32)
var j : int32 = 1
for i = 1,config.m,1 do
if mat[i].col < mat[i-1].col then
rowptr[j] = i
j += 1
end
end
rowptr[config.dim] = config.m
var rowptr_p = partition(equal,rowptr,mat_p_ind)
var ts_start = c.legion_get_current_time_in_micros()
__fence(__execution, __block)
for i in mat_p_ind do
MatVecCRSInner2(crsmat,vec,result,rowptr_p[i])
end
__fence(__execution, __block)
var ts_stop = c.legion_get_current_time_in_micros()
c.printf("CRS2: %.4f sec\n",(ts_stop - ts_start) * 1e-6)
__fence(__execution, __block)
--pv(result)
__fence(__execution, __block)
fill(result,0)
__fence(__execution, __block)
var crs_p = partition(equal,crsmat,mat_p_ind)
__fence(__execution, __block)
ts_start = c.legion_get_current_time_in_micros()
for i in mat_p_ind do
--c.printf("%d ======\n",i)
MatVecCRSInner(crs_p[i],vec,result,rowst[i])
end
__fence(__execution, __block)
ts_stop = c.legion_get_current_time_in_micros()
c.printf("CRS: %.4f sec\n",(ts_stop - ts_start) * 1e-6)
--pv(result)
__fence(__execution, __block)
fill(result,0)
ts_start = c.legion_get_current_time_in_micros()
__fence(__execution, __block)
for i in mat_p_ind do
MatVecMulInner(mat_p[i],vec,result)
end
__fence(__execution, __block)
ts_stop = c.legion_get_current_time_in_micros()
c.printf("COO: %.4f sec\n",(ts_stop - ts_start) * 1e-6)
--pv(result)
end
task vanillaCG( A : region(ispace(int1d),CrsEl),
rptr : region(ispace(int1d),int32),
b : region(ispace(int1d),double),
x : region(ispace(int1d),double),
conf : LAConfig)
where reads(A,b,rptr,x), writes(x) do
fill(x,0)
-- init x,d,r
var d = region(ispace(int1d,conf.dim),double)
var r = region(ispace(int1d,conf.dim),double)
var r2 = region(ispace(int1d,conf.dim),double)
--ispaces and paritions
var p_ind = ispace(int1d, conf.p)
var A_p = partition(equal,A,p_ind)
var rptr_p = partition(equal,rptr,p_ind)
var b_p = partition(equal,b,p_ind)
var x_p = partition(equal,x,p_ind)
var d_p = partition(equal,d,p_ind)
var r_p = partition(equal,r,p_ind)
var r2_p = partition(equal,r2,p_ind)
--e45
var i1 = region(ispace(int1d,conf.dim),double)
var i1_p = partition(equal,i1,p_ind)
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,x,i1_p[i],rptr_p[i])
end
for i in p_ind do
VecSubtInner(b_p[i],i1_p[i],d_p[i])
end
copy(d,r)
var i3 : double
i3 = 0
for i in p_ind do
i3 += DotInner(r_p[i],r_p[i])
end
var a : double
var beta : double
var i2 : double
var i4 : double
var rn : double
var tolsq : double = conf.tol*conf.tol
for i=0,1000,1 do
--e46
fill(i1,0)
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,d,i1_p[i],rptr_p[i])
end
i2 = 0
__demand(__parallel)
for i in p_ind do
i2 += DotInner(d_p[i],i1_p[i])
end
a = i3/i2
--e46.5
__demand(__parallel)
for i in p_ind do
VecScaleAddSelfInner(x_p[i],d_p[i],a)
end
--e47
__demand(__parallel)
for i in p_ind do
VecScaleAddInner(r_p[i],i1_p[i],(-a),r2_p[i])
end
--e48
beta = 0
i4 = 0
__demand(__parallel)
for i in p_ind do
i4 += DotInner(r2_p[i],r2_p[i])
end
beta = i4/i3
--e49
__demand(__parallel)
for i in p_ind do
VecAddScaleSelfInner(r2_p[i],d_p[i],beta)
end
copy(r2,r)
rn = 0
__demand(__parallel)
for i in p_ind do
rn += NormInner(r)
end
c.printf("error: %f\n",rn)
__fence(__execution, __block)
i3 = i4
--__fence(__execution, __block)
--pv(r)
--__fence(__execution, __block)
--pv(d)
--__fence(__execution, __block)
end
end
task PreConCG( A : region(ispace(int1d),CrsEl),
rptr : region(ispace(int1d),int32),
b : region(ispace(int1d),double),
x : region(ispace(int1d),double),
conf : LAConfig)
where reads(A,b,rptr,x), writes(x) do
__fence(__execution, __block) -- This blocks to make sure we only time the pagerank computation
var ts_start = c.legion_get_current_time_in_micros()
__fence(__execution, __block)
var cts : int32 = 0
var col_last : int32 = 0
var MI = region(ispace(int1d,conf.dim),CrsEl)
var MIrptr = region(ispace(int1d,conf.dim+1),int32)
for i = 0,conf.dim+1,1 do
MIrptr[i] = i
end
for i in rptr.ispace do
for j = rptr[i],rptr[i+1],1 do
if A[j].col == MIrptr[i] then
MI[i].val = 1./A[j].val
MI[i].col = i
--c.printf("%d,%d,%f\n",MIrptr[i],A[j].col,MI[i].val)
end
end
end
fill(x,0)
-- init x,d,r
var d = region(ispace(int1d,conf.dim),double)
var r = region(ispace(int1d,conf.dim),double)
var r2 = region(ispace(int1d,conf.dim),double)
-- ispaces and paritions
var p_ind = ispace(int1d, conf.p)
var A_p = partition(equal,A,p_ind)
var rptr_p = partition(equal,rptr,p_ind)
var MI_p = partition(equal,MI,p_ind)
var MIrptr_p = partition(equal,MIrptr,p_ind)
--var mxptrs = region(ispace(int1d,conf.m),MatPtr(A))
--var mxptrs_p = partition(equal,mxptrs,p_ind)
--unsafe_cast(ptr(MatEl, A), A[0])
--e45
var i1 = region(ispace(int1d,conf.dim),double)
var i5 = region(ispace(int1d,conf.dim),double)
var i1_p = partition(equal,i1,p_ind)
var i5_p = partition(equal,i5,p_ind)
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,x,i1_p[i],rptr_p[i])
end
VecSubtInner(b,i1,r)
MatVecCRSInner2(MI,r,i5,MIrptr)
copy(i5,d)
var i3 : double
i3 = DotInner(r, i5)
var a : double
var beta : double
var i2 : double
var i4 : double
var rn : double
var tolsq : double = conf.tol*conf.tol
__fence(__execution, __block) -- This blocks to make sure we only time the pagerank computation
var ts_end = c.legion_get_current_time_in_micros()
c.printf("setup time %f ms\n", (ts_end-ts_start)*1e-3);
__fence(__execution, __block)
for it=0,1000,1 do
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,d,i1_p[i],rptr_p[i])
end
i2 = DotInner(d, i1);
a = i3/i2
VecScaleAddSelfInner(x,d,a)
VecScaleAddSelfInner(r,i1,(-a))
MatVecCRSInner2(MI,r,i5,MIrptr)
beta = DotInner2(r,i5)/i3
VecAddScaleSelfInner(i5,d,beta)
i3 *= beta
if it % 5 == 0 then
rn = NormInner(r)
if rn < tolsq then
c.printf("iteration %d\n", it);
break
end
end
end
end
task AggroPreConCG( A : region(ispace(int1d),CrsEl),
rptr : region(ispace(int1d),int32),
b : region(ispace(int1d),double),
x : region(ispace(int1d),double),
conf : LAConfig)
where reads(A,b,rptr,x), writes(x) do
--[[__fence(__execution, __block) -- This blocks to make sure we only time the pagerank computation
var ts_start = c.legion_get_current_time_in_micros()
__fence(__execution, __block)
--]]
var MI = region(ispace(int1d,conf.dim),CrsEl)
var MIrptr = region(ispace(int1d,conf.dim+1),int32)
var d = region(ispace(int1d,conf.dim),double)
var r = region(ispace(int1d,conf.dim),double)
var r2 = region(ispace(int1d,conf.dim),double)
var p_ind = ispace(int1d, conf.p)
var A_p = partition(equal,A,p_ind)
var rptr_p = partition(equal,rptr,p_ind)
var MI_p = partition(equal,MI,p_ind)
var MIrptr_p = partition(equal,MIrptr,p_ind)
var i1 = region(ispace(int1d,conf.dim),double)
var i5 = region(ispace(int1d,conf.dim),double)
var i1_p = partition(equal,i1,p_ind)
var i5_p = partition(equal,i5,p_ind)
var i3 : double
var a : double
var beta : double
var i2 : double
var i4 : double
var rn : double
var tolsq : double = conf.tol*conf.tol
for i = 0,conf.dim+1,1 do
MIrptr[i] = i
end
for i in rptr.ispace do
for j = rptr[i],rptr[i+1],1 do
if A[j].col == MIrptr[i] then
MI[i].val = 1./A[j].val
MI[i].col = i
end
end
end
fill(x,0)
--[[__fence(__execution, __block)
var ts_end = c.legion_get_current_time_in_micros()
c.printf("setup time %f ms\n", (ts_end-ts_start)*1e-3);
__fence(__execution, __block)
--]]
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A_p[i],x,i1_p[i],rptr_p[i])
end
VecSubtInner(b,i1,r)
for i in p_ind do
MatVecCRSInner2(MI,r,i5_p[i],MIrptr_p[i])
end
--__fence(__execution, __block)
--c.exit(0)
copy(i5,d)
i3 = DotInner(r,i5)
for it=0,1000,1 do
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,d,i1_p[i],rptr_p[i])
end
i2 = DotInner(d, i1);
a = i3/i2
VecScaleAddSelfInner(x,d,a)
VecScaleAddSelfInner(r,i1,(-a))
MatVecCRSInner2(MI,r,i5,MIrptr)
beta = DotInner2(r,i5)/i3
VecAddScaleSelfInner(i5,d,beta)
i3 *= beta
if it % 15 == 0 then
rn = NormInner(r)
if rn < tolsq then
--c.printf("iteration %d\n", it);
break
end
end
end
end
task AggroNPPreConCG( A : region(ispace(int1d),CrsEl),
rptr : region(ispace(int1d),int32),
b : region(ispace(int1d),double),
x : region(ispace(int1d),double),
conf : LAConfig)
where reads(A,b,rptr,x), writes(x) do
var d = region(ispace(int1d,conf.dim),double)
var r = region(ispace(int1d,conf.dim),double)
var r2 = region(ispace(int1d,conf.dim),double)
var p_ind = ispace(int1d, conf.p)
var A_p = partition(equal,A,p_ind)
var rptr_p = partition(equal,rptr,p_ind)
var i1 = region(ispace(int1d,conf.dim),double)
var i5 = region(ispace(int1d,conf.dim),double)
var i1_p = partition(equal,i1,p_ind)
var i5_p = partition(equal,i5,p_ind)
var i3 : double
var a : double
var beta : double
var i2 : double
var i4 : double
var rn : double
var tolsq : double = conf.tol*conf.tol
fill(x,0)
--[[__fence(__execution, __block)
var ts_end = c.legion_get_current_time_in_micros()
c.printf("setup time %f ms\n", (ts_end-ts_start)*1e-3);
__fence(__execution, __block)
--]]
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,x,i1_p[i],rptr_p[i])
end
VecSubtInner(b,i1,r)
copy(r,d)
i3 = NormInner(r)
for it=0,1000,1 do
__demand(__parallel)
for i in p_ind do
MatVecCRSInner2(A,d,i1_p[i],rptr_p[i])
end
i2 = DotInner(d, i1);
a = i3/i2
VecScaleAddSelfInner(x,d,a)
VecScaleAddSelfInner(r,i1,(-a))
beta = NormInner(r)/i3
VecAddScaleSelfInner(r,d,beta)
i3 *= beta
if it % 2 == 0 then
rn = NormInner(r)
if rn < tolsq then
--c.printf("iteration %d\n", it);
break
end
end
end
end
--[[
PreConCG:printpretty()
DotInner:printpretty()
VecScaleAddSelfInner:printpretty()
--]]