workers_cpuid.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 Inria
  4. * Copyright (C) 2010-2013,2016,2017,2019 CNRS
  5. * Copyright (C) 2010-2012,2015-2017, 2019 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Try various values for STARPU_WORKERS_CPUID, checking that the
  22. * expected binding does happen
  23. */
  24. #if !defined(STARPU_HAVE_UNSETENV) || !defined(STARPU_USE_CPU) || !defined(STARPU_HAVE_HWLOC)
  25. #warning unsetenv is not defined or no cpu are available. Skipping test
  26. int main(void)
  27. {
  28. return STARPU_TEST_SKIPPED;
  29. }
  30. #else
  31. #include <hwloc.h>
  32. #ifdef STARPU_QUICK_CHECK
  33. #define CPUSTEP 8
  34. #define NB_TESTS 1
  35. #else
  36. #define CPUSTEP 1
  37. #define NB_TESTS 5
  38. #endif
  39. int nhwpus;
  40. long workers_cpuid[STARPU_NMAXWORKERS];
  41. int workers_id[STARPU_NMAXWORKERS];
  42. static int check_workers_mapping(long *cpuid, int *workerids, int nb_workers)
  43. {
  44. int i;
  45. for (i=0; i<nb_workers; i++)
  46. {
  47. int workerid = workerids[i];
  48. long bindid = starpu_worker_get_bindid(workerid);
  49. if ( bindid != cpuid[i])
  50. {
  51. fprintf(stderr, "Worker %d (%s) is on cpu %ld rather than on %ld\n",
  52. workerid, starpu_worker_get_type_as_string(starpu_worker_get_type(workerid)),
  53. bindid, cpuid[i]);
  54. return 0;
  55. }
  56. }
  57. return 1;
  58. }
  59. static void copy_cpuid_array(long *dst, long *src, unsigned n)
  60. {
  61. int i;
  62. memcpy(dst, src, n * sizeof(long));
  63. for (i=n; i<STARPU_NMAXWORKERS; i++)
  64. dst[i] = src[i%n];
  65. }
  66. static char *array_to_str(long *array, int n)
  67. {
  68. int i;
  69. int len = n * 3 * sizeof(long);
  70. char *str = malloc(len);
  71. char *ptr = str;
  72. for (i=0; i<n; i++)
  73. {
  74. int nchar;
  75. nchar = snprintf(ptr, len - (ptr-str), "%ld ", array[i]);
  76. ptr += nchar;
  77. }
  78. return str;
  79. }
  80. static int test_combination(long *combination, unsigned n)
  81. {
  82. int ret, device_workers;
  83. char *str;
  84. copy_cpuid_array(workers_cpuid, combination, n);
  85. str = array_to_str(workers_cpuid, n);
  86. setenv("STARPU_WORKERS_CPUID", str, 1);
  87. free(str);
  88. ret = starpu_init(NULL);
  89. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  91. device_workers = 0;
  92. /* Check for all cpus */
  93. {
  94. int nb_workers;
  95. nb_workers = starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, workers_id, starpu_worker_get_count());
  96. if (!check_workers_mapping(workers_cpuid + device_workers, workers_id, nb_workers))
  97. return -1;
  98. }
  99. starpu_shutdown();
  100. return 1;
  101. }
  102. static long * generate_arrangement(int arr_size, long *set, int set_size)
  103. {
  104. int i;
  105. STARPU_ASSERT(arr_size <= set_size);
  106. srandom(time(0));
  107. for (i=0; i<arr_size; i++)
  108. {
  109. /* Pick a random value in the set */
  110. int j = starpu_lrand48() % (set_size - i);
  111. /* Switch the value picked up with the beginning value of set */
  112. long tmp = set[i+j];
  113. set[i] = set[i+j];
  114. set[i+j] = tmp;
  115. }
  116. return set;
  117. }
  118. static void init_array(long *a, int n)
  119. {
  120. int i;
  121. for(i=0; i<n; i++)
  122. a[i] = i;
  123. }
  124. int main(void)
  125. {
  126. int i;
  127. long *cpuids;
  128. hwloc_topology_t topology;
  129. hwloc_topology_init(&topology);
  130. hwloc_topology_load(topology);
  131. nhwpus = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
  132. if (nhwpus > STARPU_NMAXWORKERS)
  133. nhwpus = STARPU_NMAXWORKERS;
  134. setenv("STARPU_NCUDA","0",1);
  135. setenv("STARPU_NOPENCL","0",1);
  136. setenv("STARPU_NMIC","0",1);
  137. setenv("STARPU_NMPI_MS","0",1);
  138. unsetenv("STARPU_NCPUS");
  139. for (i=0; i<STARPU_NMAXWORKERS; i++)
  140. workers_id[i] = -1;
  141. cpuids = malloc(nhwpus * sizeof(long));
  142. /* Evaluate several random values of STARPU_WORKERS_CPUID
  143. * and check mapping for each one
  144. */
  145. for (i=1; i<=nhwpus; i += CPUSTEP)
  146. {
  147. int j;
  148. for (j=0; j<NB_TESTS; j++)
  149. {
  150. int ret;
  151. init_array(cpuids, nhwpus);
  152. generate_arrangement(i, cpuids, nhwpus);
  153. ret = test_combination(cpuids, i);
  154. if (ret == STARPU_TEST_SKIPPED) return STARPU_TEST_SKIPPED;
  155. if (ret != 1)
  156. {
  157. free(cpuids);
  158. hwloc_topology_destroy(topology);
  159. return EXIT_FAILURE;
  160. }
  161. }
  162. }
  163. free(cpuids);
  164. hwloc_topology_destroy(topology);
  165. return EXIT_SUCCESS;
  166. }
  167. #endif