datatypes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2014, 2015, 2016, 2017 CNRS
  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 <starpu_mpi.h>
  17. #include <stdlib.h>
  18. #include "helper.h"
  19. typedef void (*check_func)(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error);
  20. void check_void(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  21. {
  22. FPRINTF_MPI(stderr, "Success with void value\n");
  23. }
  24. void check_variable(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  25. {
  26. float *v_s, *v_r;
  27. STARPU_ASSERT(starpu_variable_get_elemsize(handle_s) == starpu_variable_get_elemsize(handle_r));
  28. v_s = (float *)starpu_variable_get_local_ptr(handle_s);
  29. v_r = (float *)starpu_variable_get_local_ptr(handle_r);
  30. if (*v_s == *v_r)
  31. {
  32. FPRINTF_MPI(stderr, "Success with variable value: %f == %f\n", *v_s, *v_r);
  33. }
  34. else
  35. {
  36. *error = 1;
  37. FPRINTF_MPI(stderr, "Error with variable value: %f != %f\n", *v_s, *v_r);
  38. }
  39. }
  40. void check_vector(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  41. {
  42. int i;
  43. int nx;
  44. int *v_r, *v_s;
  45. STARPU_ASSERT(starpu_vector_get_elemsize(handle_s) == starpu_vector_get_elemsize(handle_r));
  46. STARPU_ASSERT(starpu_vector_get_nx(handle_s) == starpu_vector_get_nx(handle_r));
  47. nx = starpu_vector_get_nx(handle_r);
  48. v_r = (int *)starpu_vector_get_local_ptr(handle_r);
  49. v_s = (int *)starpu_vector_get_local_ptr(handle_s);
  50. for(i=0 ; i<nx ; i++)
  51. {
  52. if (v_s[i] == v_r[i])
  53. {
  54. FPRINTF_MPI(stderr, "Success with vector[%d] value: %d == %d\n", i, v_s[i], v_r[i]);
  55. }
  56. else
  57. {
  58. *error = 1;
  59. FPRINTF_MPI(stderr, "Error with vector[%d] value: %d != %d\n", i, v_s[i], v_r[i]);
  60. }
  61. }
  62. }
  63. void check_matrix(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  64. {
  65. STARPU_ASSERT(starpu_matrix_get_elemsize(handle_s) == starpu_matrix_get_elemsize(handle_r));
  66. STARPU_ASSERT(starpu_matrix_get_nx(handle_s) == starpu_matrix_get_nx(handle_r));
  67. STARPU_ASSERT(starpu_matrix_get_ny(handle_s) == starpu_matrix_get_ny(handle_r));
  68. STARPU_ASSERT(starpu_matrix_get_local_ld(handle_s) == starpu_matrix_get_local_ld(handle_r));
  69. char *matrix_s = (char *)starpu_matrix_get_local_ptr(handle_s);
  70. char *matrix_r = (char *)starpu_matrix_get_local_ptr(handle_r);
  71. int nx = starpu_matrix_get_nx(handle_s);
  72. int ny = starpu_matrix_get_ny(handle_s);
  73. int ldy = starpu_matrix_get_local_ld(handle_s);
  74. int x, y;
  75. for(y=0 ; y<ny ; y++)
  76. {
  77. for(x=0 ; x<nx ; x++)
  78. {
  79. int index=(y*ldy)+x;
  80. if (matrix_s[index] == matrix_r[index])
  81. {
  82. FPRINTF_MPI(stderr, "Success with matrix[%d,%d --> %d] value: %c == %c\n", x, y, index, matrix_s[index], matrix_r[index]);
  83. }
  84. else
  85. {
  86. *error = 1;
  87. FPRINTF_MPI(stderr, "Error with matrix[%d,%d --> %d] value: %c != %c\n", x, y, index, matrix_s[index], matrix_r[index]);
  88. }
  89. }
  90. }
  91. }
  92. void check_block(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  93. {
  94. STARPU_ASSERT(starpu_block_get_elemsize(handle_s) == starpu_block_get_elemsize(handle_r));
  95. STARPU_ASSERT(starpu_block_get_nx(handle_s) == starpu_block_get_nx(handle_r));
  96. STARPU_ASSERT(starpu_block_get_ny(handle_s) == starpu_block_get_ny(handle_r));
  97. STARPU_ASSERT(starpu_block_get_nz(handle_s) == starpu_block_get_nz(handle_r));
  98. STARPU_ASSERT(starpu_block_get_local_ldy(handle_s) == starpu_block_get_local_ldy(handle_r));
  99. STARPU_ASSERT(starpu_block_get_local_ldz(handle_s) == starpu_block_get_local_ldz(handle_r));
  100. starpu_data_acquire(handle_s, STARPU_R);
  101. starpu_data_acquire(handle_r, STARPU_R);
  102. float *block_s = (float *)starpu_block_get_local_ptr(handle_s);
  103. float *block_r = (float *)starpu_block_get_local_ptr(handle_r);
  104. int nx = starpu_block_get_nx(handle_s);
  105. int ny = starpu_block_get_ny(handle_s);
  106. int nz = starpu_block_get_nz(handle_s);
  107. int ldy = starpu_block_get_local_ldy(handle_s);
  108. int ldz = starpu_block_get_local_ldz(handle_s);
  109. int x, y, z;
  110. for(z=0 ; z<nz ; z++)
  111. {
  112. for(y=0 ; y<ny ; y++)
  113. for(x=0 ; x<nx ; x++)
  114. {
  115. int index=(z*ldz)+(y*ldy)+x;
  116. if (block_s[index] == block_r[index])
  117. {
  118. FPRINTF_MPI(stderr, "Success with block[%d,%d,%d --> %d] value: %f == %f\n", x, y, z, index, block_s[index], block_r[index]);
  119. }
  120. else
  121. {
  122. *error = 1;
  123. FPRINTF_MPI(stderr, "Error with block[%d,%d,%d --> %d] value: %f != %f\n", x, y, z, index, block_s[index], block_r[index]);
  124. }
  125. }
  126. }
  127. starpu_data_release(handle_s);
  128. starpu_data_release(handle_r);
  129. }
  130. void check_bcsr(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  131. {
  132. STARPU_ASSERT(starpu_bcsr_get_elemsize(handle_s) == starpu_bcsr_get_elemsize(handle_r));
  133. STARPU_ASSERT(starpu_bcsr_get_nnz(handle_s) == starpu_bcsr_get_nnz(handle_r));
  134. STARPU_ASSERT(starpu_bcsr_get_nrow(handle_s) == starpu_bcsr_get_nrow(handle_r));
  135. STARPU_ASSERT(starpu_bcsr_get_firstentry(handle_s) == starpu_bcsr_get_firstentry(handle_r));
  136. STARPU_ASSERT(starpu_bcsr_get_r(handle_s) == starpu_bcsr_get_r(handle_r));
  137. STARPU_ASSERT(starpu_bcsr_get_c(handle_s) == starpu_bcsr_get_c(handle_r));
  138. // STARPU_ASSERT(starpu_bcsr_get_local_colind(handle_s) == starpu_bcsr_get_local_colind(handle_r));
  139. // STARPU_ASSERT(starpu_bcsr_get_local_rowptr(handle_s) == starpu_bcsr_get_local_rowptr(handle_r));
  140. starpu_data_acquire(handle_s, STARPU_R);
  141. starpu_data_acquire(handle_r, STARPU_R);
  142. int *bcsr_s = (int *)starpu_bcsr_get_local_nzval(handle_s);
  143. int *bcsr_r = (int *)starpu_bcsr_get_local_nzval(handle_r);
  144. int r = starpu_bcsr_get_r(handle_s);
  145. int c = starpu_bcsr_get_c(handle_s);
  146. int nnz = starpu_bcsr_get_nnz(handle_s);
  147. int x;
  148. for(x=0 ; x<r*c*nnz ; x++)
  149. {
  150. if (bcsr_s[x] == bcsr_r[x])
  151. {
  152. FPRINTF_MPI(stderr, "Success with bcsr[%d] value: %d == %d\n", x, bcsr_s[x], bcsr_r[x]);
  153. }
  154. else
  155. {
  156. *error = 1;
  157. FPRINTF_MPI(stderr, "Error with bcsr[%d] value: %d != %d\n", x, bcsr_s[x], bcsr_r[x]);
  158. }
  159. }
  160. starpu_data_release(handle_s);
  161. starpu_data_release(handle_r);
  162. }
  163. void send_recv_and_check(int rank, int node, starpu_data_handle_t handle_s, int tag_s, starpu_data_handle_t handle_r, int tag_r, int *error, check_func func)
  164. {
  165. int ret;
  166. MPI_Status status;
  167. if (rank == 0)
  168. {
  169. ret = starpu_mpi_send(handle_s, node, tag_s, MPI_COMM_WORLD);
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  171. ret = starpu_mpi_recv(handle_r, node, tag_r, MPI_COMM_WORLD, &status);
  172. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  173. assert(func);
  174. func(handle_s, handle_r, error);
  175. }
  176. else if (rank == 1)
  177. {
  178. ret = starpu_mpi_recv(handle_s, node, tag_s, MPI_COMM_WORLD, &status);
  179. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  180. ret = starpu_mpi_send(handle_s, node, tag_r, MPI_COMM_WORLD);
  181. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  182. }
  183. }
  184. void exchange_void(int rank, int *error)
  185. {
  186. STARPU_SKIP_IF_VALGRIND;
  187. if (rank == 0)
  188. {
  189. starpu_data_handle_t void_handle[2];
  190. starpu_void_data_register(&void_handle[0]);
  191. starpu_void_data_register(&void_handle[1]);
  192. send_recv_and_check(rank, 1, void_handle[0], 0x42, void_handle[1], 0x1337, error, check_void);
  193. starpu_data_unregister(void_handle[0]);
  194. starpu_data_unregister(void_handle[1]);
  195. }
  196. else if (rank == 1)
  197. {
  198. starpu_data_handle_t void_handle;
  199. starpu_void_data_register(&void_handle);
  200. send_recv_and_check(rank, 0, void_handle, 0x42, NULL, 0x1337, NULL, NULL);
  201. starpu_data_unregister(void_handle);
  202. }
  203. }
  204. void exchange_variable(int rank, int *error)
  205. {
  206. if (rank == 0)
  207. {
  208. float v = 42.12;
  209. starpu_data_handle_t variable_handle[2];
  210. starpu_variable_data_register(&variable_handle[0], STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(v));
  211. starpu_variable_data_register(&variable_handle[1], -1, (uintptr_t)NULL, sizeof(v));
  212. send_recv_and_check(rank, 1, variable_handle[0], 0x42, variable_handle[1], 0x1337, error, check_variable);
  213. starpu_data_unregister(variable_handle[0]);
  214. starpu_data_unregister(variable_handle[1]);
  215. }
  216. else if (rank == 1)
  217. {
  218. starpu_data_handle_t variable_handle;
  219. starpu_variable_data_register(&variable_handle, -1, (uintptr_t)NULL, sizeof(float));
  220. send_recv_and_check(rank, 0, variable_handle, 0x42, NULL, 0x1337, NULL, NULL);
  221. starpu_data_unregister(variable_handle);
  222. }
  223. }
  224. void exchange_vector(int rank, int *error)
  225. {
  226. if (rank == 0)
  227. {
  228. int vector[4] = {1, 2, 3, 4};
  229. starpu_data_handle_t vector_handle[2];
  230. starpu_vector_data_register(&vector_handle[0], STARPU_MAIN_RAM, (uintptr_t)vector, 4, sizeof(vector[0]));
  231. starpu_vector_data_register(&vector_handle[1], -1, (uintptr_t)NULL, 4, sizeof(vector[0]));
  232. send_recv_and_check(rank, 1, vector_handle[0], 0x43, vector_handle[1], 0x2337, error, check_vector);
  233. starpu_data_unregister(vector_handle[0]);
  234. starpu_data_unregister(vector_handle[1]);
  235. }
  236. else if (rank == 1)
  237. {
  238. starpu_data_handle_t vector_handle;
  239. starpu_vector_data_register(&vector_handle, -1, (uintptr_t)NULL, 4, sizeof(int));
  240. send_recv_and_check(rank, 0, vector_handle, 0x43, NULL, 0x2337, NULL, NULL);
  241. starpu_data_unregister(vector_handle);
  242. }
  243. }
  244. void exchange_matrix(int rank, int *error)
  245. {
  246. int nx=3;
  247. int ny=2;
  248. if (rank == 0)
  249. {
  250. char *matrix, n='a';
  251. int x, y;
  252. starpu_data_handle_t matrix_handle[2];
  253. matrix = (char*)malloc(nx*ny*sizeof(char));
  254. assert(matrix);
  255. for(y=0 ; y<ny ; y++)
  256. {
  257. for(x=0 ; x<nx ; x++)
  258. {
  259. matrix[(y*nx)+x] = n++;
  260. }
  261. }
  262. starpu_matrix_data_register(&matrix_handle[0], STARPU_MAIN_RAM, (uintptr_t)matrix, nx, nx, ny, sizeof(char));
  263. starpu_matrix_data_register(&matrix_handle[1], -1, (uintptr_t)NULL, nx, nx, ny, sizeof(char));
  264. send_recv_and_check(rank, 1, matrix_handle[0], 0x75, matrix_handle[1], 0x8555, error, check_matrix);
  265. starpu_data_unregister(matrix_handle[0]);
  266. starpu_data_unregister(matrix_handle[1]);
  267. free(matrix);
  268. }
  269. else if (rank == 1)
  270. {
  271. starpu_data_handle_t matrix_handle;
  272. starpu_matrix_data_register(&matrix_handle, -1, (uintptr_t)NULL, nx, nx, ny, sizeof(char));
  273. send_recv_and_check(rank, 0, matrix_handle, 0x75, NULL, 0x8555, NULL, NULL);
  274. starpu_data_unregister(matrix_handle);
  275. }
  276. }
  277. void exchange_block(int rank, int *error)
  278. {
  279. int nx=3;
  280. int ny=2;
  281. int nz=4;
  282. if (rank == 0)
  283. {
  284. float *block, n=1.0;
  285. int x, y, z;
  286. starpu_data_handle_t block_handle[2];
  287. block = (float*)malloc(nx*ny*nz*sizeof(float));
  288. assert(block);
  289. for(z=0 ; z<nz ; z++)
  290. {
  291. for(y=0 ; y<ny ; y++)
  292. {
  293. for(x=0 ; x<nx ; x++)
  294. {
  295. block[(z*nx*ny)+(y*nx)+x] = n++;
  296. }
  297. }
  298. }
  299. starpu_block_data_register(&block_handle[0], STARPU_MAIN_RAM, (uintptr_t)block, nx, nx*ny, nx, ny, nz, sizeof(float));
  300. starpu_block_data_register(&block_handle[1], -1, (uintptr_t)NULL, nx, nx*ny, nx, ny, nz, sizeof(float));
  301. send_recv_and_check(rank, 1, block_handle[0], 0x73, block_handle[1], 0x8337, error, check_block);
  302. starpu_data_unregister(block_handle[0]);
  303. starpu_data_unregister(block_handle[1]);
  304. free(block);
  305. }
  306. else if (rank == 1)
  307. {
  308. starpu_data_handle_t block_handle;
  309. starpu_block_data_register(&block_handle, -1, (uintptr_t)NULL, nx, nx*ny, nx, ny, nz, sizeof(float));
  310. send_recv_and_check(rank, 0, block_handle, 0x73, NULL, 0x8337, NULL, NULL);
  311. starpu_data_unregister(block_handle);
  312. }
  313. }
  314. void exchange_bcsr(int rank, int *error)
  315. {
  316. /*
  317. * We use the following matrix:
  318. *
  319. * +----------------+
  320. * | 0 1 0 0 |
  321. * | 2 3 0 0 |
  322. * | 4 5 8 9 |
  323. * | 6 7 10 11 |
  324. * +----------------+
  325. *
  326. * nzval = [0, 1, 2, 3] ++ [4, 5, 6, 7] ++ [8, 9, 10, 11]
  327. * colind = [0, 0, 1]
  328. * rowptr = [0, 1 ]
  329. * r = c = 2
  330. */
  331. /* Size of the blocks */
  332. #define BCSR_R 2
  333. #define BCSR_C 2
  334. #define BCSR_NROW 2
  335. #define BCSR_NNZ_BLOCKS 3 /* out of 4 */
  336. #define BCSR_NZVAL_SIZE (BCSR_R*BCSR_C*BCSR_NNZ_BLOCKS)
  337. uint32_t colind[BCSR_NNZ_BLOCKS] = {0, 0, 1};
  338. uint32_t rowptr[BCSR_NROW] = {0, 1};
  339. if (rank == 0)
  340. {
  341. starpu_data_handle_t bcsr_handle[2];
  342. int nzval[BCSR_NZVAL_SIZE] =
  343. {
  344. 0, 1, 2, 3, /* First block */
  345. 4, 5, 6, 7, /* Second block */
  346. 8, 9, 10, 11 /* Third block */
  347. };
  348. starpu_bcsr_data_register(&bcsr_handle[0], STARPU_MAIN_RAM, BCSR_NNZ_BLOCKS, BCSR_NROW, (uintptr_t) nzval, colind, rowptr, 0, BCSR_R, BCSR_C, sizeof(nzval[0]));
  349. starpu_bcsr_data_register(&bcsr_handle[1], -1, BCSR_NNZ_BLOCKS, BCSR_NROW, (uintptr_t) NULL, colind, rowptr, 0, BCSR_R, BCSR_C, sizeof(nzval[0]));
  350. send_recv_and_check(rank, 1, bcsr_handle[0], 0x73, bcsr_handle[1], 0x8337, error, check_bcsr);
  351. starpu_data_unregister(bcsr_handle[0]);
  352. starpu_data_unregister(bcsr_handle[1]);
  353. }
  354. else if (rank == 1)
  355. {
  356. starpu_data_handle_t bcsr_handle;
  357. starpu_bcsr_data_register(&bcsr_handle, -1, BCSR_NNZ_BLOCKS, BCSR_NROW, (uintptr_t) NULL, colind, rowptr, 0, BCSR_R, BCSR_C, sizeof(int));
  358. send_recv_and_check(rank, 0, bcsr_handle, 0x73, NULL, 0x8337, NULL, NULL);
  359. starpu_data_unregister(bcsr_handle);
  360. }
  361. }
  362. int main(int argc, char **argv)
  363. {
  364. int ret, rank, size;
  365. int error=0;
  366. int mpi_init;
  367. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  368. ret = starpu_init(NULL);
  369. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  370. ret = starpu_mpi_init(NULL, NULL, mpi_init);
  371. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  372. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  373. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  374. if (size < 2)
  375. {
  376. if (rank == 0)
  377. FPRINTF(stderr, "We need at least 2 processes.\n");
  378. starpu_mpi_shutdown();
  379. starpu_shutdown();
  380. if (!mpi_init)
  381. MPI_Finalize();
  382. return STARPU_TEST_SKIPPED;
  383. }
  384. exchange_void(rank, &error);
  385. exchange_variable(rank, &error);
  386. exchange_vector(rank, &error);
  387. exchange_matrix(rank, &error);
  388. exchange_block(rank, &error);
  389. exchange_bcsr(rank, &error);
  390. starpu_mpi_shutdown();
  391. starpu_shutdown();
  392. if (!mpi_init)
  393. MPI_Finalize();
  394. return rank == 0 ? error : 0;
  395. }