block_interface_pinned.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-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 <stdlib.h>
  18. #include "helper.h"
  19. #ifdef STARPU_QUICK_CHECK
  20. # define NITER 16
  21. #else
  22. # define NITER 2048
  23. #endif
  24. #define BIGSIZE 128
  25. #define SIZE 64
  26. int main(int argc, char **argv)
  27. {
  28. int ret, rank, size;
  29. int mpi_init;
  30. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  31. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  32. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  33. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  34. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  35. if (size < 2)
  36. {
  37. if (rank == 0)
  38. FPRINTF(stderr, "We need at least 2 processes.\n");
  39. starpu_mpi_shutdown();
  40. if (!mpi_init)
  41. MPI_Finalize();
  42. return STARPU_TEST_SKIPPED;
  43. }
  44. /* Node 0 will allocate a big block and only register an inner part of
  45. * it as the block data, Node 1 will allocate a block of small size and
  46. * register it directly. Node 0 and 1 will then exchange the content of
  47. * their blocks. */
  48. float *block = NULL;
  49. starpu_data_handle_t block_handle = NULL;
  50. if (rank == 0)
  51. {
  52. starpu_malloc((void **)&block,
  53. BIGSIZE*BIGSIZE*BIGSIZE*sizeof(float));
  54. memset(block, 0, BIGSIZE*BIGSIZE*BIGSIZE*sizeof(float));
  55. /* fill the inner block */
  56. unsigned i, j, k;
  57. for (k = 0; k < SIZE; k++)
  58. for (j = 0; j < SIZE; j++)
  59. for (i = 0; i < SIZE; i++)
  60. {
  61. block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] = 1.0f;
  62. }
  63. starpu_block_data_register(&block_handle, STARPU_MAIN_RAM,
  64. (uintptr_t)block, BIGSIZE, BIGSIZE*BIGSIZE,
  65. SIZE, SIZE, SIZE, sizeof(float));
  66. }
  67. else if (rank == 1)
  68. {
  69. starpu_malloc((void **)&block,
  70. SIZE*SIZE*SIZE*sizeof(float));
  71. memset(block, 0, SIZE*SIZE*SIZE*sizeof(float));
  72. starpu_block_data_register(&block_handle, STARPU_MAIN_RAM,
  73. (uintptr_t)block, SIZE, SIZE*SIZE,
  74. SIZE, SIZE, SIZE, sizeof(float));
  75. }
  76. if (rank == 0)
  77. {
  78. MPI_Status status;
  79. ret = starpu_mpi_send(block_handle, 1, 0x42, MPI_COMM_WORLD);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  81. ret = starpu_mpi_recv(block_handle, 1, 0x1337, MPI_COMM_WORLD, &status);
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  83. /* check the content of the block */
  84. starpu_data_acquire(block_handle, STARPU_R);
  85. unsigned i, j, k;
  86. for (k = 0; k < SIZE; k++)
  87. for (j = 0; j < SIZE; j++)
  88. for (i = 0; i < SIZE; i++)
  89. {
  90. assert(block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] == 33.0f);
  91. }
  92. starpu_data_release(block_handle);
  93. }
  94. else if (rank == 1)
  95. {
  96. MPI_Status status;
  97. ret = starpu_mpi_recv(block_handle, 0, 0x42, MPI_COMM_WORLD, &status);
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  99. /* check the content of the block and modify it */
  100. ret = starpu_data_acquire(block_handle, STARPU_RW);
  101. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  102. unsigned i, j, k;
  103. for (k = 0; k < SIZE; k++)
  104. for (j = 0; j < SIZE; j++)
  105. for (i = 0; i < SIZE; i++)
  106. {
  107. assert(block[i + j*SIZE + k*SIZE*SIZE] == 1.0f);
  108. block[i + j*SIZE + k*SIZE*SIZE] = 33.0f;
  109. }
  110. starpu_data_release(block_handle);
  111. ret = starpu_mpi_send(block_handle, 0, 0x1337, MPI_COMM_WORLD);
  112. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  113. }
  114. if (rank == 0 || rank == 1)
  115. {
  116. starpu_data_unregister(block_handle);
  117. starpu_free(block);
  118. }
  119. FPRINTF(stdout, "Rank %d is done\n", rank);
  120. fflush(stdout);
  121. starpu_mpi_shutdown();
  122. if (!mpi_init)
  123. MPI_Finalize();
  124. return 0;
  125. }