tasks_size_overhead.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 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. static void func(void *descr[] __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. .where = STARPU_CPU,
  67. .cpu_funcs = {func, NULL},
  68. .nbuffers = 0,
  69. .modes = {STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R}
  70. };
  71. static void parse_args(int argc, char **argv)
  72. {
  73. int c;
  74. while ((c = getopt(argc, argv, "i:b:h")) != -1)
  75. switch(c)
  76. {
  77. case 'i':
  78. ntasks = atoi(optarg);
  79. break;
  80. case 'b':
  81. nbuffers = atoi(optarg);
  82. codelet.nbuffers = nbuffers;
  83. break;
  84. case 'h':
  85. fprintf(stderr, "Usage: %s [-i ntasks] [-b nbuffers] [-h]\n", argv[0]);
  86. break;
  87. }
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. int ret;
  92. unsigned i;
  93. unsigned size;
  94. unsigned totcpus, ncpus;
  95. double timing;
  96. struct timeval start;
  97. struct timeval end;
  98. struct starpu_conf conf;
  99. unsigned buffer;
  100. parse_args(argc, argv);
  101. /* Get number of CPUs */
  102. starpu_conf_init(&conf);
  103. conf.ncuda = 0;
  104. conf.nopencl = 0;
  105. ret = starpu_init(&conf);
  106. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  108. totcpus = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  109. starpu_shutdown();
  110. /* Allocate data */
  111. for (buffer = 0; buffer < nbuffers; buffer++)
  112. buffers[buffer] = (float *) malloc(16*sizeof(float));
  113. tasks = (struct starpu_task *) calloc(1, ntasks*sizeof(struct starpu_task));
  114. /* Emit headers and compute raw tasks speed */
  115. FPRINTF(stdout, "# tasks : %u buffers : %u\n", ntasks, nbuffers);
  116. FPRINTF(stdout, "# ncpus\t");
  117. for (size = START; size <= STOP; size *= FACTOR)
  118. FPRINTF(stdout, "%u iters(us)\ttotal(s)\t", size);
  119. FPRINTF(stdout, "\n");
  120. FPRINTF(stdout, "\"seq\"\t");
  121. for (size = START; size <= STOP; size *= FACTOR)
  122. {
  123. double dstart, dend;
  124. dstart = starpu_timing_now();
  125. for (i = 0; i < ntasks; i++)
  126. func(NULL, (void*) (uintptr_t) size);
  127. dend = starpu_timing_now();
  128. FPRINTF(stdout, "%.0f \t%f\t", (dend-dstart)/ntasks, (dend-dstart)/1000000);
  129. }
  130. FPRINTF(stdout, "\n");
  131. fflush(stdout);
  132. /* For each number of cpus, benchmark */
  133. for (ncpus= 1; ncpus <= totcpus; ncpus++)
  134. {
  135. FPRINTF(stdout, "%u\t", ncpus);
  136. fflush(stdout);
  137. conf.ncpus = ncpus;
  138. ret = starpu_init(&conf);
  139. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  141. for (buffer = 0; buffer < nbuffers; buffer++)
  142. starpu_vector_data_register(&data_handles[buffer], 0, (uintptr_t)buffers[buffer], 16, sizeof(float));
  143. for (size = START; size <= STOP; size *= FACTOR)
  144. {
  145. /* submit tasks */
  146. gettimeofday(&start, NULL);
  147. for (i = 0; i < ntasks; i++)
  148. {
  149. starpu_task_init(&tasks[i]);
  150. tasks[i].callback_func = NULL;
  151. tasks[i].cl = &codelet;
  152. tasks[i].cl_arg = (void*) (uintptr_t) size;
  153. tasks[i].synchronous = 0;
  154. /* we have 8 buffers at most */
  155. for (buffer = 0; buffer < nbuffers; buffer++)
  156. {
  157. tasks[i].handles[buffer] = data_handles[buffer];
  158. }
  159. ret = starpu_task_submit(&tasks[i]);
  160. if (ret == -ENODEV) goto enodev;
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task");
  162. }
  163. ret = starpu_task_wait_for_all();
  164. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  165. gettimeofday(&end, NULL);
  166. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  167. FPRINTF(stdout, "%u\t%f\t", size, timing/1000000);
  168. fflush(stdout);
  169. {
  170. char *output_dir = getenv("STARPU_BENCH_DIR");
  171. char *bench_id = getenv("STARPU_BENCH_ID");
  172. if (output_dir && bench_id)
  173. {
  174. char file[1024];
  175. FILE *f;
  176. sprintf(file, "%s/tasks_size_overhead_total.dat", output_dir);
  177. f = fopen(file, "a");
  178. fprintf(f, "%s\t%f\n", bench_id, timing/1000000);
  179. fclose(f);
  180. }
  181. }
  182. }
  183. for (buffer = 0; buffer < nbuffers; buffer++)
  184. {
  185. starpu_data_unregister(data_handles[buffer]);
  186. }
  187. starpu_shutdown();
  188. FPRINTF(stdout, "\n");
  189. }
  190. free(tasks);
  191. return EXIT_SUCCESS;
  192. enodev:
  193. fprintf(stderr, "WARNING: No one can execute this task\n");
  194. /* yes, we do not perform the computation but we did detect that no one
  195. * could perform the kernel, so this is not an error from StarPU */
  196. starpu_shutdown();
  197. free(tasks);
  198. return STARPU_TEST_SKIPPED;
  199. }