loop-2.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Validate static scheduling iteration dispatch. We only test with
  2. even thread distributions here; there are multiple valid solutions
  3. for uneven thread distributions. */
  4. /* { dg-require-effective-target sync_int_long } */
  5. #include <omp.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "libgomp_g.h"
  9. #define N 360
  10. static int data[N][2];
  11. static int INCR, NTHR, CHUNK;
  12. static void clean_data (void)
  13. {
  14. memset (data, -1, sizeof (data));
  15. }
  16. static void test_data (void)
  17. {
  18. int n, i, c, thr, iter, chunk;
  19. chunk = CHUNK;
  20. if (chunk == 0)
  21. chunk = N / INCR / NTHR;
  22. thr = iter = c = i = 0;
  23. for (n = 0; n < N; ++n)
  24. {
  25. if (i == 0)
  26. {
  27. assert (data[n][0] == thr);
  28. assert (data[n][1] == iter);
  29. }
  30. else
  31. {
  32. assert (data[n][0] == -1);
  33. assert (data[n][1] == -1);
  34. }
  35. if (++i == INCR)
  36. {
  37. i = 0;
  38. if (++c == chunk)
  39. {
  40. c = 0;
  41. if (++thr == NTHR)
  42. {
  43. thr = 0;
  44. ++iter;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. static void set_data (long i, int thr, int iter)
  51. {
  52. int old;
  53. assert (i >= 0 && i < N);
  54. old = __sync_lock_test_and_set (&data[i][0], thr);
  55. assert (old == -1);
  56. old = __sync_lock_test_and_set (&data[i][1], iter);
  57. assert (old == -1);
  58. }
  59. static void f_static_1 (void *dummy)
  60. {
  61. int iam = omp_get_thread_num ();
  62. long s0, e0, i, count = 0;
  63. if (GOMP_loop_static_start (0, N, INCR, CHUNK, &s0, &e0))
  64. do
  65. {
  66. for (i = s0; i < e0; i += INCR)
  67. set_data (i, iam, count);
  68. ++count;
  69. }
  70. while (GOMP_loop_static_next (&s0, &e0));
  71. GOMP_loop_end ();
  72. }
  73. static void test (void)
  74. {
  75. clean_data ();
  76. GOMP_parallel_start (f_static_1, NULL, NTHR);
  77. f_static_1 (NULL);
  78. GOMP_parallel_end ();
  79. test_data ();
  80. }
  81. int main()
  82. {
  83. omp_set_dynamic (0);
  84. NTHR = 5;
  85. INCR = 1, CHUNK = 0; /* chunk = 360 / 5 = 72 */
  86. test ();
  87. INCR = 4, CHUNK = 0; /* chunk = 360 / 4 / 5 = 18 */
  88. test ();
  89. INCR = 1, CHUNK = 4; /* 1 * 4 * 5 = 20 -> 360 / 20 = 18 iterations. */
  90. test ();
  91. INCR = 3, CHUNK = 4; /* 3 * 4 * 5 = 60 -> 360 / 60 = 6 iterations. */
  92. test ();
  93. return 0;
  94. }