omp-par-scope.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* This testcase is part of GDB, the GNU debugger.
  2. Copyright 2017-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <stdio.h>
  14. #include <omp.h>
  15. omp_lock_t lock;
  16. omp_lock_t lock2;
  17. /* Enforce execution order between two threads using a lock. */
  18. static void
  19. omp_set_lock_in_order (int num, omp_lock_t *lock)
  20. {
  21. /* Ensure that thread num 0 first sets the lock. */
  22. if (num == 0)
  23. omp_set_lock (lock);
  24. #pragma omp barrier
  25. /* Block thread num 1 until it can set the lock. */
  26. if (num == 1)
  27. omp_set_lock (lock);
  28. /* This bit here is guaranteed to be executed first by thread num 0, and
  29. once thread num 0 unsets the lock, to be executed by thread num 1. */
  30. ;
  31. }
  32. /* Testcase for checking access to variables in a single / outer scope.
  33. Make sure that variables not referred to in the parallel section are
  34. accessible from the debugger. */
  35. void
  36. single_scope (void)
  37. {
  38. static int s1 = -41, s2 = -42, s3 = -43;
  39. int i1 = 11, i2 = 12, i3 = 13;
  40. #pragma omp parallel num_threads (2) shared (s1, i1) private (s2, i2)
  41. {
  42. int thread_num = omp_get_thread_num ();
  43. omp_set_lock_in_order (thread_num, &lock);
  44. s2 = 100 * (thread_num + 1) + 2;
  45. i2 = s2 + 10;
  46. #pragma omp critical
  47. printf ("single_scope: thread_num=%d, s1=%d, i1=%d, s2=%d, i2=%d\n",
  48. thread_num, s1, i1, s2, i2);
  49. omp_unset_lock (&lock);
  50. }
  51. printf ("single_scope: s1=%d, s2=%d, s3=%d, i1=%d, i2=%d, i3=%d\n",
  52. s1, s2, s3, i1, i2, i3);
  53. }
  54. static int file_scope_var = 9876;
  55. /* Testcase for checking access to variables from parallel region
  56. nested within more than one lexical scope. Of particular interest
  57. are variables which are not referenced in the parallel section. */
  58. void
  59. multi_scope (void)
  60. {
  61. int i01 = 1, i02 = 2;
  62. {
  63. int i11 = 11, i12 = 12;
  64. {
  65. int i21 = -21, i22 = 22;
  66. #pragma omp parallel num_threads (2) \
  67. firstprivate (i01) \
  68. shared (i11) \
  69. private (i21)
  70. {
  71. int thread_num = omp_get_thread_num ();
  72. omp_set_lock_in_order (thread_num, &lock);
  73. i21 = 100 * (thread_num + 1) + 21;
  74. #pragma omp critical
  75. printf ("multi_scope: thread_num=%d, i01=%d, i11=%d, i21=%d\n",
  76. thread_num, i01, i11, i21);
  77. omp_unset_lock (&lock);
  78. }
  79. printf ("multi_scope: i01=%d, i02=%d, i11=%d, "
  80. "i12=%d, i21=%d, i22=%d\n",
  81. i01, i02, i11, i12, i21, i22);
  82. }
  83. }
  84. }
  85. /* Nested functions in C is a GNU extension. Some non-GNU compilers
  86. define __GNUC__, but they don't support nested functions. So,
  87. unfortunately, we can't use that for our test. */
  88. #if HAVE_NESTED_FUNCTION_SUPPORT
  89. /* Testcase for checking access of variables from within parallel
  90. region in a lexically nested function. */
  91. void
  92. nested_func (void)
  93. {
  94. static int s1 = -42;
  95. int i = 1, j = 2, k = 3;
  96. void
  97. foo (int p, int q, int r)
  98. {
  99. int x = 4;
  100. {
  101. int y = 5, z = 6;
  102. #pragma omp parallel num_threads (2) shared (i, p, x) private (j, q, y)
  103. {
  104. int tn = omp_get_thread_num ();
  105. omp_set_lock_in_order (tn, &lock);
  106. j = 1000 * (tn + 1);
  107. q = j + 1;
  108. y = q + 1;
  109. #pragma omp critical
  110. printf ("nested_func: tn=%d: i=%d, p=%d, x=%d, j=%d, q=%d, y=%d\n",
  111. tn, i, p, x, j, q, y);
  112. omp_unset_lock (&lock);
  113. }
  114. }
  115. }
  116. foo (10, 11, 12);
  117. i = 101; j = 102; k = 103;
  118. foo (20, 21, 22);
  119. }
  120. #endif
  121. /* Testcase for checking access to variables from within a nested parallel
  122. region. */
  123. void
  124. nested_parallel (void)
  125. {
  126. int i = 1, j = 2;
  127. int l = -1;
  128. omp_set_nested (1);
  129. omp_set_dynamic (0);
  130. #pragma omp parallel num_threads (2) private (l)
  131. {
  132. int num = omp_get_thread_num ();
  133. omp_set_lock_in_order (num, &lock);
  134. int nthr = omp_get_num_threads ();
  135. int off = num * nthr;
  136. int k = off + 101;
  137. l = off + 102;
  138. #pragma omp parallel num_threads (2) shared (num)
  139. {
  140. int inner_num = omp_get_thread_num ();
  141. omp_set_lock_in_order (inner_num, &lock2);
  142. #pragma omp critical
  143. printf ("nested_parallel (inner threads): outer thread num = %d, thread num = %d\n", num, inner_num);
  144. omp_unset_lock (&lock2);
  145. }
  146. #pragma omp critical
  147. printf ("nested_parallel (outer threads) %d: k = %d, l = %d\n", num, k, l);
  148. omp_unset_lock (&lock);
  149. }
  150. }
  151. int
  152. main (int argc, char **argv)
  153. {
  154. omp_init_lock (&lock);
  155. omp_init_lock (&lock2);
  156. single_scope ();
  157. multi_scope ();
  158. #if HAVE_NESTED_FUNCTION_SUPPORT
  159. nested_func ();
  160. #endif
  161. nested_parallel ();
  162. omp_destroy_lock (&lock);
  163. omp_destroy_lock (&lock2);
  164. return 0;
  165. }