mpi_complex.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <interface/complex_interface.h>
  18. #include <interface/complex_codelet.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. void display_foo_codelet(void *descr[], void *_args)
  21. {
  22. (void)_args;
  23. int *foo = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  24. FPRINTF(stderr, "foo = %d\n", *foo);
  25. }
  26. struct starpu_codelet foo_display =
  27. {
  28. .cpu_funcs = {display_foo_codelet},
  29. .nbuffers = 1,
  30. .modes = {STARPU_R},
  31. .model = &starpu_perfmodel_nop,
  32. };
  33. int main(int argc, char **argv)
  34. {
  35. int rank, nodes;
  36. int ret;
  37. int compare=0;
  38. ret = starpu_mpi_init_conf(&argc, &argv, 1, MPI_COMM_WORLD, NULL);
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  40. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  41. starpu_mpi_comm_size(MPI_COMM_WORLD, &nodes);
  42. if (nodes < 2 || (starpu_cpu_worker_get_count() == 0))
  43. {
  44. if (rank == 0)
  45. {
  46. if (nodes < 2)
  47. fprintf(stderr, "We need at least 2 processes.\n");
  48. else
  49. fprintf(stderr, "We need at least 1 CPU.\n");
  50. }
  51. starpu_mpi_shutdown();
  52. return 77;
  53. }
  54. starpu_data_handle_t handle;
  55. starpu_data_handle_t handle2;
  56. double real[2] = {4.0, 2.0};
  57. double imaginary[2] = {7.0, 9.0};
  58. double real2[2] = {14.0, 12.0};
  59. double imaginary2[2] = {17.0, 19.0};
  60. if (rank == 1)
  61. {
  62. real[0] = 0.0;
  63. real[1] = 0.0;
  64. imaginary[0] = 0.0;
  65. imaginary[1] = 0.0;
  66. }
  67. starpu_complex_data_register(&handle, STARPU_MAIN_RAM, real, imaginary, 2);
  68. starpu_complex_data_register(&handle2, -1, real2, imaginary2, 2);
  69. // Ping-pong
  70. if (rank == 0)
  71. {
  72. int *compare_ptr = &compare;
  73. starpu_task_insert(&cl_display, STARPU_VALUE, "node0 initial value", strlen("node0 initial value")+1, STARPU_R, handle, 0);
  74. starpu_mpi_isend_detached(handle, 1, 10, MPI_COMM_WORLD, NULL, NULL);
  75. starpu_mpi_irecv_detached(handle2, 1, 20, MPI_COMM_WORLD, NULL, NULL);
  76. starpu_task_insert(&cl_display, STARPU_VALUE, "node0 received value", strlen("node0 received value")+1, STARPU_R, handle2, 0);
  77. starpu_task_insert(&cl_compare, STARPU_R, handle, STARPU_R, handle2, STARPU_VALUE, &compare_ptr, sizeof(compare_ptr), 0);
  78. }
  79. else if (rank == 1)
  80. {
  81. starpu_mpi_irecv_detached(handle, 0, 10, MPI_COMM_WORLD, NULL, NULL);
  82. starpu_task_insert(&cl_display, STARPU_VALUE, "node1 received value", strlen("node1 received value")+1, STARPU_R, handle, 0);
  83. starpu_mpi_isend_detached(handle, 0, 20, MPI_COMM_WORLD, NULL, NULL);
  84. }
  85. // Ping
  86. if (rank == 0)
  87. {
  88. starpu_data_handle_t xhandle;
  89. double xreal = 4.0;
  90. double ximaginary = 8.0;
  91. starpu_complex_data_register(&xhandle, STARPU_MAIN_RAM, &xreal, &ximaginary, 1);
  92. starpu_mpi_send(xhandle, 1, 10, MPI_COMM_WORLD);
  93. starpu_data_unregister(xhandle);
  94. }
  95. else if (rank == 1)
  96. {
  97. MPI_Status status;
  98. starpu_data_handle_t xhandle;
  99. double xreal = 14.0;
  100. double ximaginary = 18.0;
  101. starpu_complex_data_register(&xhandle, STARPU_MAIN_RAM, &xreal, &ximaginary, 1);
  102. starpu_mpi_recv(xhandle, 0, 10, MPI_COMM_WORLD, &status);
  103. starpu_data_unregister(xhandle);
  104. FPRINTF(stderr, "[received] real %f imaginary %f\n", xreal, ximaginary);
  105. STARPU_ASSERT_MSG(xreal == 4 && ximaginary == 8, "Incorrect received value\n");
  106. }
  107. starpu_task_wait_for_all();
  108. starpu_data_unregister(handle);
  109. starpu_data_unregister(handle2);
  110. starpu_mpi_shutdown();
  111. return (rank == 0) ? !compare : 0;
  112. }