tasks_size_overhead.c 5.6 KB

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