Skip to content

Commit 81327f5

Browse files
committed
wip: Listen to signal interuption and do early exit
1 parent e64c6dd commit 81327f5

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/core/InfomapBase.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
#include <cstdlib>
3131
#include <algorithm>
3232
#include <cmath>
33+
#include <csignal>
3334

3435
#ifdef _OPENMP
3536
#include <omp.h>
3637
#endif
3738

3839
namespace infomap {
3940

41+
bool InfomapBase::shouldExit = false;
42+
4043
std::map<unsigned int, std::vector<unsigned int>> InfomapBase::getMultilevelModules(bool states)
4144
{
4245
if (haveMemory() && !states) {
@@ -95,6 +98,12 @@ unsigned int InfomapBase::maxTreeDepth() const
9598
return maxDepth;
9699
}
97100

101+
void InfomapBase::signalHandler(int signal)
102+
{
103+
// shouldExit.store(true);
104+
InfomapBase::shouldExit = true;
105+
}
106+
98107
// ===================================================
99108
// Run
100109
// ===================================================
@@ -106,6 +115,12 @@ void InfomapBase::run(const std::string& parameters)
106115
return;
107116
}
108117

118+
// Register signal handlers
119+
std::signal(SIGINT, InfomapBase::signalHandler);
120+
std::signal(SIGTERM, InfomapBase::signalHandler);
121+
// shouldExit.store(false);
122+
shouldExit = false;
123+
109124
m_elapsedTime = Stopwatch(true);
110125
m_startDate = Date();
111126

src/core/InfomapBase.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string>
3030
#include <iostream>
3131
#include <sstream>
32+
#include <atomic>
3233

3334
namespace infomap {
3435

@@ -171,6 +172,7 @@ class InfomapBase : public InfomapConfig<InfomapBase> {
171172
void run(Network& network);
172173

173174
private:
175+
static void signalHandler(int signal);
174176
bool isFullNetwork() const { return m_isMain && m_aggregationLevel == 0; }
175177
bool isFirstLoop() const { return m_tuneIterationIndex == 0 && isFullNetwork(); }
176178

@@ -516,6 +518,10 @@ class InfomapBase : public InfomapConfig<InfomapBase> {
516518
std::string m_initialParameters;
517519
std::string m_currentParameters;
518520

521+
// Atomic flag to indicate the program should exit
522+
// static std::atomic<bool> shouldExit;
523+
static bool shouldExit;
524+
519525
std::unique_ptr<InfomapOptimizerBase> m_optimizer;
520526
};
521527

src/core/InfomapOptimizer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <set>
2121
#include <utility>
2222

23+
#include <chrono>
24+
#include <thread>
25+
2326
namespace infomap {
2427

2528
template <typename Objective>
@@ -272,6 +275,12 @@ inline unsigned int InfomapOptimizer<Objective>::optimizeActiveNetwork()
272275
}
273276

274277
do {
278+
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
279+
if (m_infomap->shouldExit) {
280+
Log() << "\nGot interrupt signal, early exiting.\n";
281+
return numEffectiveLoops;
282+
}
283+
275284
++coreLoopCount;
276285
unsigned int numNodesMoved = m_infomap->innerParallelization
277286
? tryMoveEachNodeIntoBestModuleInParallel()

0 commit comments

Comments
 (0)