matrix2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 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. MPI_Init(&argc, &argv);
  55. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  56. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  57. ret = starpu_init(NULL);
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  59. ret = starpu_mpi_init(NULL, NULL, 0);
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  61. if (size < 3)
  62. {
  63. if (rank == 0)
  64. FPRINTF(stderr, "We need at least 3 processes.\n");
  65. starpu_mpi_shutdown();
  66. starpu_shutdown();
  67. MPI_Finalize();
  68. return STARPU_TEST_SKIPPED;
  69. }
  70. for(n = 0; n < N; n++)
  71. {
  72. A[n] = (n+1)*10;
  73. X[n] = n+1;
  74. }
  75. FPRINTF_MPI(stderr, "A = ");
  76. for(n = 0; n < N; n++)
  77. {
  78. FPRINTF(stderr, "%u ", A[n]);
  79. }
  80. FPRINTF(stderr, "\n");
  81. FPRINTF_MPI(stderr, "X = ");
  82. for(n = 0; n < N; n++)
  83. {
  84. FPRINTF(stderr, "%u ", X[n]);
  85. }
  86. FPRINTF(stderr, "\n");
  87. for(n = 0; n < N; n++)
  88. {
  89. if (rank == n%2)
  90. starpu_variable_data_register(&data_A[n], STARPU_MAIN_RAM, (uintptr_t)&A[n], sizeof(unsigned));
  91. else
  92. starpu_variable_data_register(&data_A[n], -1, (uintptr_t)NULL, sizeof(unsigned));
  93. starpu_mpi_data_register(data_A[n], n+100, n%2);
  94. FPRINTF_MPI(stderr, "Registering A[%d] to %p with tag %d and node %d\n", n,data_A[n], n+100, n%2);
  95. }
  96. for(n = 0; n < N; n++)
  97. {
  98. if (rank == 2)
  99. starpu_variable_data_register(&data_X[n], STARPU_MAIN_RAM, (uintptr_t)&X[n], sizeof(unsigned));
  100. else
  101. starpu_variable_data_register(&data_X[n], -1, (uintptr_t)NULL, sizeof(unsigned));
  102. starpu_mpi_data_register(data_X[n], n+200, 2);
  103. FPRINTF_MPI(stderr, "Registering X[%d] to %p with tag %d and node %d\n", n, data_X[n], n+200, 2);
  104. }
  105. for(n = 0; n < N-1; n++)
  106. {
  107. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  108. STARPU_R, data_A[n],
  109. STARPU_R, data_X[n],
  110. STARPU_RW, data_X[N-1],
  111. STARPU_EXECUTE_ON_DATA, data_A[n],
  112. 0);
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  114. }
  115. FPRINTF(stderr, "Waiting ...\n");
  116. starpu_task_wait_for_all();
  117. for(n = 0; n < N; n++)
  118. {
  119. starpu_data_unregister(data_A[n]);
  120. starpu_data_unregister(data_X[n]);
  121. }
  122. starpu_mpi_shutdown();
  123. starpu_shutdown();
  124. FPRINTF(stdout, "[%d] X[%d]=%u\n", rank, N-1, X[N-1]);
  125. #ifndef STARPU_SIMGRID
  126. if (rank == 2)
  127. {
  128. STARPU_ASSERT_MSG(X[N-1]==144, "Error when calculating X[N-1]=%u\n", X[N-1]);
  129. }
  130. #endif
  131. MPI_Finalize();
  132. return 0;
  133. }