bandwidth.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Measure the memory bandwidth available to kernels depending on the number of
  22. * kernels and number of idle workers.
  23. */
  24. #ifdef STARPU_QUICK_CHECK
  25. static size_t size = 1024;
  26. static unsigned cpustep = 4;
  27. #else
  28. /* Must be bigger than available cache size per core, 64MiB should be enough */
  29. static size_t size = 64UL << 20;
  30. static unsigned cpustep = 1;
  31. #endif
  32. static unsigned noalone = 0;
  33. static unsigned iter = 30;
  34. static unsigned ncpus;
  35. static starpu_pthread_barrier_t barrier;
  36. static float *result;
  37. void bw_func(void *descr[], void *arg)
  38. {
  39. void *src = malloc(size);
  40. void *dst = malloc(size);
  41. unsigned i;
  42. double start, stop;
  43. memset(src, 0, size);
  44. STARPU_PTHREAD_BARRIER_WAIT(&barrier);
  45. start = starpu_timing_now();
  46. for (i = 0; i < iter; i++)
  47. memcpy(dst, src, size);
  48. stop = starpu_timing_now();
  49. STARPU_PTHREAD_BARRIER_WAIT(&barrier);
  50. result[starpu_worker_get_id()] = (size*iter) / (stop - start);
  51. free(src);
  52. free(dst);
  53. }
  54. static struct starpu_codelet bw_codelet =
  55. {
  56. .cpu_funcs = {bw_func},
  57. .model = NULL,
  58. .nbuffers = 0,
  59. };
  60. static void usage(char **argv)
  61. {
  62. fprintf(stderr, "Usage: %s [-n iter] [-s size (MB)] [-i increment] [-a]\n", argv[0]);
  63. fprintf(stderr, "\t-n iter\tNumber of iterations\n");
  64. fprintf(stderr, "\t-s size\tBuffer size in MB\n");
  65. fprintf(stderr, "\t-i increment\tCpu number increment\n");
  66. fprintf(stderr, "\t-a\tDo not run the alone test\n");
  67. exit(EXIT_FAILURE);
  68. }
  69. static void parse_args(int argc, char **argv)
  70. {
  71. int c;
  72. while ((c = getopt(argc, argv, "n:s:c:ah")) != -1)
  73. switch(c)
  74. {
  75. case 'n':
  76. iter = atoi(optarg);
  77. break;
  78. case 's':
  79. size = (long)atoi(optarg) << 20;
  80. break;
  81. case 'c':
  82. cpustep = atoi(optarg);
  83. break;
  84. case 'a':
  85. noalone = 1;
  86. break;
  87. case 'h':
  88. usage(argv);
  89. break;
  90. }
  91. }
  92. static float bench(int *argc, char ***argv, unsigned nbusy, unsigned nidle)
  93. {
  94. int ret;
  95. unsigned i;
  96. struct starpu_conf conf;
  97. float bw;
  98. starpu_conf_init(&conf);
  99. conf.ncuda = 0;
  100. conf.nopencl = 0;
  101. conf.nmic = 0;
  102. conf.nmpi_ms = 0;
  103. conf.ncpus = nbusy + nidle;
  104. ret = starpu_initialize(&conf, argc, argv);
  105. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  107. STARPU_PTHREAD_BARRIER_INIT(&barrier, NULL, nbusy);
  108. for (i = 0; i < nbusy; i++)
  109. {
  110. struct starpu_task *task = starpu_task_create();
  111. task->cl = &bw_codelet;
  112. task->execute_on_a_specific_worker = 1;
  113. task->workerid = i;
  114. ret = starpu_task_submit(task);
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  116. }
  117. starpu_task_wait_for_all();
  118. starpu_shutdown();
  119. for (bw = 0., i = 0; i < nbusy; i++)
  120. {
  121. bw += result[i];
  122. }
  123. return bw;
  124. }
  125. int main(int argc, char **argv)
  126. {
  127. int ret;
  128. unsigned n;
  129. struct starpu_conf conf;
  130. float alone, idle;
  131. parse_args(argc, argv);
  132. starpu_conf_init(&conf);
  133. conf.ncuda = 0;
  134. conf.nopencl = 0;
  135. conf.nmic = 0;
  136. conf.nmpi_ms = 0;
  137. ret = starpu_initialize(&conf, &argc, &argv);
  138. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  140. ncpus = starpu_cpu_worker_get_count();
  141. starpu_shutdown();
  142. result = malloc(ncpus * sizeof(result[0]));
  143. printf("# nw\talone\t\t+idle\t\tidle efficiency\n");
  144. for (n = 1; n <= ncpus; n += cpustep)
  145. {
  146. if (noalone)
  147. alone = 0.;
  148. else
  149. alone = bench(&argc, &argv, n, 0);
  150. idle = bench(&argc, &argv, n, ncpus-n);
  151. printf("%d\t%f\t%f\t%f\n", n, alone/1000, idle/1000, idle*100/alone);
  152. }
  153. free(result);
  154. return EXIT_SUCCESS;
  155. enodev:
  156. fprintf(stderr, "WARNING: No one can execute this task\n");
  157. free(result);
  158. starpu_shutdown();
  159. return STARPU_TEST_SKIPPED;
  160. }