-
Notifications
You must be signed in to change notification settings - Fork 1
/
nqueens_chpl.chpl
123 lines (96 loc) · 2.82 KB
/
nqueens_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
/*
Sequential backtracking to solve instances of the N-Queens problem in Chapel.
*/
use Time;
use Pool;
use NQueens_node;
/*******************************************************************************
Implementation of the sequential N-Queens search.
*******************************************************************************/
config const N = 14;
config const g = 1;
proc check_parameters()
{
if ((N <= 0) || (g <= 0)) {
halt("All parameters must be positive integers.\n");
}
}
proc print_settings()
{
writeln("\n=================================================");
writeln("Sequential Chapel\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(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.pushBack(child);
tree_loc += 1;
}
}
}
// Sequential N-Queens search.
proc nqueens_search(ref exploredTree: uint, ref exploredSol: uint, ref elapsedTime: real)
{
var root = new Node(N);
var pool = new SinglePool(Node);
pool.pushBack(root);
var timer: stopwatch;
timer.start();
while true {
var hasWork = 0;
var parent = pool.popBack(hasWork);
if !hasWork then break;
decompose(parent, exploredTree, exploredSol, pool);
}
timer.stop();
elapsedTime = timer.elapsed();
writeln("\nExploration terminated.");
}
proc main()
{
check_parameters();
print_settings();
var exploredTree: uint = 0;
var exploredSol: uint = 0;
var elapsedTime: real;
nqueens_search(exploredTree, exploredSol, elapsedTime);
print_results(exploredTree, exploredSol, elapsedTime);
return 0;
}