starpu_init.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012, 2013, 2014, 2017 CNRS
  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. /*
  20. * Try initializing starpu with various CPU parameters
  21. */
  22. #if !defined(STARPU_HAVE_UNSETENV) || !defined(STARPU_HAVE_SETENV) || !defined(STARPU_USE_CPU)
  23. #warning unsetenv or setenv are not defined. Or CPU are not enabled. Skipping test
  24. int main(int argc, char **argv)
  25. {
  26. return STARPU_TEST_SKIPPED;
  27. }
  28. #else
  29. static int check_cpu(int env_cpu, int conf_cpu, int expected_cpu, int *cpu)
  30. {
  31. int ret;
  32. FPRINTF(stderr, "Testing with env=%d - conf=%d\n", env_cpu, conf_cpu);
  33. if (env_cpu != -1)
  34. {
  35. char string[11];
  36. snprintf(string, sizeof(string), "%d", env_cpu);
  37. setenv("STARPU_NCPUS", string, 1);
  38. }
  39. struct starpu_conf user_conf;
  40. starpu_conf_init(&user_conf);
  41. if (conf_cpu != -1)
  42. {
  43. user_conf.ncpus = conf_cpu;
  44. }
  45. ret = starpu_init(&user_conf);
  46. if (env_cpu != -1)
  47. {
  48. unsetenv("STARPU_NCPUS");
  49. }
  50. if (ret == -ENODEV)
  51. {
  52. return STARPU_TEST_SKIPPED;
  53. }
  54. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  55. *cpu = starpu_cpu_worker_get_count();
  56. starpu_shutdown();
  57. if (expected_cpu == -1)
  58. {
  59. FPRINTF(stderr, "Number of CPUS: %3d\n", *cpu);
  60. return 0;
  61. }
  62. else
  63. {
  64. FPRINTF(stderr, "Number of CPUS: %3d -- Number of expected CPUs: %3d\n", *cpu, expected_cpu);
  65. return *cpu != expected_cpu;
  66. }
  67. }
  68. int main(int argc, char **argv)
  69. {
  70. int ret;
  71. int cpu, cpu_init;
  72. int cpu_test1, cpu_test2, cpu_test3;
  73. unsetenv("STARPU_NCPUS");
  74. unsetenv("STARPU_NCPU");
  75. ret = check_cpu(-1, -1, -1, &cpu_init);
  76. if (ret) return ret;
  77. if (cpu_init <= 1) return STARPU_TEST_SKIPPED;
  78. if (cpu_init >= STARPU_MAXCPUS-5)
  79. {
  80. cpu_test1 = cpu_init-1;
  81. cpu_test2 = cpu_init-2;
  82. cpu_test3 = cpu_init-3;
  83. }
  84. else
  85. {
  86. cpu_test1 = cpu_init+1;
  87. cpu_test2 = cpu_init+2;
  88. cpu_test3 = cpu_init+3;
  89. }
  90. ret = check_cpu(cpu_test1, -1, cpu_test1, &cpu);
  91. if (ret) return ret;
  92. ret = check_cpu(-1, -1, -1, &cpu);
  93. if (ret) return ret;
  94. if (cpu != cpu_init)
  95. {
  96. FPRINTF(stderr, "The number of CPUs is incorrect\n");
  97. return 1;
  98. }
  99. ret = check_cpu(-1, cpu_test2, cpu_test2, &cpu);
  100. if (ret) return ret;
  101. ret = check_cpu(cpu_test3, cpu_test1, cpu_test3, &cpu);
  102. if (ret) return ret;
  103. STARPU_RETURN(ret);
  104. }
  105. #endif