ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/NQueensCS.java
Revision: 1.7
Committed: Mon Nov 29 20:58:07 2010 UTC (13 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
consistent ternary operator style

File Contents

# User Rev Content
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.6
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.7 ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
39 dl 1.1 new ForkJoinPool(procs);
40     lastStealCount = g.getStealCount();
41     for (int i = FIRST_SIZE; i <= LAST_SIZE; i++)
42     test(g, i);
43     System.out.println(g);
44 jsr166 1.6 g.shutdown();
45 dl 1.1 }
46     }
47    
48     static void test(ForkJoinPool g, int i) throws Exception {
49     boardSize = i;
50     int ps = g.getParallelism();
51     long start = System.nanoTime();
52     NQueensCS task = new NQueensCS(new int[0]);
53     g.invoke(task);
54     int solutions = task.solutions;
55     long time = System.nanoTime() - start;
56 dl 1.5 double secs = ((double)time) / NPS;
57 dl 1.1 if (solutions != expectedSolutions[i])
58     throw new Error();
59     System.out.printf("NQueensCS %3d", i);
60     System.out.printf(" Time: %7.3f", secs);
61     long sc = g.getStealCount();
62     long ns = sc - lastStealCount;
63     lastStealCount = sc;
64     System.out.printf(" Steals/t: %5d", ns/ps);
65     System.out.println();
66 jsr166 1.6 }
67 dl 1.1
68 jsr166 1.6 // Boards are represented as arrays where each cell
69 dl 1.1 // holds the column number of the queen in that row
70    
71     final int[] sofar;
72     NQueensCS nextSubtask; // to link subtasks
73     int solutions;
74 jsr166 1.6 NQueensCS(int[] a) {
75     this.sofar = a;
76 dl 1.1 }
77    
78     public final void compute() {
79     NQueensCS subtasks;
80     int bs = boardSize;
81     if (sofar.length >= bs)
82     solutions = 1;
83     else if ((subtasks = explore(sofar, bs)) != null)
84     solutions = processSubtasks(subtasks);
85     }
86    
87     private static NQueensCS explore(int[] array, int bs) {
88     int row = array.length;
89     NQueensCS s = null; // subtask list
90     outer:
91     for (int q = 0; q < bs; ++q) {
92     for (int i = 0; i < row; i++) {
93     int p = array[i];
94     if (q == p || q == p - (row - i) || q == p + (row - i))
95     continue outer; // attacked
96     }
97     NQueensCS first = s; // lag forks to ensure 1 kept
98     if (first != null)
99     first.fork();
100     int[] next = Arrays.copyOf(array, row+1);
101     next[row] = q;
102     NQueensCS subtask = new NQueensCS(next);
103     subtask.nextSubtask = first;
104     s = subtask;
105     }
106     return s;
107     }
108    
109     private static int processSubtasks(NQueensCS s) {
110     // Always run first the task held instead of forked
111 jsr166 1.6 s.compute();
112 dl 1.1 int ns = s.solutions;
113     s = s.nextSubtask;
114     // Then the unstolen ones
115     while (s != null && s.tryUnfork()) {
116     s.compute();
117     ns += s.solutions;
118     s = s.nextSubtask;
119     }
120     // Then wait for the stolen ones
121     while (s != null) {
122     s.join();
123     ns += s.solutions;
124     s = s.nextSubtask;
125     }
126     return ns;
127     }
128     }