starpu_init.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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. /*
  21. * Try initializing starpu with various CPU parameters
  22. */
  23. #if !defined(STARPU_HAVE_UNSETENV) || !defined(STARPU_HAVE_SETENV) || !defined(STARPU_USE_CPU)
  24. #warning unsetenv or setenv are not defined. Or CPU are not enabled. Skipping test
  25. int main(void)
  26. {
  27. return STARPU_TEST_SKIPPED;
  28. }
  29. #else
  30. static int check_cpu(int env_cpu, int conf_cpu, int precedence_over_env, int expected_cpu, int *cpu)
  31. {
  32. int ret;
  33. FPRINTF(stderr, "\nTesting with env=%d - conf=%d - expected %d (ignore env %d)\n", env_cpu, conf_cpu, expected_cpu, precedence_over_env);
  34. if (env_cpu != -1)
  35. {
  36. char string[11];
  37. snprintf(string, sizeof(string), "%d", env_cpu);
  38. setenv("STARPU_NCPUS", string, 1);
  39. }
  40. struct starpu_conf user_conf;
  41. starpu_conf_init(&user_conf);
  42. user_conf.precedence_over_environment_variables = precedence_over_env;
  43. if (conf_cpu != -1)
  44. {
  45. user_conf.ncpus = conf_cpu;
  46. }
  47. ret = starpu_init(&user_conf);
  48. if (env_cpu != -1)
  49. {
  50. unsetenv("STARPU_NCPUS");
  51. }
  52. if (ret == -ENODEV)
  53. {
  54. return STARPU_TEST_SKIPPED;
  55. }
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. *cpu = starpu_cpu_worker_get_count();
  58. starpu_shutdown();
  59. if (expected_cpu == -1)
  60. {
  61. FPRINTF(stderr, "Number of CPUS: %3d\n", *cpu);
  62. return 0;
  63. }
  64. else
  65. {
  66. FPRINTF(stderr, "Number of CPUS: %3d -- Number of expected CPUs: %3d --> %s\n", *cpu, expected_cpu, *cpu==expected_cpu?"SUCCESS":"FAILURE");
  67. return *cpu != expected_cpu;
  68. }
  69. }
  70. int main(void)
  71. {
  72. int ret;
  73. int cpu, cpu_init;
  74. int cpu_test1, cpu_test2, cpu_test3;
  75. unsetenv("STARPU_NCPUS");
  76. unsetenv("STARPU_NCPU");
  77. ret = check_cpu(-1, -1, 0, -1, &cpu_init);
  78. if (ret) return ret;
  79. if (cpu_init <= 1) return STARPU_TEST_SKIPPED;
  80. if (cpu_init >= STARPU_MAXCPUS-5)
  81. {
  82. cpu_test1 = cpu_init-1;
  83. cpu_test2 = cpu_init-2;
  84. cpu_test3 = cpu_init-3;
  85. }
  86. else
  87. {
  88. cpu_test1 = cpu_init+1;
  89. cpu_test2 = cpu_init+2;
  90. cpu_test3 = cpu_init+3;
  91. }
  92. ret = check_cpu(cpu_test1, -1, 0, cpu_test1, &cpu);
  93. if (ret) return ret;
  94. // Do not set anything --> default value
  95. ret = check_cpu(-1, -1, 0, -1, &cpu);
  96. if (ret) return ret;
  97. if (cpu != cpu_init)
  98. {
  99. FPRINTF(stderr, "The number of CPUs is incorrect\n");
  100. return 1;
  101. }
  102. // Do not set environment variable, set starpu_conf::ncpus --> starpu_conf::ncpus
  103. ret = check_cpu(-1, cpu_test2, 0, cpu_test2, &cpu);
  104. if (ret) return ret;
  105. // Set environment variable, and do not set starpu_conf::ncpus --> starpu_conf::ncpus
  106. ret = check_cpu(cpu_test2, -1, 0, cpu_test2, &cpu);
  107. if (ret) return ret;
  108. // Set both environment variable and starpu_conf::ncpus --> environment variable
  109. ret = check_cpu(cpu_test3, cpu_test1, 0, cpu_test3, &cpu);
  110. if (ret) return ret;
  111. // Set both environment variable and starpu_conf::ncpus AND prefer starpu_conf over env --> starpu_conf::ncpus
  112. ret = check_cpu(cpu_test3, cpu_test1, 1, cpu_test1, &cpu);
  113. if (ret) return ret;
  114. // Set environment variable, and do no set starpu_conf, AND prefer starpu_conf over env --> environment variable
  115. ret = check_cpu(cpu_test2, -1, 1, cpu_test2, &cpu);
  116. if (ret) return ret;
  117. return 0;
  118. }
  119. #endif