File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 30
30
#include < cstdlib>
31
31
#include < algorithm>
32
32
#include < cmath>
33
+ #include < csignal>
33
34
34
35
#ifdef _OPENMP
35
36
#include < omp.h>
36
37
#endif
37
38
38
39
namespace infomap {
39
40
41
+ bool InfomapBase::shouldExit = false ;
42
+
40
43
std::map<unsigned int , std::vector<unsigned int >> InfomapBase::getMultilevelModules (bool states)
41
44
{
42
45
if (haveMemory () && !states) {
@@ -95,6 +98,12 @@ unsigned int InfomapBase::maxTreeDepth() const
95
98
return maxDepth;
96
99
}
97
100
101
+ void InfomapBase::signalHandler (int signal)
102
+ {
103
+ // shouldExit.store(true);
104
+ InfomapBase::shouldExit = true ;
105
+ }
106
+
98
107
// ===================================================
99
108
// Run
100
109
// ===================================================
@@ -106,6 +115,12 @@ void InfomapBase::run(const std::string& parameters)
106
115
return ;
107
116
}
108
117
118
+ // Register signal handlers
119
+ std::signal (SIGINT, InfomapBase::signalHandler);
120
+ std::signal (SIGTERM, InfomapBase::signalHandler);
121
+ // shouldExit.store(false);
122
+ shouldExit = false ;
123
+
109
124
m_elapsedTime = Stopwatch (true );
110
125
m_startDate = Date ();
111
126
Original file line number Diff line number Diff line change 29
29
#include < string>
30
30
#include < iostream>
31
31
#include < sstream>
32
+ #include < atomic>
32
33
33
34
namespace infomap {
34
35
@@ -171,6 +172,7 @@ class InfomapBase : public InfomapConfig<InfomapBase> {
171
172
void run (Network& network);
172
173
173
174
private:
175
+ static void signalHandler (int signal);
174
176
bool isFullNetwork () const { return m_isMain && m_aggregationLevel == 0 ; }
175
177
bool isFirstLoop () const { return m_tuneIterationIndex == 0 && isFullNetwork (); }
176
178
@@ -516,6 +518,10 @@ class InfomapBase : public InfomapConfig<InfomapBase> {
516
518
std::string m_initialParameters;
517
519
std::string m_currentParameters;
518
520
521
+ // Atomic flag to indicate the program should exit
522
+ // static std::atomic<bool> shouldExit;
523
+ static bool shouldExit;
524
+
519
525
std::unique_ptr<InfomapOptimizerBase> m_optimizer;
520
526
};
521
527
Original file line number Diff line number Diff line change 20
20
#include < set>
21
21
#include < utility>
22
22
23
+ #include < chrono>
24
+ #include < thread>
25
+
23
26
namespace infomap {
24
27
25
28
template <typename Objective>
@@ -272,6 +275,12 @@ inline unsigned int InfomapOptimizer<Objective>::optimizeActiveNetwork()
272
275
}
273
276
274
277
do {
278
+ std::this_thread::sleep_for (std::chrono::nanoseconds (1 ));
279
+ if (m_infomap->shouldExit ) {
280
+ Log () << " \n Got interrupt signal, early exiting.\n " ;
281
+ return numEffectiveLoops;
282
+ }
283
+
275
284
++coreLoopCount;
276
285
unsigned int numNodesMoved = m_infomap->innerParallelization
277
286
? tryMoveEachNodeIntoBestModuleInParallel ()
You can’t perform that action at this time.
0 commit comments