tasks_size_overhead.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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 <stdio.h>
  25. #include <unistd.h>
  26. #include <starpu.h>
  27. #include "../helper.h"
  28. #define START 4
  29. #define STOP 4096
  30. #ifdef STARPU_QUICK_CHECK
  31. #define FACTOR 8
  32. #else
  33. #define FACTOR 2
  34. #endif
  35. #ifdef STARPU_QUICK_CHECK
  36. static unsigned ntasks = 10;
  37. #else
  38. static unsigned ntasks = 1000;
  39. #endif
  40. static unsigned nbuffers = 0;
  41. struct starpu_task *tasks;
  42. void func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg)
  43. {
  44. double tv1, tv2;
  45. unsigned n = (uintptr_t)arg;
  46. long usec = 0;
  47. tv1 = starpu_timing_now();
  48. do
  49. {
  50. tv2 = starpu_timing_now();
  51. usec = tv2 - tv1;
  52. }
  53. while (usec < n);
  54. }
  55. static struct starpu_codelet codelet =
  56. {
  57. .cpu_funcs = {func},
  58. .nbuffers = 0,
  59. .modes = {STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  60. };
  61. static void parse_args(int argc, char **argv)
  62. {
  63. int c;
  64. while ((c = getopt(argc, argv, "i:b:h")) != -1)
  65. switch(c)
  66. {
  67. case 'i':
  68. ntasks = atoi(optarg);
  69. break;
  70. case 'b':
  71. nbuffers = atoi(optarg);
  72. codelet.nbuffers = nbuffers;
  73. break;
  74. case 'h':
  75. fprintf(stderr, "Usage: %s [-i ntasks] [-b nbuffers] [-h]\n", argv[0]);
  76. exit(EXIT_SUCCESS);
  77. break;
  78. }
  79. }
  80. int main(int argc, char **argv)
  81. {
  82. int ret;
  83. unsigned i;
  84. unsigned size;
  85. unsigned totcpus, ncpus;
  86. double timing;
  87. double start;
  88. double end;
  89. struct starpu_conf conf;
  90. unsigned buffer;
  91. parse_args(argc, argv);
  92. /* Get number of CPUs */
  93. starpu_conf_init(&conf);
  94. conf.ncuda = 0;
  95. conf.nopencl = 0;
  96. ret = starpu_init(&conf);
  97. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  99. totcpus = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  100. starpu_shutdown();
  101. float *buffers[nbuffers];
  102. /* Allocate data */
  103. for (buffer = 0; buffer < nbuffers; buffer++)
  104. buffers[buffer] = (float *) malloc(16*sizeof(float));
  105. tasks = (struct starpu_task *) calloc(1, ntasks*sizeof(struct starpu_task));
  106. /* Emit headers and compute raw tasks speed */
  107. FPRINTF(stdout, "# tasks : %u buffers : %u\n", ntasks, nbuffers);
  108. FPRINTF(stdout, "# ncpus\t");
  109. for (size = START; size <= STOP; size *= FACTOR)
  110. FPRINTF(stdout, "%u iters(us)\ttotal(s)\t", size);
  111. FPRINTF(stdout, "\n");
  112. FPRINTF(stdout, "\"seq\"\t");
  113. for (size = START; size <= STOP; size *= FACTOR)
  114. {
  115. double dstart, dend;
  116. dstart = starpu_timing_now();
  117. for (i = 0; i < ntasks; i++)
  118. func(NULL, (void*) (uintptr_t) size);
  119. dend = starpu_timing_now();
  120. FPRINTF(stdout, "%.0f \t%f\t", (dend-dstart)/ntasks, (dend-dstart)/1000000);
  121. }
  122. FPRINTF(stdout, "\n");
  123. fflush(stdout);
  124. starpu_data_handle_t data_handles[nbuffers];
  125. /* For each number of cpus, benchmark */
  126. for (ncpus= 1; ncpus <= totcpus; ncpus++)
  127. {
  128. FPRINTF(stdout, "%u\t", ncpus);
  129. fflush(stdout);
  130. conf.ncpus = ncpus;
  131. ret = starpu_init(&conf);
  132. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  133. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  134. for (buffer = 0; buffer < nbuffers; buffer++)
  135. starpu_vector_data_register(&data_handles[buffer], STARPU_MAIN_RAM, (uintptr_t)buffers[buffer], 16, sizeof(float));
  136. for (size = START; size <= STOP; size *= FACTOR)
  137. {
  138. /* submit tasks */
  139. start = starpu_timing_now();
  140. for (i = 0; i < ntasks; i++)
  141. {
  142. starpu_task_init(&tasks[i]);
  143. tasks[i].callback_func = NULL;
  144. tasks[i].cl = &codelet;
  145. tasks[i].cl_arg = (void*) (uintptr_t) size;
  146. tasks[i].synchronous = 0;
  147. if (nbuffers > STARPU_NMAXBUFS)
  148. {
  149. tasks[i].dyn_handles = malloc(nbuffers * sizeof(*data_handles));
  150. memcpy(tasks[i].dyn_handles, data_handles, nbuffers * sizeof(*data_handles));
  151. tasks[i].dyn_modes = malloc(nbuffers * sizeof(tasks[i].dyn_modes));
  152. for (buffer = 0; buffer < nbuffers; buffer++)
  153. tasks[i].dyn_modes[buffer] = STARPU_R;
  154. }
  155. else
  156. for (buffer = 0; buffer < nbuffers; buffer++)
  157. {
  158. tasks[i].handles[buffer] = data_handles[buffer];
  159. }
  160. ret = starpu_task_submit(&tasks[i]);
  161. if (ret == -ENODEV) goto enodev;
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task");
  163. }
  164. ret = starpu_task_wait_for_all();
  165. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  166. end = starpu_timing_now();
  167. for (i = 0; i < ntasks; i++)
  168. starpu_task_clean(&tasks[i]);
  169. timing = end - start;
  170. FPRINTF(stdout, "%u\t%f\t", size, timing/1000000);
  171. fflush(stdout);
  172. {
  173. char *output_dir = getenv("STARPU_BENCH_DIR");
  174. char *bench_id = getenv("STARPU_BENCH_ID");
  175. if (output_dir && bench_id)
  176. {
  177. char file[1024];
  178. FILE *f;
  179. sprintf(file, "%s/tasks_size_overhead_total.dat", output_dir);
  180. f = fopen(file, "a");
  181. fprintf(f, "%s\t%f\n", bench_id, timing/1000000);
  182. fclose(f);
  183. }
  184. }
  185. }
  186. for (buffer = 0; buffer < nbuffers; buffer++)
  187. {
  188. starpu_data_unregister(data_handles[buffer]);
  189. }
  190. starpu_shutdown();
  191. FPRINTF(stdout, "\n");
  192. fflush(stdout);
  193. }
  194. free(tasks);
  195. return EXIT_SUCCESS;
  196. enodev:
  197. fprintf(stderr, "WARNING: No one can execute this task\n");
  198. /* yes, we do not perform the computation but we did detect that no one
  199. * could perform the kernel, so this is not an error from StarPU */
  200. starpu_shutdown();
  201. free(tasks);
  202. return STARPU_TEST_SKIPPED;
  203. }