sched_ctx_utils.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  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. pthread_mutex_t mut;
  46. struct retvals rv[2];
  47. struct params p1, p2;
  48. 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. pthread_key_create(&key, NULL);
  69. }
  70. void update_sched_ctx_timing_results(double flops, double avg_timing)
  71. {
  72. unsigned *id = 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. pthread_setspecific(key, &p->id);
  81. if(p->ctx != 0)
  82. starpu_set_sched_ctx(&p->ctx);
  83. for(i = 0; i < NSAMPLES; i++)
  84. p->bench(p->size, p->nblocks);
  85. if(p->ctx != 0)
  86. {
  87. pthread_mutex_lock(&mut);
  88. if(first)
  89. {
  90. starpu_sched_ctx_delete(p->ctx, p->the_other_ctx);
  91. }
  92. first = 0;
  93. 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 %d\n", size1);
  104. p1.nblocks = nblocks1;
  105. p2.bench = bench;
  106. p2.size = size2;
  107. printf("size %d\n", size2);
  108. p2.nblocks = nblocks2;
  109. pthread_t tid[2];
  110. pthread_mutex_init(&mut, NULL);
  111. struct timeval start;
  112. struct timeval end;
  113. gettimeofday(&start, NULL);
  114. pthread_create(&tid[0], NULL, (void*)start_bench, (void*)&p1);
  115. pthread_create(&tid[1], NULL, (void*)start_bench, (void*)&p2);
  116. pthread_join(tid[0], NULL);
  117. pthread_join(tid[1], NULL);
  118. gettimeofday(&end, NULL);
  119. pthread_mutex_destroy(&mut);
  120. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  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. struct timeval start;
  131. struct timeval end;
  132. gettimeofday(&start, NULL);
  133. start_bench((void*)&p1);
  134. gettimeofday(&end, NULL);
  135. pthread_mutex_destroy(&mut);
  136. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  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. struct timeval start;
  147. struct timeval end;
  148. gettimeofday(&start, NULL);
  149. start_bench((void*)&p2);
  150. gettimeofday(&end, NULL);
  151. pthread_mutex_destroy(&mut);
  152. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  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. int nprocs1 = cpu1 + gpu + gpu1;
  160. int nprocs2 = cpu2 + gpu + gpu2;
  161. unsigned n_all_gpus = gpu + gpu1 + gpu2;
  162. int procs[nprocs1];
  163. int i;
  164. int k = 0;
  165. for(i = 0; i < gpu; i++)
  166. {
  167. procs[k++] = i;
  168. printf("%d ", i);
  169. }
  170. for(i = gpu; i < gpu + gpu1; i++)
  171. {
  172. procs[k++] = i;
  173. printf("%d ", i);
  174. }
  175. for(i = n_all_gpus; i < n_all_gpus + cpu1; i++)
  176. {
  177. procs[k++] = i;
  178. printf("%d ", i);
  179. }
  180. printf("\n ");
  181. p1.ctx = starpu_sched_ctx_create("heft", procs, nprocs1, "sched_ctx1");
  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("%d ", i);
  191. }
  192. for(i = gpu + gpu1; i < gpu + gpu1 + gpu2; i++)
  193. {
  194. procs2[k++] = i;
  195. printf("%d ", i);
  196. }
  197. for(i = n_all_gpus + cpu1; i < n_all_gpus + cpu1 + cpu2; i++)
  198. {
  199. procs2[k++] = i;
  200. printf("%d ", i);
  201. }
  202. printf("\n");
  203. p2.ctx = starpu_sched_ctx_create("heft", procs2, nprocs2, "sched_ctx2");
  204. p1.the_other_ctx = (int)p2.ctx;
  205. p2.procs = procs2;
  206. p2.nprocs = nprocs2;
  207. }
  208. void parse_args_ctx(int argc, char **argv)
  209. {
  210. init();
  211. int i;
  212. for (i = 1; i < argc; i++)
  213. {
  214. if (strcmp(argv[i], "-size1") == 0)
  215. {
  216. char *argptr;
  217. size1 = strtol(argv[++i], &argptr, 10);
  218. }
  219. if (strcmp(argv[i], "-nblocks1") == 0)
  220. {
  221. char *argptr;
  222. nblocks1 = strtol(argv[++i], &argptr, 10);
  223. }
  224. if (strcmp(argv[i], "-size2") == 0)
  225. {
  226. char *argptr;
  227. size2 = strtol(argv[++i], &argptr, 10);
  228. }
  229. if (strcmp(argv[i], "-nblocks2") == 0)
  230. {
  231. char *argptr;
  232. nblocks2 = strtol(argv[++i], &argptr, 10);
  233. }
  234. if (strcmp(argv[i], "-cpu1") == 0)
  235. {
  236. char *argptr;
  237. cpu1 = strtol(argv[++i], &argptr, 10);
  238. }
  239. if (strcmp(argv[i], "-cpu2") == 0)
  240. {
  241. char *argptr;
  242. cpu2 = strtol(argv[++i], &argptr, 10);
  243. }
  244. if (strcmp(argv[i], "-gpu") == 0)
  245. {
  246. char *argptr;
  247. gpu = strtol(argv[++i], &argptr, 10);
  248. }
  249. if (strcmp(argv[i], "-gpu1") == 0)
  250. {
  251. char *argptr;
  252. gpu1 = strtol(argv[++i], &argptr, 10);
  253. }
  254. if (strcmp(argv[i], "-gpu2") == 0)
  255. {
  256. char *argptr;
  257. gpu2 = strtol(argv[++i], &argptr, 10);
  258. }
  259. }
  260. }