tasks_size_overhead.c 5.4 KB

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