sched_ctx_utils.c 6.2 KB

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