datatypes.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2014 Centre National de la Recherche Scientifique
  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("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. int ret;
  27. float *v_s, *v_r;
  28. STARPU_ASSERT(starpu_variable_get_elemsize(handle_s) == starpu_variable_get_elemsize(handle_r));
  29. v_s = (float *)starpu_variable_get_local_ptr(handle_s);
  30. v_r = (float *)starpu_variable_get_local_ptr(handle_r);
  31. if (*v_s == *v_r)
  32. {
  33. FPRINTF_MPI("Success with variable value: %f == %f\n", *v_s, *v_r);
  34. }
  35. else
  36. {
  37. *error = 1;
  38. FPRINTF_MPI("Error with variable value: %f != %f\n", *v_s, *v_r);
  39. }
  40. }
  41. void check_vector(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  42. {
  43. int ret, i;
  44. int nx;
  45. int *v_r, *v_s;
  46. STARPU_ASSERT(starpu_vector_get_elemsize(handle_s) == starpu_vector_get_elemsize(handle_r));
  47. STARPU_ASSERT(starpu_vector_get_nx(handle_s) == starpu_vector_get_nx(handle_r));
  48. nx = starpu_vector_get_nx(handle_r);
  49. v_r = (int *)starpu_vector_get_local_ptr(handle_r);
  50. v_s = (int *)starpu_vector_get_local_ptr(handle_s);
  51. for(i=0 ; i<nx ; i++)
  52. {
  53. if (v_s[i] == v_r[i])
  54. {
  55. FPRINTF_MPI("Success with vector[%d] value: %d == %d\n", i, v_s[i], v_r[i]);
  56. }
  57. else
  58. {
  59. *error = 1;
  60. FPRINTF_MPI("Error with vector[%d] value: %d != %d\n", i, v_s[i], v_r[i]);
  61. }
  62. }
  63. }
  64. void check_matrix(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  65. {
  66. STARPU_ASSERT(starpu_matrix_get_elemsize(handle_s) == starpu_matrix_get_elemsize(handle_r));
  67. STARPU_ASSERT(starpu_matrix_get_nx(handle_s) == starpu_matrix_get_nx(handle_r));
  68. STARPU_ASSERT(starpu_matrix_get_ny(handle_s) == starpu_matrix_get_ny(handle_r));
  69. STARPU_ASSERT(starpu_matrix_get_local_ld(handle_s) == starpu_matrix_get_local_ld(handle_r));
  70. char *matrix_s = (char *)starpu_matrix_get_local_ptr(handle_s);
  71. char *matrix_r = (char *)starpu_matrix_get_local_ptr(handle_r);
  72. int nx = starpu_matrix_get_nx(handle_s);
  73. int ny = starpu_matrix_get_ny(handle_s);
  74. int ldy = starpu_matrix_get_local_ld(handle_s);
  75. int x, y;
  76. for(y=0 ; y<ny ; y++)
  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("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("Error with matrix[%d,%d --> %d] value: %c != %c\n", x, y, index, matrix_s[index], matrix_r[index]);
  88. }
  89. }
  90. }
  91. void check_block(starpu_data_handle_t handle_s, starpu_data_handle_t handle_r, int *error)
  92. {
  93. STARPU_ASSERT(starpu_block_get_elemsize(handle_s) == starpu_block_get_elemsize(handle_r));
  94. STARPU_ASSERT(starpu_block_get_nx(handle_s) == starpu_block_get_nx(handle_r));
  95. STARPU_ASSERT(starpu_block_get_ny(handle_s) == starpu_block_get_ny(handle_r));
  96. STARPU_ASSERT(starpu_block_get_nz(handle_s) == starpu_block_get_nz(handle_r));
  97. STARPU_ASSERT(starpu_block_get_local_ldy(handle_s) == starpu_block_get_local_ldy(handle_r));
  98. STARPU_ASSERT(starpu_block_get_local_ldz(handle_s) == starpu_block_get_local_ldz(handle_r));
  99. float *block_s = (float *)starpu_block_get_local_ptr(handle_s);
  100. float *block_r = (float *)starpu_block_get_local_ptr(handle_r);
  101. int nx = starpu_block_get_nx(handle_s);
  102. int ny = starpu_block_get_ny(handle_s);
  103. int nz = starpu_block_get_nz(handle_s);
  104. int ldy = starpu_block_get_local_ldy(handle_s);
  105. int ldz = starpu_block_get_local_ldz(handle_s);
  106. int x, y, z;
  107. for(z=0 ; z<nz ; z++)
  108. for(y=0 ; y<ny ; y++)
  109. for(x=0 ; x<nx ; x++)
  110. {
  111. int index=(z*ldz)+(y*ldy)+x;
  112. if (block_s[index] == block_r[index])
  113. {
  114. FPRINTF_MPI("Success with block[%d,%d,%d --> %d] value: %f == %f\n", x, y, z, index, block_s[index], block_r[index]);
  115. }
  116. else
  117. {
  118. *error = 1;
  119. FPRINTF_MPI("Error with block[%d,%d,%d --> %d] value: %f != %f\n", x, y, z, index, block_s[index], block_r[index]);
  120. }
  121. }
  122. }
  123. 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)
  124. {
  125. int ret;
  126. MPI_Status status;
  127. if (rank == 0)
  128. {
  129. ret = starpu_mpi_send(handle_s, node, tag_s, MPI_COMM_WORLD);
  130. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  131. ret = starpu_mpi_recv(handle_r, node, tag_r, MPI_COMM_WORLD, &status);
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  133. func(handle_s, handle_r, error);
  134. }
  135. else
  136. {
  137. ret = starpu_mpi_recv(handle_s, node, tag_s, MPI_COMM_WORLD, &status);
  138. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  139. ret = starpu_mpi_send(handle_s, node, tag_r, MPI_COMM_WORLD);
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  141. }
  142. }
  143. int main(int argc, char **argv)
  144. {
  145. int ret, rank, size;
  146. int error=0;
  147. int nx=3;
  148. int ny=2;
  149. int nz=4;
  150. MPI_Init(&argc, &argv);
  151. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  152. MPI_Comm_size(MPI_COMM_WORLD, &size);
  153. if (size < 2)
  154. {
  155. if (rank == 0)
  156. FPRINTF(stderr, "We need at least 2 processes.\n");
  157. MPI_Finalize();
  158. return STARPU_TEST_SKIPPED;
  159. }
  160. ret = starpu_init(NULL);
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  162. ret = starpu_mpi_init(NULL, NULL, 0);
  163. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  164. if (rank == 0)
  165. {
  166. MPI_Status status;
  167. {
  168. starpu_data_handle_t void_handle[2];
  169. starpu_void_data_register(&void_handle[0]);
  170. starpu_void_data_register(&void_handle[1]);
  171. send_recv_and_check(rank, 1, void_handle[0], 0x42, void_handle[1], 0x1337, &error, check_void);
  172. starpu_data_unregister(void_handle[0]);
  173. starpu_data_unregister(void_handle[1]);
  174. }
  175. {
  176. float v = 42.12;
  177. starpu_data_handle_t variable_handle[2];
  178. starpu_variable_data_register(&variable_handle[0], STARPU_MAIN_RAM, (uintptr_t)&v, sizeof(v));
  179. starpu_variable_data_register(&variable_handle[1], -1, (uintptr_t)NULL, sizeof(v));
  180. send_recv_and_check(rank, 1, variable_handle[0], 0x42, variable_handle[1], 0x1337, &error, check_variable);
  181. starpu_data_unregister(variable_handle[0]);
  182. starpu_data_unregister(variable_handle[1]);
  183. }
  184. {
  185. int vector[4] = {1, 2, 3, 4};
  186. starpu_data_handle_t vector_handle[2];
  187. starpu_vector_data_register(&vector_handle[0], STARPU_MAIN_RAM, (uintptr_t)vector, 4, sizeof(vector[0]));
  188. starpu_vector_data_register(&vector_handle[1], -1, (uintptr_t)NULL, 4, sizeof(vector[0]));
  189. send_recv_and_check(rank, 1, vector_handle[0], 0x43, vector_handle[1], 0x2337, &error, check_vector);
  190. starpu_data_unregister(vector_handle[0]);
  191. starpu_data_unregister(vector_handle[1]);
  192. }
  193. {
  194. char *matrix, n='a';
  195. int x, y;
  196. starpu_data_handle_t matrix_handle[2];
  197. matrix = (char*)malloc(nx*ny*nz*sizeof(char));
  198. assert(matrix);
  199. for(y=0 ; y<ny ; y++)
  200. {
  201. for(x=0 ; x<nx ; x++)
  202. {
  203. matrix[(y*nx)+x] = n++;
  204. }
  205. }
  206. starpu_matrix_data_register(&matrix_handle[0], STARPU_MAIN_RAM, (uintptr_t)matrix, nx, nx, ny, sizeof(char));
  207. starpu_matrix_data_register(&matrix_handle[1], -1, (uintptr_t)NULL, nx, nx, ny, sizeof(char));
  208. send_recv_and_check(rank, 1, matrix_handle[0], 0x75, matrix_handle[1], 0x8555, &error, check_matrix);
  209. starpu_data_unregister(matrix_handle[0]);
  210. starpu_data_unregister(matrix_handle[1]);
  211. free(matrix);
  212. }
  213. {
  214. float *block, n=1.0;
  215. int x, y, z;
  216. starpu_data_handle_t block_handle[2];
  217. block = (float*)malloc(nx*ny*nz*sizeof(float));
  218. assert(block);
  219. for(z=0 ; z<nz ; z++)
  220. {
  221. for(y=0 ; y<ny ; y++)
  222. {
  223. for(x=0 ; x<nx ; x++)
  224. {
  225. block[(z*nx*ny)+(y*nx)+x] = n++;
  226. }
  227. }
  228. }
  229. starpu_block_data_register(&block_handle[0], STARPU_MAIN_RAM, (uintptr_t)block, nx, nx*ny, nx, ny, nz, sizeof(float));
  230. starpu_block_data_register(&block_handle[1], -1, (uintptr_t)NULL, nx, nx*ny, nx, ny, nz, sizeof(float));
  231. send_recv_and_check(rank, 1, block_handle[0], 0x73, block_handle[1], 0x8337, &error, check_block);
  232. starpu_data_unregister(block_handle[0]);
  233. starpu_data_unregister(block_handle[1]);
  234. free(block);
  235. }
  236. }
  237. else if (rank == 1)
  238. {
  239. MPI_Status status;
  240. {
  241. starpu_data_handle_t void_handle;
  242. starpu_void_data_register(&void_handle);
  243. send_recv_and_check(rank, 0, void_handle, 0x42, NULL, 0x1337, NULL, NULL);
  244. starpu_data_unregister(void_handle);
  245. }
  246. {
  247. starpu_data_handle_t variable_handle;
  248. starpu_variable_data_register(&variable_handle, -1, (uintptr_t)NULL, sizeof(float));
  249. send_recv_and_check(rank, 0, variable_handle, 0x42, NULL, 0x1337, NULL, NULL);
  250. starpu_data_unregister(variable_handle);
  251. }
  252. {
  253. starpu_data_handle_t vector_handle;
  254. starpu_vector_data_register(&vector_handle, -1, (uintptr_t)NULL, 4, sizeof(int));
  255. send_recv_and_check(rank, 0, vector_handle, 0x43, NULL, 0x2337, NULL, NULL);
  256. starpu_data_unregister(vector_handle);
  257. }
  258. {
  259. starpu_data_handle_t matrix_handle;
  260. starpu_matrix_data_register(&matrix_handle, -1, (uintptr_t)NULL, nx, nx, ny, sizeof(char));
  261. send_recv_and_check(rank, 0, matrix_handle, 0x75, NULL, 0x8555, NULL, NULL);
  262. starpu_data_unregister(matrix_handle);
  263. }
  264. {
  265. starpu_data_handle_t block_handle;
  266. starpu_block_data_register(&block_handle, -1, (uintptr_t)NULL, nx, nx*ny, nx, ny, nz, sizeof(float));
  267. send_recv_and_check(rank, 0, block_handle, 0x73, NULL, 0x8337, NULL, NULL);
  268. starpu_data_unregister(block_handle);
  269. }
  270. }
  271. starpu_mpi_shutdown();
  272. starpu_shutdown();
  273. MPI_Finalize();
  274. return rank == 0 ? error : 0;
  275. }