21 BooleanSemaphoreService::getInstance().releaseLock(_semaphoreNumber);
22 _localRankLockRequestSemaphore.leaveCriticalSection();
26 _semaphoreNumber( _semaphoreCounter ) {
36 rankThatLastLocked(-1) {
42 return "(locked,by-rank=" + std::to_string(rankThatLastLocked) +
")";
45 return "(free,last-lock-by-rank=" + std::to_string(rankThatLastLocked) +
")";
60 std::ostringstream msg;
62 msg <<
"#sections:" <<
_map.size();
64 msg <<
"," << p.first <<
":" << p.second.toString();
68 msg <<
"," <<
"lock " << p.second <<
" from " << p.first;
84 if ( not
_map[number].locked ) {
86 _map[number].locked =
true;
87 _map[number].rankThatLastLocked = forRank;
88 logDebug(
"tryLockSemaphoreOnGlobalMaster(int,int)",
"successfully locked semaphore " << number <<
" for rank " << forRank );
101 logDebug(
"lockSemaphoreOnGlobalMaster(int,int)",
"will try to lock semahpore " << number <<
" for rank " << forRank );
103 bool gotLock =
false;
104 bool wroteWarning =
false;
105 while (not gotLock) {
110 logWarning(
"lockSemaphoreOnGlobalMaster()",
"semaphore " << number <<
" is locked by " <<
_map[number].rankThatLastLocked <<
" and therefore cannot be locked for " << forRank );
126 _map[number].locked =
false;
128 logDebug(
"unlockSemaphoreOnGlobalMaster()",
"successfully released lock " << number <<
". state=" <<
toString() );
138 bool servedLockRequest =
true;
139 while (servedLockRequest) {
140 servedLockRequest =
false;
144 servedLockRequest =
true;
145 logDebug(
"receiveDanglingMessages()",
"locked sempahore " << request->second <<
" for rank " << request->first <<
". state=" <<
toString() );
168 logDebug(
"receiveDanglingMessages()",
"there's a pending message from " << status.MPI_SOURCE );
172 logDebug(
"receiveDanglingMessages()",
"received number " << number <<
" from rank " << status.MPI_SOURCE );
175 std::pair<int,int> newEntry( status.MPI_SOURCE, number );
208 if (
_map.count(number)==0 ) {
224 logDebug(
"acquireLock()",
"wait for confirmation from global master rank" );
238 MPI_Test(&request, &flag, MPI_STATUS_IGNORE);
255 logDebug(
"releaseLock()",
"send global master " << number <<
" to release global lock" );
#define assertion2(expr, param0, param1)
#define assertionEquals(lhs, rhs)
#define assertion1(expr, param)
#define assertionMsg(expr, message)
#define logDebug(methodName, logMacroMessageStream)
#define logWarning(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
int _semaphoreTag
Tag used to exchange locking MPI messages.
tarch::multicore::BooleanSemaphore _mapAccessSemaphore
Semaphore for the global master's map of locks.
std::string toString() const
void acquireLock(int number)
Acquire the lock.
virtual void receiveDanglingMessages() override
Receive any lock-related dangling messages.
bool tryLockSemaphoreOnGlobalMaster(int number, int forRank)
Try to lock a semaphore.
tarch::multicore::BooleanSemaphore _reserverationRequestsSemaphore
std::vector< std::pair< int, int > > _pendingLockRequests
List of pending lock requests.
void releaseLock(int number)
Release a lock.
void lockSemaphoreOnGlobalMaster(int number, int forRank)
static BooleanSemaphoreService _singleton
void serveLockRequests()
Serve pending lock requests.
static BooleanSemaphoreService & getInstance()
Don't use this routine.
void addMapEntryLazily(int number)
Add map entry.
std::map< int, SemaphoreMapEntry > _map
Map of semaphores.
virtual void shutdown() override
int getNumberOfLockedSemaphores()
void unlockSemaphoreOnGlobalMaster(int number, int forRank)
Unlock semaphore on global master.
static tarch::logging::Log _log
static int _semaphoreCounter
void leaveCriticalSection()
Tells the semaphore that it is about to leave.
tarch::multicore::BooleanSemaphore _localRankLockRequestSemaphore
const int _semaphoreNumber
void enterCriticalSection()
Waits until I can enter the critical section.
BooleanSemaphore()
Create new boolean semaphore spanning all MPI ranks.
static int getGlobalMasterRank()
Get the global master.
void triggerDeadlockTimeOut(const std::string &className, const std::string &methodName, int communicationPartnerRank, int tag, int numberOfExpectedMessages=1, const std::string &comment="")
Triggers a time out and shuts down the cluster if a timeout is violated.
void setDeadlockWarningTimeStamp()
Memorise global timeout.
void writeTimeOutWarning(const std::string &className, const std::string &methodName, int communicationPartnerRank, int tag, int numberOfExpectedMessages=1)
Writes a warning if relevant.
void setDeadlockTimeOutTimeStamp()
static Rank & getInstance()
This operation returns the singleton instance.
static int reserveFreeTag(const std::string &fullQualifiedMessageName, int numberOfTags=1)
Return a Free Tag.
MPI_Comm getCommunicator() const
void enterCriticalSection()
Create a lock around a boolean semaphore region.
virtual void receiveDanglingMessages() override
Answer to MPI Messages.
static ServiceRepository & getInstance()
void removeService(Service *const service)
This routine is thread-safe, i.e.
void addService(Service *const service, const std::string &name)
Add a new service.
SemaphoreMapEntry()
Create new semaphore entry.
std::string toString() const