insert_task_block.c 3.7 KB

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