matrix2.c 4.0 KB

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