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) ; |
---|
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_) ; |
---|
24 | if (leader_== commRank) { |
---|
25 | memset( winBufferCurrent_, 0, windowSize ); |
---|
26 | memset( winBufferRetrieved_, 0, windowSize ); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | size_t getToken(void) |
---|
31 | { |
---|
32 | size_t inc=1 ; |
---|
33 | size_t token ; |
---|
34 | MPI_Win_lock(MPI_LOCK_EXCLUSIVE, leader_, 0, winCurrentToken_) ; |
---|
35 | MPI_Fetch_and_op(&inc, &token, MPI_SIZE_T, leader_, 0, MPI_SUM, winCurrentToken_) ; |
---|
36 | MPI_Win_unlock(leader_, winCurrentToken_) ; |
---|
37 | return token ; |
---|
38 | } |
---|
39 | |
---|
40 | bool lockToken(size_t token) |
---|
41 | { |
---|
42 | size_t tokenRead ; |
---|
43 | MPI_Win_lock(MPI_LOCK_SHARED, leader_, 0, winRetrievedToken_) ; |
---|
44 | MPI_Get(&tokenRead, 1, MPI_SIZE_T, leader_, 0, 1, MPI_SIZE_T, winRetrievedToken_ ) ; |
---|
45 | MPI_Win_unlock(leader_, winRetrievedToken_) ; |
---|
46 | if (token==tokenRead) return true ; |
---|
47 | else return false ; |
---|
48 | } |
---|
49 | |
---|
50 | void unlockToken(size_t token) |
---|
51 | { |
---|
52 | size_t inc=1 ; |
---|
53 | size_t tokenRead ; |
---|
54 | MPI_Win_lock(MPI_LOCK_EXCLUSIVE, leader_, 0, winRetrievedToken_) ; |
---|
55 | MPI_Fetch_and_op(&inc, &tokenRead, MPI_SIZE_T, leader_, 0, MPI_SUM, winRetrievedToken_) ; |
---|
56 | MPI_Win_unlock(leader_, winRetrievedToken_) ; |
---|
57 | |
---|
58 | if (token!=tokenRead) ERROR("void CTokenManager::unlockToken(size_t token)",<<"Cannot release token="<<token<< |
---|
59 | " that is not corresponding to the locked token="<<tokenRead) ; |
---|
60 | } |
---|
61 | |
---|
62 | private: |
---|
63 | |
---|
64 | MPI_Win winCurrentToken_ ; |
---|
65 | void* winBufferCurrent_ ; |
---|
66 | MPI_Win winRetrievedToken_ ; |
---|
67 | void* winBufferRetrieved_ ; |
---|
68 | |
---|
69 | int leader_ ; |
---|
70 | |
---|
71 | size_t currentToken_=0 ; |
---|
72 | size_t retrievedToken_=0 ; |
---|
73 | |
---|
74 | |
---|
75 | } ; |
---|
76 | |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | #endif |
---|