]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgomp/doc/the-libgomp-abi/implementing-for-construct.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / libgomp / doc / the-libgomp-abi / implementing-for-construct.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. _implementing-for-construct:
7
8 Implementing FOR construct
9 **************************
10
11 .. code-block:: c++
12
13 #pragma omp parallel for
14 for (i = lb; i <= ub; i++)
15 body;
16
17 becomes
18
19 .. code-block:: c++
20
21 void subfunction (void *data)
22 {
23 long _s0, _e0;
24 while (GOMP_loop_static_next (&_s0, &_e0))
25 {
26 long _e1 = _e0, i;
27 for (i = _s0; i < _e1; i++)
28 body;
29 }
30 GOMP_loop_end_nowait ();
31 }
32
33 GOMP_parallel_loop_static (subfunction, NULL, 0, lb, ub+1, 1, 0);
34 subfunction (NULL);
35 GOMP_parallel_end ();
36
37 .. code-block:: c++
38
39 #pragma omp for schedule(runtime)
40 for (i = 0; i < n; i++)
41 body;
42
43 becomes
44
45 .. code-block:: c++
46
47 {
48 long i, _s0, _e0;
49 if (GOMP_loop_runtime_start (0, n, 1, &_s0, &_e0))
50 do {
51 long _e1 = _e0;
52 for (i = _s0, i < _e0; i++)
53 body;
54 } while (GOMP_loop_runtime_next (&_s0, _&e0));
55 GOMP_loop_end ();
56 }
57
58 Note that while it looks like there is trickiness to propagating
59 a non-constant STEP, there isn't really. We're explicitly allowed
60 to evaluate it as many times as we want, and any variables involved
61 should automatically be handled as PRIVATE or SHARED like any other
62 variables. So the expression should remain evaluable in the
63 subfunction. We can also pull it into a local variable if we like,
64 but since its supposed to remain unchanged, we can also not if we like.
65
66 If we have SCHEDULE(STATIC), and no ORDERED, then we ought to be
67 able to get away with no work-sharing context at all, since we can
68 simply perform the arithmetic directly in each thread to divide up
69 the iterations. Which would mean that we wouldn't need to call any
70 of these routines.
71
72 There are separate routines for handling loops with an ORDERED
73 clause. Bookkeeping for that is non-trivial...