bandwidth.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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;
  40. void *dst;
  41. unsigned i;
  42. double start, stop;
  43. int ret;
  44. ret = posix_memalign(&src, getpagesize(), size);
  45. STARPU_ASSERT(ret == 0);
  46. ret = posix_memalign(&dst, getpagesize(), size);
  47. STARPU_ASSERT(ret == 0);
  48. memset(src, 0, size);
  49. STARPU_PTHREAD_BARRIER_WAIT(&barrier);
  50. start = starpu_timing_now();
  51. for (i = 0; i < iter; i++)
  52. memcpy(dst, src, size);
  53. stop = starpu_timing_now();
  54. STARPU_PTHREAD_BARRIER_WAIT(&barrier);
  55. result[starpu_worker_get_id()] = (size*iter) / (stop - start);
  56. free(src);
  57. free(dst);
  58. }
  59. static struct starpu_codelet bw_codelet =
  60. {
  61. .cpu_funcs = {bw_func},
  62. .model = NULL,
  63. .nbuffers = 0,
  64. };
  65. static void usage(char **argv)
  66. {
  67. fprintf(stderr, "Usage: %s [-n iter] [-s size (MB)] [-i increment] [-a]\n", argv[0]);
  68. fprintf(stderr, "\t-n iter\tNumber of iterations\n");
  69. fprintf(stderr, "\t-s size\tBuffer size in MB\n");
  70. fprintf(stderr, "\t-i increment\tCpu number increment\n");
  71. fprintf(stderr, "\t-a\tDo not run the alone test\n");
  72. exit(EXIT_FAILURE);
  73. }
  74. static void parse_args(int argc, char **argv)
  75. {
  76. int c;
  77. while ((c = getopt(argc, argv, "n:s:c:ah")) != -1)
  78. switch(c)
  79. {
  80. case 'n':
  81. iter = atoi(optarg);
  82. break;
  83. case 's':
  84. size = (long)atoi(optarg) << 20;
  85. break;
  86. case 'c':
  87. cpustep = atoi(optarg);
  88. break;
  89. case 'a':
  90. noalone = 1;
  91. break;
  92. case 'h':
  93. usage(argv);
  94. break;
  95. }
  96. }
  97. static float bench(int *argc, char ***argv, unsigned nbusy, unsigned nidle)
  98. {
  99. int ret;
  100. unsigned i;
  101. struct starpu_conf conf;
  102. float bw;
  103. starpu_conf_init(&conf);
  104. conf.ncuda = 0;
  105. conf.nopencl = 0;
  106. conf.nmic = 0;
  107. conf.nmpi_ms = 0;
  108. conf.ncpus = nbusy + nidle;
  109. ret = starpu_initialize(&conf, argc, argv);
  110. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  112. STARPU_PTHREAD_BARRIER_INIT(&barrier, NULL, nbusy);
  113. for (i = 0; i < nbusy; i++)
  114. {
  115. struct starpu_task *task = starpu_task_create();
  116. task->cl = &bw_codelet;
  117. task->execute_on_a_specific_worker = 1;
  118. task->workerid = i;
  119. ret = starpu_task_submit(task);
  120. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  121. }
  122. starpu_task_wait_for_all();
  123. starpu_shutdown();
  124. for (bw = 0., i = 0; i < nbusy; i++)
  125. {
  126. bw += result[i];
  127. }
  128. return bw;
  129. }
  130. int main(int argc, char **argv)
  131. {
  132. int ret;
  133. unsigned n;
  134. struct starpu_conf conf;
  135. float alone, idle;
  136. parse_args(argc, argv);
  137. starpu_conf_init(&conf);
  138. conf.ncuda = 0;
  139. conf.nopencl = 0;
  140. conf.nmic = 0;
  141. conf.nmpi_ms = 0;
  142. ret = starpu_initialize(&conf, &argc, &argv);
  143. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  144. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  145. ncpus = starpu_cpu_worker_get_count();
  146. starpu_shutdown();
  147. result = malloc(ncpus * sizeof(result[0]));
  148. printf("# nw\talone\t\t+idle\t\tidle efficiency\n");
  149. for (n = 1; n <= ncpus; n += cpustep)
  150. {
  151. if (noalone)
  152. alone = 0.;
  153. else
  154. alone = bench(&argc, &argv, n, 0);
  155. idle = bench(&argc, &argv, n, ncpus-n);
  156. printf("%d\t%f\t%f\t%f\n", n, alone/1000, idle/1000, idle*100/alone);
  157. }
  158. free(result);
  159. return EXIT_SUCCESS;
  160. enodev:
  161. fprintf(stderr, "WARNING: No one can execute this task\n");
  162. free(result);
  163. starpu_shutdown();
  164. return STARPU_TEST_SKIPPED;
  165. }