matrix.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. struct starpu_codelet mycodelet =
  28. {
  29. .cpu_funcs = {func_cpu},
  30. .nbuffers = 3,
  31. .modes = {STARPU_R, STARPU_R, STARPU_RW},
  32. .model = &starpu_nop_perf_model,
  33. };
  34. #define N 4
  35. int main(int argc, char **argv)
  36. {
  37. int rank, n;
  38. int ret;
  39. unsigned A[N];
  40. unsigned X[N];
  41. unsigned Y;
  42. starpu_data_handle_t data_A[N];
  43. starpu_data_handle_t data_X[N];
  44. starpu_data_handle_t data_Y;
  45. ret = starpu_init(NULL);
  46. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  47. ret = starpu_mpi_init(&argc, &argv, 1);
  48. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
  49. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  50. if (starpu_cpu_worker_get_count() == 0)
  51. {
  52. if (rank == 0)
  53. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  54. starpu_mpi_shutdown();
  55. starpu_shutdown();
  56. return STARPU_TEST_SKIPPED;
  57. }
  58. for(n = 0; n < N; n++)
  59. {
  60. A[n] = (n+1)*10;
  61. X[n] = n+1;
  62. }
  63. Y = 0;
  64. FPRINTF_MPI(stderr, "A = ");
  65. for(n = 0; n < N; n++)
  66. {
  67. FPRINTF(stderr, "%u ", A[n]);
  68. }
  69. FPRINTF(stderr, "\n");
  70. FPRINTF_MPI(stderr, "X = ");
  71. for(n = 0; n < N; n++)
  72. {
  73. FPRINTF(stderr, "%u ", X[n]);
  74. }
  75. FPRINTF(stderr, "\n");
  76. for(n = 0; n < N; n++)
  77. {
  78. if (rank == n%2)
  79. starpu_variable_data_register(&data_A[n], STARPU_MAIN_RAM, (uintptr_t)&A[n], sizeof(unsigned));
  80. else
  81. starpu_variable_data_register(&data_A[n], -1, (uintptr_t)NULL, sizeof(unsigned));
  82. starpu_mpi_data_register(data_A[n], n+100, n%2);
  83. FPRINTF_MPI(stderr, "Registering A[%d] to %p with tag %d and node %d\n", n, data_A[n], n+100, n%2);
  84. if (rank == n%2)
  85. starpu_variable_data_register(&data_X[n], STARPU_MAIN_RAM, (uintptr_t)&X[n], sizeof(unsigned));
  86. else
  87. starpu_variable_data_register(&data_X[n], -1, (uintptr_t)NULL, sizeof(unsigned));
  88. starpu_mpi_data_register(data_X[n], n+200, n%2);
  89. FPRINTF_MPI(stderr, "Registering X[%d] to %p with tag %d and node %d\n", n, data_X[n], n+200, n%2);
  90. }
  91. if (rank == 0)
  92. starpu_variable_data_register(&data_Y, STARPU_MAIN_RAM, (uintptr_t)&Y, sizeof(unsigned));
  93. else
  94. starpu_variable_data_register(&data_Y, -1, (uintptr_t)NULL, sizeof(unsigned));
  95. starpu_mpi_data_register(data_Y, 10, 0);
  96. FPRINTF_MPI(stderr, "Registering Y to %p with tag %d and node %d\n", data_Y, 10, 0);
  97. for(n = 0; n < N; n++)
  98. {
  99. ret = starpu_mpi_task_insert(MPI_COMM_WORLD, &mycodelet,
  100. STARPU_R, data_A[n],
  101. STARPU_R, data_X[n],
  102. STARPU_RW, data_Y,
  103. STARPU_EXECUTE_ON_DATA, data_A[n],
  104. 0);
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_task_insert");
  106. }
  107. FPRINTF(stderr, "Waiting ...\n");
  108. starpu_task_wait_for_all();
  109. for(n = 0; n < N; n++)
  110. {
  111. starpu_data_unregister(data_A[n]);
  112. starpu_data_unregister(data_X[n]);
  113. }
  114. starpu_data_unregister(data_Y);
  115. starpu_mpi_shutdown();
  116. starpu_shutdown();
  117. FPRINTF(stdout, "[%d] Y=%u\n", rank, Y);
  118. #ifndef STARPU_SIMGRID
  119. if (rank == 0)
  120. {
  121. STARPU_ASSERT_MSG(Y==300, "Error when calculating Y=%u\n", Y);
  122. }
  123. #endif
  124. return 0;
  125. }