-
Notifications
You must be signed in to change notification settings - Fork 30
/
benchmark.swift
326 lines (287 loc) · 12.4 KB
/
benchmark.swift
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
/* Copyright 2020 Stanford University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import io;
import sys;
import location;
type pair {
int timestep;
int point;
}
@dispatch=WORKER
(string appStr) initApp (string args) "turbine" "0.0" [
"load ./interop.so interop; addArg dummy; addArgs <<args>>; set <<appStr>> [app_create [getArgc] [getArgv]]; clearAll;"
];
@dispatch=WORKER
() displayApp (string appStr) "turbine" "0.0" [
"app_display <<appStr>>"
];
@dispatch=WORKER
() reportTiming(string appStr, float elapsedTime) "turbine" "0.0" [
"app_report_timing <<appStr>> <<elapsedTime>>"
];
@dispatch=WORKER
(string list) getTaskGraphList (string appStr) "turbine" "0.0" [
"set <<list>> [app_task_graphs <<appStr>>]"
];
@dispatch=WORKER
(int num) numGraphsInList (string list) "turbine" "0.0" [
"set <<num>> [task_graph_list_num_task_graphs <<list>>]"
];
@dispatch=WORKER
(string g) getGraph (string list, int index) "turbine" "0.0" [
"set <<g>> [task_graph_list_task_graph <<list>> <<index>>]"
];
@dispatch=WORKER
(int w) getMaxWidth (string graph) "turbine" "0.0" [
"set <<w>> [task_graph_t_max_width_get <<graph>>]"
];
@dispatch=WORKER
(int t) getTimesteps (string graph) "turbine" "0.0" [
"set <<t>> [task_graph_t_timesteps_get <<graph>>]"
];
@dispatch=WORKER
(int o) getOffset (string graph, int timestep) "turbine" "0.0" [
"set <<o>> [expr <<timestep>> < 0 ? 0 : [task_graph_offset_at_timestep <<graph>> <<timestep>>]]"
];
@dispatch=WORKER
(int w) getWidth (string graph, int timestep) "turbine" "0.0" [
"set <<w>> [expr <<timestep>> < 0 ? 0 : [task_graph_width_at_timestep <<graph>> <<timestep>>]]"
];
@dispatch=WORKER
(int d) getDependenceSet (string graph, int timestep) "turbine" "0.0" [
"set <<d>> [task_graph_dependence_set_at_timestep <<graph>> <<timestep>>]"
];
@dispatch=WORKER
(string intervalList) getDependencies (string graph, int dset, int point) "turbine" "0.0" [
"set <<intervalList>> [task_graph_dependencies <<graph>> <<dset>> <<point>>]"
];
@dispatch=WORKER
(string intervalList) getReverseDependencies (string graph, int dset, int point) "turbine" "0.0" [
"set <<intervalList>> [task_graph_reverse_dependencies <<graph>> <<dset>> <<point>>]"
];
@dispatch=WORKER
(int n) getNumIntervals (string list) "turbine" "0.0" [
"set <<n>> [interval_list_num_intervals <<list>>]"
];
@dispatch=WORKER
(string i) getInterval (string list, int index) "turbine" "0.0" [
"set <<i>> [interval_list_interval <<list>> <<index>>]"
];
@dispatch=WORKER
(int s) getStart (string interval) "turbine" "0.0" [
"set <<s>> [interval_t_start_get <<interval>>]"
];
@dispatch=WORKER
(int e) getEnd (string interval) "turbine" "0.0" [
"set <<e>> [interval_t_end_get <<interval>>]"
];
@dispatch=WORKER
(string a) getInputArray (string list, int offset, int width) "turbine" "0.0" [
"set <<a>> [getInputArray <<list>> <<offset>> <<width>>]"
];
@dispatch=WORKER
(int n) getNumInputs (string list, int offset, int width) "turbine" "0.0" [
"set <<n>> [getNumInputs <<list>> <<offset>> <<width>>]"
];
@dispatch=WORKER
(string b) getInputBytes (int numInputs) "turbine" "0.0" [
"set <<b>> [getInputBytes <<numInputs>>]"
];
@dispatch=WORKER
(void v) placeInput (string loc, int timestep, int point, int idx) "turbine" "0.0" [
"placeInput <<loc>> <<timestep>> <<point>> <<idx>>"
];
@dispatch=WORKER
(void v) coreExecutePoint (string graph, int timestep, int point, string inputPointer, string inputBytes, int numInputs) "turbine" "0.0" [
"executePoint <<graph>> <<timestep>> <<point>> <<inputPointer>> <<inputBytes>> <<numInputs>>"
];
(string deps[][][], string revDeps[][][], string inputMem[][][], string inputBytes[], int nInputs[][][]) getNeededArrays (string list, int rank) {
location L = locationFromRank(rank);
int numGraphs = @location=L numGraphsInList(list);
foreach graphIndex in [0 : numGraphs - 1] {
string graph = @location=L getGraph(list, graphIndex);
int maxWidth = @location=L getMaxWidth(graph);
int timesteps = @location=L getTimesteps(graph);
inputBytes[graphIndex] = @location=L getInputBytes(maxWidth);
foreach taskIndex in [rank : maxWidth - 1 : turbine_workers()] {
for (int timestep = 0; timestep < timesteps; timestep = timestep + 1) {
// Gather all necessary information from graph.
int lastOffset = @location=L getOffset(graph, timestep - 1);
int lastWidth = @location=L getWidth(graph, timestep - 1);
int dset = @location=L getDependenceSet(graph, timestep);
int nextDset = @location=L getDependenceSet(graph, timestep + 1);
string currDeps = @location=L getDependencies(graph, dset, taskIndex);
string currRevDeps = @location=L getReverseDependencies(graph, nextDset, taskIndex);
string currInputMem = @location=L getInputArray(currDeps, lastOffset, lastWidth);
int currNumInputs = @location=L getNumInputs(currDeps, lastOffset, lastWidth);
deps[graphIndex][taskIndex][timestep] = currDeps;
revDeps[graphIndex][taskIndex][timestep] = currRevDeps;
inputMem[graphIndex][taskIndex][timestep] = currInputMem;
nInputs[graphIndex][taskIndex][timestep] = currNumInputs;
}
}
}
}
(int maxWidths[], int numTimesteps[]) getGraphInfo (string list) {
location L = locationFromRank(0);
int numGraphs = @location=L numGraphsInList(list);
foreach graphIndex in [0 : numGraphs - 1] {
string graph = @location=L getGraph(list, graphIndex);
maxWidths[graphIndex] = @location=L getMaxWidth(graph);
numTimesteps[graphIndex] = @location=L getTimesteps(graph);
}
}
(pair outputPair) executePoint (string graph, int timestep, int taskIndex, pair inputs[][], string inputSpace, string inputBytes, int numInputs) {
location L = locationFromRank(taskIndex %% turbine_workers());
int offset = @location=L getOffset(graph, timestep);
int width = @location=L getWidth(graph, timestep);
// Only execute point if this point is in the current timestep.
if (offset <= taskIndex && taskIndex < offset + width) {
int waitFor[];
// Place all inputs in the correct places.
foreach inputPair, idx in inputs[timestep] {
@location=L placeInput(inputSpace, inputPair.timestep, inputPair.point, idx) =>
waitFor[idx] = 0;
}
wait deep (waitFor) {
// Execute point once inputs are in correct place.
@location=L coreExecutePoint(graph, timestep, taskIndex, inputSpace, inputBytes, numInputs) =>
outputPair = pair(timestep, taskIndex);
}
} else {
outputPair = pair(timestep, taskIndex);
}
}
(boolean finished[]) runBenchmark (string lists[], location locs[], string deps[][][][], string revDeps[][][][], string inputMem[][][][], string inputBytes[][], int nInputs[][][][], int maxWidths[], int numTimesteps[]) {
pair data[][][][];
int numRanks = turbine_workers();
location first = locs[0];
int numGraphs = @location=first numGraphsInList(lists[0]);
foreach graphIndex in [0 : numGraphs - 1] {
int maxWidth = maxWidths[graphIndex];
int timesteps = numTimesteps[graphIndex];
foreach taskIndex in [0 : maxWidth - 1] {
int rank = taskIndex %% numRanks;
location L = locationFromRank(rank);
pair inputs[][];
pair empty[];
pair outputs[];
for (int timestep = 0; timestep < timesteps; timestep = timestep + 1) {
// Get initial data that is necessary for current timestep.
string graph = @location=L getGraph(lists[rank], graphIndex);
int offset = @location=L getOffset(graph, timestep);
int width = @location=L getWidth(graph, timestep);
int lastOffset = @location=L getOffset(graph, timestep - 1);
int lastWidth = @location=L getWidth(graph, timestep - 1);
int nextOffset = @location=L getOffset(graph, timestep + 1);
int nextWidth = @location=L getWidth(graph, timestep + 1);
// RECEIVING
if (timestep != 0 && taskIndex >= offset && taskIndex < offset + width) {
string receivingIntervals = deps[rank][graphIndex][taskIndex][timestep];
int numReceivingIntervals = @location=L getNumIntervals(receivingIntervals);
int greaterIdx;
for (int i = 0, greaterIdx = 0; i < numReceivingIntervals; i = i + 1, greaterIdx = greaterIdx + lesserIdx) {
string interval = @location=L getInterval(receivingIntervals, i);
int start = @location=L getStart(interval);
int end = @location=L getEnd(interval);
int lesserIdx;
for (int sender = start, lesserIdx = 0; sender <= end; sender = sender + 1, lesserIdx = lesserIdx + received) {
int received;
if (sender >= lastOffset && sender < lastWidth + lastOffset) {
int idx = greaterIdx + lesserIdx;
// Will wait until data is available from the sending.
inputs[timestep][idx] = data[graphIndex][taskIndex][timestep][sender];
received = 1;
} else {
received = 0;
}
}
}
// If greaterIdx is still zero, then we never received anything.
if (greaterIdx == 0) {
inputs[timestep] = empty;
}
} else {
inputs[timestep] = empty;
}
// EXECUTE POINT
outputs[timestep] = executePoint(graph, timestep, taskIndex, inputs, inputMem[rank][graphIndex][taskIndex][timestep], inputBytes[rank][graphIndex], nInputs[rank][graphIndex][taskIndex][timestep]);
// SENDING
if (timestep != timesteps - 1 && taskIndex >= offset && taskIndex < offset + width) {
string sendingIntervals = revDeps[rank][graphIndex][taskIndex][timestep];
int numSendingIntervals = @location=L getNumIntervals(sendingIntervals);
for (int i = 0; i < numSendingIntervals; i = i + 1) {
string interval = @location=L getInterval(sendingIntervals, i);
int start = @location=L getStart(interval);
int end = @location=L getEnd(interval);
for (int target = start; target <= end; target = target + 1) {
if (target >= nextOffset && target < nextWidth + nextOffset) {
// Place the correct data in the correct place so the right task can access it.
data[graphIndex][target][timestep + 1][taskIndex] = outputs[timestep];
}
}
}
} else {
if (timestep == timesteps - 1) {
wait deep (outputs) {
finished[taskIndex] = true;
}
}
}
}
}
}
}
() main () {
// Initialize arrays.
string dependencies[][][][], reverseDependencies[][][][], inputMemory[][][][], inputBytes[][], lists[], apps[];
int numInputs[][][][], maxWidths[], numTimesteps[];
location locations[];
// Get data set up in proper locations.
foreach i in [0 : turbine_workers() - 1] {
location L = locationFromRank(i);
locations[i] = L;
apps[i] = @location=L initApp(args());
if (i == 0) {
@location=L displayApp(apps[i]);
}
lists[i] = @location=L getTaskGraphList(apps[i]);
(dependencies[i], reverseDependencies[i], inputMemory[i], inputBytes[i], numInputs[i]) = getNeededArrays(lists[i], i);
}
(maxWidths, numTimesteps) = getGraphInfo(lists[0]);
// First iteration is for warmup.
boolean finishedIter[];
for (int i = 0; i < 2; i = i + 1) {
boolean startIter;
if (i == 0) {
startIter = true;
} else {
startIter = finishedIter[i-1];
}
// Run the benchmark.
wait deep (startIter, lists, locations, dependencies, reverseDependencies, inputMemory, inputBytes, numInputs, maxWidths, numTimesteps) {
float start = clock() =>
boolean finished[] = runBenchmark(lists, locations, dependencies, reverseDependencies, inputMemory, inputBytes, numInputs, maxWidths, numTimesteps);
wait deep (finished) {
float end = clock() =>
if (i == 1) {
location L = locationFromRank(0);
@location=L reportTiming(apps[0], end - start);
}
finishedIter[i] = true;
}
}
}
}