/
pitched2d.chpl
78 lines (65 loc) · 1.98 KB
/
pitched2d.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
use GPUAPI;
use CTypes;
extern proc kernel(dA: c_ptr(void), nRows: c_size_t, nCols: c_size_t, dpitch: c_size_t);
var D = {0..8, 0..8};
var A: [D] int;
var V: [D] int; // for verification
// initialization proc
proc initialize() {
for (i, j) in D {
A[i, j] = (i+1)*10 + j;
}
V = A + 1;
}
// MID-LOW
initialize();
var dA: c_ptr(void);
var hpitch: c_size_t = D.dim(1).size:c_size_t * c_sizeof(A.eltType);
var dpitch: c_size_t;
MallocPitch(dA, dpitch, hpitch, D.dim(0).size: c_size_t);
writeln("MID-LOW: pitch on the host:", hpitch);
writeln("MID-LOW: pitch on the device: ", dpitch);
Memcpy2D(dA, dpitch, c_ptrTo(A), hpitch, hpitch, D.dim(0).size: c_size_t, 0);
kernel(dA, D.dim(0).size: c_size_t, D.dim(1).size: c_size_t, dpitch);
DeviceSynchronize();
Memcpy2D(c_ptrTo(A), hpitch, dA, dpitch, hpitch, D.dim(0).size: c_size_t, 1);
// Verify
if (A.equals(V)) {
writeln("MID-LOW Verified");
} else {
writeln("MID-LOW Not Verified");
}
// MID (not pitched)
initialize();
var dA2 = new GPUArray(A);
writeln("MID (pitch=false) pitch on the host:", dA2.hpitch);
writeln("MID (pitch=false) pitch on the device: ", dA2.dpitch);
if (dA2.hpitch != dA2.dpitch) {
writeln("Error: the pitch on the host must be the same as that on the device when pitch=false");
exit();
}
dA2.toDevice();
kernel(dA2.dPtr(), D.dim(0).size: c_size_t, D.dim(1).size: c_size_t, dA2.dpitch);
DeviceSynchronize();
dA2.fromDevice();
// Verify
if (A.equals(V)) {
writeln("MID (pitch=false) Verified");
} else {
writeln("MID (pitch=false) Not Verified");
}
// MID (pitched)
initialize();
var dA3 = new GPUArray(A, true);
writeln("MID (pitch=true) pitch on the host:", dA3.hpitch);
writeln("MID (pitch=true) pitch on the device: ", dA3.dpitch);
dA3.toDevice();
kernel(dA3.dPtr(), D.dim(0).size: c_size_t, D.dim(1).size: c_size_t, dA3.dpitch);
DeviceSynchronize();
dA3.fromDevice();
// Verify
if (A.equals(V)) {
writeln("MID (pitch=true) Verified");
} else {
writeln("MID (pitch=true) Not Verified");
}