Skip to content

Commit

Permalink
ldList rdList colList and startQueensIjklList now also split up for m…
Browse files Browse the repository at this point in the history
…ultithreading
  • Loading branch information
olepoeschl committed Jan 14, 2022
1 parent 2fec7d9 commit 164211a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 49 deletions.
78 changes: 39 additions & 39 deletions src/de/nqueensfaf/SolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,53 @@ void setUp() throws Exception {
void tearDown() throws Exception {
}

// Manual test
// @Test
// void testCpuSolver() {
// CpuSolver s = new CpuSolver();
// s.setN(17);
// s.setThreadcount(4);
// s.addTerminationCallback(() -> {
// System.out.println(s.getSolutions() + " solutions found in " + s.getDuration() + " ms");
// });
// s.solve();
// }

// Manual test
@Test
void testGpuSolver() {
// try {
// // for making sure the profiler (VisualVM) is ready
// Thread.sleep(5000);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
GpuSolver s = new GpuSolver();
s.setDevice(0);
s.setProgressUpdatesEnabled(false);
void testCpuSolver() {
CpuSolver s = new CpuSolver();
s.setN(18);
new Thread(() -> {
while(true) {
if(s.getGlobalWorkSize() == 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
System.out.println("globalWorkSize: " + s.getGlobalWorkSize());
break;
}
}).start();
s.setOnProgressUpdateCallback((progress, solutions) -> {
System.out.println("progress: " + progress + ", solutions: " + solutions);
});
s.setThreadcount(8);
s.addTerminationCallback(() -> {
System.out.println(s.getSolutions() + " solutions found in " + s.getDuration() + " ms");
});
s.solve();
}

// Manual test
// @Test
// void testGpuSolver() {
//// try {
//// // for making sure the profiler (VisualVM) is ready
//// Thread.sleep(5000);
//// } catch (InterruptedException e1) {
//// e1.printStackTrace();
//// }
// GpuSolver s = new GpuSolver();
// s.setDevice(0);
// s.setProgressUpdatesEnabled(false);
// s.setN(18);
// new Thread(() -> {
// while(true) {
// if(s.getGlobalWorkSize() == 0) {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// continue;
// }
// System.out.println("globalWorkSize: " + s.getGlobalWorkSize());
// break;
// }
// }).start();
// s.setOnProgressUpdateCallback((progress, solutions) -> {
// System.out.println("progress: " + progress + ", solutions: " + solutions);
// });
// s.addTerminationCallback(() -> {
// System.out.println(s.getSolutions() + " solutions found in " + s.getDuration() + " ms");
// });
// s.solve();
// }

/*
// Manual test
Expand Down
48 changes: 42 additions & 6 deletions src/de/nqueensfaf/compute/CpuSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CpuSolver extends Solver {

private final int smallestN = 6;
private int threadcount = 1;
private int kbit, lbit, preQueens = 4, L, mask, LD, RD, counter;
private int kbit, lbit, preQueens = 5, L, mask, LD, RD, counter;
private long start, end;
private HashSet<Integer> startConstellations = new HashSet<Integer>();
private ArrayList<Integer> ldList = new ArrayList<Integer>(), rdList = new ArrayList<Integer>(), colList = new ArrayList<Integer>();
Expand Down Expand Up @@ -53,20 +53,51 @@ protected void run() {
startConstCount = startConstellations.size();
}

System.out.println(startConstellations.size() == ldList.size());

// split starting constellations in [cpu] many lists (splitting the work for the threads)
ArrayList<ArrayDeque<Integer>> threadConstellations = new ArrayList<ArrayDeque<Integer>>(threadcount);
for(int i = 0; i < threadcount; i++) {
threadConstellations.add(new ArrayDeque<Integer>());
ArrayList<ArrayList<ArrayDeque<Integer>>> threadConstellations = new ArrayList<ArrayList<ArrayDeque<Integer>>>();
threadConstellations.add(new ArrayList<ArrayDeque<Integer>>(threadcount)); // startConstellations, just keeping for backwards compatibility
threadConstellations.add(new ArrayList<ArrayDeque<Integer>>(threadcount)); // ld
threadConstellations.add(new ArrayList<ArrayDeque<Integer>>(threadcount)); // rd
threadConstellations.add(new ArrayList<ArrayDeque<Integer>>(threadcount)); // col
threadConstellations.add(new ArrayList<ArrayDeque<Integer>>(threadcount)); // startQueensIjkl
for(var list : threadConstellations) {
for(int i = 0; i < threadcount; i++) {
list.add(new ArrayDeque<Integer>());
}
}
// startConstellations
int i = 0;
for(int constellation : startConstellations) {
threadConstellations.get((i++) % threadcount).addFirst(constellation);
threadConstellations.get(0).get((i++) % threadcount).addFirst(constellation);
}
// ld
i = 0;
for(int ld : ldList) {
threadConstellations.get(1).get((i++) % threadcount).addFirst(ld);
}
// rd
i = 0;
for(int rd : rdList) {
threadConstellations.get(2).get((i++) % threadcount).addFirst(rd);
}
// col
i = 0;
for(int col : colList) {
threadConstellations.get(3).get((i++) % threadcount).addFirst(col);
}
// startQueensIjkl
i = 0;
for(int startQueensIjkl : startQueensIjklList) {
threadConstellations.get(4).get((i++) % threadcount).addFirst(startQueensIjkl);
}

// start the threads and wait until they are all finished
ExecutorService executor = Executors.newFixedThreadPool(threadcount);
for(i = 0; i < threadcount; i++) {
CpuSolverThread cpuSolverThread = new CpuSolverThread(N, threadConstellations.get(i), this, ldList, rdList, colList, startQueensIjklList);
CpuSolverThread cpuSolverThread = new CpuSolverThread(this, N, threadConstellations.get(0).get(i), threadConstellations.get(1).get(i),
threadConstellations.get(2).get(i), threadConstellations.get(3).get(i), threadConstellations.get(4).get(i));
threads.add(cpuSolverThread);
executor.submit(cpuSolverThread);
}
Expand Down Expand Up @@ -147,6 +178,10 @@ public void reset() {
timePassed = 0;
pauseStart = 0;
startConstellations.clear();
ldList.clear();
rdList.clear();
colList.clear();
startQueensIjklList.clear();
solvedConstellations = 0;
solutions = 0;
threads.clear();
Expand Down Expand Up @@ -256,6 +291,7 @@ private void genConstellations() {
setPreQueens(ld, rd, col, k, l, 1, 4);
currentSize = startQueensIjklList.size();
// jkl and sym and start are the same for all subconstellations
System.out.println(counter);
for(int a = 0; a < counter; a++) {
startQueensIjklList.set(currentSize-a-1, startQueensIjklList.get(currentSize-a-1) | (preQueens << 20) | toijkl(i, j, k, l));
}
Expand Down
12 changes: 8 additions & 4 deletions src/de/nqueensfaf/compute/CpuSolverThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class CpuSolverThread extends Thread {
private int mark1, mark2, mark3;

// list of uncalculated starting positions, their indices
private ArrayDeque<Integer> startConstellations;
private ArrayDeque<Integer> startConstellations, ldList, rdList, colList, startQueensIjklList;

// for pausing or cancelling the run
private boolean cancel = false, running = false;
private int pause = 0;
private CpuSolver caller;

CpuSolverThread(int N, ArrayDeque<Integer> startConstellations, CpuSolver caller,
ArrayList<Integer> ldList, ArrayList<Integer> rdList, ArrayList<Integer> colList, ArrayList<Integer> startQueensIjklList) {
CpuSolverThread(CpuSolver caller, int N, ArrayDeque<Integer> startConstellations, ArrayDeque<Integer> ldList,
ArrayDeque<Integer> rdList, ArrayDeque<Integer> colList, ArrayDeque<Integer> startQueensIjklList) {
this.caller = caller;
this.N = N;
N3 = N - 3;
N4 = N - 4;
Expand All @@ -29,7 +30,10 @@ class CpuSolverThread extends Thread {
L3 = 1 << N3;
L4 = 1 << N4;
this.startConstellations = startConstellations;
this.caller = caller;
this.ldList = ldList;
this.rdList = rdList;
this.colList = colList;
this.startQueensIjklList = startQueensIjklList;
}

// Recursive functions for Placing the Queens
Expand Down

0 comments on commit 164211a

Please sign in to comment.