cannam@127
|
1 /*
|
cannam@127
|
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
|
cannam@127
|
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
|
cannam@127
|
4 *
|
cannam@127
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@127
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@127
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@127
|
8 * (at your option) any later version.
|
cannam@127
|
9 *
|
cannam@127
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@127
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@127
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@127
|
13 * GNU General Public License for more details.
|
cannam@127
|
14 *
|
cannam@127
|
15 * You should have received a copy of the GNU General Public License
|
cannam@127
|
16 * along with this program; if not, write to the Free Software
|
cannam@127
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@127
|
18 *
|
cannam@127
|
19 */
|
cannam@127
|
20
|
cannam@127
|
21 /* openmp.c: thread spawning via OpenMP */
|
cannam@127
|
22
|
cannam@127
|
23 #include "threads.h"
|
cannam@127
|
24
|
cannam@127
|
25 #if !defined(_OPENMP)
|
cannam@127
|
26 #error OpenMP enabled but not using an OpenMP compiler
|
cannam@127
|
27 #endif
|
cannam@127
|
28
|
cannam@127
|
29 int X(ithreads_init)(void)
|
cannam@127
|
30 {
|
cannam@127
|
31 return 0; /* no error */
|
cannam@127
|
32 }
|
cannam@127
|
33
|
cannam@127
|
34 /* Distribute a loop from 0 to loopmax-1 over nthreads threads.
|
cannam@127
|
35 proc(d) is called to execute a block of iterations from d->min
|
cannam@127
|
36 to d->max-1. d->thr_num indicate the number of the thread
|
cannam@127
|
37 that is executing proc (from 0 to nthreads-1), and d->data is
|
cannam@127
|
38 the same as the data parameter passed to X(spawn_loop).
|
cannam@127
|
39
|
cannam@127
|
40 This function returns only after all the threads have completed. */
|
cannam@127
|
41 void X(spawn_loop)(int loopmax, int nthr, spawn_function proc, void *data)
|
cannam@127
|
42 {
|
cannam@127
|
43 int block_size;
|
cannam@127
|
44 spawn_data d;
|
cannam@127
|
45 int i;
|
cannam@127
|
46
|
cannam@127
|
47 A(loopmax >= 0);
|
cannam@127
|
48 A(nthr > 0);
|
cannam@127
|
49 A(proc);
|
cannam@127
|
50
|
cannam@127
|
51 if (!loopmax) return;
|
cannam@127
|
52
|
cannam@127
|
53 /* Choose the block size and number of threads in order to (1)
|
cannam@127
|
54 minimize the critical path and (2) use the fewest threads that
|
cannam@127
|
55 achieve the same critical path (to minimize overhead).
|
cannam@127
|
56 e.g. if loopmax is 5 and nthr is 4, we should use only 3
|
cannam@127
|
57 threads with block sizes of 2, 2, and 1. */
|
cannam@127
|
58 block_size = (loopmax + nthr - 1) / nthr;
|
cannam@127
|
59 nthr = (loopmax + block_size - 1) / block_size;
|
cannam@127
|
60
|
cannam@127
|
61 THREAD_ON; /* prevent debugging mode from failing under threads */
|
cannam@127
|
62 #pragma omp parallel for private(d)
|
cannam@127
|
63 for (i = 0; i < nthr; ++i) {
|
cannam@127
|
64 d.max = (d.min = i * block_size) + block_size;
|
cannam@127
|
65 if (d.max > loopmax)
|
cannam@127
|
66 d.max = loopmax;
|
cannam@127
|
67 d.thr_num = i;
|
cannam@127
|
68 d.data = data;
|
cannam@127
|
69 proc(&d);
|
cannam@127
|
70 }
|
cannam@127
|
71 THREAD_OFF; /* prevent debugging mode from failing under threads */
|
cannam@127
|
72 }
|
cannam@127
|
73
|
cannam@127
|
74 void X(threads_cleanup)(void)
|
cannam@127
|
75 {
|
cannam@127
|
76 }
|
cannam@127
|
77
|
cannam@127
|
78 /* FIXME [Matteo Frigo 2015-05-25] What does "thread-safe"
|
cannam@127
|
79 mean for openmp? */
|
cannam@127
|
80 void X(threads_register_planner_hooks)(void)
|
cannam@127
|
81 {
|
cannam@127
|
82 }
|