tasks_size_overhead.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /* This benchmark creates a thousand tasks of the same (small) duration, with
  18. * various number of cpus and various durations.
  19. *
  20. * Use ./tasks_size_overhead.sh to generate a plot of the result.
  21. *
  22. * Thanks Martin Tillenius for the idea.
  23. */
  24. #include <sys/time.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. #include <starpu.h>
  28. #include "../helper.h"
  29. #define START 4
  30. #define STOP 4096
  31. #ifdef STARPU_QUICK_CHECK
  32. #define FACTOR 8
  33. #else
  34. #define FACTOR 2
  35. #endif
  36. starpu_data_handle_t data_handles[8];
  37. float *buffers[8];
  38. #ifdef STARPU_QUICK_CHECK
  39. static unsigned ntasks = 10;
  40. #else
  41. static unsigned ntasks = 1000;
  42. #endif
  43. static unsigned nbuffers = 0;
  44. struct starpu_task *tasks;
  45. void func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  46. {
  47. struct timeval tv1, tv2;
  48. unsigned n = (uintptr_t)arg;
  49. long usec = 0;
  50. gettimeofday(&tv1, NULL);
  51. do
  52. {
  53. gettimeofday(&tv2, NULL);
  54. if (tv2.tv_usec < tv1.tv_usec)
  55. {
  56. tv2.tv_usec += 1000000;
  57. tv2.tv_sec--;
  58. }
  59. usec = (tv2.tv_sec-tv1.tv_sec)*1000000
  60. + (tv2.tv_usec - tv1.tv_usec);
  61. }
  62. while (usec < n);
  63. }
  64. static struct starpu_codelet codelet =
  65. {
  66. .cpu_funcs = {func, NULL},
  67. .nbuffers = 0,
  68. .modes = {STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  69. };
  70. static void parse_args(int argc, char **argv)
  71. {
  72. int c;
  73. while ((c = getopt(argc, argv, "i:b:h")) != -1)
  74. switch(c)
  75. {
  76. case 'i':
  77. ntasks = atoi(optarg);
  78. break;
  79. case 'b':
  80. nbuffers = atoi(optarg);
  81. codelet.nbuffers = nbuffers;
  82. break;
  83. case 'h':
  84. fprintf(stderr, "Usage: %s [-i ntasks] [-b nbuffers] [-h]\n", argv[0]);
  85. break;
  86. }
  87. }
  88. int main(int argc, char **argv)
  89. {
  90. int ret;
  91. unsigned i;
  92. unsigned size;
  93. unsigned totcpus, ncpus;
  94. double timing;
  95. struct timeval start;
  96. struct timeval end;
  97. struct starpu_conf conf;
  98. unsigned buffer;
  99. parse_args(argc, argv);
  100. /* Get number of CPUs */
  101. starpu_conf_init(&conf);
  102. conf.ncuda = 0;
  103. conf.nopencl = 0;
  104. ret = starpu_init(&conf);
  105. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  107. totcpus = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  108. starpu_shutdown();
  109. /* Allocate data */
  110. for (buffer = 0; buffer < nbuffers; buffer++)
  111. buffers[buffer] = (float *) malloc(16*sizeof(float));
  112. tasks = (struct starpu_task *) calloc(1, ntasks*sizeof(struct starpu_task));
  113. /* Emit headers and compute raw tasks speed */
  114. FPRINTF(stdout, "# tasks : %u buffers : %u\n", ntasks, nbuffers);
  115. FPRINTF(stdout, "# ncpus\t");
  116. for (size = START; size <= STOP; size *= FACTOR)
  117. FPRINTF(stdout, "%u iters(us)\ttotal(s)\t", size);
  118. FPRINTF(stdout, "\n");
  119. FPRINTF(stdout, "\"seq\"\t");
  120. for (size = START; size <= STOP; size *= FACTOR)
  121. {
  122. double dstart, dend;
  123. dstart = starpu_timing_now();
  124. for (i = 0; i < ntasks; i++)
  125. func(NULL, (void*) (uintptr_t) size);
  126. dend = starpu_timing_now();
  127. FPRINTF(stdout, "%.0f \t%f\t", (dend-dstart)/ntasks, (dend-dstart)/1000000);
  128. }
  129. FPRINTF(stdout, "\n");
  130. fflush(stdout);
  131. /* For each number of cpus, benchmark */
  132. for (ncpus= 1; ncpus <= totcpus; ncpus++)
  133. {
  134. FPRINTF(stdout, "%u\t", ncpus);
  135. fflush(stdout);
  136. conf.ncpus = ncpus;
  137. ret = starpu_init(&conf);
  138. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  140. for (buffer = 0; buffer < nbuffers; buffer++)
  141. starpu_vector_data_register(&data_handles[buffer], STARPU_MAIN_RAM, (uintptr_t)buffers[buffer], 16, sizeof(float));
  142. for (size = START; size <= STOP; size *= FACTOR)
  143. {
  144. /* submit tasks */
  145. gettimeofday(&start, NULL);
  146. for (i = 0; i < ntasks; i++)
  147. {
  148. starpu_task_init(&tasks[i]);
  149. tasks[i].callback_func = NULL;
  150. tasks[i].cl = &codelet;
  151. tasks[i].cl_arg = (void*) (uintptr_t) size;
  152. tasks[i].synchronous = 0;
  153. /* we have 8 buffers at most */
  154. for (buffer = 0; buffer < nbuffers; buffer++)
  155. {
  156. tasks[i].handles[buffer] = data_handles[buffer];
  157. }
  158. ret = starpu_task_submit(&tasks[i]);
  159. if (ret == -ENODEV) goto enodev;
  160. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task");
  161. }
  162. ret = starpu_task_wait_for_all();
  163. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  164. gettimeofday(&end, NULL);
  165. for (i = 0; i < ntasks; i++)
  166. starpu_task_clean(&tasks[i]);
  167. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  168. FPRINTF(stdout, "%u\t%f\t", size, timing/1000000);
  169. fflush(stdout);
  170. {
  171. char *output_dir = getenv("STARPU_BENCH_DIR");
  172. char *bench_id = getenv("STARPU_BENCH_ID");
  173. if (output_dir && bench_id)
  174. {
  175. char file[1024];
  176. FILE *f;
  177. sprintf(file, "%s/tasks_size_overhead_total.dat", output_dir);
  178. f = fopen(file, "a");
  179. fprintf(f, "%s\t%f\n", bench_id, timing/1000000);
  180. fclose(f);
  181. }
  182. }
  183. }
  184. for (buffer = 0; buffer < nbuffers; buffer++)
  185. {
  186. starpu_data_unregister(data_handles[buffer]);
  187. }
  188. starpu_shutdown();
  189. FPRINTF(stdout, "\n");
  190. fflush(stdout);
  191. }
  192. free(tasks);
  193. return EXIT_SUCCESS;
  194. enodev:
  195. fprintf(stderr, "WARNING: No one can execute this task\n");
  196. /* yes, we do not perform the computation but we did detect that no one
  197. * could perform the kernel, so this is not an error from StarPU */
  198. starpu_shutdown();
  199. free(tasks);
  200. return STARPU_TEST_SKIPPED;
  201. }