1/*
2 * Copyright (c) 2014 ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Universitaetsstrasse 6, CH-8092 Zurich. Attn: Systems Group.
8 */
9#include <bomp_internal.h>
10
11/*
12 * These functions implement the PARALLEL construct
13 *
14 * #pragma omp parallel
15 * {
16 *  body;
17 * }
18 *
19 * is translated into
20 * void subfunction (void *data)
21 * {
22 *  use data;
23 *  body;
24 *  }
25 *  setup data;
26 *  GOMP_parallel_start (subfunction, &data, num_threads);
27 *  subfunction (&data);
28 *  GOMP_parallel_end ();
29 */
30
31void GOMP_parallel_start(void (*fn)(void *),
32                         void *data,
33                         unsigned nthreads)
34{
35    assert(g_bomp_state != NULL);
36
37    /*
38     * TODO:
39     * 1) work out how many threads can be usedfor executing the parallel task
40     * 2) create a new team for solving the task
41     * 3) start the team work
42     */
43
44    /* Identify the number of threads that can be spawned and start the processing */
45    if (!omp_in_parallel()) {
46        g_bomp_state->bomp_threads = omp_get_max_threads();
47        if (nthreads == 0 || (g_bomp_state->behaviour_dynamic
48                                && g_bomp_state->num_threads < nthreads)) {
49
50            nthreads = g_bomp_state->bomp_threads;
51        }
52        g_bomp_state->backend.start_processing(fn, data, nthreads);
53    }
54    g_bomp_state->nested++;
55}
56
57void GOMP_parallel_end(void)
58{
59    /*
60     * TODO:
61     * 1)
62     */
63    assert(g_bomp_state != NULL);
64    if (g_bomp_state->nested == 1) {
65        g_bomp_state->backend.end_processing();
66    }
67    g_bomp_state->nested--;
68}
69
70void GOMP_parallel(void (*fn)(void *),
71                   void *data,
72                   unsigned num_threads,
73                   unsigned int flags)
74{
75    /*
76     * TODO:
77     * 1)  work out how many threads
78     * 2)  allocate and start a new team
79     * 3) call the function
80     * 4) call parallel end
81     */
82
83    GOMP_parallel_start(fn, data, num_threads);
84    fn(data);
85    GOMP_parallel_end();
86}
87
88#if OMP_VERSION >= OMP_VERSION_40
89bool GOMP_cancel(int which,
90                 bool do_cancel)
91{
92    assert(!"NYI");
93    return 0;
94}
95
96bool GOMP_cancellation_point(int which)
97{
98    assert(!"NYI");
99    return 0;
100}
101#endif
102