insert_task_cache.c 4.9 KB

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