mm.c 10 KB

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