sched_ctx_utils.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 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 "sched_ctx_utils.h"
  17. #include <starpu.h>
  18. unsigned size1;
  19. unsigned size2;
  20. unsigned nblocks1;
  21. unsigned nblocks2;
  22. unsigned cpu1;
  23. unsigned cpu2;
  24. unsigned gpu;
  25. unsigned gpu1;
  26. unsigned gpu2;
  27. struct params
  28. {
  29. unsigned id;
  30. unsigned ctx;
  31. int the_other_ctx;
  32. int *procs;
  33. int nprocs;
  34. void (*bench)(unsigned, unsigned);
  35. unsigned size;
  36. unsigned nblocks;
  37. };
  38. struct retvals
  39. {
  40. double flops;
  41. double avg_timing;
  42. };
  43. #define NSAMPLES 1
  44. int first = 1;
  45. starpu_pthread_mutex_t mut;
  46. struct retvals rv[2];
  47. struct params p1, p2;
  48. starpu_pthread_key_t key;
  49. void init()
  50. {
  51. size1 = 4*1024;
  52. size2 = 4*1024;
  53. nblocks1 = 16;
  54. nblocks2 = 16;
  55. cpu1 = 0;
  56. cpu2 = 0;
  57. gpu = 0;
  58. gpu1 = 0;
  59. gpu2 = 0;
  60. rv[0].flops = 0.0;
  61. rv[1].flops = 0.0;
  62. rv[1].avg_timing = 0.0;
  63. p1.ctx = 0;
  64. p2.ctx = 0;
  65. p1.id = 0;
  66. p2.id = 1;
  67. STARPU_PTHREAD_KEY_CREATE(&key, NULL);
  68. }
  69. void update_sched_ctx_timing_results(double flops, double avg_timing)
  70. {
  71. unsigned *id = STARPU_PTHREAD_GETSPECIFIC(key);
  72. rv[*id].flops += flops;
  73. rv[*id].avg_timing += avg_timing;
  74. }
  75. void* start_bench(void *val)
  76. {
  77. struct params *p = (struct params*)val;
  78. int i;
  79. STARPU_PTHREAD_SETSPECIFIC(key, &p->id);
  80. if(p->ctx != 0)
  81. starpu_sched_ctx_set_context(&p->ctx);
  82. for(i = 0; i < NSAMPLES; i++)
  83. p->bench(p->size, p->nblocks);
  84. if(p->ctx != 0)
  85. {
  86. STARPU_PTHREAD_MUTEX_LOCK(&mut);
  87. if(first)
  88. {
  89. starpu_sched_ctx_delete(p->ctx);
  90. }
  91. first = 0;
  92. STARPU_PTHREAD_MUTEX_UNLOCK(&mut);
  93. }
  94. rv[p->id].flops /= NSAMPLES;
  95. rv[p->id].avg_timing /= NSAMPLES;
  96. return NULL;
  97. }
  98. void start_2benchs(void (*bench)(unsigned, unsigned))
  99. {
  100. p1.bench = bench;
  101. p1.size = size1;
  102. printf("size %u\n", size1);
  103. p1.nblocks = nblocks1;
  104. p2.bench = bench;
  105. p2.size = size2;
  106. printf("size %u\n", size2);
  107. p2.nblocks = nblocks2;
  108. starpu_pthread_t tid[2];
  109. STARPU_PTHREAD_MUTEX_INIT(&mut, NULL);
  110. double start;
  111. double end;
  112. start = starpu_timing_now();
  113. STARPU_PTHREAD_CREATE(&tid[0], NULL, (void*)start_bench, (void*)&p1);
  114. STARPU_PTHREAD_CREATE(&tid[1], NULL, (void*)start_bench, (void*)&p2);
  115. STARPU_PTHREAD_JOIN(tid[0], NULL);
  116. STARPU_PTHREAD_JOIN(tid[1], NULL);
  117. end = starpu_timing_now();
  118. STARPU_PTHREAD_MUTEX_DESTROY(&mut);
  119. double timing = end - start;
  120. timing /= 1000000;
  121. printf("%2.2f %2.2f ", rv[0].flops, rv[1].flops);
  122. printf("%2.2f %2.2f %2.2f\n", rv[0].avg_timing, rv[1].avg_timing, timing);
  123. }
  124. void start_1stbench(void (*bench)(unsigned, unsigned))
  125. {
  126. p1.bench = bench;
  127. p1.size = size1;
  128. p1.nblocks = nblocks1;
  129. double start;
  130. double end;
  131. start = starpu_timing_now();
  132. start_bench((void*)&p1);
  133. end = starpu_timing_now();
  134. STARPU_PTHREAD_MUTEX_DESTROY(&mut);
  135. double timing = end - start;
  136. timing /= 1000000;
  137. printf("%2.2f ", rv[0].flops);
  138. printf("%2.2f %2.2f\n", rv[0].avg_timing, timing);
  139. }
  140. void start_2ndbench(void (*bench)(unsigned, unsigned))
  141. {
  142. p2.bench = bench;
  143. p2.size = size2;
  144. p2.nblocks = nblocks2;
  145. double start;
  146. double end;
  147. start = starpu_timing_now();
  148. start_bench((void*)&p2);
  149. end = starpu_timing_now();
  150. STARPU_PTHREAD_MUTEX_DESTROY(&mut);
  151. double timing = end - start;
  152. timing /= 1000000;
  153. printf("%2.2f ", rv[1].flops);
  154. printf("%2.2f %2.2f\n", rv[1].avg_timing, timing);
  155. }
  156. void construct_contexts()
  157. {
  158. unsigned nprocs1 = cpu1 + gpu + gpu1;
  159. unsigned nprocs2 = cpu2 + gpu + gpu2;
  160. unsigned n_all_gpus = gpu + gpu1 + gpu2;
  161. int procs[nprocs1];
  162. unsigned i;
  163. int k = 0;
  164. for(i = 0; i < gpu; i++)
  165. {
  166. procs[k++] = i;
  167. printf("%u ", i);
  168. }
  169. for(i = gpu; i < gpu + gpu1; i++)
  170. {
  171. procs[k++] = i;
  172. printf("%u ", i);
  173. }
  174. for(i = n_all_gpus; i < n_all_gpus + cpu1; i++)
  175. {
  176. procs[k++] = i;
  177. printf("%u ", i);
  178. }
  179. printf("\n ");
  180. p1.ctx = starpu_sched_ctx_create(procs, nprocs1, "sched_ctx1", STARPU_SCHED_CTX_POLICY_NAME, "heft", 0);
  181. p2.the_other_ctx = (int)p1.ctx;
  182. p1.procs = procs;
  183. p1.nprocs = nprocs1;
  184. int procs2[nprocs2];
  185. k = 0;
  186. for(i = 0; i < gpu; i++)
  187. {
  188. procs2[k++] = i;
  189. printf("%u ", i);
  190. }
  191. for(i = gpu + gpu1; i < gpu + gpu1 + gpu2; i++)
  192. {
  193. procs2[k++] = i;
  194. printf("%u ", i);
  195. }
  196. for(i = n_all_gpus + cpu1; i < n_all_gpus + cpu1 + cpu2; i++)
  197. {
  198. procs2[k++] = i;
  199. printf("%u ", i);
  200. }
  201. printf("\n");
  202. p2.ctx = starpu_sched_ctx_create(procs2, nprocs2, "sched_ctx2", STARPU_SCHED_CTX_POLICY_NAME, "heft", 0);
  203. p1.the_other_ctx = (int)p2.ctx;
  204. p2.procs = procs2;
  205. starpu_sched_ctx_set_inheritor(p1.ctx, p2.ctx);
  206. starpu_sched_ctx_set_inheritor(p2.ctx, p1.ctx);
  207. p2.nprocs = nprocs2;
  208. }
  209. void parse_args_ctx(int argc, char **argv)
  210. {
  211. init();
  212. int i;
  213. for (i = 1; i < argc; i++)
  214. {
  215. if (strcmp(argv[i], "-size1") == 0)
  216. {
  217. char *argptr;
  218. size1 = strtol(argv[++i], &argptr, 10);
  219. }
  220. if (strcmp(argv[i], "-nblocks1") == 0)
  221. {
  222. char *argptr;
  223. nblocks1 = strtol(argv[++i], &argptr, 10);
  224. }
  225. if (strcmp(argv[i], "-size2") == 0)
  226. {
  227. char *argptr;
  228. size2 = strtol(argv[++i], &argptr, 10);
  229. }
  230. if (strcmp(argv[i], "-nblocks2") == 0)
  231. {
  232. char *argptr;
  233. nblocks2 = strtol(argv[++i], &argptr, 10);
  234. }
  235. if (strcmp(argv[i], "-cpu1") == 0)
  236. {
  237. char *argptr;
  238. cpu1 = strtol(argv[++i], &argptr, 10);
  239. }
  240. if (strcmp(argv[i], "-cpu2") == 0)
  241. {
  242. char *argptr;
  243. cpu2 = strtol(argv[++i], &argptr, 10);
  244. }
  245. if (strcmp(argv[i], "-gpu") == 0)
  246. {
  247. char *argptr;
  248. gpu = strtol(argv[++i], &argptr, 10);
  249. }
  250. if (strcmp(argv[i], "-gpu1") == 0)
  251. {
  252. char *argptr;
  253. gpu1 = strtol(argv[++i], &argptr, 10);
  254. }
  255. if (strcmp(argv[i], "-gpu2") == 0)
  256. {
  257. char *argptr;
  258. gpu2 = strtol(argv[++i], &argptr, 10);
  259. }
  260. }
  261. }