tasks_size_overhead.c 5.7 KB

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