insert_task_block.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 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 <math.h>
  18. #include "helper.h"
  19. void func_cpu(void *descr[], __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. .where = STARPU_CPU,
  45. .cpu_funcs = {func_cpu, NULL},
  46. .nbuffers = 1,
  47. .modes = {STARPU_RW}
  48. };
  49. #define SIZE 6
  50. #define BLOCKS 3
  51. /* Returns the MPI node number where data indexes index is */
  52. int my_distrib(int x, int y, int nb_nodes)
  53. {
  54. return x % nb_nodes;
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. int rank, size, x, y;
  59. int ret, value=0;
  60. unsigned matrix[SIZE*SIZE];
  61. starpu_data_handle_t data_handles[SIZE][SIZE];
  62. ret = starpu_init(NULL);
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  64. ret = starpu_mpi_initialize_extended(&rank, &size);
  65. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_initialize_extended");
  66. for(x = 0; x < SIZE; x++)
  67. {
  68. for (y = 0; y < SIZE; y++)
  69. {
  70. matrix[x+y*SIZE] = rank*100 + value;
  71. value++;
  72. }
  73. }
  74. #if 1
  75. for(x = 0; x < SIZE; x++) {
  76. FPRINTF(stdout, "[%d] ", rank);
  77. for (y = 0; y < SIZE; y++) {
  78. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  79. }
  80. FPRINTF(stdout, "\n");
  81. }
  82. #endif
  83. for(x = 0; x < BLOCKS ; x++)
  84. {
  85. for (y = 0; y < BLOCKS; y++)
  86. {
  87. int mpi_rank = my_distrib(x, y, size);
  88. if (mpi_rank == rank)
  89. {
  90. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  91. starpu_matrix_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  92. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  93. }
  94. else if (rank == mpi_rank+1 || rank == mpi_rank-1)
  95. {
  96. /* I don't own that index, but will need it for my computations */
  97. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  98. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  99. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  100. }
  101. else
  102. {
  103. /* I know it's useless to allocate anything for this */
  104. data_handles[x][y] = NULL;
  105. }
  106. if (data_handles[x][y])
  107. {
  108. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  109. starpu_data_set_tag(data_handles[x][y], (y*BLOCKS)+x);
  110. }
  111. }
  112. }
  113. for(x = 0; x < BLOCKS; x++)
  114. {
  115. for (y = 0; y < BLOCKS; y++)
  116. {
  117. ret = starpu_mpi_insert_task(MPI_COMM_WORLD, &mycodelet,
  118. STARPU_RW, data_handles[x][y],
  119. 0);
  120. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_insert_task");
  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. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  141. }
  142. FPRINTF(stdout, "\n");
  143. }
  144. #endif
  145. return 0;
  146. }