loop-1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Test that all loop iterations are touched. This doesn't verify
  2. scheduling order, merely coverage. */
  3. /* { dg-require-effective-target sync_int_long } */
  4. #include <omp.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include "libgomp_g.h"
  8. #define N 10000
  9. static int S, E, INCR, CHUNK, NTHR;
  10. static int data[N];
  11. static void clean_data (void)
  12. {
  13. memset (data, -1, sizeof (data));
  14. }
  15. static void test_data (void)
  16. {
  17. int i, j;
  18. for (i = 0; i < S; ++i)
  19. assert (data[i] == -1);
  20. for (j = 0; i < E; ++i, j = (j + 1) % INCR)
  21. if (j == 0)
  22. assert (data[i] != -1);
  23. else
  24. assert (data[i] == -1);
  25. for (; i < N; ++i)
  26. assert (data[i] == -1);
  27. }
  28. static void set_data (long i, int val)
  29. {
  30. int old;
  31. assert (i >= 0 && i < N);
  32. old = __sync_lock_test_and_set (data+i, val);
  33. assert (old == -1);
  34. }
  35. #define TMPL_1(sched) \
  36. static void f_##sched##_1 (void *dummy) \
  37. { \
  38. int iam = omp_get_thread_num (); \
  39. long s0, e0, i; \
  40. if (GOMP_loop_##sched##_start (S, E, INCR, CHUNK, &s0, &e0)) \
  41. do \
  42. { \
  43. for (i = s0; i < e0; i += INCR) \
  44. set_data (i, iam); \
  45. } \
  46. while (GOMP_loop_##sched##_next (&s0, &e0)); \
  47. GOMP_loop_end (); \
  48. } \
  49. static void t_##sched##_1 (void) \
  50. { \
  51. clean_data (); \
  52. GOMP_parallel_start (f_##sched##_1, NULL, NTHR); \
  53. f_##sched##_1 (NULL); \
  54. GOMP_parallel_end (); \
  55. test_data (); \
  56. }
  57. TMPL_1(static)
  58. TMPL_1(dynamic)
  59. TMPL_1(guided)
  60. #define TMPL_2(sched) \
  61. static void f_##sched##_2 (void *dummy) \
  62. { \
  63. int iam = omp_get_thread_num (); \
  64. long s0, e0, i; \
  65. while (GOMP_loop_##sched##_next (&s0, &e0)) \
  66. { \
  67. for (i = s0; i < e0; i += INCR) \
  68. set_data (i, iam); \
  69. } \
  70. GOMP_loop_end_nowait (); \
  71. } \
  72. static void t_##sched##_2 (void) \
  73. { \
  74. clean_data (); \
  75. GOMP_parallel_loop_##sched##_start \
  76. (f_##sched##_2, NULL, NTHR, S, E, INCR, CHUNK); \
  77. f_##sched##_2 (NULL); \
  78. GOMP_parallel_end (); \
  79. test_data (); \
  80. }
  81. TMPL_2(static)
  82. TMPL_2(dynamic)
  83. TMPL_2(guided)
  84. static void test (void)
  85. {
  86. t_static_1 ();
  87. t_dynamic_1 ();
  88. t_guided_1 ();
  89. t_static_2 ();
  90. t_dynamic_2 ();
  91. t_guided_2 ();
  92. }
  93. int main()
  94. {
  95. omp_set_dynamic (0);
  96. NTHR = 4;
  97. S = 0, E = N, INCR = 1, CHUNK = 4;
  98. test ();
  99. S = 0, E = N, INCR = 2, CHUNK = 4;
  100. test ();
  101. S = 1, E = N-1, INCR = 1, CHUNK = 5;
  102. test ();
  103. S = 1, E = N-1, INCR = 2, CHUNK = 5;
  104. test ();
  105. S = 2, E = 4, INCR = 1, CHUNK = 1;
  106. test ();
  107. S = 0, E = N, INCR = 1, CHUNK = 0;
  108. t_static_1 ();
  109. t_static_2 ();
  110. S = 1, E = N-1, INCR = 1, CHUNK = 0;
  111. t_static_1 ();
  112. t_static_2 ();
  113. return 0;
  114. }