matrix2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015, 2016, 2017 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 *A = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  22. unsigned *X = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  23. unsigned *Y = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[2]);
  24. FPRINTF_MPI(stderr, "VALUES: Y=%3u A=%3u X=%3u\n", *Y, *A, *X);
  25. *Y = *Y + *A * *X;
  26. }
  27. /* Dummy cost function for simgrid */
  28. static double cost_function(struct starpu_task *task STARPU_ATTRIBUTE_UNUSED, unsigned nimpl STARPU_ATTRIBUTE_UNUSED)
  29. {
  30. return 0.000001;
  31. }
  32. static struct starpu_perfmodel dumb_model =
  33. {
  34. .type = STARPU_COMMON,
  35. .cost_function = cost_function
  36. };
  37. struct starpu_codelet mycodelet =
  38. {
  39. .cpu_funcs = {func_cpu},
  40. .nbuffers = 3,
  41. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  42. .model = &dumb_model
  43. };
  44. #define N 4
  45. int main(int argc, char **argv)
  46. {
  47. int rank, size;
  48. int n;
  49. int ret;
  50. unsigned A[N];
  51. unsigned X[N];
  52. starpu_data_handle_t data_A[N];
  53. starpu_data_handle_t data_X[N];
  54. int mpi_init;
  55. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  56. ret = starpu_init(NULL);
  57. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  58. ret = starpu_mpi_init(NULL, NULL, mpi_init);
  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. if ((size < 3) || (starpu_cpu_worker_get_count() == 0))
  63. {
  64. if (rank == 0)
  65. {
  66. if (size < 3)
  67. FPRINTF(stderr, "We need at least 3 processes.\n");
  68. else
  69. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  70. }
  71. starpu_mpi_shutdown();
  72. starpu_shutdown();
  73. if (!mpi_init)
  74. MPI_Finalize();
  75. return STARPU_TEST_SKIPPED;
  76. }
  77. for(n = 0; n < N; n++)
  78. {
  79. A[n] = (n+1)*10;
  80. X[n] = n+1;
  81. }
  82. FPRINTF_MPI(stderr, "A = ");
  83. for(n = 0; n < N; n++)
  84. {
  85. FPRINTF(stderr, "%u ", A[n]);
  86. }
  87. FPRINTF(stderr, "\n");
  88. FPRINTF_MPI(stderr, "X = ");
  89. for(n = 0; n < N; n++)
  90. {
  91. FPRINTF(stderr, "%u ", X[n]);
  92. }
  93. FPRINTF(stderr, "\n");
  94. for(n = 0; n < N; n++)
  95. {
  96. if (rank == n%2)
  97. starpu_variable_data_register(&data_A[n], STARPU_MAIN_RAM, (uintptr_t)&A[n], sizeof(unsigned));
  98. else
  99. starpu_variable_data_register(&data_A[n], -1, (uintptr_t)NULL, sizeof(unsigned));
  100. starpu_mpi_data_register(data_A[n], n+100, n%2);
  101. FPRINTF_MPI(stderr, "Registering A[%d] to %p with tag %d and node %d\n", n,data_A[n], n+100, n%2);
  102. }
  103. for(n = 0; n < N; n++)
  104. {
  105. if (rank == 2)
  106. starpu_variable_data_register(&data_X[n], STARPU_MAIN_RAM, (uintptr_t)&X[n], sizeof(unsigned));
  107. else
  108. starpu_variable_data_register(&data_X[n], -1, (uintptr_t)NULL, sizeof(unsigned));
  109. starpu_mpi_data_register(data_X[n], n+200, 2);
  110. FPRINTF_MPI(stderr, "Registering X[%d] to %p with tag %d and node %d\n", n, data_X[n], n+200, 2);
  111. }
  112. for(n = 0; n < N-1; n++)
  113. {
  114. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  115. STARPU_R, data_A[n],
  116. STARPU_R, data_X[n],
  117. STARPU_RW, data_X[N-1],
  118. STARPU_EXECUTE_ON_DATA, data_A[n],
  119. 0);
  120. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  121. }
  122. FPRINTF(stderr, "Waiting ...\n");
  123. starpu_task_wait_for_all();
  124. for(n = 0; n < N; n++)
  125. {
  126. starpu_data_unregister(data_A[n]);
  127. starpu_data_unregister(data_X[n]);
  128. }
  129. starpu_mpi_shutdown();
  130. starpu_shutdown();
  131. FPRINTF(stdout, "[%d] X[%d]=%u\n", rank, N-1, X[N-1]);
  132. #ifndef STARPU_SIMGRID
  133. if (rank == 2)
  134. {
  135. STARPU_ASSERT_MSG(X[N-1]==144, "Error when calculating X[N-1]=%u\n", X[N-1]);
  136. }
  137. #endif
  138. if (!mpi_init)
  139. MPI_Finalize();
  140. return 0;
  141. }