-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.chpl
116 lines (93 loc) · 2.1 KB
/
matrix.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Basic generic matrix class
*/
use Random;
use CyclicDist;
use Time;
config const tileSize = 64;
config const matrixSize = 4;
config param debug = true;
class tileMatrix {
/********** GENERICS *************/
type itemType;
/********** VARIABLES ************/
const space : domain (2) dmapped Cyclic(startIdx=(0,0));
const tileSpace : domain(1) = [0..tileSize-1];
var tiles : [space] tile;
/********** SUBCLASS ************/
class tile { // use record breaks in current release
var flag$ : single int;
var data : [tileSpace] itemType;
// generic function to set tile
proc set(a) {
data = a;
flag$ = 1;
}
proc get() {
return data;
}
proc data var {
// make sure data is only read after the flag is checked
if (!setter && flag$!=1) then halt ("Error that should not happen 23b");
return data;
}
}
/******* CONSTRUCTORS **********/
proc tileMatrix(type itemType, xTiles : int, yTiles : int) {
space = [0..xTiles-1, 0..yTiles-1];
coforall i in tiles {
on (i) {
writeln (i.locale.id);
i = new tile();
}
}
}
/******* DATA ACCESS FUNCTIONS ************/
proc this ((x,y) : 2*int) var : tile {
return tiles(x,y);
}
iter these() var {
for i in tiles {
yield i;
}
}
}
proc compute (matrix, id, all) : tileMatrix(int) {
var returnee = new tileMatrix(int, matrixSize, matrixSize);
forall (x,y) in [0..matrixSize-1, 0..matrixSize-1] {
//writeln ("e: ", (x,y));
on (returnee((x,y))) {
var t = matrix((x,y));
var r = returnee((x,y));
if (x==0) {
var a = t.get();
r.set(a);
} else {
var t0 = matrix((x-1,y));
r.set(t0.get() + t.get());
}
}
}
return returnee;
}
if (debug) {
for loc in Locales {
on loc {
writeln ("Hello from node ", loc.id, " of " , numLocales);
}
}
}
var matrix = new tileMatrix(int, matrixSize, matrixSize);
// for all is not compiling
// -> requires leader/follow iterator
for/*all*/ i in matrix {
on (i) {
i.set(i.locale.id);
}
}
var t: Timer;
t.start();
var m = compute(matrix, 0, matrixSize);
t.stop();
if debug then writeln(m);
writeln ("TIME: ", t.elapsed());