05_vector_scale.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* StarPU --- Resource Management Layer.
  2. *
  3. * Copyright (C) 2017, 2018 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. /* This example shows a basic StarPU vector scale app on top of StarPURM */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <assert.h>
  20. #include <starpu.h>
  21. #include <starpurm.h>
  22. static int rm_cpu_type_id = -1;
  23. static int rm_nb_cpu_units = 0;
  24. static void usage(void);
  25. static void test1(const int N);
  26. static void test2(const int N, const int task_mult);
  27. static void init_rm_infos(void);
  28. /* vector scale codelet */
  29. static void vector_scale_func(void *cl_buffers[], void *cl_arg)
  30. {
  31. double scalar = -1.0;
  32. int n = STARPU_VECTOR_GET_NX(cl_buffers[0]);
  33. double *vector = (double *)STARPU_VECTOR_GET_PTR(cl_buffers[0]);
  34. int i;
  35. starpu_codelet_unpack_args(cl_arg, &scalar);
  36. int workerid = starpu_worker_get_id();
  37. hwloc_cpuset_t worker_cpuset = starpu_worker_get_hwloc_cpuset(workerid);
  38. hwloc_cpuset_t check_cpuset = starpurm_get_selected_cpuset();
  39. {
  40. int strl1 = hwloc_bitmap_snprintf(NULL, 0, worker_cpuset);
  41. char str1[strl1+1];
  42. hwloc_bitmap_snprintf(str1, strl1+1, worker_cpuset);
  43. int strl2 = hwloc_bitmap_snprintf(NULL, 0, check_cpuset);
  44. char str2[strl2+1];
  45. hwloc_bitmap_snprintf(str2, strl2+1, check_cpuset);
  46. printf("worker[%03d] - task: vector=%p, n=%d, scalar=%lf, worker cpuset = %s, selected cpuset = %s\n", workerid, vector, n, scalar, str1, str2);
  47. }
  48. hwloc_bitmap_and(check_cpuset, check_cpuset, worker_cpuset);
  49. assert(!hwloc_bitmap_iszero(check_cpuset));
  50. hwloc_bitmap_free(check_cpuset);
  51. hwloc_bitmap_free(worker_cpuset);
  52. for (i = 0; i < n; i++)
  53. {
  54. vector[i] *= scalar;
  55. }
  56. }
  57. static struct starpu_codelet vector_scale_cl =
  58. {
  59. .cpu_funcs = {vector_scale_func},
  60. .nbuffers = 1
  61. };
  62. /* main routines */
  63. static void usage(void)
  64. {
  65. fprintf(stderr, "usage: 05_vector_scale [VECTOR_SIZE]\n");
  66. exit(1);
  67. }
  68. static void test1(const int N)
  69. {
  70. double *vector = NULL;
  71. const double scalar = 2.0;
  72. starpu_data_handle_t vector_handle;
  73. int ret;
  74. vector = malloc(N * sizeof(*vector));
  75. {
  76. int i;
  77. for (i = 0; i < N; i++)
  78. {
  79. vector[i] = i;
  80. }
  81. }
  82. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, N, sizeof(*vector));
  83. ret = starpu_task_insert(&vector_scale_cl,
  84. STARPU_RW, vector_handle,
  85. STARPU_VALUE, &scalar, sizeof(scalar),
  86. 0);
  87. assert(ret == 0);
  88. starpu_task_wait_for_all();
  89. starpu_data_unregister(vector_handle);
  90. {
  91. int i;
  92. for (i = 0; i < N; i++)
  93. {
  94. double d_i = i;
  95. if (vector[i] != d_i*scalar)
  96. {
  97. fprintf(stderr, "%s: check_failed\n", __func__);
  98. exit(1);
  99. }
  100. }
  101. }
  102. free(vector);
  103. }
  104. static void test2(const int N, const int task_mult)
  105. {
  106. double *vector = NULL;
  107. const double scalar = 3.0;
  108. starpu_data_handle_t vector_handle;
  109. int ret;
  110. vector = malloc(N * sizeof(*vector));
  111. {
  112. int i;
  113. for (i = 0; i < N; i++)
  114. {
  115. vector[i] = i;
  116. }
  117. }
  118. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, N, sizeof(*vector));
  119. struct starpu_data_filter partition_filter =
  120. {
  121. .filter_func = starpu_vector_filter_block,
  122. .nchildren = rm_nb_cpu_units * task_mult
  123. };
  124. starpu_data_partition(vector_handle, &partition_filter);
  125. {
  126. int i;
  127. for (i = 0; i < rm_nb_cpu_units*task_mult; i++)
  128. {
  129. starpu_data_handle_t sub_vector_handle = starpu_data_get_sub_data(vector_handle, 1, i);
  130. ret = starpu_task_insert(&vector_scale_cl,
  131. STARPU_RW, sub_vector_handle,
  132. STARPU_VALUE, &scalar, sizeof(scalar),
  133. 0);
  134. assert(ret == 0);
  135. }
  136. }
  137. starpu_task_wait_for_all();
  138. starpu_data_unpartition(vector_handle, STARPU_MAIN_RAM);
  139. starpu_data_unregister(vector_handle);
  140. {
  141. int i;
  142. for (i = 0; i < N; i++)
  143. {
  144. double d_i = i;
  145. if (vector[i] != d_i*scalar)
  146. {
  147. fprintf(stderr, "%s: check_failed\n", __func__);
  148. exit(1);
  149. }
  150. }
  151. }
  152. free(vector);
  153. }
  154. static void init_rm_infos(void)
  155. {
  156. int cpu_type = starpurm_get_device_type_id("cpu");
  157. int nb_cpu_units = starpurm_get_nb_devices_by_type(cpu_type);
  158. if (nb_cpu_units < 1)
  159. {
  160. /* No CPU unit available. */
  161. exit(77);
  162. }
  163. rm_cpu_type_id = cpu_type;
  164. rm_nb_cpu_units = nb_cpu_units;
  165. }
  166. int main(int argc, char *argv[])
  167. {
  168. int param_N = 1000000;
  169. int drs_enabled;
  170. if (argc > 1)
  171. {
  172. param_N = atoi(argv[1]);
  173. if (param_N < 1)
  174. {
  175. usage();
  176. }
  177. }
  178. starpurm_initialize();
  179. init_rm_infos();
  180. test1(param_N);
  181. test2(param_N, 1);
  182. test2(param_N, 10);
  183. test2(param_N, 100);
  184. if (rm_nb_cpu_units > 1)
  185. {
  186. const int half_nb_cpus = rm_nb_cpu_units/2;
  187. starpurm_set_drs_enable(NULL);
  188. drs_enabled = starpurm_drs_enabled_p();
  189. assert(drs_enabled != 0);
  190. printf("withdrawing %d cpus from StarPU\n", half_nb_cpus);
  191. starpurm_withdraw_cpus_from_starpu(NULL, half_nb_cpus);
  192. test2(param_N, 1);
  193. test2(param_N, 10);
  194. test2(param_N, 100);
  195. printf("assigning %d cpus to StarPU\n", half_nb_cpus);
  196. starpurm_assign_cpus_to_starpu(NULL, half_nb_cpus);
  197. test2(param_N, 1);
  198. test2(param_N, 10);
  199. test2(param_N, 100);
  200. int i;
  201. for (i = rm_nb_cpu_units-1; i > 0; i--)
  202. {
  203. starpurm_set_max_parallelism(NULL, i);
  204. test2(param_N, 10);
  205. }
  206. printf("withdrawing all cpus from StarPU\n");
  207. starpurm_withdraw_all_cpus_from_starpu(NULL);
  208. printf("assigning %d cpus to StarPU\n", rm_nb_cpu_units);
  209. starpurm_assign_cpus_to_starpu(NULL, rm_nb_cpu_units);
  210. test2(param_N, 1);
  211. test2(param_N, 10);
  212. test2(param_N, 100);
  213. starpurm_set_drs_disable(NULL);
  214. drs_enabled = starpurm_drs_enabled_p();
  215. assert(drs_enabled == 0);
  216. }
  217. starpurm_shutdown();
  218. return 0;
  219. }