workers_cpuid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. /*
  19. * Try various values for STARPU_WORKERS_CPUID, checking that the
  20. * expected binding does happen
  21. */
  22. #if !defined(STARPU_USE_CPU) || !defined(STARPU_HAVE_HWLOC) || !defined(STARPU_HAVE_SETENV)
  23. #warning no cpu are available. Skipping test
  24. int main(void)
  25. {
  26. return STARPU_TEST_SKIPPED;
  27. }
  28. #else
  29. #include <hwloc.h>
  30. #ifdef STARPU_QUICK_CHECK
  31. #define CPUSTEP 8
  32. #define NB_TESTS 1
  33. #else
  34. #define CPUSTEP 1
  35. #define NB_TESTS 5
  36. #endif
  37. int nhwpus;
  38. long workers_cpuid[STARPU_NMAXWORKERS];
  39. int workers_id[STARPU_NMAXWORKERS];
  40. static int check_workers_mapping(long *cpuid, int *workerids, int nb_workers)
  41. {
  42. int i;
  43. for (i=0; i<nb_workers; i++)
  44. {
  45. int workerid = workerids[i];
  46. long bindid = starpu_worker_get_bindid(workerid);
  47. if ( bindid != cpuid[i])
  48. {
  49. fprintf(stderr, "Worker %d (%s) is on cpu %ld rather than on %ld\n",
  50. workerid, starpu_worker_get_type_as_string(starpu_worker_get_type(workerid)),
  51. bindid, cpuid[i]);
  52. return 0;
  53. }
  54. }
  55. return 1;
  56. }
  57. static void copy_cpuid_array(long *dst, long *src, unsigned n)
  58. {
  59. int i;
  60. memcpy(dst, src, n * sizeof(long));
  61. for (i=n; i<STARPU_NMAXWORKERS; i++)
  62. dst[i] = src[i%n];
  63. }
  64. static char *array_to_str(long *array, int n)
  65. {
  66. int i;
  67. int len = n * 3 * sizeof(long);
  68. char *str = malloc(len);
  69. char *ptr = str;
  70. for (i=0; i<n; i++)
  71. {
  72. int nchar;
  73. nchar = snprintf(ptr, len - (ptr-str), "%ld ", array[i]);
  74. ptr += nchar;
  75. }
  76. return str;
  77. }
  78. static int test_combination(long *combination, unsigned n)
  79. {
  80. int ret, device_workers;
  81. char *str;
  82. copy_cpuid_array(workers_cpuid, combination, n);
  83. str = array_to_str(workers_cpuid, n);
  84. setenv("STARPU_WORKERS_CPUID", str, 1);
  85. free(str);
  86. struct starpu_conf conf;
  87. starpu_conf_init(&conf);
  88. conf.precedence_over_environment_variables = 1;
  89. starpu_conf_noworker(&conf);
  90. conf.ncpus = -1;
  91. ret = starpu_init(&conf);
  92. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  94. device_workers = 0;
  95. /* Check for all cpus */
  96. {
  97. int nb_workers;
  98. nb_workers = starpu_worker_get_ids_by_type(STARPU_CPU_WORKER, workers_id, starpu_worker_get_count());
  99. if (!check_workers_mapping(workers_cpuid + device_workers, workers_id, nb_workers))
  100. return -1;
  101. }
  102. starpu_shutdown();
  103. return 1;
  104. }
  105. static long * generate_arrangement(int arr_size, long *set, int set_size)
  106. {
  107. int i;
  108. STARPU_ASSERT(arr_size <= set_size);
  109. for (i=0; i<arr_size; i++)
  110. {
  111. /* Pick a random value in the set */
  112. int j = starpu_lrand48() % (set_size - i);
  113. /* Switch the value picked up with the beginning value of set */
  114. long tmp = set[i+j];
  115. set[i] = set[i+j];
  116. set[i+j] = tmp;
  117. }
  118. return set;
  119. }
  120. static void init_array(long *a, int n)
  121. {
  122. int i;
  123. for(i=0; i<n; i++)
  124. a[i] = i;
  125. }
  126. int main(void)
  127. {
  128. int i;
  129. long *cpuids;
  130. hwloc_topology_t topology;
  131. hwloc_topology_init(&topology);
  132. hwloc_topology_load(topology);
  133. nhwpus = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU);
  134. if (nhwpus > STARPU_NMAXWORKERS)
  135. nhwpus = STARPU_NMAXWORKERS;
  136. for (i=0; i<STARPU_NMAXWORKERS; i++)
  137. workers_id[i] = -1;
  138. cpuids = malloc(nhwpus * sizeof(long));
  139. /* Evaluate several random values of STARPU_WORKERS_CPUID
  140. * and check mapping for each one
  141. */
  142. for (i=1; i<=nhwpus; i += CPUSTEP)
  143. {
  144. int j;
  145. for (j=0; j<NB_TESTS; j++)
  146. {
  147. int ret;
  148. init_array(cpuids, nhwpus);
  149. generate_arrangement(i, cpuids, nhwpus);
  150. ret = test_combination(cpuids, i);
  151. if (ret == STARPU_TEST_SKIPPED) return STARPU_TEST_SKIPPED;
  152. if (ret != 1)
  153. {
  154. free(cpuids);
  155. hwloc_topology_destroy(topology);
  156. return EXIT_FAILURE;
  157. }
  158. }
  159. }
  160. free(cpuids);
  161. hwloc_topology_destroy(topology);
  162. return EXIT_SUCCESS;
  163. }
  164. #endif