[2455] | 1 | #ifndef __TOKEN_MANAGER_HPP__ |
---|
| 2 | #define __TOKEN_MANAGER_HPP__ |
---|
| 3 | |
---|
| 4 | #include "xios_spl.hpp" |
---|
| 5 | #include "exception.hpp" |
---|
| 6 | #include "mpi.hpp" |
---|
| 7 | |
---|
| 8 | namespace xios |
---|
| 9 | { |
---|
| 10 | |
---|
| 11 | class CTokenManager |
---|
| 12 | { |
---|
| 13 | |
---|
| 14 | public: |
---|
| 15 | CTokenManager(MPI_Comm& comm, int leader) : leader_(leader) |
---|
| 16 | { |
---|
| 17 | int commRank ; |
---|
| 18 | MPI_Comm_rank(comm, &commRank) ; |
---|
| 19 | MPI_Aint size = 0 ; |
---|
| 20 | if (leader_== commRank) size = sizeof(size_t) ; |
---|
[2462] | 21 | const MPI_Aint windowSize=sizeof(size_t); |
---|
| 22 | MPI_Win_allocate(windowSize, 1, MPI_INFO_NULL, comm, &winBufferCurrent_, &winCurrentToken_) ; |
---|
| 23 | MPI_Win_allocate(windowSize, 1, MPI_INFO_NULL, comm, &winBufferRetrieved_, &winRetrievedToken_) ; |
---|
[2498] | 24 | if (leader_== commRank) { |
---|
| 25 | memset( winBufferCurrent_, 0, windowSize ); |
---|
| 26 | memset( winBufferRetrieved_, 0, windowSize ); |
---|
| 27 | } |
---|
[2547] | 28 | MPI_Win_lock_all(0, winCurrentToken_) ; |
---|
| 29 | MPI_Win_lock_all(0, winRetrievedToken_) ; |
---|
[2455] | 30 | } |
---|
[2547] | 31 | |
---|
| 32 | ~CTokenManager() |
---|
| 33 | { |
---|
| 34 | MPI_Win_unlock_all(winCurrentToken_) ; |
---|
| 35 | MPI_Win_unlock_all(winRetrievedToken_) ; |
---|
| 36 | MPI_Win_free(&winCurrentToken_) ; |
---|
| 37 | MPI_Win_free(&winRetrievedToken_) ; |
---|
| 38 | } |
---|
| 39 | |
---|
[2455] | 40 | size_t getToken(void) |
---|
| 41 | { |
---|
| 42 | size_t inc=1 ; |
---|
| 43 | size_t token ; |
---|
| 44 | MPI_Fetch_and_op(&inc, &token, MPI_SIZE_T, leader_, 0, MPI_SUM, winCurrentToken_) ; |
---|
[2547] | 45 | MPI_Win_flush(leader_, winCurrentToken_); |
---|
[2455] | 46 | return token ; |
---|
| 47 | } |
---|
| 48 | |
---|
[2547] | 49 | bool checkToken(size_t token) |
---|
[2455] | 50 | { |
---|
| 51 | size_t tokenRead ; |
---|
[2547] | 52 | size_t inc=0 ; |
---|
| 53 | MPI_Fetch_and_op(&inc, &tokenRead, MPI_SIZE_T, leader_, 0, MPI_NO_OP, winRetrievedToken_) ; |
---|
| 54 | MPI_Win_flush(leader_, winRetrievedToken_); |
---|
| 55 | return tokenRead==token ; |
---|
[2455] | 56 | } |
---|
[2547] | 57 | |
---|
| 58 | void updateToken(size_t token) |
---|
[2455] | 59 | { |
---|
| 60 | size_t inc=1 ; |
---|
| 61 | size_t tokenRead ; |
---|
[2547] | 62 | MPI_Fetch_and_op(&inc, &tokenRead, MPI_SIZE_T, leader_, 0, MPI_SUM, winRetrievedToken_) ; |
---|
| 63 | MPI_Win_flush(leader_, winRetrievedToken_); |
---|
| 64 | if (token!=tokenRead) ERROR("void CTokenManager::unlockToken(size_t token)",<<"Cannot release token="<<token<< |
---|
| 65 | " that is not corresponding to the locked token="<<tokenRead) ; |
---|
| 66 | } |
---|
| 67 | /* void unlockToken(size_t token) |
---|
| 68 | { |
---|
| 69 | size_t inc=1 ; |
---|
| 70 | size_t tokenRead ; |
---|
[2455] | 71 | MPI_Win_lock(MPI_LOCK_EXCLUSIVE, leader_, 0, winRetrievedToken_) ; |
---|
| 72 | MPI_Fetch_and_op(&inc, &tokenRead, MPI_SIZE_T, leader_, 0, MPI_SUM, winRetrievedToken_) ; |
---|
| 73 | MPI_Win_unlock(leader_, winRetrievedToken_) ; |
---|
| 74 | |
---|
| 75 | if (token!=tokenRead) ERROR("void CTokenManager::unlockToken(size_t token)",<<"Cannot release token="<<token<< |
---|
| 76 | " that is not corresponding to the locked token="<<tokenRead) ; |
---|
| 77 | } |
---|
[2547] | 78 | */ |
---|
[2455] | 79 | private: |
---|
| 80 | |
---|
| 81 | MPI_Win winCurrentToken_ ; |
---|
[2462] | 82 | void* winBufferCurrent_ ; |
---|
[2455] | 83 | MPI_Win winRetrievedToken_ ; |
---|
[2462] | 84 | void* winBufferRetrieved_ ; |
---|
[2455] | 85 | |
---|
| 86 | int leader_ ; |
---|
| 87 | |
---|
| 88 | size_t currentToken_=0 ; |
---|
| 89 | size_t retrievedToken_=0 ; |
---|
| 90 | |
---|
| 91 | |
---|
| 92 | } ; |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | } |
---|
| 96 | |
---|
[2462] | 97 | #endif |
---|