api_01.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 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. #if !defined(STARPU_OPENMP)
  21. int main(int argc, char **argv)
  22. {
  23. return STARPU_TEST_SKIPPED;
  24. }
  25. #else
  26. __attribute__((constructor))
  27. static void omp_constructor(void)
  28. {
  29. int ret;
  30. /* we clear the whole OMP environment for this test, to check the
  31. * default behaviour of API functions */
  32. unsetenv("OMP_DYNAMIC");
  33. unsetenv("OMP_NESTED");
  34. unsetenv("OMP_SCHEDULE");
  35. unsetenv("OMP_STACKSIZE");
  36. unsetenv("OMP_WAIT_POLICY");
  37. unsetenv("OMP_THREAD_LIMIT");
  38. unsetenv("OMP_MAX_ACTIVE_LEVELS");
  39. unsetenv("OMP_CANCELLATION");
  40. unsetenv("OMP_DEFAULT_DEVICE");
  41. unsetenv("OMP_PROC_BIND");
  42. unsetenv("OMP_NUM_THREADS");
  43. unsetenv("OMP_PLACES");
  44. unsetenv("OMP_DISPLAY_ENV");
  45. ret = starpu_omp_init();
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_omp_init");
  47. }
  48. __attribute__((destructor))
  49. static void omp_destructor(void)
  50. {
  51. starpu_omp_shutdown();
  52. }
  53. #define check_omp_func(f,_tv) \
  54. { \
  55. const int v = (f()); \
  56. const int tv = (_tv); \
  57. printf(#f ": %d (should be %d)\n", v, tv); \
  58. STARPU_ASSERT(v == tv); \
  59. }
  60. const char * get_sched_name(int sched_value)
  61. {
  62. const char *sched_name = NULL;
  63. switch (sched_value)
  64. {
  65. case starpu_omp_sched_undefined: sched_name = "<undefined>"; break;
  66. case starpu_omp_sched_static: sched_name = "static"; break;
  67. case starpu_omp_sched_dynamic: sched_name = "dynamic"; break;
  68. case starpu_omp_sched_guided: sched_name = "guided"; break;
  69. case starpu_omp_sched_auto: sched_name = "auto"; break;
  70. case starpu_omp_sched_runtime: sched_name = "runtime"; break;
  71. default: _STARPU_ERROR("invalid omp schedule value");
  72. }
  73. return sched_name;
  74. }
  75. int
  76. main (int argc, char *argv[])
  77. {
  78. const int nb_cpus = starpu_cpu_worker_get_count();
  79. check_omp_func(starpu_omp_get_num_threads, 1);
  80. check_omp_func(starpu_omp_get_thread_num, 0);
  81. /* since OMP_NUM_THREADS is cleared, starpu_omp_get_max_threads() should return nb_cpus */
  82. check_omp_func(starpu_omp_get_max_threads, nb_cpus);
  83. check_omp_func(starpu_omp_get_num_procs, nb_cpus);
  84. check_omp_func(starpu_omp_in_parallel, 0);
  85. check_omp_func(starpu_omp_get_dynamic, 0);
  86. check_omp_func(starpu_omp_get_nested, 0);
  87. check_omp_func(starpu_omp_get_cancellation, 0);
  88. {
  89. const enum starpu_omp_sched_value target_kind = starpu_omp_sched_static;
  90. const int target_modifier = 0;
  91. enum starpu_omp_sched_value kind;
  92. int modifier;
  93. const char *sched_name;
  94. const char *target_sched_name;
  95. starpu_omp_get_schedule(&kind, &modifier);
  96. sched_name = get_sched_name(kind);
  97. target_sched_name = get_sched_name(target_kind);
  98. printf("starpu_omp_get_schedule: %s,%d (should be %s,%d)\n", sched_name, modifier, target_sched_name, target_modifier);
  99. STARPU_ASSERT(kind == target_kind && modifier == target_modifier);
  100. }
  101. check_omp_func(starpu_omp_get_thread_limit, nb_cpus);
  102. check_omp_func(starpu_omp_get_max_active_levels, 1);
  103. check_omp_func(starpu_omp_get_level, 0);
  104. {
  105. const int tv = 0;
  106. const int v = starpu_omp_get_ancestor_thread_num(0);
  107. printf("starpu_omp_get_ancestor_thread_num(0): %d (should be %d)\n", v, tv);
  108. STARPU_ASSERT(v == tv);
  109. }
  110. {
  111. const int tv = 1;
  112. const int v = starpu_omp_get_team_size(0);
  113. printf("starpu_omp_get_team_size(0): %d (should be %d)\n", v, tv);
  114. STARPU_ASSERT(v == tv);
  115. }
  116. check_omp_func(starpu_omp_get_active_level, 0);
  117. check_omp_func(starpu_omp_in_final, 0);
  118. check_omp_func(starpu_omp_get_proc_bind, starpu_omp_proc_bind_undefined);
  119. check_omp_func(starpu_omp_get_default_device, 0);
  120. /* TODO: support more than one device */
  121. check_omp_func(starpu_omp_get_num_devices, 1);
  122. check_omp_func(starpu_omp_get_num_teams, 1);
  123. check_omp_func(starpu_omp_get_team_num, 0);
  124. check_omp_func(starpu_omp_is_initial_device, 1);
  125. return 0;
  126. }
  127. #endif