sections-1.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Test that all sections are touched. */
  2. /* { dg-require-effective-target sync_int_long } */
  3. #include <omp.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "libgomp_g.h"
  7. #define N 100
  8. static int data[N];
  9. static int NTHR;
  10. static void clean_data (void)
  11. {
  12. memset (data, -1, sizeof (data));
  13. }
  14. static void test_data (void)
  15. {
  16. int i;
  17. for (i = 0; i < N; ++i)
  18. assert (data[i] != -1);
  19. }
  20. static void set_data (unsigned i, int val)
  21. {
  22. int old;
  23. assert (i >= 1 && i <= N);
  24. old = __sync_lock_test_and_set (data+i-1, val);
  25. assert (old == -1);
  26. }
  27. static void f_1 (void *dummy)
  28. {
  29. int iam = omp_get_thread_num ();
  30. unsigned long s;
  31. for (s = GOMP_sections_start (N); s ; s = GOMP_sections_next ())
  32. set_data (s, iam);
  33. GOMP_sections_end ();
  34. }
  35. static void test_1 (void)
  36. {
  37. clean_data ();
  38. GOMP_parallel_start (f_1, NULL, NTHR);
  39. f_1 (NULL);
  40. GOMP_parallel_end ();
  41. test_data ();
  42. }
  43. static void f_2 (void *dummy)
  44. {
  45. int iam = omp_get_thread_num ();
  46. unsigned s;
  47. while ((s = GOMP_sections_next ()))
  48. set_data (s, iam);
  49. GOMP_sections_end_nowait ();
  50. }
  51. static void test_2 (void)
  52. {
  53. clean_data ();
  54. GOMP_parallel_sections_start (f_2, NULL, NTHR, N);
  55. f_2 (NULL);
  56. GOMP_parallel_end ();
  57. test_data ();
  58. }
  59. int main()
  60. {
  61. omp_set_dynamic (0);
  62. NTHR = 4;
  63. test_1 ();
  64. test_2 ();
  65. return 0;
  66. }