-
Notifications
You must be signed in to change notification settings - Fork 2
/
arbitraryTaskGraph.chpl
41 lines (32 loc) · 998 Bytes
/
arbitraryTaskGraph.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
use IO;
// take in a sparse matrix that represents a task graph
// an entry in A(i,j) means task i can't start before task j
// the matrix is lower triangular because it describes a DAG
// FIXME: starting out with a dense 2D array
config const N = 4;
var A : [0..<N,0..<N] bool;
// 0 and 1 can execute right away
// 2 and 3 have to wait for
A[2,0] = true;
A[2,1] = true;
A[3,1] = true;
// can I declare an array of atomics? yes
var numToWaitFor : [0..<N] atomic int;
writeln(numToWaitFor);
// encoding how many each task needs to wait for
forall (i,j) in A.domain {
if A[i,j] { numToWaitFor[i].fetchAdd(1); }
}
writeln(numToWaitFor);
// start all of the tasks and have them wait as needed and
// have them decrement the appropriate numToWaitFor
for i in 0..<N {
begin {
numToWaitFor[i].waitFor(0);
// do task i work
writeln("Task: ", i, " is working away");
for j in i+1..<N { // should only be later tasks
if A[j,i] { numToWaitFor[j].fetchAdd(-1); }
}
}
}