insert_task_block.c 3.7 KB

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