sched_ctx_utils.c 6.2 KB

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