/*
* Copyright (C) 2022 Camden Dixie O'Brien
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* .
*/
#include "ds.h"
#include "solve.h"
#include "sud.h"
#include
#include
#include
#include
#include
#include
#include
#define IFNAME "puzzles"
#define OFNAME "results"
#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, o;
const char *libuf = ibuf + start;
char *lobuf = obuf + start;
struct sudoku sud;
for (o = 0; o < CELLPT; o += NCELLS) {
load(&sud, libuf + o);
solve(&sud);
save(&sud, lobuf + o);
}
return NULL;
}
int main(void)
{
int fd;
pthread_t pool[NTHREADS];
uintptr_t i;
fd = open(IFNAME, O_RDONLY);
if (fd == -1) {
fputs("Failed to open puzzles file\n", stderr);
return EXIT_FAILURE;
}
ibuf = mmap(NULL, FSIZE, PROT_READ, MAP_PRIVATE, fd, 0);
if (ibuf == MAP_FAILED) {
fputs("Failed to mmap() puzzles file\n", stderr);
return EXIT_FAILURE;
}
close(fd);
fd = open(OFNAME, O_RDWR);
if (fd == -1) {
fputs("Failed to open results file\n", stderr);
return EXIT_FAILURE;
}
obuf = mmap(NULL, FSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (obuf == MAP_FAILED) {
fputs("Failed to mmap() results file\n", stderr);
return EXIT_FAILURE;
}
close(fd);
for (i = 0; i < NTHREADS; ++i) {
if (pthread_create(&pool[i], NULL, threadproc, (void *)i) != 0)
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(obuf, FSIZE);
return EXIT_SUCCESS;
}