sendrecv_gemm_bench.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /*
  17. * Simple *not distributed* parallel GEMM implementation and sendrecv bench at the same time.
  18. *
  19. * This bench is a merge of mpi/tests/sendrecv_bench and examples/mult/sgemm
  20. *
  21. * A *non-distributed* GEMM is computed on each node, while a sendrecv bench is running,
  22. * completely independently. The goal is to measure the impact of worker computations on
  23. * communications.
  24. *
  25. * Use the -nblocks parameter to define the matrix size (matrix size = nblocks * 320), such as
  26. * the GEMM finishes after the sendrecv bench.
  27. */
  28. #include <limits.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <starpu_mpi.h>
  33. #include <starpu_fxt.h>
  34. #include "helper.h"
  35. #include "abstract_sendrecv_bench.h"
  36. #include "gemm_helper.h"
  37. static int mpi_rank;
  38. static starpu_pthread_barrier_t thread_barrier;
  39. static void* comm_thread_func(void* arg)
  40. {
  41. if (comm_thread_cpuid < 0)
  42. {
  43. comm_thread_cpuid = starpu_get_next_bindid(STARPU_THREAD_ACTIVE, NULL, 0);
  44. }
  45. if (starpu_bind_thread_on(comm_thread_cpuid, 0, "Comm") < 0)
  46. {
  47. char hostname[65];
  48. gethostname(hostname, sizeof(hostname));
  49. fprintf(stderr, "[%s] No core was available for the comm thread. You should increase STARPU_RESERVE_NCPU or decrease STARPU_NCPU\n", hostname);
  50. }
  51. sendrecv_bench(mpi_rank, &thread_barrier, /* half-duplex communications */ 0);
  52. return NULL;
  53. }
  54. void parse_args(int argc, char **argv)
  55. {
  56. int i;
  57. for (i = 1; i < argc; i++)
  58. {
  59. if (strcmp(argv[i], "-nblocks") == 0)
  60. {
  61. char *argptr;
  62. nslices = strtol(argv[++i], &argptr, 10);
  63. matrix_dim = 320 * nslices;
  64. }
  65. else if (strcmp(argv[i], "-size") == 0)
  66. {
  67. char *argptr;
  68. unsigned matrix_dim_tmp = strtol(argv[++i], &argptr, 10);
  69. if (matrix_dim_tmp % 320 != 0)
  70. {
  71. fprintf(stderr, "Matrix size has to be a multiple of 320\n");
  72. }
  73. else
  74. {
  75. matrix_dim = matrix_dim_tmp;
  76. nslices = matrix_dim / 320;
  77. }
  78. }
  79. else if (strcmp(argv[i], "-check") == 0)
  80. {
  81. check = 1;
  82. }
  83. else if (strcmp(argv[i], "-comm-thread-cpuid") == 0)
  84. {
  85. comm_thread_cpuid = atoi(argv[++i]);
  86. }
  87. else if (strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  88. {
  89. fprintf(stderr,"Usage: %s [-nblocks n] [-size size] [-check] [-comm-thread-cpuid cpuid]\n", argv[0]);
  90. fprintf(stderr,"Currently selected: matrix size: %u - %u blocks\n", matrix_dim, nslices);
  91. fprintf(stderr, "Use -comm-thread-cpuid to specifiy where to bind the comm benchmarking thread\n");
  92. exit(EXIT_SUCCESS);
  93. }
  94. else
  95. {
  96. fprintf(stderr,"Unrecognized option %s\n", argv[i]);
  97. exit(EXIT_FAILURE);
  98. }
  99. }
  100. }
  101. int main(int argc, char **argv)
  102. {
  103. double start, end;
  104. int ret, worldsize;
  105. starpu_pthread_t comm_thread;
  106. char hostname[255];
  107. gethostname(hostname, 255);
  108. parse_args(argc, argv);
  109. starpu_fxt_autostart_profiling(0);
  110. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  111. if (ret == -ENODEV)
  112. return 77;
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  114. starpu_mpi_comm_rank(MPI_COMM_WORLD, &mpi_rank);
  115. starpu_mpi_comm_size(MPI_COMM_WORLD, &worldsize);
  116. if (worldsize < 2)
  117. {
  118. if (mpi_rank == 0)
  119. FPRINTF(stderr, "We need 2 processes.\n");
  120. starpu_mpi_shutdown();
  121. return STARPU_TEST_SKIPPED;
  122. }
  123. STARPU_PTHREAD_BARRIER_INIT(&thread_barrier, NULL, 2);
  124. // Start comm thread, benchmarking sendrecv:
  125. STARPU_PTHREAD_CREATE(&comm_thread, NULL, comm_thread_func, NULL);
  126. // Main thread will submit GEMM tasks:
  127. gemm_alloc_data();
  128. if (mpi_rank == 0)
  129. {
  130. printf("# node\tx\ty\tz\tms\tGFlops\n");
  131. }
  132. starpu_pause();
  133. if(gemm_init_data() == -ENODEV || gemm_submit_tasks() == -ENODEV)
  134. goto enodev;
  135. starpu_mpi_barrier(MPI_COMM_WORLD);
  136. starpu_fxt_start_profiling();
  137. STARPU_PTHREAD_BARRIER_WAIT(&thread_barrier);
  138. start = starpu_timing_now();
  139. starpu_resume();
  140. starpu_task_wait_for_all();
  141. end = starpu_timing_now();
  142. starpu_pause(); // Pause not to disturb comm thread if it isn't done
  143. double timing = end - start;
  144. double flops = 2.0*((unsigned long long)matrix_dim) * ((unsigned long long)matrix_dim)*((unsigned long long)matrix_dim);
  145. printf("%s\t%u\t%u\t%u\t%.0f\t%.1f\n", hostname, matrix_dim, matrix_dim, matrix_dim, timing/1000.0, flops/timing/1000.0);
  146. enodev:
  147. gemm_release();
  148. // Wait comm thread:
  149. STARPU_PTHREAD_JOIN(comm_thread, NULL);
  150. STARPU_PTHREAD_BARRIER_DESTROY(&thread_barrier);
  151. starpu_fxt_stop_profiling();
  152. starpu_resume();
  153. starpu_mpi_shutdown();
  154. return ret;
  155. }