block_interface.c 3.9 KB

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