starpu_init.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2012-2015,2017 CNRS
  5. * Copyright (C) 2013-2017 Université de Bordeaux
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include "../helper.h"
  21. #include <stdlib.h>
  22. /*
  23. * Try initializing starpu with various CPU parameters
  24. */
  25. #if !defined(STARPU_HAVE_UNSETENV) || !defined(STARPU_HAVE_SETENV) || !defined(STARPU_USE_CPU)
  26. #warning unsetenv or setenv are not defined. Or CPU are not enabled. Skipping test
  27. int main(void)
  28. {
  29. return STARPU_TEST_SKIPPED;
  30. }
  31. #else
  32. static int check_cpu(int env_cpu, int conf_cpu, int expected_cpu, int *cpu)
  33. {
  34. int ret;
  35. FPRINTF(stderr, "Testing with env=%d - conf=%d\n", env_cpu, conf_cpu);
  36. if (env_cpu != -1)
  37. {
  38. char string[11];
  39. snprintf(string, sizeof(string), "%d", env_cpu);
  40. setenv("STARPU_NCPUS", string, 1);
  41. }
  42. struct starpu_conf user_conf;
  43. starpu_conf_init(&user_conf);
  44. if (conf_cpu != -1)
  45. {
  46. user_conf.ncpus = conf_cpu;
  47. }
  48. ret = starpu_init(&user_conf);
  49. if (env_cpu != -1)
  50. {
  51. unsetenv("STARPU_NCPUS");
  52. }
  53. if (ret == -ENODEV)
  54. {
  55. return STARPU_TEST_SKIPPED;
  56. }
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  58. *cpu = starpu_cpu_worker_get_count();
  59. starpu_shutdown();
  60. if (expected_cpu == -1)
  61. {
  62. FPRINTF(stderr, "Number of CPUS: %3d\n", *cpu);
  63. return 0;
  64. }
  65. else
  66. {
  67. FPRINTF(stderr, "Number of CPUS: %3d -- Number of expected CPUs: %3d\n", *cpu, expected_cpu);
  68. return *cpu != expected_cpu;
  69. }
  70. }
  71. int main(void)
  72. {
  73. int ret;
  74. int cpu, cpu_init;
  75. int cpu_test1, cpu_test2, cpu_test3;
  76. unsetenv("STARPU_NCPUS");
  77. unsetenv("STARPU_NCPU");
  78. ret = check_cpu(-1, -1, -1, &cpu_init);
  79. if (ret) return ret;
  80. if (cpu_init <= 1) return STARPU_TEST_SKIPPED;
  81. if (cpu_init >= STARPU_MAXCPUS-5)
  82. {
  83. cpu_test1 = cpu_init-1;
  84. cpu_test2 = cpu_init-2;
  85. cpu_test3 = cpu_init-3;
  86. }
  87. else
  88. {
  89. cpu_test1 = cpu_init+1;
  90. cpu_test2 = cpu_init+2;
  91. cpu_test3 = cpu_init+3;
  92. }
  93. ret = check_cpu(cpu_test1, -1, cpu_test1, &cpu);
  94. if (ret) return ret;
  95. ret = check_cpu(-1, -1, -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. ret = check_cpu(-1, cpu_test2, cpu_test2, &cpu);
  103. if (ret) return ret;
  104. ret = check_cpu(cpu_test3, cpu_test1, cpu_test3, &cpu);
  105. if (ret) return ret;
  106. STARPU_RETURN(ret);
  107. }
  108. #endif