tasks_size_overhead.c 5.3 KB

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