Run 8 solving threads in parallel
This commit is contained in:
parent
516c9f5f55
commit
98a99ac340
6
Makefile
6
Makefile
@ -22,12 +22,16 @@ CFLAGS += -D_XOPEN_SOURCE=700
|
|||||||
CFLAGS += -march=native
|
CFLAGS += -march=native
|
||||||
CFLAGS += -static
|
CFLAGS += -static
|
||||||
|
|
||||||
|
LDFLAGS += -lpthread
|
||||||
|
|
||||||
# For profiling
|
# For profiling
|
||||||
# CFLAGS += -pg
|
# CFLAGS += -pg
|
||||||
|
|
||||||
SRC = main.c sud.c solve.c
|
SRC = main.c sud.c solve.c
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
default: sudoku eval
|
||||||
|
|
||||||
sudoku: $(OBJ)
|
sudoku: $(OBJ)
|
||||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@
|
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@
|
||||||
|
|
||||||
@ -55,4 +59,4 @@ run: sudoku eval puzzles solns results
|
|||||||
time ./sudoku
|
time ./sudoku
|
||||||
./eval
|
./eval
|
||||||
|
|
||||||
.PHONY: clean run
|
.PHONY: default clean run
|
||||||
|
42
main.c
42
main.c
@ -21,6 +21,7 @@
|
|||||||
#include "sud.h"
|
#include "sud.h"
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -31,6 +32,29 @@
|
|||||||
#define OFNAME "results"
|
#define OFNAME "results"
|
||||||
#define FSIZE (NPUZZ * NCELLS)
|
#define FSIZE (NPUZZ * NCELLS)
|
||||||
|
|
||||||
|
#define NTHREADS 8U
|
||||||
|
#define PUZZPT (NPUZZ / NTHREADS)
|
||||||
|
#define CELLPT (PUZZPT * NCELLS)
|
||||||
|
|
||||||
|
static char *ibuf;
|
||||||
|
static char *obuf;
|
||||||
|
|
||||||
|
void *threadproc(void *arg)
|
||||||
|
{
|
||||||
|
unsigned start = CELLPT * (uintptr_t)arg;
|
||||||
|
const char *libuf = ibuf + start;
|
||||||
|
char *lobuf = obuf + start;
|
||||||
|
|
||||||
|
struct sudoku sud;
|
||||||
|
for (unsigned o = 0; o < CELLPT; o += NCELLS) {
|
||||||
|
load(&sud, libuf + o);
|
||||||
|
solve(&sud);
|
||||||
|
save(&sud, lobuf + o);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
@ -40,7 +64,7 @@ int main(void)
|
|||||||
fputs("Failed to open puzzles file\n", stderr);
|
fputs("Failed to open puzzles file\n", stderr);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
char *ibuf = mmap(NULL, FSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
|
ibuf = mmap(NULL, FSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
if (ibuf == MAP_FAILED) {
|
if (ibuf == MAP_FAILED) {
|
||||||
fputs("Failed to mmap() puzzles file\n", stderr);
|
fputs("Failed to mmap() puzzles file\n", stderr);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@ -52,18 +76,22 @@ int main(void)
|
|||||||
fputs("Failed to open results file\n", stderr);
|
fputs("Failed to open results file\n", stderr);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
char *obuf = mmap(NULL, FSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
obuf = mmap(NULL, FSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
if (obuf == MAP_FAILED) {
|
if (obuf == MAP_FAILED) {
|
||||||
fputs("Failed to mmap() results file\n", stderr);
|
fputs("Failed to mmap() results file\n", stderr);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
struct sudoku sud;
|
pthread_t pool[NTHREADS];
|
||||||
for (unsigned o = 0; o < FSIZE; o += NCELLS) {
|
uintptr_t i;
|
||||||
load(&sud, ibuf + o);
|
for (i = 0; i < NTHREADS; ++i) {
|
||||||
solve(&sud);
|
if (pthread_create(&pool[i], NULL, threadproc, (void *)i) != 0)
|
||||||
save(&sud, obuf + o);
|
fprintf(stderr, "Failed to create thread #%lu\n", i);
|
||||||
|
}
|
||||||
|
for (i = 0; i < NTHREADS; ++i) {
|
||||||
|
if (pthread_join(pool[i], NULL) != 0)
|
||||||
|
fprintf(stderr, "Failed to join thread #%lu\n", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
munmap(ibuf, FSIZE);
|
munmap(ibuf, FSIZE);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user