starpu_init.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2020 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 expected_cpu, int *cpu)
  31. {
  32. int ret;
  33. FPRINTF(stderr, "Testing with env=%d - conf=%d\n", env_cpu, conf_cpu);
  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. if (conf_cpu != -1)
  43. {
  44. user_conf.ncpus = conf_cpu;
  45. }
  46. ret = starpu_init(&user_conf);
  47. if (env_cpu != -1)
  48. {
  49. unsetenv("STARPU_NCPUS");
  50. }
  51. if (ret == -ENODEV)
  52. {
  53. return STARPU_TEST_SKIPPED;
  54. }
  55. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  56. *cpu = starpu_cpu_worker_get_count();
  57. starpu_shutdown();
  58. if (expected_cpu == -1)
  59. {
  60. FPRINTF(stderr, "Number of CPUS: %3d\n", *cpu);
  61. return 0;
  62. }
  63. else
  64. {
  65. FPRINTF(stderr, "Number of CPUS: %3d -- Number of expected CPUs: %3d\n", *cpu, expected_cpu);
  66. return *cpu != expected_cpu;
  67. }
  68. }
  69. int main(void)
  70. {
  71. int ret;
  72. int cpu, cpu_init;
  73. int cpu_test1, cpu_test2, cpu_test3;
  74. unsetenv("STARPU_NCPUS");
  75. unsetenv("STARPU_NCPU");
  76. ret = check_cpu(-1, -1, -1, &cpu_init);
  77. if (ret) return ret;
  78. if (cpu_init <= 1) return STARPU_TEST_SKIPPED;
  79. if (cpu_init >= STARPU_MAXCPUS-5)
  80. {
  81. cpu_test1 = cpu_init-1;
  82. cpu_test2 = cpu_init-2;
  83. cpu_test3 = cpu_init-3;
  84. }
  85. else
  86. {
  87. cpu_test1 = cpu_init+1;
  88. cpu_test2 = cpu_init+2;
  89. cpu_test3 = cpu_init+3;
  90. }
  91. ret = check_cpu(cpu_test1, -1, cpu_test1, &cpu);
  92. if (ret) return ret;
  93. ret = check_cpu(-1, -1, -1, &cpu);
  94. if (ret) return ret;
  95. if (cpu != cpu_init)
  96. {
  97. FPRINTF(stderr, "The number of CPUs is incorrect\n");
  98. return 1;
  99. }
  100. ret = check_cpu(-1, cpu_test2, cpu_test2, &cpu);
  101. if (ret) return ret;
  102. ret = check_cpu(cpu_test3, cpu_test1, cpu_test3, &cpu);
  103. if (ret) return ret;
  104. return 0;
  105. }
  106. #endif