mm.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016 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. * This example illustrates how to distribute a pre-existing data structure to
  18. * a set of computing nodes using StarPU-MPI routines.
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #include <math.h>
  24. #include <starpu.h>
  25. #include <starpu_mpi.h>
  26. #include "helper.h"
  27. #define VERBOSE 0
  28. static int N = 16; /* Matrix size */
  29. static int BS = 4; /* Block size */
  30. #define NB ((N)/(BS)) /* Number of blocks */
  31. /* Matrices. Will be allocated as regular, linearized C arrays */
  32. static double *A = NULL; /* A will be partitioned as BS rows x N cols blocks */
  33. static double *B = NULL; /* B will be partitioned as N rows x BS cols blocks */
  34. static double *C = NULL; /* C will be partitioned as BS rows x BS cols blocks */
  35. /* Arrays of data handles for managing matrix blocks */
  36. static starpu_data_handle_t *A_h;
  37. static starpu_data_handle_t *B_h;
  38. static starpu_data_handle_t *C_h;
  39. static int comm_rank; /* mpi rank of the process */
  40. static int comm_size; /* size of the mpi session */
  41. static void alloc_matrices(void)
  42. {
  43. /* Regular 'malloc' can also be used instead, however, starpu_malloc make sure that
  44. * the area is allocated in suitably pinned memory to improve data transfers, especially
  45. * with CUDA */
  46. starpu_malloc((void **)&A, N*N*sizeof(double));
  47. starpu_malloc((void **)&B, N*N*sizeof(double));
  48. starpu_malloc((void **)&C, N*N*sizeof(double));
  49. }
  50. static void free_matrices(void)
  51. {
  52. starpu_free(A);
  53. starpu_free(B);
  54. starpu_free(C);
  55. }
  56. static void init_matrices(void)
  57. {
  58. int row,col;
  59. for (row = 0; row < N; row++)
  60. {
  61. for (col = 0; col < N; col++)
  62. {
  63. A[row*N+col] = (row==col)?2:0;
  64. B[row*N+col] = row*N+col;
  65. C[row*N+col] = 0;
  66. }
  67. }
  68. }
  69. #if VERBOSE
  70. static void disp_matrix(double *m)
  71. {
  72. int row,col;
  73. for (row = 0; row < N; row++)
  74. {
  75. for (col = 0; col < N; col++)
  76. {
  77. printf("\t%.2lf", m[row*N+col]);
  78. }
  79. printf("\n");
  80. }
  81. }
  82. #endif
  83. static void check_result(void)
  84. {
  85. int row,col;
  86. for (row = 0; row < N; row++)
  87. {
  88. for (col = 0; col < N; col++)
  89. {
  90. if (fabs(C[row*N+col] - 2*(row*N+col)) > 1.0)
  91. {
  92. fprintf(stderr, "check failed\n");
  93. exit(1);
  94. }
  95. }
  96. }
  97. #if VERBOSE
  98. printf("success\n");
  99. #endif
  100. }
  101. /* Register the matrix blocks to StarPU and to StarPU-MPI */
  102. static void register_matrices()
  103. {
  104. A_h = calloc(NB, sizeof(starpu_data_handle_t));
  105. B_h = calloc(NB, sizeof(starpu_data_handle_t));
  106. C_h = calloc(NB*NB, sizeof(starpu_data_handle_t));
  107. /* Memory region, where the data being registered resides.
  108. * In this example, all blocks are allocated by node 0, thus
  109. * - node 0 specifies STARPU_MAIN_RAM to indicate that it owns the block in its main memory
  110. * - nodes !0 specify -1 to indicate that they don't have a copy of the block initially
  111. */
  112. int mr = (comm_rank == 0) ? STARPU_MAIN_RAM : -1;
  113. /* mpi tag used for the block */
  114. int tag = 0;
  115. int b_row,b_col;
  116. for (b_row = 0; b_row < NB; b_row++)
  117. {
  118. /* Register a block to StarPU */
  119. starpu_matrix_data_register(&A_h[b_row],
  120. mr,
  121. (comm_rank == 0)?(uintptr_t)(A+b_row*BS*N):0, N, N, BS,
  122. sizeof(double));
  123. /* Register a block to StarPU-MPI, specifying the mpi tag to use for transfering the block
  124. * and the rank of the owner node.
  125. *
  126. * Note: StarPU-MPI is an autonomous layer built on top of StarPU, hence the two separate
  127. * registration steps.
  128. */
  129. starpu_data_set_coordinates(A_h[b_row], 2, 0, b_row);
  130. starpu_mpi_data_register(A_h[b_row], tag++, 0);
  131. }
  132. for (b_col = 0; b_col < NB; b_col++)
  133. {
  134. starpu_matrix_data_register(&B_h[b_col],
  135. mr,
  136. (comm_rank == 0)?(uintptr_t)(B+b_col*BS):0, N, BS, N,
  137. sizeof(double));
  138. starpu_data_set_coordinates(B_h[b_col], 2, b_col, 0);
  139. starpu_mpi_data_register(B_h[b_col], tag++, 0);
  140. }
  141. for (b_row = 0; b_row < NB; b_row++)
  142. {
  143. for (b_col = 0; b_col < NB; b_col++)
  144. {
  145. starpu_matrix_data_register(&C_h[b_row*NB+b_col],
  146. mr,
  147. (comm_rank == 0)?(uintptr_t)(C+b_row*BS*N+b_col*BS):0, N, BS, BS,
  148. sizeof(double));
  149. starpu_data_set_coordinates(C_h[b_row*NB+b_col], 2, b_col, b_row);
  150. starpu_mpi_data_register(C_h[b_row*NB+b_col], tag++, 0);
  151. }
  152. }
  153. }
  154. /* Transfer ownership of the C matrix blocks following some user-defined distribution over the nodes.
  155. * Note: since C will be Write-accessed, it will implicitly define which node perform the task
  156. * associated to a given block. */
  157. static void distribute_matrix_C(void)
  158. {
  159. int b_row,b_col;
  160. for (b_row = 0; b_row < NB; b_row++)
  161. {
  162. for (b_col = 0; b_col < NB; b_col++)
  163. {
  164. starpu_data_handle_t h = C_h[b_row*NB+b_col];
  165. /* Select the node where the block should be computed. */
  166. int target_rank = (b_row+b_col)%comm_size;
  167. /* Move the block on to its new owner. */
  168. starpu_mpi_data_migrate(MPI_COMM_WORLD, h, target_rank);
  169. }
  170. }
  171. }
  172. /* Transfer ownership of the C matrix blocks back to node 0, for display purpose. This is not mandatory. */
  173. static void undistribute_matrix_C(void)
  174. {
  175. int b_row,b_col;
  176. for (b_row = 0; b_row < NB; b_row++)
  177. {
  178. for (b_col = 0; b_col < NB; b_col++)
  179. {
  180. starpu_data_handle_t h = C_h[b_row*NB+b_col];
  181. starpu_mpi_data_migrate(MPI_COMM_WORLD, h, 0);
  182. }
  183. }
  184. }
  185. /* Unregister matrices from the StarPU management. */
  186. static void unregister_matrices()
  187. {
  188. int b_row,b_col;
  189. for (b_row = 0; b_row < NB; b_row++)
  190. {
  191. starpu_data_unregister(A_h[b_row]);
  192. }
  193. for (b_col = 0; b_col < NB; b_col++)
  194. {
  195. starpu_data_unregister(B_h[b_col]);
  196. }
  197. for (b_row = 0; b_row < NB; b_row++)
  198. {
  199. for (b_col = 0; b_col < NB; b_col++)
  200. {
  201. starpu_data_unregister(C_h[b_row*NB+b_col]);
  202. }
  203. }
  204. free(A_h);
  205. free(B_h);
  206. free(C_h);
  207. }
  208. /* Perform the actual computation. In a real-life case, this would rather call a BLAS 'gemm' routine
  209. * instead. */
  210. static void cpu_mult(void *handles[], STARPU_ATTRIBUTE_UNUSED void *arg)
  211. {
  212. double *block_A = (double *)STARPU_MATRIX_GET_PTR(handles[0]);
  213. double *block_B = (double *)STARPU_MATRIX_GET_PTR(handles[1]);
  214. double *block_C = (double *)STARPU_MATRIX_GET_PTR(handles[2]);
  215. unsigned n_col_A = STARPU_MATRIX_GET_NX(handles[0]);
  216. unsigned n_col_B = STARPU_MATRIX_GET_NX(handles[1]);
  217. unsigned n_col_C = STARPU_MATRIX_GET_NX(handles[2]);
  218. unsigned n_row_A = STARPU_MATRIX_GET_NY(handles[0]);
  219. unsigned n_row_B = STARPU_MATRIX_GET_NY(handles[1]);
  220. unsigned n_row_C = STARPU_MATRIX_GET_NY(handles[2]);
  221. unsigned ld_A = STARPU_MATRIX_GET_LD(handles[0]);
  222. unsigned ld_B = STARPU_MATRIX_GET_LD(handles[1]);
  223. unsigned ld_C = STARPU_MATRIX_GET_LD(handles[2]);
  224. /* Sanity check, not needed in real life case */
  225. assert(n_col_C == n_col_B);
  226. assert(n_row_C == n_row_A);
  227. assert(n_col_A == n_row_B);
  228. unsigned i,j,k;
  229. for (k = 0; k < n_row_C; k++)
  230. {
  231. for (j = 0; j < n_col_C; j++)
  232. {
  233. for (i = 0; i < n_col_A; i++)
  234. {
  235. block_C[k*ld_C+j] += block_A[k*ld_A+i] * block_B[i*ld_B+j];
  236. }
  237. #if VERBOSE
  238. /* For illustration purpose, shows which node computed
  239. * the block in the decimal part of the cell */
  240. block_C[k*ld_C+j] += comm_rank / 100.0;
  241. #endif
  242. }
  243. }
  244. }
  245. /* Define a StarPU 'codelet' structure for the matrix multiply kernel above.
  246. * This structure enable specifying multiple implementations for the kernel (such as CUDA or OpenCL versions)
  247. */
  248. static struct starpu_codelet gemm_cl =
  249. {
  250. .cpu_funcs = {cpu_mult}, /* cpu implementation(s) of the routine */
  251. .nbuffers = 3, /* number of data handles referenced by this routine */
  252. .modes = {STARPU_R, STARPU_R, STARPU_RW} /* access modes for each data handle */
  253. };
  254. int main(int argc, char *argv[])
  255. {
  256. /* Initializes the StarPU core */
  257. int ret = starpu_init(NULL);
  258. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  259. /* Initializes the StarPU-MPI layer */
  260. ret = starpu_mpi_init(&argc, &argv, 1);
  261. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  262. if (starpu_cpu_worker_get_count() == 0)
  263. {
  264. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  265. starpu_mpi_shutdown();
  266. starpu_shutdown();
  267. return STARPU_TEST_SKIPPED;
  268. }
  269. /* Parse the matrix size and block size optional args */
  270. if (argc > 1)
  271. {
  272. N = atoi(argv[1]);
  273. if (N < 1)
  274. {
  275. fprintf(stderr, "invalid matrix size\n");
  276. exit(1);
  277. }
  278. if (argc > 2)
  279. {
  280. BS = atoi(argv[2]);
  281. }
  282. if (BS < 1 || N % BS != 0)
  283. {
  284. fprintf(stderr, "invalid block size\n");
  285. exit(1);
  286. }
  287. }
  288. /* Get the process rank and session size */
  289. starpu_mpi_comm_rank(MPI_COMM_WORLD, &comm_rank);
  290. starpu_mpi_comm_size(MPI_COMM_WORLD, &comm_size);
  291. if (comm_rank == 0)
  292. {
  293. #if VERBOSE
  294. printf("N = %d\n", N);
  295. printf("BS = %d\n", BS);
  296. printf("NB = %d\n", NB);
  297. printf("comm_size = %d\n", comm_size);
  298. #endif
  299. /* In this example, node rank 0 performs all the memory allocations and initializations,
  300. * and the blocks are later distributed on the other nodes.
  301. * This is not mandatory however, and blocks could be allocated on other nodes right
  302. * from the beginning, depending on the application needs (in particular for the case
  303. * where the session wide data footprint is larger than a single node available memory. */
  304. alloc_matrices();
  305. init_matrices();
  306. }
  307. /* Register matrices to StarPU and StarPU-MPI */
  308. register_matrices();
  309. /* Distribute C blocks */
  310. distribute_matrix_C();
  311. int b_row,b_col;
  312. for (b_row = 0; b_row < NB; b_row++)
  313. {
  314. for (b_col = 0; b_col < NB; b_col++)
  315. {
  316. starpu_mpi_task_insert(MPI_COMM_WORLD, &gemm_cl,
  317. STARPU_R, A_h[b_row],
  318. STARPU_R, B_h[b_col],
  319. STARPU_RW, C_h[b_row*NB+b_col],
  320. 0);
  321. }
  322. }
  323. starpu_task_wait_for_all();
  324. undistribute_matrix_C();
  325. unregister_matrices();
  326. if (comm_rank == 0)
  327. {
  328. #if VERBOSE
  329. disp_matrix(C);
  330. #endif
  331. check_result();
  332. free_matrices();
  333. }
  334. starpu_mpi_shutdown();
  335. starpu_shutdown();
  336. return 0;
  337. }