insert_task_block.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifdef STARPU_SIMGRID
  43. /* Dummy cost function for simgrid */
  44. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  45. {
  46. return 0.000001;
  47. }
  48. static struct starpu_perfmodel dumb_model =
  49. {
  50. .type = STARPU_COMMON,
  51. .cost_function = cost_function
  52. };
  53. #endif
  54. struct starpu_codelet mycodelet =
  55. {
  56. .cpu_funcs = {func_cpu},
  57. .nbuffers = 1,
  58. #ifdef STARPU_SIMGRID
  59. .model = &dumb_model,
  60. #endif
  61. .modes = {STARPU_RW}
  62. };
  63. #define SIZE 6
  64. #define BLOCKS 3
  65. /* Returns the MPI node number where data indexes index is */
  66. int my_distrib(int x, int y, int nb_nodes)
  67. {
  68. return (x + y) % nb_nodes;
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. int rank, size, x, y;
  73. int ret, value=0;
  74. unsigned matrix[SIZE*SIZE];
  75. starpu_data_handle_t data_handles[SIZE][SIZE];
  76. ret = starpu_init(NULL);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  78. ret = starpu_mpi_init(&argc, &argv, 1);
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_initialize_extended");
  80. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  81. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  82. for(x = 0; x < SIZE; x++)
  83. {
  84. for (y = 0; y < SIZE; y++)
  85. {
  86. matrix[x+y*SIZE] = rank*100 + value;
  87. value++;
  88. }
  89. }
  90. #if 1
  91. for(x = 0; x < SIZE; x++)
  92. {
  93. FPRINTF(stdout, "[%d] ", rank);
  94. for (y = 0; y < SIZE; y++)
  95. {
  96. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  97. }
  98. FPRINTF(stdout, "\n");
  99. }
  100. #endif
  101. for(x = 0; x < BLOCKS ; x++)
  102. {
  103. for (y = 0; y < BLOCKS; y++)
  104. {
  105. int mpi_rank = my_distrib(x, y, size);
  106. if (mpi_rank == rank)
  107. {
  108. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  109. starpu_matrix_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  110. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  111. }
  112. else
  113. {
  114. /* I don't own that index, but will need it for my computations */
  115. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  116. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  117. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  118. }
  119. if (data_handles[x][y])
  120. {
  121. starpu_mpi_data_register(data_handles[x][y], (y*BLOCKS)+x, mpi_rank);
  122. }
  123. }
  124. }
  125. for(x = 0; x < BLOCKS; x++)
  126. {
  127. for (y = 0; y < BLOCKS; y++)
  128. {
  129. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  130. STARPU_RW, data_handles[x][y],
  131. 0);
  132. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  133. }
  134. }
  135. FPRINTF(stderr, "Waiting ...\n");
  136. starpu_task_wait_for_all();
  137. for(x = 0; x < BLOCKS; x++)
  138. {
  139. for (y = 0; y < BLOCKS; y++)
  140. {
  141. if (data_handles[x][y])
  142. starpu_data_unregister(data_handles[x][y]);
  143. }
  144. }
  145. starpu_mpi_shutdown();
  146. starpu_shutdown();
  147. #if 1
  148. for(x = 0; x < SIZE; x++)
  149. {
  150. FPRINTF(stdout, "[%d] ", rank);
  151. for (y = 0; y < SIZE; y++)
  152. {
  153. FPRINTF(stdout, "%3u ", matrix[x+y*SIZE]);
  154. }
  155. FPRINTF(stdout, "\n");
  156. }
  157. #endif
  158. return 0;
  159. }