Line data Source code
1 : /* Copyright (C) 2013 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 : #include <pthread.h>
15 : #include "pari.h"
16 : #include "paripriv.h"
17 : #include "mt.h"
18 : #if defined(_WIN32)
19 : # include "../systems/mingw/mingw.h"
20 : #endif
21 :
22 : #define DEBUGLEVEL DEBUGLEVEL_mt
23 :
24 : struct mt_queue
25 : {
26 : long no;
27 : pari_sp avma;
28 : struct pari_mainstack *mainstack;
29 : GEN input, output;
30 : GEN worker;
31 : long workid;
32 : pthread_cond_t cond;
33 : pthread_mutex_t mut;
34 : pthread_cond_t *pcond;
35 : pthread_mutex_t *pmut;
36 : };
37 :
38 : struct mt_pstate
39 : {
40 : pthread_t *th;
41 : struct pari_thread *pth;
42 : struct mt_queue *mq;
43 : long n, nbint, last;
44 : long pending;
45 : pthread_cond_t pcond;
46 : pthread_mutex_t pmut;
47 : };
48 :
49 : static THREAD long mt_thread_no = -1;
50 : static struct mt_pstate *pari_mt;
51 :
52 : #define LOCK(x) pthread_mutex_lock(x); do
53 : #define UNLOCK(x) while(0); pthread_mutex_unlock(x)
54 :
55 : void
56 209919659 : mt_sigint_block(void)
57 : {
58 209919659 : if (mt_thread_no>=0)
59 30425918 : pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
60 211889943 : }
61 :
62 : void
63 210645126 : mt_sigint_unblock(void)
64 : {
65 210645126 : if (mt_thread_no>=0)
66 31138976 : pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
67 211998110 : }
68 :
69 : void
70 1869 : mt_err_recover(long er)
71 : {
72 1869 : if (mt_thread_no>=0)
73 : {
74 10 : struct mt_pstate *mt = pari_mt;
75 10 : struct mt_queue *mq = mt->mq+mt_thread_no;
76 10 : GEN err = pari_err_last();
77 10 : err = err_get_num(err)==e_STACK ? err_e_STACK: bin_copy(copy_bin(err));
78 10 : pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
79 10 : LOCK(mq->pmut)
80 : {
81 10 : mq->output = err;
82 10 : pthread_cond_signal(mq->pcond);
83 10 : } UNLOCK(mq->pmut);
84 10 : pthread_exit((void*)1);
85 : }
86 1859 : else mtsingle_err_recover(er);
87 1859 : }
88 :
89 : void
90 0 : mt_break_recover(void)
91 : {
92 0 : if (mt_thread_no<0) mtsingle_err_recover(0);
93 0 : }
94 :
95 : void
96 0 : mt_sigint(void)
97 : {
98 0 : if (pari_mt) pthread_cond_broadcast(&pari_mt->pcond);
99 0 : }
100 :
101 : int
102 220885 : mt_is_parallel(void)
103 : {
104 220885 : return !!pari_mt;
105 : }
106 :
107 : int
108 31106657 : mt_is_thread(void)
109 : {
110 31106657 : return mt_thread_no>=0 ? 1: mtsingle_is_thread();
111 : }
112 :
113 : long
114 389739 : mt_nbthreads(void)
115 : {
116 389739 : return pari_mt ? 1: pari_mt_nbthreads;
117 : }
118 :
119 : void
120 373794 : mt_thread_init(void) { mt_thread_no = 0; }
121 :
122 : void
123 13 : mt_export_add(const char *str, GEN val)
124 : {
125 13 : if (pari_mt)
126 0 : pari_err(e_MISC,"export() not allowed during parallel sections");
127 13 : export_add(str, val);
128 13 : }
129 :
130 : void
131 8 : mt_export_del(const char *str)
132 : {
133 8 : if (pari_mt)
134 0 : pari_err(e_MISC,"unexport() not allowed during parallel sections");
135 8 : export_del(str);
136 8 : }
137 :
138 1 : void mt_broadcast(GEN code) {(void) code;}
139 :
140 269 : void pari_mt_init(void)
141 : {
142 269 : pari_mt = NULL;
143 : #ifdef _SC_NPROCESSORS_CONF
144 269 : if (!pari_mt_nbthreads) pari_mt_nbthreads = sysconf(_SC_NPROCESSORS_CONF);
145 : #elif defined(_WIN32)
146 : if (!pari_mt_nbthreads) pari_mt_nbthreads = win32_nbthreads();
147 : #else
148 : pari_mt_nbthreads = 1;
149 : #endif
150 269 : }
151 :
152 269 : void pari_mt_close(void) { }
153 :
154 : static void
155 369837 : mt_queue_cleanup(void *arg)
156 : {
157 : (void) arg;
158 369837 : pari_thread_close();
159 369058 : }
160 :
161 : static void
162 2 : mt_queue_unlock(void *arg)
163 2 : { pthread_mutex_unlock((pthread_mutex_t*) arg); }
164 :
165 : static void*
166 376628 : mt_queue_run(void *arg)
167 : {
168 376628 : GEN args = pari_thread_start((struct pari_thread*) arg);
169 373646 : pari_sp av = avma;
170 373646 : struct mt_queue *mq = (struct mt_queue *) args;
171 373646 : mt_thread_no = mq->no;
172 373646 : pthread_cleanup_push(mt_queue_cleanup,NULL);
173 374023 : LOCK(mq->pmut)
174 : {
175 376633 : mq->mainstack = pari_mainstack;
176 376633 : mq->avma = av;
177 376633 : pthread_cond_signal(mq->pcond);
178 376633 : } UNLOCK(mq->pmut);
179 : for(;;)
180 510924 : {
181 : GEN work, done;
182 887517 : LOCK(&mq->mut)
183 : {
184 887486 : pthread_cleanup_push(mt_queue_unlock, &mq->mut);
185 1748028 : while(!mq->input)
186 861830 : pthread_cond_wait(&mq->cond, &mq->mut);
187 886198 : pthread_cleanup_pop(0);
188 885806 : } UNLOCK(&mq->mut);
189 886435 : pari_mainstack = mq->mainstack;
190 886435 : set_avma(mq->avma);
191 883942 : work = mq->input;
192 883942 : if (typ(work)==t_ERROR && err_get_num(work)==e_STOP) break;
193 509297 : pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
194 510868 : done = closure_callgenvec(mq->worker,work);
195 509337 : pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
196 510807 : LOCK(mq->pmut)
197 : {
198 510954 : mq->mainstack = pari_mainstack;
199 510954 : mq->avma = av;
200 510954 : mq->input = NULL;
201 510954 : mq->output = done;
202 510954 : pthread_cond_signal(mq->pcond);
203 510954 : } UNLOCK(mq->pmut);
204 : }
205 371873 : pthread_cleanup_pop(1);
206 : #ifdef __GNUC__
207 : return NULL; /* LCOV_EXCL_LINE */
208 : #endif
209 : }
210 :
211 : static long
212 724394 : mt_queue_check(struct mt_pstate *mt)
213 : {
214 : long i;
215 5740778 : for(i=0; i<mt->n; i++)
216 : {
217 5527341 : struct mt_queue *mq = mt->mq+i;
218 5527341 : if (mq->output) return i;
219 : }
220 213437 : return -1;
221 : }
222 :
223 : static GEN
224 815014 : mtpthread_queue_get(struct mt_state *junk, long *workid, long *pending)
225 : {
226 815014 : struct mt_pstate *mt = pari_mt;
227 : struct mt_queue *mq;
228 815014 : GEN done = NULL;
229 : long last;
230 : (void) junk;
231 815014 : if (mt->nbint<mt->n)
232 : {
233 304055 : mt->last = mt->nbint;
234 304055 : *pending = mt->pending;
235 304055 : return NULL;
236 : }
237 510959 : BLOCK_SIGINT_START
238 510959 : LOCK(&mt->pmut)
239 : {
240 724394 : while ((last = mt_queue_check(mt)) < 0)
241 : {
242 213437 : pthread_cond_wait(&mt->pcond, &mt->pmut);
243 213437 : if (PARI_SIGINT_pending)
244 : {
245 2 : int sig = PARI_SIGINT_pending;
246 2 : PARI_SIGINT_pending = 0;
247 2 : pthread_mutex_unlock(&mt->pmut);
248 2 : PARI_SIGINT_block = 0;
249 2 : raise(sig);
250 0 : PARI_SIGINT_block = 1;
251 0 : pthread_mutex_lock(&mt->pmut);
252 : }
253 : }
254 510957 : } UNLOCK(&mt->pmut);
255 510957 : BLOCK_SIGINT_END
256 510957 : mq = mt->mq+last;
257 510957 : done = gcopy(mq->output);
258 510957 : mq->output = NULL;
259 510957 : if (workid) *workid = mq->workid;
260 510957 : if (typ(done) == t_ERROR)
261 : {
262 5 : if (err_get_num(done)==e_STACK)
263 0 : pari_err(e_STACKTHREAD);
264 : else
265 5 : pari_err(0,done);
266 : }
267 510952 : mt->last = last;
268 510952 : mt->pending--;
269 510952 : *pending = mt->pending;
270 510952 : return done;
271 : }
272 :
273 : static void
274 815014 : mtpthread_queue_submit(struct mt_state *junk, long workid, GEN work)
275 : {
276 815014 : struct mt_pstate *mt = pari_mt;
277 815014 : struct mt_queue *mq = mt->mq+mt->last;
278 : (void) junk;
279 815014 : if (!work) { mt->nbint=mt->n; return; }
280 511013 : BLOCK_SIGINT_START
281 511013 : if (mt->nbint<mt->n)
282 : {
283 369107 : mt->nbint++;
284 369107 : LOCK(mq->pmut)
285 : {
286 446670 : while(!mq->avma)
287 77563 : pthread_cond_wait(mq->pcond, mq->pmut);
288 369107 : } UNLOCK(mq->pmut);
289 : }
290 511013 : LOCK(&mq->mut)
291 : {
292 511013 : pari_sp av = avma;
293 511013 : struct pari_mainstack *st = pari_mainstack;
294 511013 : mq->output = NULL;
295 511013 : mq->workid = workid;
296 511013 : pari_mainstack = mq->mainstack;
297 511013 : set_avma(mq->avma);
298 511013 : mq->input = gcopy(work);
299 511013 : mq->avma = avma;
300 511013 : mq->mainstack = pari_mainstack;
301 511013 : pari_mainstack = st;
302 511013 : set_avma(av);
303 511013 : pthread_cond_signal(&mq->cond);
304 511013 : } UNLOCK(&mq->mut);
305 511013 : mt->pending++;
306 511013 : BLOCK_SIGINT_END
307 : }
308 :
309 : static void
310 65841 : mtpthread_queue_cleanup(struct mt_pstate *mt)
311 : {
312 : long i;
313 65841 : if (DEBUGLEVEL) pari_warn(warner,"stopping %ld threads", mt->n);
314 65841 : BLOCK_SIGINT_START
315 65841 : pari_mt = NULL;
316 442474 : for (i=0;i<mt->n;i++)
317 : {
318 376633 : struct mt_queue *mq = mt->mq+i;
319 376633 : pthread_cond_destroy(&mq->cond);
320 376633 : pthread_mutex_destroy(&mq->mut);
321 376633 : pari_thread_free(&mt->pth[i]);
322 : }
323 65841 : pthread_cond_destroy(&mt->pcond);
324 65841 : pthread_mutex_destroy(&mt->pmut);
325 65841 : BLOCK_SIGINT_END
326 65841 : pari_free(mt->mq);
327 65841 : pari_free(mt->pth);
328 65841 : pari_free(mt->th);
329 65841 : pari_free(mt);
330 65841 : }
331 :
332 : static void
333 65834 : mtpthread_queue_end(void)
334 : {
335 65834 : const long err_e_STOP[] = { evaltyp(t_ERROR) | _evallg(2), e_STOP};
336 65834 : struct mt_pstate *mt = pari_mt;
337 : long i;
338 65834 : BLOCK_SIGINT_START
339 442406 : for (i=0; i<mt->n; i++)
340 : {
341 376572 : struct mt_queue *mq = mt->mq+i;
342 376572 : LOCK(&mq->mut)
343 : {
344 376572 : mq->output = NULL;
345 376572 : mq->workid = 0;
346 376572 : mq->input = (GEN) err_e_STOP;
347 376572 : pthread_cond_signal(&mq->cond);
348 376572 : } UNLOCK(&mq->mut);
349 : }
350 442406 : for (i=0; i<mt->n; i++)
351 376572 : pthread_join(mt->th[i],NULL);
352 65834 : mtpthread_queue_cleanup(mt);
353 65834 : BLOCK_SIGINT_END
354 65834 : }
355 :
356 : void
357 7 : mt_queue_reset(void)
358 : {
359 7 : struct mt_pstate *mt = pari_mt;
360 : long i;
361 7 : BLOCK_SIGINT_START
362 68 : for (i=0; i<mt->n; i++)
363 61 : pthread_cancel(mt->th[i]);
364 68 : for (i=0; i<mt->n; i++)
365 61 : pthread_join(mt->th[i],NULL);
366 7 : mtpthread_queue_cleanup(mt);
367 7 : BLOCK_SIGINT_END
368 7 : }
369 :
370 : static long
371 65841 : closure_has_clone(GEN fun)
372 : {
373 65841 : if (isclone(fun)) return 1;
374 65835 : if (lg(fun) >= 8)
375 : {
376 65329 : GEN f = closure_get_frame(fun);
377 65329 : long i, l = lg(f);
378 241886 : for (i = 1; i < l; i++)
379 178217 : if (isclone(gel(f,i))) return 1;
380 : }
381 64175 : return 0;
382 : }
383 :
384 : void
385 152722 : mt_queue_start_lim(struct pari_mt *pt, GEN worker, long lim)
386 : {
387 152722 : if (lim==0) lim = pari_mt_nbthreads;
388 152260 : else lim = minss(pari_mt_nbthreads, lim);
389 152720 : if (mt_thread_no >= 0)
390 50410 : mtsequential_queue_start(pt, worker);
391 102310 : else if (pari_mt || lim <= 1)
392 36469 : mtsingle_queue_start(pt, worker);
393 : else
394 : {
395 : struct mt_pstate *mt =
396 65841 : (struct mt_pstate*) pari_malloc(sizeof(struct mt_pstate));
397 65841 : long mtparisize = GP_DATA->threadsize? GP_DATA->threadsize: pari_mainstack->rsize;
398 65841 : long mtparisizemax = GP_DATA->threadsizemax;
399 : long i;
400 65841 : if (closure_has_clone(worker))
401 1666 : worker = gcopy(worker); /* to avoid clone_lock race */
402 65841 : mt->mq = (struct mt_queue *) pari_malloc(sizeof(*mt->mq)*lim);
403 65841 : mt->th = (pthread_t *) pari_malloc(sizeof(*mt->th)*lim);
404 65841 : mt->pth = (struct pari_thread *) pari_malloc(sizeof(*mt->pth)*lim);
405 65841 : mt->pending = 0;
406 65841 : mt->n = lim;
407 65841 : mt->nbint = 0;
408 65841 : mt->last = 0;
409 65841 : pthread_cond_init(&mt->pcond,NULL);
410 65841 : pthread_mutex_init(&mt->pmut,NULL);
411 442474 : for (i=0;i<lim;i++)
412 : {
413 376633 : struct mt_queue *mq = mt->mq+i;
414 376633 : mq->no = i;
415 376633 : mq->avma = 0;
416 376633 : mq->mainstack = NULL;
417 376633 : mq->worker = worker;
418 376633 : mq->input = NULL;
419 376633 : mq->output = NULL;
420 376633 : mq->pcond = &mt->pcond;
421 376633 : mq->pmut = &mt->pmut;
422 376633 : pthread_cond_init(&mq->cond,NULL);
423 376633 : pthread_mutex_init(&mq->mut,NULL);
424 376633 : if (mtparisizemax)
425 0 : pari_thread_valloc(&mt->pth[i],mtparisize,mtparisizemax,(GEN)mq);
426 : else
427 376633 : pari_thread_alloc(&mt->pth[i],mtparisize,(GEN)mq);
428 : }
429 65841 : if (DEBUGLEVEL) pari_warn(warner,"starting %ld threads", lim);
430 65841 : BLOCK_SIGINT_START
431 : {
432 : sigset_t set, oldset;
433 65841 : sigfillset(&set);
434 65841 : pthread_sigmask(SIG_SETMASK, &set, &oldset);
435 442474 : for (i = 0; i < lim; i++)
436 376633 : pthread_create(&mt->th[i], NULL, &mt_queue_run, (void*)&mt->pth[i]);
437 65841 : pthread_sigmask(SIG_SETMASK, &oldset, NULL);
438 65841 : pari_mt = mt;
439 : }
440 65841 : BLOCK_SIGINT_END
441 65841 : pt->get=&mtpthread_queue_get;
442 65841 : pt->submit=&mtpthread_queue_submit;
443 65841 : pt->end=&mtpthread_queue_end;
444 : }
445 152719 : }
|