source: XIOS3/trunk/src/manager/token_manager.hpp @ 2486

Last change on this file since 2486 was 2462, checked in by jderouillat, 17 months ago

Replaced MPI_Win_create calls by MPI_Win_allocate calls to avoid deadlock on JeanZay?

  • Property svn:executable set to *
File size: 2.2 KB
Line 
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
8namespace 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      }
25
26      size_t getToken(void)
27      {
28        size_t inc=1 ;
29        size_t token ;
30        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, leader_, 0, winCurrentToken_) ;
31        MPI_Fetch_and_op(&inc, &token, MPI_SIZE_T, leader_, 0, MPI_SUM, winCurrentToken_) ;
32        MPI_Win_unlock(leader_, winCurrentToken_) ;
33        return token ;
34      }
35
36      bool lockToken(size_t token)
37      {
38        size_t tokenRead ;
39        MPI_Win_lock(MPI_LOCK_SHARED, leader_, 0, winRetrievedToken_) ;
40        MPI_Get(&tokenRead, 1, MPI_SIZE_T, leader_, 0, 1, MPI_SIZE_T, winRetrievedToken_ ) ;
41        MPI_Win_unlock(leader_, winRetrievedToken_) ;
42        if (token==tokenRead) return true ;
43        else return false ;
44      }
45
46      void unlockToken(size_t token)
47      {
48        size_t inc=1 ;
49        size_t tokenRead ;
50        MPI_Win_lock(MPI_LOCK_EXCLUSIVE, leader_, 0, winRetrievedToken_) ;
51        MPI_Fetch_and_op(&inc, &tokenRead, MPI_SIZE_T, leader_, 0, MPI_SUM, winRetrievedToken_) ;
52        MPI_Win_unlock(leader_, winRetrievedToken_) ;
53       
54        if (token!=tokenRead)  ERROR("void CTokenManager::unlockToken(size_t token)",<<"Cannot release token="<<token<<
55                                     " that is not corresponding to the locked token="<<tokenRead) ;     
56      }
57
58    private:
59
60      MPI_Win winCurrentToken_ ;
61      void* winBufferCurrent_ ;
62      MPI_Win winRetrievedToken_ ;
63      void* winBufferRetrieved_ ;
64     
65      int leader_ ;
66
67      size_t currentToken_=0 ;
68      size_t retrievedToken_=0 ;
69
70
71  } ;
72
73
74}
75
76#endif
Note: See TracBrowser for help on using the repository browser.