datatypes.c 9.3 KB

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