block_interface_pinned.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2014,2015,2017,2018 Université de Bordeaux
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2010-2012,2015,2017,2019 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu_mpi.h>
  19. #include <stdlib.h>
  20. #include "helper.h"
  21. #ifdef STARPU_QUICK_CHECK
  22. # define NITER 16
  23. #else
  24. # define NITER 2048
  25. #endif
  26. #define BIGSIZE 128
  27. #define SIZE 64
  28. int main(int argc, char **argv)
  29. {
  30. int ret, rank, size;
  31. int mpi_init;
  32. MPI_INIT_THREAD(&argc, &argv, MPI_THREAD_SERIALIZED, &mpi_init);
  33. ret = starpu_mpi_init_conf(&argc, &argv, mpi_init, MPI_COMM_WORLD, NULL);
  34. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init_conf");
  35. starpu_mpi_comm_rank(MPI_COMM_WORLD, &rank);
  36. starpu_mpi_comm_size(MPI_COMM_WORLD, &size);
  37. if (size < 2)
  38. {
  39. if (rank == 0)
  40. FPRINTF(stderr, "We need at least 2 processes.\n");
  41. starpu_mpi_shutdown();
  42. if (!mpi_init)
  43. MPI_Finalize();
  44. return STARPU_TEST_SKIPPED;
  45. }
  46. /* Node 0 will allocate a big block and only register an inner part of
  47. * it as the block data, Node 1 will allocate a block of small size and
  48. * register it directly. Node 0 and 1 will then exchange the content of
  49. * their blocks. */
  50. float *block = NULL;
  51. starpu_data_handle_t block_handle = NULL;
  52. if (rank == 0)
  53. {
  54. starpu_malloc((void **)&block,
  55. BIGSIZE*BIGSIZE*BIGSIZE*sizeof(float));
  56. memset(block, 0, BIGSIZE*BIGSIZE*BIGSIZE*sizeof(float));
  57. /* fill the inner block */
  58. unsigned i, j, k;
  59. for (k = 0; k < SIZE; k++)
  60. for (j = 0; j < SIZE; j++)
  61. for (i = 0; i < SIZE; i++)
  62. {
  63. block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] = 1.0f;
  64. }
  65. starpu_block_data_register(&block_handle, STARPU_MAIN_RAM,
  66. (uintptr_t)block, BIGSIZE, BIGSIZE*BIGSIZE,
  67. SIZE, SIZE, SIZE, sizeof(float));
  68. }
  69. else if (rank == 1)
  70. {
  71. starpu_malloc((void **)&block,
  72. SIZE*SIZE*SIZE*sizeof(float));
  73. memset(block, 0, SIZE*SIZE*SIZE*sizeof(float));
  74. starpu_block_data_register(&block_handle, STARPU_MAIN_RAM,
  75. (uintptr_t)block, SIZE, SIZE*SIZE,
  76. SIZE, SIZE, SIZE, sizeof(float));
  77. }
  78. if (rank == 0)
  79. {
  80. MPI_Status status;
  81. ret = starpu_mpi_send(block_handle, 1, 0x42, MPI_COMM_WORLD);
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  83. ret = starpu_mpi_recv(block_handle, 1, 0x1337, MPI_COMM_WORLD, &status);
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  85. /* check the content of the block */
  86. starpu_data_acquire(block_handle, STARPU_R);
  87. unsigned i, j, k;
  88. for (k = 0; k < SIZE; k++)
  89. for (j = 0; j < SIZE; j++)
  90. for (i = 0; i < SIZE; i++)
  91. {
  92. assert(block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] == 33.0f);
  93. }
  94. starpu_data_release(block_handle);
  95. }
  96. else if (rank == 1)
  97. {
  98. MPI_Status status;
  99. ret = starpu_mpi_recv(block_handle, 0, 0x42, MPI_COMM_WORLD, &status);
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  101. /* check the content of the block and modify it */
  102. ret = starpu_data_acquire(block_handle, STARPU_RW);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  104. unsigned i, j, k;
  105. for (k = 0; k < SIZE; k++)
  106. for (j = 0; j < SIZE; j++)
  107. for (i = 0; i < SIZE; i++)
  108. {
  109. assert(block[i + j*SIZE + k*SIZE*SIZE] == 1.0f);
  110. block[i + j*SIZE + k*SIZE*SIZE] = 33.0f;
  111. }
  112. starpu_data_release(block_handle);
  113. ret = starpu_mpi_send(block_handle, 0, 0x1337, MPI_COMM_WORLD);
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  115. }
  116. if (rank == 0 || rank == 1)
  117. {
  118. starpu_data_unregister(block_handle);
  119. starpu_free(block);
  120. }
  121. FPRINTF(stdout, "Rank %d is done\n", rank);
  122. fflush(stdout);
  123. starpu_mpi_shutdown();
  124. if (!mpi_init)
  125. MPI_Finalize();
  126. return 0;
  127. }