api_01.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014,2015,2017,2019 CNRS
  4. * Copyright (C) 2014,2016 Inria
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. /*
  22. * Check the OpenMP API getters return proper default results.
  23. */
  24. #if !defined(STARPU_OPENMP)
  25. int main(void)
  26. {
  27. return STARPU_TEST_SKIPPED;
  28. }
  29. #else
  30. __attribute__((constructor))
  31. static void omp_constructor(void)
  32. {
  33. int ret;
  34. /* we clear the whole OMP environment for this test, to check the
  35. * default behaviour of API functions */
  36. unsetenv("OMP_DYNAMIC");
  37. unsetenv("OMP_NESTED");
  38. unsetenv("OMP_SCHEDULE");
  39. unsetenv("OMP_STACKSIZE");
  40. unsetenv("OMP_WAIT_POLICY");
  41. unsetenv("OMP_THREAD_LIMIT");
  42. unsetenv("OMP_MAX_ACTIVE_LEVELS");
  43. unsetenv("OMP_CANCELLATION");
  44. unsetenv("OMP_DEFAULT_DEVICE");
  45. unsetenv("OMP_MAX_TASK_PRIORITY");
  46. unsetenv("OMP_PROC_BIND");
  47. unsetenv("OMP_NUM_THREADS");
  48. unsetenv("OMP_PLACES");
  49. unsetenv("OMP_DISPLAY_ENV");
  50. ret = starpu_omp_init();
  51. if (ret == -EINVAL) exit(STARPU_TEST_SKIPPED);
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_omp_init");
  53. }
  54. __attribute__((destructor))
  55. static void omp_destructor(void)
  56. {
  57. starpu_omp_shutdown();
  58. }
  59. #define check_omp_func(f,_tv) \
  60. { \
  61. const int v = (f()); \
  62. const int tv = (_tv); \
  63. printf(#f ": %d (should be %d)\n", v, tv); \
  64. STARPU_ASSERT(v == tv); \
  65. }
  66. const char * get_sched_name(int sched_value)
  67. {
  68. const char *sched_name = NULL;
  69. switch (sched_value)
  70. {
  71. case starpu_omp_sched_undefined: sched_name = "<undefined>"; break;
  72. case starpu_omp_sched_static: sched_name = "static"; break;
  73. case starpu_omp_sched_dynamic: sched_name = "dynamic"; break;
  74. case starpu_omp_sched_guided: sched_name = "guided"; break;
  75. case starpu_omp_sched_auto: sched_name = "auto"; break;
  76. case starpu_omp_sched_runtime: sched_name = "runtime"; break;
  77. default: _STARPU_ERROR("invalid omp schedule value");
  78. }
  79. return sched_name;
  80. }
  81. int
  82. main (void)
  83. {
  84. const int nb_cpus = starpu_cpu_worker_get_count();
  85. check_omp_func(starpu_omp_get_num_threads, 1);
  86. check_omp_func(starpu_omp_get_thread_num, 0);
  87. /* since OMP_NUM_THREADS is cleared, starpu_omp_get_max_threads() should return nb_cpus */
  88. check_omp_func(starpu_omp_get_max_threads, nb_cpus);
  89. check_omp_func(starpu_omp_get_num_procs, nb_cpus);
  90. check_omp_func(starpu_omp_in_parallel, 0);
  91. check_omp_func(starpu_omp_get_dynamic, 0);
  92. check_omp_func(starpu_omp_get_nested, 0);
  93. check_omp_func(starpu_omp_get_cancellation, 0);
  94. {
  95. const enum starpu_omp_sched_value target_kind = starpu_omp_sched_static;
  96. const int target_modifier = 0;
  97. enum starpu_omp_sched_value kind;
  98. int modifier;
  99. const char *sched_name;
  100. const char *target_sched_name;
  101. starpu_omp_get_schedule(&kind, &modifier);
  102. sched_name = get_sched_name(kind);
  103. target_sched_name = get_sched_name(target_kind);
  104. printf("starpu_omp_get_schedule: %s,%d (should be %s,%d)\n", sched_name, modifier, target_sched_name, target_modifier);
  105. STARPU_ASSERT(kind == target_kind && modifier == target_modifier);
  106. }
  107. check_omp_func(starpu_omp_get_thread_limit, nb_cpus);
  108. check_omp_func(starpu_omp_get_max_active_levels, 1);
  109. check_omp_func(starpu_omp_get_level, 0);
  110. {
  111. const int tv = 0;
  112. const int v = starpu_omp_get_ancestor_thread_num(0);
  113. printf("starpu_omp_get_ancestor_thread_num(0): %d (should be %d)\n", v, tv);
  114. STARPU_ASSERT(v == tv);
  115. }
  116. {
  117. const int tv = 1;
  118. const int v = starpu_omp_get_team_size(0);
  119. printf("starpu_omp_get_team_size(0): %d (should be %d)\n", v, tv);
  120. STARPU_ASSERT(v == tv);
  121. }
  122. check_omp_func(starpu_omp_get_active_level, 0);
  123. check_omp_func(starpu_omp_in_final, 0);
  124. check_omp_func(starpu_omp_get_proc_bind, starpu_omp_proc_bind_false);
  125. check_omp_func(starpu_omp_get_default_device, 0);
  126. /* TODO: support more than one device */
  127. check_omp_func(starpu_omp_get_num_devices, 1);
  128. check_omp_func(starpu_omp_get_num_teams, 1);
  129. check_omp_func(starpu_omp_get_team_num, 0);
  130. check_omp_func(starpu_omp_is_initial_device, 1);
  131. check_omp_func(starpu_omp_get_initial_device, 0);
  132. check_omp_func(starpu_omp_get_max_task_priority, 0);
  133. return 0;
  134. }
  135. #endif