insert_task_block.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifdef STARPU_SIMGRID
  47. .model = &starpu_nop_perf_model,
  48. #endif
  49. .modes = {STARPU_RW}
  50. };
  51. #define SIZE 6
  52. #define BLOCKS 3
  53. /* Returns the MPI node number where data indexes index is */
  54. int my_distrib(int x, int y, int nb_nodes)
  55. {
  56. return (x + y) % nb_nodes;
  57. }
  58. int main(int argc, char **argv)
  59. {
  60. int rank, size, x, y;
  61. int ret, value=0;
  62. unsigned matrix[SIZE*SIZE];
  63. starpu_data_handle_t data_handles[SIZE][SIZE];
  64. ret = starpu_init(NULL);
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  66. ret = starpu_mpi_init(&argc, &argv, 1);
  67. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_initialize_extended");
  68. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  69. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  70. for(x = 0; x < SIZE; x++)
  71. {
  72. for (y = 0; y < SIZE; y++)
  73. {
  74. matrix[x+y*SIZE] = rank*100 + value;
  75. value++;
  76. }
  77. }
  78. #if 1
  79. for(x = 0; x < SIZE; x++)
  80. {
  81. FPRINTF(stdout, "[%d] ", rank);
  82. for (y = 0; y < SIZE; y++)
  83. {
  84. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  85. }
  86. FPRINTF(stdout, "\n");
  87. }
  88. #endif
  89. for(x = 0; x < BLOCKS ; x++)
  90. {
  91. for (y = 0; y < BLOCKS; y++)
  92. {
  93. int mpi_rank = my_distrib(x, y, size);
  94. if (mpi_rank == rank)
  95. {
  96. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  97. starpu_matrix_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  98. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  99. }
  100. else
  101. {
  102. /* I don't own that index, but will need it for my computations */
  103. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  104. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  105. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  106. }
  107. if (data_handles[x][y])
  108. {
  109. starpu_mpi_data_register(data_handles[x][y], (y*BLOCKS)+x, mpi_rank);
  110. }
  111. }
  112. }
  113. for(x = 0; x < BLOCKS; x++)
  114. {
  115. for (y = 0; y < BLOCKS; y++)
  116. {
  117. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  118. STARPU_RW, data_handles[x][y],
  119. 0);
  120. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  121. }
  122. }
  123. FPRINTF(stderr, "Waiting ...\n");
  124. starpu_task_wait_for_all();
  125. for(x = 0; x < BLOCKS; x++)
  126. {
  127. for (y = 0; y < BLOCKS; y++)
  128. {
  129. if (data_handles[x][y])
  130. starpu_data_unregister(data_handles[x][y]);
  131. }
  132. }
  133. starpu_mpi_shutdown();
  134. starpu_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. }