workers_cpuid.c 4.3 KB

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