diff --git a/Lab1/README.md b/Lab1/README.md index 8a0e781f..1c754ff4 100644 --- a/Lab1/README.md +++ b/Lab1/README.md @@ -122,7 +122,7 @@ Your program should be able to: 2. Successfully solve the puzzles in the input file, and output the results in the format described before. 3. Use multiple threads/processes to make use of most of your machine's CPU cores. -\[Tips\]: 1) Use event queue to dispatch tasks and merge results to/from worker threads. 2) Dynamically detect how many CPU cores are there on your machine, in order to decide how many threads/processes your program uses. 3) Be careful about the contention among multiple threads/processes +\[Tips\]: 1) Use event **queue** to dispatch tasks and merge results to/from worker threads. 2) Dynamically detect how many CPU cores are there on your machine, in order to decide how many threads/processes your program uses. 3) Be careful about the contention among multiple threads/processes ##### 3.1.3.2 Advanced version diff --git a/Lab1/src/Sudoku/Makefile b/Lab1/src/Sudoku/Makefile index c5e2a079..5b6aef98 100644 --- a/Lab1/src/Sudoku/Makefile +++ b/Lab1/src/Sudoku/Makefile @@ -1,7 +1,7 @@ CXXFLAGS+=-O2 -ggdb -DDEBUG CXXFLAGS+=-Wall -Wextra -all: sudoku +all: sudoku_solve -sudoku: main.cc neighbor.cc sudoku_basic.cc sudoku_min_arity.cc sudoku_min_arity_cache.cc sudoku_dancing_links.cc +sudoku_solve: sudoku_solve.cc neighbor.cc sudoku_basic.cc sudoku_min_arity.cc sudoku_min_arity_cache.cc sudoku_dancing_links.cc g++ -O2 -o $@ $^ diff --git a/Lab1/src/Sudoku/Makefile0 b/Lab1/src/Sudoku/Makefile0 new file mode 100644 index 00000000..c5e2a079 --- /dev/null +++ b/Lab1/src/Sudoku/Makefile0 @@ -0,0 +1,7 @@ +CXXFLAGS+=-O2 -ggdb -DDEBUG +CXXFLAGS+=-Wall -Wextra + +all: sudoku + +sudoku: main.cc neighbor.cc sudoku_basic.cc sudoku_min_arity.cc sudoku_min_arity_cache.cc sudoku_dancing_links.cc + g++ -O2 -o $@ $^ diff --git a/Lab1/src/Sudoku/main.cc b/Lab1/src/Sudoku/main.cc index 4ed2f3a5..aa0730e2 100644 --- a/Lab1/src/Sudoku/main.cc +++ b/Lab1/src/Sudoku/main.cc @@ -55,3 +55,4 @@ int main(int argc, char* argv[]) return 0; } +// This is a test \ No newline at end of file diff --git a/Lab1/src/Sudoku/sudoku b/Lab1/src/Sudoku/sudoku new file mode 100644 index 00000000..5fb59225 Binary files /dev/null and b/Lab1/src/Sudoku/sudoku differ diff --git a/Lab1/src/Sudoku/sudoku_solve b/Lab1/src/Sudoku/sudoku_solve new file mode 100644 index 00000000..96819086 Binary files /dev/null and b/Lab1/src/Sudoku/sudoku_solve differ diff --git a/Lab1/src/Sudoku/sudoku_solve.cc b/Lab1/src/Sudoku/sudoku_solve.cc new file mode 100644 index 00000000..53fafcbf --- /dev/null +++ b/Lab1/src/Sudoku/sudoku_solve.cc @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +#include "sudoku.h" + +int64_t now() +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec * 1000000 + tv.tv_usec; +} + +int main(int argc, char const *argv[]) +{ + init_neighbors(); + + FILE* fp = fopen(argv[1], "r"); + char puzzle[128]; + int total_solved = 0; + int total = 0; + bool (*solve)(int) = solve_sudoku_basic; + + solve = solve_sudoku_min_arity; + // solve = solve_sudoku_min_arity_cache; + // solve = solve_sudoku_dancing_links; + + int64_t start = now(); + // 一直读取, 读到 puzzle 中 + while (fgets(puzzle, sizeof puzzle, fp) != NULL) { + if (strlen(puzzle) >= N) { + ++total; + input(puzzle); + init_cache(); + + if (solve(0)) { + ++total_solved; + if (!solved()) assert(0); + } + else { + printf("No: %s", puzzle); + } + } + } + int64_t end = now(); + double sec = (end - start)/1000000.0; + printf("%f sec %f ms each %d\n", sec, 1000*sec/total, total_solved); + + return 0; +} \ No newline at end of file