-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel_thread.c
426 lines (359 loc) · 13.2 KB
/
kernel_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* This file is part of the MAYLIB libray.
Copyright 2014 Patrick Pelissier
This Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This Library 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with th Library; see the file COPYING.LESSER.txt.
If not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA. */
#if defined (MAY_WANT_THREAD)
#include <pthread.h>
#include "may-impl.h"
/* Define shared data between thread, needed to handle them */
may_mt_t may_mt_g;
/* Reset the globals of a thread.
Needed to have a clean state for a thread which is started */
static void reset_may_global(void)
{
memset (&may_g, 0, sizeof (may_g));
may_heap_init(&may_g.Heap, may_mt_g.stack_size, 0, 0);
/* FIXME: How to design this properly? */
may_g.kara.threshold = 10;
may_g.kara.tmpnum = MAY_DUMMY;
}
/* Code of the main function of each working thread */
static void *may_mt_thread_main (void *arg)
{
may_mt_comm_t *mc = arg;
pthread_mutex_lock(&mc->mutex);
/* Save reference of personnal may_g variable */
mc->may_g_ref = &may_g;
/* Reset may_g variable (& allocate a new heap) */
reset_may_global();
while (1) {
/* Wait for data to process... */
/* If not working, sleep until wake up */
if (mc->working <= WAITING_FOR_DATA) {
/* Check for terminate condition */
if (mc->working == TERMINATE_THREAD) {
break;
}
/* Wait for next cycle */
pthread_cond_wait(&mc->cond, &mc->mutex);
continue;
}
pthread_mutex_unlock(&mc->mutex);
/* Disable error handling and string cache ***after*** master thread
set may_g.frame with its own may_g.frame */
may_g.frame.next = NULL;
may_g.frame.error_handler = NULL;
may_g.frame.cache_set_str_i = 0;
may_g.frame.cache_set_str_n = 0;
/* Execute thread */
(*mc->func) (mc->data);
/* Save MAY stack thread within the heap */
struct may_heap_s *heap = may_alloc(sizeof ( struct may_heap_s));
memcpy (heap, &may_g.Heap, sizeof ( struct may_heap_s));
/* Unwork thread */
pthread_mutex_lock(&mc->mutex);
mc->working = WAITING_FOR_DATA;
/* Enter Signal terminaison block */
pthread_mutex_lock (&may_mt_g.master_mutex);
/* Chain the heap of this Thread into the list of the threads
to be free after the compact operation of the master thread */
struct may_heap_s *previous = MAY_MARK_HEAP_TO_FREE(mc->mark);
MAY_MARK_HEAP_TO_FREE(mc->mark) = heap;
heap->next_heap_to_free = previous;
/* Signal terminaison */
*mc->num_spawn_ptr += 1;
pthread_cond_broadcast(&may_mt_g.master_cond);
pthread_mutex_unlock (&may_mt_g.master_mutex);
/* Reset may_g variable (& allocate a new heap) */
reset_may_global();
}
pthread_mutex_unlock(&mc->mutex);
return NULL;
}
/* TODO: Support failure in initialization */
void may_thread_init(int num_thread, size_t size)
{
int rc;
MAY_ASSERT (1 <= num_thread && num_thread <= MAY_MAX_THREAD);
/* Initialize global memory for MT handling */
memset(&may_mt_g, 0, sizeof may_mt_g);
may_mt_g.stack_size = size;
/* Initialize global mutex */
rc = pthread_mutex_init(&may_mt_g.master_mutex, NULL);
if (rc != 0)
abort();
rc = pthread_cond_init(&may_mt_g.master_cond, NULL);
if (rc != 0)
abort();
/* Initialize threads */
may_mt_g.num_thread = num_thread - 1;
for (int i = 0 ; i < num_thread-1; i++) {
rc = pthread_mutex_init(&may_mt_g.comm[i].mutex, NULL);
if (rc != 0)
abort();
rc = pthread_cond_init(&may_mt_g.comm[i].cond, NULL);
if (rc != 0)
abort();
rc = pthread_create (&may_mt_g.comm[i].idx,
NULL, may_mt_thread_main, &may_mt_g.comm[i]);
if (rc != 0)
abort();
}
/* Wait for the threads to block, waiting for data to work on */
for (int i = 0 ; i < num_thread-1; i++) {
/* This is reported as a data race since it fails to see that
this was initialised to NULL using the memset before and
we are waiting for the thread to set it to non-null */
while (may_mt_g.comm[i].may_g_ref == NULL) {
pthread_mutex_lock (&may_mt_g.master_mutex);
pthread_mutex_unlock (&may_mt_g.master_mutex);
}
}
may_mt_g.is_initialized = true;
}
int may_thread_quit(void)
{
int rc;
int previous = may_mt_g.num_thread+1;
UNUSED(rc);
/* If the thread system has been initialized */
if (may_mt_g.is_initialized != false) {
for(int i = 0; i < may_mt_g.num_thread ; i++) {
may_mt_comm_t *mc = &may_mt_g.comm[i];
/* Request terminaison */
rc = pthread_mutex_lock (&mc->mutex);
MAY_ASSERT (rc == 0);
/* No thread should be working when calling this function */
MAY_ASSERT (mc->working == WAITING_FOR_DATA);
mc->working = TERMINATE_THREAD; /* Request terminaison */
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock (&mc->mutex);
/* Join it to terminate it */
rc = pthread_join(may_mt_g.comm[i].idx, NULL);
MAY_ASSERT (rc == 0);
/* mutex_destroy needs mutex to be unlocked */
rc = pthread_mutex_destroy(&may_mt_g.comm[i].mutex);
MAY_ASSERT (rc == 0);
rc = pthread_cond_destroy(&may_mt_g.comm[i].cond);
MAY_ASSERT (rc == 0);
/* Free heap */
may_heap_clear (&(may_mt_g.comm[i].may_g_ref->Heap));
}
/* mutex_destroy needs mutex to be unlocked */
rc = pthread_mutex_destroy(&may_mt_g.master_mutex);
MAY_ASSERT (rc == 0);
rc = pthread_cond_destroy(&may_mt_g.master_cond);
MAY_ASSERT (rc == 0);
may_mt_g.is_initialized = false;
may_mt_g.num_thread = 0;
}
return previous;
}
/* Start a new parallel job.
The parallel job reference is block.
Once the parallel job is finished, garbashed collect the memory
when dealing with the next compact involving mark */
void may_spawn_start(may_spawn_block_t block, may_mark_t mark)
{
block->num_spawn = 0;
block->num_terminated_spawn = 0;
block->mark = &mark[0];
}
/* Launch (or not) a new parallel job to compute func(data) */
void may_spawn (may_spawn_block_t block, void (*func)(void *), void *data)
{
for(int i = 0; i < may_mt_g.num_thread ; i++) {
may_mt_comm_t *mc = &may_mt_g.comm[i];
/* If the thread is not working */
if (mc->working == WAITING_FOR_DATA) {
pthread_mutex_lock (&mc->mutex);
/* If the thread is still not working */
if (MAY_UNLIKELY (mc->working != WAITING_FOR_DATA)) {
pthread_mutex_unlock (&mc->mutex);
continue;
}
/* Setup data for the thread */
mc->working = THREAD_RUNNING;
mc->func = func;
mc->data = data;
mc->num_spawn_ptr = &block->num_terminated_spawn;
mc->mark = block->mark;
block->num_spawn +=1;
/* Setup frame of the thread:
intmaxsize, prec, rnd_mode, etc... */
/* FIXME: How to speed up this ?*/
MAY_ASSERT (mc->may_g_ref != NULL);
memcpy (&mc->may_g_ref->frame, &may_g.frame, sizeof may_g.frame);
/* Signal to thread that some work are available */
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock (&mc->mutex);
return ;
}
}
/* No thread available. Call the function ourself */
(*func) (data);
}
void may_spawn_sync(may_spawn_block_t block)
{
/* If the number of spawns is greated than the number
of terminated spawns, some spawns are still working.
So wait for terminaison */
if (block->num_spawn > block->num_terminated_spawn) {
pthread_mutex_lock (&may_mt_g.master_mutex);
while (1) {
if (block->num_spawn == block->num_terminated_spawn)
break;
pthread_cond_wait(&may_mt_g.master_cond, &may_mt_g.master_mutex);
}
pthread_mutex_unlock (&may_mt_g.master_mutex);
}
}
void may_spawn_for(may_mark_t mark,
may_int_t begin, may_int_t end,
void (*func)(void*), void *data)
{
MAY_SPAWN_BLOCK(block, mark);
/* Compute number of cores */
// FIXME: Preemption? Could it be bad?
int nb_core_available = 1;
for(int i = 0; i < may_mt_g.num_thread ; i++)
nb_core_available += (may_mt_g.comm[i].working == WAITING_FOR_DATA);
MAY_ASSERT (begin < end);
const int nb_core_needed = (end-begin + MAY_SPAWN_FOR_TH-1)
/ (MAY_SPAWN_FOR_TH/2);
MAY_ASSERT (nb_core_needed > 0);
const int nb_core = MIN(nb_core_needed, nb_core_available);
const may_int_t step = (end-begin) / nb_core;
may_int_t var = begin;
for(int i = 0; i < may_mt_g.num_thread && var < end; i++) {
may_mt_comm_t *mc = &may_mt_g.comm[i];
if (mc->working == WAITING_FOR_DATA) {
pthread_mutex_lock (&mc->mutex);
/* If the thread is still not working */
if (MAY_UNLIKELY (mc->working != WAITING_FOR_DATA)) {
pthread_mutex_unlock (&mc->mutex);
continue;
}
/* Setup data for the thread: handle the for from var to var+step */
mc->working = THREAD_RUNNING;
mc->func = func;
mc->data4for.begin = var;
mc->data4for.end = var + step;
mc->data4for.data = data;
mc->data = &mc->data4for.data;
mc->num_spawn_ptr = &block->num_terminated_spawn;
mc->mark = block->mark;
block->num_spawn +=1;
/* Setup frame of the thread:
intmaxsize, prec, rnd_mode, etc... */
/* FIXME: How to speed up this ?*/
memcpy (&mc->may_g_ref->frame, &may_g.frame, sizeof may_g.frame);
/* Signal to thread that some work are available */
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock (&mc->mutex);
/* Next one */
var += step;
}
}
/* Finish the for loop with the remainder */
may_data4for_t data4for;
data4for.begin = var;
data4for.end = end;
data4for.data = data;
(*func)(&data4for);
MAY_SPAWN_SYNC(block);
}
void
may_spawn_for_reduce (may_mark_t mark, may_int_t begin, may_int_t end,
void (*compute_func)(void *),
void (*reduce_func)(int, const void *, void*),
void *data,
size_t size_global_reduced_var,
void *global_reduced_var_ptr)
{
/* Nearly copy / paste of may_spawn_for
except we have to set data->thread_reduced_var
+ the final reduction step */
MAY_SPAWN_BLOCK(block, mark);
/* Compute number of cores */
// FIXME: Preemption? Could it be bad?
int nb_core_available = 1;
for(int i = 0; i < may_mt_g.num_thread ; i++)
nb_core_available += (may_mt_g.comm[i].working == WAITING_FOR_DATA);
MAY_ASSERT (begin < end);
const int nb_core_needed = (end-begin + MAY_SPAWN_FOR_TH-1)
/ (MAY_SPAWN_FOR_TH/2);
MAY_ASSERT (nb_core_needed > 0);
const int nb_core = MIN(nb_core_needed, nb_core_available);
/* Alloc the array of the reduced variable for each thread */
char *thread_reduced_var_tab = may_alloc (nb_core * size_global_reduced_var);
/* Compute the step of a thread */
const may_int_t step = (end-begin) / nb_core;
may_int_t var = begin;
int real_nb_core = 0;
for(int i = 0; i < may_mt_g.num_thread && var < end; i++) {
may_mt_comm_t *mc = &may_mt_g.comm[i];
if (mc->working == WAITING_FOR_DATA) {
pthread_mutex_lock (&mc->mutex);
/* If the thread is still not working */
if (MAY_UNLIKELY (mc->working != WAITING_FOR_DATA)) {
pthread_mutex_unlock (&mc->mutex);
continue;
}
/* Setup data for the thread: handle the for from var to var+step */
mc->working = THREAD_RUNNING;
mc->func = compute_func;
mc->data4for.begin = var;
mc->data4for.end = var + step;
mc->data4for.data = data;
mc->data4for.thread_reduced_var = thread_reduced_var_tab +
(real_nb_core++ * size_global_reduced_var);
mc->data = &mc->data4for.data;
mc->num_spawn_ptr = &block->num_terminated_spawn;
mc->mark = block->mark;
block->num_spawn +=1;
/* Setup frame of the thread:
intmaxsize, prec, rnd_mode, etc... */
/* FIXME: How to speed up this ?*/
memcpy (&mc->may_g_ref->frame, &may_g.frame, sizeof may_g.frame);
/* Signal to thread that some work are available */
pthread_cond_signal(&mc->cond);
pthread_mutex_unlock (&mc->mutex);
/* Next one */
var += step;
}
}
/* Finish the for loop with the remainder */
may_data4for_t data4for;
data4for.begin = var;
data4for.end = end;
data4for.data = data;
data4for.thread_reduced_var = thread_reduced_var_tab +
(real_nb_core++ * size_global_reduced_var);
(*compute_func)(&data4for);
MAY_SPAWN_SYNC(block);
/* Final Reduction of the reduced data of all threads into the global one */
(*reduce_func)(real_nb_core, thread_reduced_var_tab, global_reduced_var_ptr);
}
#if defined(MAY_NEED_ATOMIC_ADD)
unsigned int may_atomic_add(unsigned int *var, unsigned int inc)
{
pthread_mutex_lock (&may_mt_g.master_mutex);
unsigned int ret = *value;
*value += inc;
pthread_mutex_unlock (&may_mt_g.master_mutex);
return ret;
}
#endif
#endif