block_interface.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  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(NULL, NULL);
  27. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  28. MPI_Comm_size(MPI_COMM_WORLD, &size);
  29. if (size < 2)
  30. {
  31. if (rank == 0)
  32. FPRINTF(stderr, "We need at least processes.\n");
  33. MPI_Finalize();
  34. return STARPU_TEST_SKIPPED;
  35. }
  36. /* We only use 2 nodes for that test */
  37. if (rank >= 2)
  38. {
  39. MPI_Finalize();
  40. return STARPU_TEST_SKIPPED;
  41. }
  42. ret = starpu_init(NULL);
  43. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  44. ret = starpu_mpi_initialize();
  45. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_initialize");
  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;
  51. starpu_data_handle_t block_handle;
  52. if (rank == 0)
  53. {
  54. block = calloc(BIGSIZE*BIGSIZE*BIGSIZE, sizeof(float));
  55. assert(block);
  56. /* fill the inner block */
  57. unsigned i, j, k;
  58. for (k = 0; k < SIZE; k++)
  59. for (j = 0; j < SIZE; j++)
  60. for (i = 0; i < SIZE; i++)
  61. {
  62. block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] = 1.0f;
  63. }
  64. starpu_block_data_register(&block_handle, 0,
  65. (uintptr_t)block, BIGSIZE, BIGSIZE*BIGSIZE,
  66. SIZE, SIZE, SIZE, sizeof(float));
  67. }
  68. else /* rank == 1 */
  69. {
  70. block = calloc(SIZE*SIZE*SIZE, sizeof(float));
  71. assert(block);
  72. starpu_block_data_register(&block_handle, 0,
  73. (uintptr_t)block, SIZE, SIZE*SIZE,
  74. SIZE, SIZE, SIZE, sizeof(float));
  75. }
  76. if (rank == 0)
  77. {
  78. ret = starpu_mpi_send(block_handle, 1, 0x42, MPI_COMM_WORLD);
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  80. MPI_Status status;
  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. ret = starpu_data_acquire(block_handle, STARPU_R);
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  86. unsigned i, j, k;
  87. for (k = 0; k < SIZE; k++)
  88. for (j = 0; j < SIZE; j++)
  89. for (i = 0; i < SIZE; i++)
  90. {
  91. assert(block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] == 33.0f);
  92. }
  93. starpu_data_release(block_handle);
  94. }
  95. else /* rank == 1 */
  96. {
  97. MPI_Status status;
  98. ret = starpu_mpi_recv(block_handle, 0, 0x42, MPI_COMM_WORLD, &status);
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_recv");
  100. /* check the content of the block and modify it */
  101. ret = starpu_data_acquire(block_handle, STARPU_RW);
  102. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  103. unsigned i, j, k;
  104. for (k = 0; k < SIZE; k++)
  105. for (j = 0; j < SIZE; j++)
  106. for (i = 0; i < SIZE; i++)
  107. {
  108. assert(block[i + j*SIZE + k*SIZE*SIZE] == 1.0f);
  109. block[i + j*SIZE + k*SIZE*SIZE] = 33.0f;
  110. }
  111. starpu_data_release(block_handle);
  112. ret = starpu_mpi_send(block_handle, 0, 0x1337, MPI_COMM_WORLD);
  113. STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_send");
  114. }
  115. FPRINTF(stdout, "Rank %d is done\n", rank);
  116. fflush(stdout);
  117. starpu_mpi_shutdown();
  118. starpu_shutdown();
  119. MPI_Finalize();
  120. return 0;
  121. }