insert_task.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 *x = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  22. unsigned *y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  23. FPRINTF(stdout, "VALUES: %u %u\n", *x, *y);
  24. *x = (*x + *y) / 2;
  25. }
  26. /* Dummy cost function for simgrid */
  27. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  28. {
  29. return 0.000001;
  30. }
  31. static struct starpu_perfmodel dumb_model =
  32. {
  33. .type = STARPU_COMMON,
  34. .cost_function = cost_function
  35. };
  36. struct starpu_codelet mycodelet =
  37. {
  38. .cpu_funcs = {func_cpu},
  39. .nbuffers = 2,
  40. .modes = {STARPU_RW, STARPU_R},
  41. .model = &dumb_model
  42. };
  43. #define X 4
  44. #define Y 5
  45. /* Returns the MPI node number where data indexes index is */
  46. int my_distrib(int x, int y, int nb_nodes)
  47. {
  48. return (x + y) % nb_nodes;
  49. }
  50. int main(int argc, char **argv)
  51. {
  52. int rank, size, x, y;
  53. int value=0, ret;
  54. unsigned matrix[X][Y];
  55. starpu_data_handle_t data_handles[X][Y];
  56. ret = starpu_init(NULL);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  58. ret = starpu_mpi_init(&argc, &argv, 1);
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  60. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  61. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  62. for(x = 0; x < X; x++)
  63. {
  64. for (y = 0; y < Y; y++)
  65. {
  66. matrix[x][y] = (rank+1)*10 + value;
  67. value++;
  68. }
  69. }
  70. #if 0
  71. for(x = 0; x < X; x++)
  72. {
  73. FPRINTF(stdout, "[%d] ", rank);
  74. for (y = 0; y < Y; y++)
  75. {
  76. FPRINTF(stdout, "%3d ", matrix[x][y]);
  77. }
  78. FPRINTF(stdout, "\n");
  79. }
  80. #endif
  81. for(x = 0; x < X; x++)
  82. {
  83. for (y = 0; y < Y; y++)
  84. {
  85. int mpi_rank = my_distrib(x, y, size);
  86. if (mpi_rank == rank)
  87. {
  88. //FPRINTF(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  89. starpu_variable_data_register(&data_handles[x][y], STARPU_MAIN_RAM, (uintptr_t)&(matrix[x][y]), sizeof(unsigned));
  90. }
  91. else
  92. {
  93. /* I don't own that index, but will need it for my computations */
  94. //FPRINTF(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  95. starpu_variable_data_register(&data_handles[x][y], -1, (uintptr_t)NULL, sizeof(unsigned));
  96. }
  97. if (data_handles[x][y])
  98. {
  99. starpu_mpi_data_register(data_handles[x][y], (y*X)+x, mpi_rank);
  100. }
  101. }
  102. }
  103. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[1][1], STARPU_R, data_handles[0][1], 0);
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  105. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[3][1], STARPU_R, data_handles[0][1], 0);
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  107. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[0][1], STARPU_R, data_handles[0][0], 0);
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  109. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet, STARPU_RW, data_handles[3][1], STARPU_R, data_handles[0][1], 0);
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  111. FPRINTF(stderr, "Waiting ...\n");
  112. starpu_task_wait_for_all();
  113. for(x = 0; x < X; x++)
  114. {
  115. for (y = 0; y < Y; y++)
  116. {
  117. if (data_handles[x][y])
  118. starpu_data_unregister(data_handles[x][y]);
  119. }
  120. }
  121. starpu_mpi_shutdown();
  122. starpu_shutdown();
  123. #if 0
  124. for(x = 0; x < X; x++)
  125. {
  126. FPRINTF(stdout, "[%d] ", rank);
  127. for (y = 0; y < Y; y++)
  128. {
  129. FPRINTF(stdout, "%3d ", matrix[x][y]);
  130. }
  131. FPRINTF(stdout, "\n");
  132. }
  133. #endif
  134. return 0;
  135. }