Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5. #include <string.h>
  6.  
  7. static int allocate_window(void * baseptr, size_t nbytes, MPI_Win *win);
  8. static int deallocate_window(void * baseptr, MPI_Win *win);
  9.  
  10. #define COUNT 10
  11. int main(int argc, char** argv)
  12. {
  13. int myrank, nprocs, nprocs_per_node;
  14.  
  15. MPI_Init(&argc, &argv);
  16.  
  17. MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  18. MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  19.  
  20. if (nprocs != 2)
  21. {
  22. if (myrank == 0) fprintf(stderr, "usage: the number of processes must be 2!\n");
  23. MPI_Abort(MPI_COMM_WORLD, 1);
  24. return EXIT_FAILURE;
  25. }
  26.  
  27. MPI_Win w1;
  28.  
  29. int *w1_mem;
  30.  
  31. //allocate window 1
  32. allocate_window(&w1_mem, COUNT * sizeof(int), &w1);
  33.  
  34. for (size_t idx = 0; idx < COUNT; ++idx) {
  35. w1_mem[idx] = idx * COUNT + 1 + myrank;
  36. }
  37.  
  38. MPI_Win_lock_all(0, w1);
  39.  
  40. MPI_Aint ldisp;
  41. //Displacements of Window 1
  42. MPI_Aint *w1_disps = malloc(nprocs * sizeof(MPI_Aint));
  43. assert(MPI_Get_address(w1_mem, &ldisp) == MPI_SUCCESS);
  44. assert(MPI_Allgather(&ldisp, 1, MPI_AINT, w1_disps, 1, MPI_AINT, MPI_COMM_WORLD) == MPI_SUCCESS);
  45.  
  46.  
  47. /**************************************************************************
  48. * Communication with a remote rank --> Works
  49. **************************************************************************/
  50.  
  51. int other = 1 - myrank;
  52. int recv_buf[COUNT];
  53. printf("rank %d reads memory from rank %d\n", myrank, other);
  54. MPI_Get(recv_buf, COUNT, MPI_INT, other, w1_disps[other], COUNT, MPI_INT, w1);
  55. MPI_Win_flush_all(w1);
  56. //verify values
  57. for (int idx = 0; idx < COUNT; ++idx) {
  58. assert(recv_buf[idx] == idx * COUNT + 1 + other);
  59. }
  60.  
  61.  
  62. /**************************************************************************
  63. * Communication with local memory --> Hangs
  64. **************************************************************************/
  65. printf("rank %d reads memory from rank %d\n", myrank, myrank);
  66. MPI_Get(recv_buf, COUNT, MPI_INT, myrank, w1_disps[myrank], COUNT, MPI_INT, w1);
  67.  
  68. //verify values
  69. for (int idx = 0; idx < COUNT; ++idx) {
  70. assert(recv_buf[idx] == idx * COUNT + 1 + myrank);
  71. }
  72.  
  73. MPI_Win_unlock_all(w1);
  74.  
  75. MPI_Barrier(MPI_COMM_WORLD);
  76.  
  77. deallocate_window(w1_mem, &w1);
  78.  
  79. free(w1_disps);
  80.  
  81. printf("Test finished\n");
  82.  
  83. MPI_Finalize();
  84. return EXIT_SUCCESS;
  85. }
  86.  
  87.  
  88. static int allocate_window(void * baseptr, size_t nbytes, MPI_Win *win)
  89. {
  90. assert(MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, win) == MPI_SUCCESS);
  91. assert(MPI_Alloc_mem(nbytes, MPI_INFO_NULL, baseptr) == MPI_SUCCESS);
  92. return MPI_Win_attach(*win, baseptr, nbytes);
  93. }
  94.  
  95. static int deallocate_window(void * baseptr, MPI_Win *win) {
  96. MPI_Win_detach(*win, baseptr);
  97. MPI_Win_free(win);
  98. MPI_Free_mem(baseptr);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement