-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnqueens_dist_multigpu_chpl.chpl
355 lines (289 loc) · 9.74 KB
/
nqueens_dist_multigpu_chpl.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Distributed multi-GPU backtracking to solve instances of the N-Queens problem in Chapel.
*/
use Time;
use Pool;
use Pool_par;
use PrivateDist;
use GpuDiagnostics;
use NQueens_node;
config const BLOCK_SIZE = 512;
/*******************************************************************************
Implementation of the distributed multi-GPU N-Queens search.
*******************************************************************************/
config const N = 14;
config const g = 1;
config const m = 25;
config const M = 50000;
config const D = 1;
proc check_parameters()
{
if ((N <= 0) || (g <= 0) || (m <= 0) || (M <= 0) || (D <= 0)) {
halt("All parameters must be positive integers.\n");
}
}
proc print_settings()
{
writeln("\n=================================================");
writeln("Distributed multi-GPU Chapel (", numLocales, "x", D, " GPUs)\n");
writeln("Resolution of the ", N, "-Queens instance");
writeln(" with ", g, " safety check(s) per evaluation");
writeln("=================================================");
}
proc print_results(const exploredTree: uint, const exploredSol: uint, const timer: real)
{
writeln("\n=================================================");
writeln("Size of the explored tree: ", exploredTree);
writeln("Number of explored solutions: ", exploredSol);
writeln("Elapsed time: ", timer, " [s]");
writeln("=================================================\n");
}
// Check queen's safety.
proc isSafe(const board, const queen_num, const row_pos): uint(8)
{
var isSafe: uint(8) = 1;
for i in 0..#queen_num {
const other_row_pos = board[i];
for _g in 0..#g {
if (other_row_pos == row_pos - (queen_num - i) ||
other_row_pos == row_pos + (queen_num - i)) {
isSafe = 0;
}
}
}
return isSafe;
}
// Evaluate and generate children nodes on CPU.
proc decompose(const parent: Node, ref tree_loc: uint, ref num_sol: uint, ref pool: SinglePool_par(Node))
{
const depth = parent.depth;
if (depth == N) {
num_sol += 1;
}
for j in depth..(N-1) {
if isSafe(parent.board, depth, parent.board[j]) {
var child = new Node();
child.depth = parent.depth;
child.board = parent.board;
child.board[depth] <=> child.board[j];
child.depth += 1;
pool.pushBackFree(child);
tree_loc += 1;
}
}
}
// Evaluate a bulk of parent nodes on GPU.
proc evaluate_gpu(const parents_d: [] Node, const size, ref labels_d)
{
@assertOnGpu
foreach threadId in 0..#size {
const parentId = threadId / N;
const k = threadId % N;
const parent = parents_d[parentId];
const depth = parent.depth;
const queen_num = parent.board[k];
var isSafe: uint(8);
// If child 'k' is not scheduled, we evaluate its safety 'G' times, otherwise 0.
if (k >= depth) {
isSafe = 1;
/* const G_notScheduled = g * (k >= depth); */
for i in 0..#depth {
const pbi = parent.board[i];
for _g in 0..#g {//G_notScheduled {
isSafe *= (pbi != queen_num - (depth - i) &&
pbi != queen_num + (depth - i));
}
}
labels_d[threadId] = isSafe;
}
}
}
// Generate children nodes (evaluated on GPU) on CPU.
proc generate_children(const ref parents: [] Node, const size: int, const ref labels: [] uint(8),
ref exploredTree: uint, ref exploredSol: uint, ref pool: SinglePool(Node))
{
for i in 0..#size {
const parent = parents[i];
const depth = parent.depth;
if (depth == N) {
exploredSol += 1;
}
for j in depth..(N-1) {
if (labels[j + i * N] == 1) {
var child = new Node();
child.depth = depth + 1;
child.board = parent.board;
child.board[depth] <=> child.board[j];
pool.pushBack(child);
exploredTree += 1;
}
}
}
}
class WrapperClassArrayParents {
forwarding var arr: [0..#M] Node = noinit;
}
type WrapperArrayParents = owned WrapperClassArrayParents?;
class WrapperClassArrayLabels {
forwarding var arr: [0..#(M*N)] uint(8) = noinit;
}
type WrapperArrayLabels = owned WrapperClassArrayLabels?;
// Distributed multi-GPU N-Queens search.
proc nqueens_search(ref exploredTree: uint, ref exploredSol: uint, ref elapsedTime: real)
{
var root = new Node(N);
var pool = new SinglePool_par(Node);
pool.pushBackFree(root);
var timer: stopwatch;
/*
Step 1: We perform a partial breadth-first search on CPU in order to create
a sufficiently large amount of work for GPU computation.
*/
timer.start();
while (pool.size < D*m*numLocales) {
var hasWork = 0;
var parent = pool.popFrontFree(hasWork);
if !hasWork then break;
decompose(parent, exploredTree, exploredSol, pool);
}
timer.stop();
const res1 = (timer.elapsed(), exploredTree, exploredSol);
writeln("\nInitial search on CPU completed");
writeln("Size of the explored tree: ", res1[1]);
writeln("Number of explored solutions: ", res1[2]);
writeln("Elapsed time: ", res1[0], " [s]\n");
/*
Step 2: We continue the search on GPU in a depth-first manner, until there
is not enough work.
*/
timer.start();
var eachLocaleExploredTree, eachLocaleExploredSol: [PrivateSpace] uint = noinit;
const poolSize = pool.size;
const c = poolSize / numLocales;
const l = poolSize - (numLocales-1)*c;
const f = pool.front;
pool.front = 0;
pool.size = 0;
coforall (locID, loc) in zip(0..#numLocales, Locales) with (ref pool,
ref eachLocaleExploredTree, ref eachLocaleExploredSol) do on loc {
var eachExploredTree, eachExploredSol: [0..#D] uint = noinit;
var pool_lloc = new SinglePool(Node);
// each locale gets its chunk
pool_lloc.elements[0..#c] = pool.elements[locID+f.. by numLocales #c];
pool_lloc.size += c;
if (locID == numLocales-1) {
pool_lloc.elements[c..#(l-c)] = pool.elements[(numLocales*c)+f..#(l-c)];
pool_lloc.size += l-c;
}
const poolSize_l = pool_lloc.size;
const c_l = poolSize_l / D;
const l_l = poolSize_l - (D-1)*c_l;
const f_l = pool_lloc.front;
pool_lloc.front = 0;
pool_lloc.size = 0;
coforall gpuID in 0..#D with (ref pool, ref eachExploredTree, ref eachExploredSol) {
const device = here.gpus[gpuID];
var tree, sol: uint;
var pool_loc = new SinglePool(Node);
// each task gets its chunk
pool_loc.elements[0..#c_l] = pool_lloc.elements[gpuID+f_l.. by D #c_l];
pool_loc.size += c_l;
if (gpuID == D-1) {
pool_loc.elements[c_l..#(l_l-c_l)] = pool_lloc.elements[(D*c_l)+f_l..#(l_l-c_l)];
pool_loc.size += l_l-c_l;
}
var parents: [0..#M] Node = noinit;
var labels: [0..#(M*N)] uint(8) = noinit;
var parents_d: WrapperArrayParents;
var labels_d: WrapperArrayLabels;
on device {
parents_d = new WrapperArrayParents();
labels_d = new WrapperArrayLabels();
}
while true {
/*
Each task gets its parents nodes from the pool.
*/
var poolSize = pool_loc.size;
if (poolSize >= m) {
poolSize = min(poolSize, M);
for i in 0..#poolSize {
var hasWork = 0;
parents[i] = pool_loc.popBack(hasWork);
if !hasWork then break;
}
const numLabels = N * poolSize;
on device {
parents_d!.arr = parents; // host-to-device
evaluate_gpu(parents_d!.arr, numLabels, labels_d!.arr);
labels = labels_d!.arr; // device-to-host
}
/*
Each task generates and inserts its children nodes to the pool.
*/
generate_children(parents, poolSize, labels, tree, sol, pool_loc);
}
else {
break;
}
}
const poolLocSize = pool_loc.size;
if (poolLocSize > 0) {
for p in 0..#poolLocSize {
var hasWork = 0;
pool.pushBack(pool_loc.popBack(hasWork));
if !hasWork then break;
}
}
eachExploredTree[gpuID] = tree;
eachExploredSol[gpuID] = sol;
}
eachLocaleExploredTree[locID] = (+ reduce eachExploredTree);
eachLocaleExploredSol[locID] = (+ reduce eachExploredSol);
}
timer.stop();
exploredTree += (+ reduce eachLocaleExploredTree);
exploredSol += (+ reduce eachLocaleExploredSol);
writeln("workload per Locale: ", 100.0*eachLocaleExploredTree/(exploredTree-res1[1]):real, "\n");
const res2 = (timer.elapsed(), exploredTree, exploredSol) - res1;
writeln("Search on GPU completed");
writeln("Size of the explored tree: ", res2[1]);
writeln("Number of explored solutions: ", res2[2]);
writeln("Elapsed time: ", res2[0], " [s]\n");
/*
Step 3: We complete the depth-first search on CPU.
*/
timer.start();
while true {
var hasWork = 0;
var parent = pool.popBackFree(hasWork);
if !hasWork then break;
decompose(parent, exploredTree, exploredSol, pool);
}
timer.stop();
elapsedTime = timer.elapsed();
const res3 = (elapsedTime, exploredTree, exploredSol) - res1 - res2;
writeln("Search on CPU completed");
writeln("Size of the explored tree: ", res3[1]);
writeln("Number of explored solutions: ", res3[2]);
writeln("Elapsed time: ", res3[0], " [s]");
writeln("\nExploration terminated.");
}
proc main()
{
check_parameters();
print_settings();
var exploredTree: uint = 0;
var exploredSol: uint = 0;
var elapsedTime: real;
startGpuDiagnostics();
nqueens_search(exploredTree, exploredSol, elapsedTime);
stopGpuDiagnostics();
print_results(exploredTree, exploredSol, elapsedTime);
writeln("GPU diagnostics:");
writeln(" kernel_launch: ", getGpuDiagnostics().kernel_launch);
writeln(" host_to_device: ", getGpuDiagnostics().host_to_device);
writeln(" device_to_host: ", getGpuDiagnostics().device_to_host);
writeln(" device_to_device: ", getGpuDiagnostics().device_to_device);
return 0;
}