/
jagged.chpl
101 lines (90 loc) · 1.98 KB
/
jagged.chpl
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
use GPUAPI;
use CTypes;
extern proc kernelLOW(a: [] c_ptr(int), b: [] c_size_t, n: int);
extern proc kernelMIDLOW(a: c_ptr(c_ptr(void)), n: int);
class C {
var n: int;
proc init(_n: int) { n = _n; }
var x: [0..#n] int;
}
var Cs = [new C(256), new C(512)];
var Vs = [new C(256), new C(512)];
const N = Cs.size;
proc initialize() {
for e in Cs {
for i in e.x.domain {
e.x[i] = i;
}
}
for e in Vs {
for i in e.x.domain {
e.x[i] = i+1;
}
}
}
proc verify(prefix) {
var verified = true;
for i in 0..#N {
if (Cs[i].x.equals(Vs[i].x) == false) {
verified = false;
}
}
if (verified) {
writeln(prefix + " Verified");
} else {
writeln(prefix + " Not Verified");
}
}
// LOW
writeln("[LOW]");
{
initialize();
ref ptrs = [c_ptrTo(Cs[0].x), c_ptrTo(Cs[1].x)];
ref sizes = [Cs[0].x.size:c_size_t*c_sizeof(int), Cs[1].x.size:c_size_t*c_sizeof(int)];
kernelLOW(ptrs, sizes, N);
verify("LOW");
}
// MIDLOW
writeln("[MIDLOW]");
{
initialize();
var dA: [0..#N] c_ptr(void);
var dAs: c_ptr(c_ptr(void));
for i in 0..#N {
const size = Cs[i].x.size:c_size_t*c_sizeof(int);
Malloc(dA[i], size);
Memcpy(dA[i], c_ptrTo(Cs[i].x), size, 0);
}
const size = N: c_size_t * c_sizeof(c_ptr(c_ptr(void)));
Malloc(dAs, size);
Memcpy(dAs, c_ptrTo(dA), size, 0);
kernelMIDLOW(dAs, N);
DeviceSynchronize();
for i in 0..#N {
const size = Cs[i].x.size:c_size_t*c_sizeof(int);
Memcpy(c_ptrTo(Cs[i].x), dA[i], size, 1);
}
// Verification
verify("LOWMID");
}
// MID
writeln("[MID: multiple args]");
{
initialize();
var dAs = new GPUJaggedArray(Cs[0].x, Cs[1].x);
dAs.toDevice();
kernelMIDLOW(dAs.dPtr(), N);
dAs.fromDevice();
// Verification
verify("MID: multiple args");
}
writeln("[MID: promoted]");
{
initialize();
var dAs = new GPUJaggedArray(Cs.x);
dAs.toDevice();
kernelMIDLOW(dAs.dPtr(), N);
dAs.fromDevice();
// Verification
verify("MID: promoted");
}