api_01.c 4.5 KB

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