Solving a Sudoku in Parallel
description
Transcript of Solving a Sudoku in Parallel
![Page 1: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/1.jpg)
1
Solving a Sudoku in Parallel
“Sudoku is a denial of service attack on human intellect” -- Ben Laurie
by:Alton Chiu, Ehsan Nasiri, Rafat Rashid
![Page 2: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/2.jpg)
2
Sudoku
9x9 Puzzle
16x16 Puzzle
![Page 3: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/3.jpg)
3
Sudoku Singleton
9x9 Puzzle
16x16 Puzzle
CELLSingleton
![Page 4: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/4.jpg)
4
Sudoku Peers
9x9 Puzzle
16x16 Puzzle
CELL PEERS
![Page 5: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/5.jpg)
5
4 8 53
72 6
8 41
6 3 75 21 4
•
•
Brute Force You Say?
![Page 6: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/6.jpg)
6
4 8 53
72 6
8 41
6 3 75 21 4
• If a cell has one value x, remove x from its peers’ possibility list
• If none of your peers have value x in their possibility list, you are x
Constraint Propagation (CP)
Possibility list = {4}
Possibility list = {2,6,7,8,9}
4
.
.
.
![Page 7: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/7.jpg)
7
• If a cell has one value x, remove x from its peers’ possibility list
• If none of your peers have value x in their possibility list, you are x
Constraint Propagation (CP)
![Page 8: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/8.jpg)
8
• Try all possibilities until you hit one that works
Search
Possibility list = {7,2}
![Page 9: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/9.jpg)
9
• Try all possibilities until you hit one that works
Search
Possibility list = {7,2}
7 2
![Page 10: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/10.jpg)
10
• Algorithm: CP Search CP Search …
Decision Tree
Possibility list = {7,2}
7 2
![Page 11: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/11.jpg)
11
Decision Tree7/2
1/3/4 5/6/7
![Page 12: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/12.jpg)
12
Decision Tree7/2
1/3/4 5/6/7
Search Picked: 7Do CP()
7
1/3/4 6/7
2
1/3/4 5/6/7
Search Picked: 2Do CP()
![Page 13: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/13.jpg)
13
Decision Tree7/2
1/3/4 5/6/7
Search Picked: 7Do CP()
7
1/3/4 6/7
2
1/3/4 5/6/7
Search Picked: 2Do CP()
7
4 7
Pick: 7Do CP()Pick: 6
Do CP()
7
1 7
7
3 7
![Page 14: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/14.jpg)
14
Decision Tree – Search Candidate
. . . . . .
.
.
.
.
.
.
![Page 15: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/15.jpg)
15
Decision Tree – Search Candidate
. . . . . .
.
.
.
.
.
.
![Page 16: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/16.jpg)
16
Serial Algorithm: DFS
. . .✔
![Page 17: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/17.jpg)
17
Parallel Algorithm: DFS
. . .✔
![Page 18: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/18.jpg)
18
Improving the Parallel Algorithm: Message Passing
432
. . . 1 5
Thread#2 List= {5,2,3,4}Thread#1 List= {}
Thread#1 List= {3}
Thread#2 List= {5,2,4}
![Page 19: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/19.jpg)
19
Improving the Parallel Algorithm: Message Passing
Thread #1 Thread #2 Thread #3 Thread #4
Private Puzzle List
Ask for work
Ask for work Ask for work
Ask for work
![Page 20: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/20.jpg)
20
Improving the Parallel Algorithm: LockingGlobal Puzzle List (shared memory)
POP()
✔
lock_acquire();List.pop_front();lock_release();
lock_acquire();List.push_back(new_node);lock_release();
Broadcast
![Page 21: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/21.jpg)
21
• Used pthreads library for parallelism• Amortized results: – 100 ‘evil’ puzzles, 10 runs for each algorithm– Evil = the puzzle can’t be solved if one more cell is removed
• Measured on UG machines– Intel Core 2 Quad (2.66 GHz)– 4 GB RAM
Evaluation Methodology
![Page 22: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/22.jpg)
22
Results - Runtime
0 1 2 3 4 5 6 7 80
2
4
6
8
10
12
14
16
18
20
Runtime for 16x16 (amortized)
Parallel_MsgPassingSerialParallel_Locking (fine)Parallel_Locking(coarse)
Number of Threads
Aver
age
Runti
me
(Sec
onds
)
![Page 23: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/23.jpg)
23
Results - Yielding
1 2 3 4 5 6 7 84
6
8
10
12
14
16
18
Effect of Yielding
MsgPassing_pthread_yield()MsgPassing_Spinning
Number of Threads
Aver
age
Runti
me
(Sec
onds
)
• pthread_yield() can save you a large number of CPU cycles
![Page 24: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/24.jpg)
24
Results – Conditional Signaling• pthread_cond_signal() is expensive!• Can’t always avoid it. Our application was simple enough to
avoid it.
1 2 3 4 5 6 7 80
2
4
6
8
10
12
14
16
18
Using pthread_condition_signal
MsgPassing_pthread_yieldMsgPassing_pthread_cond_signal()
Number of Threads
Aver
age
Runti
me
(Sec
onds
)
![Page 25: Solving a Sudoku in Parallel](https://reader034.fdocuments.net/reader034/viewer/2022042505/5681638d550346895dd48266/html5/thumbnails/25.jpg)
25
• Solving a Sudoku is fun… until you try to parallelize it!• Strongly connected dependencies make it extremely
difficult to parallelize constraint propagation • Traversing the solution space tree in parallel is the
best way to reach a solution faster.• We achieved an average of 4.6X speedup using 4
threads (using locking and yielding)
Conclusions