diff options
Diffstat (limited to 'src/solvers/h48')
| -rw-r--r-- | src/solvers/h48/thread.h | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/src/solvers/h48/thread.h b/src/solvers/h48/thread.h index 91e092f..fb5ccbc 100644 --- a/src/solvers/h48/thread.h +++ b/src/solvers/h48/thread.h | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | #define MAX_QUEUE_SIZE 244 | 3 | #define MAX_QUEUE_SIZE 244 |
| 4 | #define BFS_DEPTH 2 | 4 | #define BFS_DEPTH 2 |
| 5 | typedef struct { | 5 | typedef struct { |
| 6 | dfsarg_solveh48_t *tasks[MAX_QUEUE_SIZE]; | 6 | dfsarg_solveh48_t tasks[MAX_QUEUE_SIZE]; |
| 7 | int front; | 7 | int front; |
| 8 | int rear; | 8 | int rear; |
| 9 | int tasks_count; | 9 | int tasks_count; |
| @@ -16,7 +16,7 @@ typedef struct { | |||
| 16 | 16 | ||
| 17 | STATIC void solve_h48_appendsolution_thread(dfsarg_solveh48_t *, task_queue_t *); | 17 | STATIC void solve_h48_appendsolution_thread(dfsarg_solveh48_t *, task_queue_t *); |
| 18 | STATIC void init_queue(task_queue_t *); | 18 | STATIC void init_queue(task_queue_t *); |
| 19 | STATIC void submit_task(task_queue_t *, dfsarg_solveh48_t *); | 19 | STATIC void submit_task(task_queue_t *, dfsarg_solveh48_t); |
| 20 | STATIC void copy_queue(task_queue_t *, task_queue_t *, int, int64_t *); | 20 | STATIC void copy_queue(task_queue_t *, task_queue_t *, int, int64_t *); |
| 21 | STATIC void *start_thread(void *); | 21 | STATIC void *start_thread(void *); |
| 22 | STATIC int64_t solve_h48_bfs(dfsarg_solveh48_t *, task_queue_t *); | 22 | STATIC int64_t solve_h48_bfs(dfsarg_solveh48_t *, task_queue_t *); |
| @@ -65,7 +65,7 @@ init_queue(task_queue_t *queue) | |||
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | STATIC void | 67 | STATIC void |
| 68 | submit_task(task_queue_t *queue, dfsarg_solveh48_t *task) | 68 | submit_task(task_queue_t *queue, dfsarg_solveh48_t task) |
| 69 | { | 69 | { |
| 70 | pthread_mutex_lock(&queue->mutex); | 70 | pthread_mutex_lock(&queue->mutex); |
| 71 | queue->tasks[queue->rear] = task; | 71 | queue->tasks[queue->rear] = task; |
| @@ -81,11 +81,8 @@ copy_queue(task_queue_t *src, task_queue_t *dest, int depth, int64_t *nsols) | |||
| 81 | pthread_mutex_lock(&dest->mutex); | 81 | pthread_mutex_lock(&dest->mutex); |
| 82 | for (int i = src->front; i != src->rear; i = (i + 1) % MAX_QUEUE_SIZE) | 82 | for (int i = src->front; i != src->rear; i = (i + 1) % MAX_QUEUE_SIZE) |
| 83 | { | 83 | { |
| 84 | if (src->tasks[i] != NULL) | ||
| 85 | { | ||
| 86 | dest->tasks[i] = src->tasks[i]; | 84 | dest->tasks[i] = src->tasks[i]; |
| 87 | dest->tasks[i]->depth = depth; | 85 | dest->tasks[i].depth = depth; |
| 88 | } | ||
| 89 | } | 86 | } |
| 90 | dest->front = src->front; | 87 | dest->front = src->front; |
| 91 | dest->rear = src->rear; | 88 | dest->rear = src->rear; |
| @@ -109,13 +106,13 @@ start_thread(void *arg) | |||
| 109 | } | 106 | } |
| 110 | 107 | ||
| 111 | if (queue->tasks_count > 0) { | 108 | if (queue->tasks_count > 0) { |
| 112 | dfsarg_solveh48_t *task = queue->tasks[queue->front]; | 109 | dfsarg_solveh48_t task = queue->tasks[queue->front]; |
| 113 | queue->front = (queue->front + 1) % MAX_QUEUE_SIZE; | 110 | queue->front = (queue->front + 1) % MAX_QUEUE_SIZE; |
| 114 | queue->tasks_count--; | 111 | queue->tasks_count--; |
| 115 | queue->active++; | 112 | queue->active++; |
| 116 | pthread_mutex_unlock(&queue->mutex); | 113 | pthread_mutex_unlock(&queue->mutex); |
| 117 | 114 | ||
| 118 | solve_h48_single(task, queue); | 115 | solve_h48_single(&task, queue); |
| 119 | 116 | ||
| 120 | pthread_mutex_lock(&queue->mutex); | 117 | pthread_mutex_lock(&queue->mutex); |
| 121 | queue->active--; | 118 | queue->active--; |
| @@ -139,6 +136,8 @@ solve_h48_bfs(dfsarg_solveh48_t *arg_zero, task_queue_t *tq) | |||
| 139 | int nodes_at_next_depth = 0; | 136 | int nodes_at_next_depth = 0; |
| 140 | queue[rear++] = *arg_zero; | 137 | queue[rear++] = *arg_zero; |
| 141 | 138 | ||
| 139 | dfsarg_solveh48_t task_pool[MAX_QUEUE_SIZE]; | ||
| 140 | |||
| 142 | while (front < rear){ | 141 | while (front < rear){ |
| 143 | dfsarg_solveh48_t arg = queue[front++]; | 142 | dfsarg_solveh48_t arg = queue[front++]; |
| 144 | nodes_at_current_depth--; | 143 | nodes_at_current_depth--; |
| @@ -165,9 +164,9 @@ solve_h48_bfs(dfsarg_solveh48_t *arg_zero, task_queue_t *tq) | |||
| 165 | nextarg.inverse = premove(arg.inverse, m); | 164 | nextarg.inverse = premove(arg.inverse, m); |
| 166 | 165 | ||
| 167 | if (nextarg.nmoves == BFS_DEPTH){ | 166 | if (nextarg.nmoves == BFS_DEPTH){ |
| 168 | dfsarg_solveh48_t *task = malloc(sizeof(dfsarg_solveh48_t)); | 167 | dfsarg_solveh48_t *task = &task_pool[rear % MAX_QUEUE_SIZE]; |
| 169 | *task = nextarg; | 168 | *task = nextarg; |
| 170 | submit_task(tq, task); | 169 | submit_task(tq, *task); |
| 171 | } else { | 170 | } else { |
| 172 | queue[rear++] = nextarg; | 171 | queue[rear++] = nextarg; |
| 173 | nodes_at_next_depth++; | 172 | nodes_at_next_depth++; |
