1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
//import jsr166y.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
import java.util.concurrent.*; |
10 |
|
|
|
11 |
|
|
public final class NQueensCS extends RecursiveAction { |
12 |
|
|
|
13 |
|
|
static long lastStealCount; |
14 |
|
|
static int boardSize; |
15 |
|
|
|
16 |
|
|
static final int[] expectedSolutions = new int[] { |
17 |
|
|
0, 1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, |
18 |
|
|
73712, 365596, 2279184, 14772512, 95815104, 666090624 |
19 |
|
|
}; // see http://www.durangobill.com/N_Queens.html |
20 |
|
|
|
21 |
|
|
static final int FIRST_SIZE = 8; // smaller ones too short to measure well |
22 |
|
|
static final int LAST_SIZE = 15; // bigger ones too long to wait for |
23 |
|
|
|
24 |
|
|
/** for time conversion */ |
25 |
|
|
static final long NPS = (1000L * 1000 * 1000); |
26 |
jsr166 |
1.2 |
|
27 |
dl |
1.1 |
public static void main(String[] args) throws Exception { |
28 |
|
|
int procs = 0; |
29 |
|
|
try { |
30 |
|
|
if (args.length > 0) |
31 |
|
|
procs = Integer.parseInt(args[0]); |
32 |
|
|
} |
33 |
|
|
catch (Exception e) { |
34 |
|
|
System.out.println("Usage: java NQueensCS <threads> "); |
35 |
|
|
return; |
36 |
|
|
} |
37 |
|
|
for (int reps = 0; reps < 2; ++reps) { |
38 |
jsr166 |
1.3 |
ForkJoinPool g = (procs == 0) ? |
39 |
|
|
new ForkJoinPool() : |
40 |
dl |
1.1 |
new ForkJoinPool(procs); |
41 |
|
|
lastStealCount = g.getStealCount(); |
42 |
|
|
for (int i = FIRST_SIZE; i <= LAST_SIZE; i++) |
43 |
|
|
test(g, i); |
44 |
|
|
System.out.println(g); |
45 |
jsr166 |
1.2 |
g.shutdown(); |
46 |
dl |
1.1 |
} |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
static void test(ForkJoinPool g, int i) throws Exception { |
50 |
|
|
boardSize = i; |
51 |
|
|
int ps = g.getParallelism(); |
52 |
|
|
long start = System.nanoTime(); |
53 |
|
|
NQueensCS task = new NQueensCS(new int[0]); |
54 |
|
|
g.invoke(task); |
55 |
|
|
int solutions = task.solutions; |
56 |
|
|
long time = System.nanoTime() - start; |
57 |
jsr166 |
1.4 |
double secs = (double) time / NPS; |
58 |
dl |
1.1 |
if (solutions != expectedSolutions[i]) |
59 |
|
|
throw new Error(); |
60 |
|
|
System.out.printf("NQueensCS %3d", i); |
61 |
|
|
System.out.printf(" Time: %7.3f", secs); |
62 |
|
|
long sc = g.getStealCount(); |
63 |
|
|
long ns = sc - lastStealCount; |
64 |
|
|
lastStealCount = sc; |
65 |
|
|
System.out.printf(" Steals/t: %5d", ns/ps); |
66 |
|
|
System.out.println(); |
67 |
jsr166 |
1.2 |
} |
68 |
dl |
1.1 |
|
69 |
jsr166 |
1.2 |
// Boards are represented as arrays where each cell |
70 |
dl |
1.1 |
// holds the column number of the queen in that row |
71 |
|
|
|
72 |
|
|
final int[] sofar; |
73 |
|
|
NQueensCS nextSubtask; // to link subtasks |
74 |
|
|
int solutions; |
75 |
jsr166 |
1.2 |
NQueensCS(int[] a) { |
76 |
|
|
this.sofar = a; |
77 |
dl |
1.1 |
} |
78 |
|
|
|
79 |
|
|
public final void compute() { |
80 |
|
|
NQueensCS subtasks; |
81 |
|
|
int bs = boardSize; |
82 |
|
|
if (sofar.length >= bs) |
83 |
|
|
solutions = 1; |
84 |
|
|
else if ((subtasks = explore(sofar, bs)) != null) |
85 |
|
|
solutions = processSubtasks(subtasks); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
private static NQueensCS explore(int[] array, int bs) { |
89 |
|
|
int row = array.length; |
90 |
|
|
NQueensCS s = null; // subtask list |
91 |
|
|
outer: |
92 |
|
|
for (int q = 0; q < bs; ++q) { |
93 |
|
|
for (int i = 0; i < row; i++) { |
94 |
|
|
int p = array[i]; |
95 |
|
|
if (q == p || q == p - (row - i) || q == p + (row - i)) |
96 |
|
|
continue outer; // attacked |
97 |
|
|
} |
98 |
|
|
NQueensCS first = s; // lag forks to ensure 1 kept |
99 |
|
|
if (first != null) |
100 |
|
|
first.fork(); |
101 |
|
|
int[] next = Arrays.copyOf(array, row+1); |
102 |
|
|
next[row] = q; |
103 |
|
|
NQueensCS subtask = new NQueensCS(next); |
104 |
|
|
subtask.nextSubtask = first; |
105 |
|
|
s = subtask; |
106 |
|
|
} |
107 |
|
|
return s; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
private static int processSubtasks(NQueensCS s) { |
111 |
|
|
// Always run first the task held instead of forked |
112 |
jsr166 |
1.2 |
s.compute(); |
113 |
dl |
1.1 |
int ns = s.solutions; |
114 |
|
|
s = s.nextSubtask; |
115 |
|
|
// Then the unstolen ones |
116 |
|
|
while (s != null && s.tryUnfork()) { |
117 |
|
|
s.compute(); |
118 |
|
|
ns += s.solutions; |
119 |
|
|
s = s.nextSubtask; |
120 |
|
|
} |
121 |
|
|
// Then wait for the stolen ones |
122 |
|
|
while (s != null) { |
123 |
|
|
s.join(); |
124 |
|
|
ns += s.solutions; |
125 |
|
|
s = s.nextSubtask; |
126 |
|
|
} |
127 |
|
|
return ns; |
128 |
|
|
} |
129 |
|
|
} |