sched_ctx_utils.c 6.1 KB

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