Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lab1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions Lab1/src/Sudoku/Makefile
Original file line number Diff line number Diff line change
@@ -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 $@ $^
7 changes: 7 additions & 0 deletions Lab1/src/Sudoku/Makefile0
Original file line number Diff line number Diff line change
@@ -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 $@ $^
1 change: 1 addition & 0 deletions Lab1/src/Sudoku/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ int main(int argc, char* argv[])
return 0;
}

// This is a test
Binary file added Lab1/src/Sudoku/sudoku
Binary file not shown.
Binary file added Lab1/src/Sudoku/sudoku_solve
Binary file not shown.
52 changes: 52 additions & 0 deletions Lab1/src/Sudoku/sudoku_solve.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#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;
}