block_interface_pinned.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #define NITER 2048
  19. #define BIGSIZE 64
  20. #define SIZE 64
  21. int main(int argc, char **argv)
  22. {
  23. MPI_Init(NULL, NULL);
  24. int rank, size;
  25. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  26. MPI_Comm_size(MPI_COMM_WORLD, &size);
  27. if (size < 2)
  28. {
  29. if (rank == 0)
  30. fprintf(stderr, "We need at least processes.\n");
  31. MPI_Finalize();
  32. return 0;
  33. }
  34. /* We only use 2 nodes for that test */
  35. if (rank >= 2)
  36. {
  37. MPI_Finalize();
  38. return 0;
  39. }
  40. starpu_init(NULL);
  41. starpu_mpi_initialize();
  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;
  47. starpu_data_handle block_handle;
  48. if (rank == 0)
  49. {
  50. starpu_data_malloc_pinned_if_possible((void **)&block,
  51. BIGSIZE*BIGSIZE*BIGSIZE*sizeof(float));
  52. memset(block, 0, BIGSIZE*BIGSIZE*BIGSIZE*sizeof(float));
  53. /* fill the inner block */
  54. unsigned i, j, k;
  55. for (k = 0; k < SIZE; k++)
  56. for (j = 0; j < SIZE; j++)
  57. for (i = 0; i < SIZE; i++)
  58. {
  59. block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] = 1.0f;
  60. }
  61. starpu_block_data_register(&block_handle, 0,
  62. (uintptr_t)block, BIGSIZE, BIGSIZE*BIGSIZE,
  63. SIZE, SIZE, SIZE, sizeof(float));
  64. }
  65. else /* rank == 1 */
  66. {
  67. starpu_data_malloc_pinned_if_possible((void **)&block,
  68. SIZE*SIZE*SIZE*sizeof(float));
  69. memset(block, 0, SIZE*SIZE*SIZE*sizeof(float));
  70. starpu_block_data_register(&block_handle, 0,
  71. (uintptr_t)block, SIZE, SIZE*SIZE,
  72. SIZE, SIZE, SIZE, sizeof(float));
  73. }
  74. if (rank == 0)
  75. {
  76. starpu_mpi_send(block_handle, 1, 0x42, MPI_COMM_WORLD);
  77. MPI_Status status;
  78. starpu_mpi_recv(block_handle, 1, 0x1337, MPI_COMM_WORLD, &status);
  79. /* check the content of the block */
  80. starpu_data_acquire(block_handle, STARPU_R);
  81. unsigned i, j, k;
  82. for (k = 0; k < SIZE; k++)
  83. for (j = 0; j < SIZE; j++)
  84. for (i = 0; i < SIZE; i++)
  85. {
  86. assert(block[i + j*BIGSIZE + k*BIGSIZE*BIGSIZE] == 33.0f);
  87. }
  88. starpu_data_release(block_handle);
  89. }
  90. else /* rank == 1 */
  91. {
  92. MPI_Status status;
  93. starpu_mpi_recv(block_handle, 0, 0x42, MPI_COMM_WORLD, &status);
  94. /* check the content of the block and modify it */
  95. starpu_data_acquire(block_handle, STARPU_RW);
  96. unsigned i, j, k;
  97. for (k = 0; k < SIZE; k++)
  98. for (j = 0; j < SIZE; j++)
  99. for (i = 0; i < SIZE; i++)
  100. {
  101. assert(block[i + j*SIZE + k*SIZE*SIZE] == 1.0f);
  102. block[i + j*SIZE + k*SIZE*SIZE] = 33.0f;
  103. }
  104. starpu_data_release(block_handle);
  105. starpu_mpi_send(block_handle, 0, 0x1337, MPI_COMM_WORLD);
  106. }
  107. fprintf(stdout, "Rank %d is done\n", rank);
  108. fflush(stdout);
  109. starpu_mpi_shutdown();
  110. starpu_shutdown();
  111. MPI_Finalize();
  112. return 0;
  113. }