insert_task_block.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Inria
  4. * Copyright (C) 2011-2015,2017,2018 CNRS
  5. * Copyright (C) 2013-2015,2017,2018 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. #include <starpu_mpi.h>
  19. #include <math.h>
  20. #include "helper.h"
  21. void func_cpu(void *descr[], void *_args)
  22. {
  23. (void)_args;
  24. unsigned *matrix = (unsigned *)STARPU_MATRIX_GET_PTR(descr[0]);
  25. int nx = (int)STARPU_MATRIX_GET_NX(descr[0]);
  26. int ny = (int)STARPU_MATRIX_GET_NY(descr[0]);
  27. int ld = (int)STARPU_MATRIX_GET_LD(descr[0]);
  28. int i, j;
  29. unsigned sum=0;
  30. for (i = 0; i < nx; i++)
  31. {
  32. for (j = 0; j < ny; j++)
  33. {
  34. sum += matrix[i+j*ld];
  35. }
  36. }
  37. for (i = 0; i < nx; i++)
  38. {
  39. for (j = 0; j < ny; j++)
  40. {
  41. matrix[i+j*ld] = sum;///(nx*ny);
  42. }
  43. }
  44. }
  45. struct starpu_codelet mycodelet =
  46. {
  47. .cpu_funcs = {func_cpu},
  48. .nbuffers = 1,
  49. #ifdef STARPU_SIMGRID
  50. .model = &starpu_perfmodel_nop,
  51. #endif
  52. .modes = {STARPU_RW}
  53. };
  54. #define SIZE 6
  55. #define BLOCKS 3
  56. /* Returns the MPI node number where data indexes index is */
  57. int my_distrib(int x, int y, int nb_nodes)
  58. {
  59. return (x + y) % nb_nodes;
  60. }
  61. int main(int argc, char **argv)
  62. {
  63. int rank, size, x, y;
  64. int ret, value=0;
  65. unsigned matrix[SIZE*SIZE];
  66. starpu_data_handle_t data_handles[SIZE][SIZE];
  67. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  69. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  70. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  71. for(x = 0; x < SIZE; x++)
  72. {
  73. for (y = 0; y < SIZE; y++)
  74. {
  75. matrix[x+y*SIZE] = rank*100 + value;
  76. value++;
  77. }
  78. }
  79. #if 1
  80. for(x = 0; x < SIZE; x++)
  81. {
  82. FPRINTF(stdout, "[%d] ", rank);
  83. for (y = 0; y < SIZE; y++)
  84. {
  85. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  86. }
  87. FPRINTF(stdout, "\n");
  88. }
  89. #endif
  90. for(x = 0; x < BLOCKS ; x++)
  91. {
  92. for (y = 0; y < BLOCKS; y++)
  93. {
  94. int mpi_rank = my_distrib(x, y, size);
  95. if (mpi_rank == rank)
  96. {
  97. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  98. starpu_matrix_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  99. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  100. }
  101. else
  102. {
  103. /* I don't own this index, but will need it for my computations */
  104. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  105. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  106. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  107. }
  108. if (data_handles[x][y])
  109. {
  110. starpu_mpi_data_register(data_handles[x][y], (y*BLOCKS)+x, mpi_rank);
  111. }
  112. }
  113. }
  114. for(x = 0; x < BLOCKS; x++)
  115. {
  116. for (y = 0; y < BLOCKS; y++)
  117. {
  118. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  119. STARPU_RW, data_handles[x][y],
  120. 0);
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  122. }
  123. }
  124. FPRINTF(stderr, "Waiting ...\n");
  125. starpu_task_wait_for_all();
  126. for(x = 0; x < BLOCKS; x++)
  127. {
  128. for (y = 0; y < BLOCKS; y++)
  129. {
  130. if (data_handles[x][y])
  131. starpu_data_unregister(data_handles[x][y]);
  132. }
  133. }
  134. starpu_mpi_shutdown();
  135. #if 1
  136. for(x = 0; x < SIZE; x++)
  137. {
  138. FPRINTF(stdout, "[%d] ", rank);
  139. for (y = 0; y < SIZE; y++)
  140. {
  141. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  142. }
  143. FPRINTF(stdout, "\n");
  144. }
  145. #endif
  146. return 0;
  147. }