Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 40c2cae

Browse files
authored
Merge pull request #206 from facebookresearch/abort_tuning_sigterm
Genetic tuner: std::abort on SIGTERM
2 parents 57d9b0e + c8452aa commit 40c2cae

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

include/tc/autotuner/genetic_tuning_harness.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ namespace tc {
3232
namespace autotune {
3333
namespace detail {
3434

35-
extern volatile std::sig_atomic_t signal_;
36-
extern volatile std::sig_atomic_t killRequested_;
37-
3835
class GeneticTunerHarness {
3936
public:
4037
GeneticTunerHarness(
@@ -50,6 +47,7 @@ class GeneticTunerHarness {
5047
std::vector<MappingOptions> startingPoints,
5148
const TuningParameterFixer& fixedParams);
5249
void run(size_t numGenerations);
50+
void stopAfterCurrentGeneration();
5351

5452
private:
5553
void setupTuningParameters();
@@ -110,6 +108,8 @@ class GeneticTunerHarness {
110108
std::unordered_map<size_t, std::vector<DLTensor*>> outputs_;
111109
const MappingOptions kBaseMapping_;
112110
const std::vector<MappingOptions> kStartingPoints_;
111+
112+
std::atomic_bool stopRequested_{false};
113113
};
114114

115115
std::vector<size_t> parseGpus();

src/autotuner/genetic_autotuner.cc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ std::vector<MappingOptions> GeneticAutotuner::load(
8383
return tc::autotune::restoreCandidates(tcName, inputs, outputs);
8484
}
8585

86+
namespace {
87+
volatile std::sig_atomic_t sigint_ = 0;
88+
volatile std::sig_atomic_t sigterm_ = 0;
89+
} // namespace
90+
8691
llvm::Optional<MappingOptions> GeneticAutotuner::tune(
8792
const std::string& cacheFileName,
8893
const std::string& tcName,
@@ -122,13 +127,13 @@ llvm::Optional<MappingOptions> GeneticAutotuner::tune(
122127
startingPoints,
123128
fixedParams);
124129

125-
std::signal(SIGTERM, [](int sig) {
126-
signal_ = sig;
127-
killRequested_ = 1;
128-
});
129-
std::signal(SIGINT, [](int sig) {
130-
signal_ = sig;
131-
killRequested_ = 1;
130+
sigterm_ = 0;
131+
sigint_ = 0;
132+
auto sigtermOrigHandler = std::signal(SIGTERM, [](int) { sigterm_ = 1; });
133+
auto sigintOrigHandler = std::signal(SIGINT, [](int) { sigint_ = 1; });
134+
ScopeGuard handlersGuard([&]() {
135+
std::signal(SIGTERM, sigtermOrigHandler);
136+
std::signal(SIGINT, sigintOrigHandler);
132137
});
133138

134139
std::atomic_bool tunerFinished(false);
@@ -148,15 +153,23 @@ llvm::Optional<MappingOptions> GeneticAutotuner::tune(
148153
});
149154
while (not tunerFinished) {
150155
std::this_thread::sleep_for(std::chrono::milliseconds(100));
151-
if (killRequested_) {
156+
if (sigint_) {
157+
std::cerr
158+
<< "Autotuning will stop after the current generation has finished."
159+
<< std::endl;
160+
tuner.stopAfterCurrentGeneration();
161+
tunerThread.join();
162+
storeCaches(cacheFileName);
163+
}
164+
if (sigterm_) {
152165
std::cerr << "Autotuning aborted." << std::endl;
153166
storeCaches(cacheFileName);
154-
tunerThread.join();
155-
killRequested_ = 0;
156-
throw std::runtime_error("Abort requested");
167+
std::abort();
157168
}
158169
}
170+
159171
tunerThread.join();
172+
160173
if (tunerThreadEx) {
161174
std::rethrow_exception(tunerThreadEx);
162175
}

src/autotuner/genetic_tuning_harness.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include <atomic>
2020
#include <chrono>
21-
#include <csignal>
2221
#include <numeric>
2322
#include <thread>
2423

@@ -41,9 +40,6 @@ namespace tc {
4140
namespace autotune {
4241
namespace detail {
4342

44-
volatile std::sig_atomic_t signal_ = 0;
45-
volatile std::sig_atomic_t killRequested_ = 0;
46-
4743
GeneticTunerHarness::GeneticTunerHarness(
4844
size_t n,
4945
uint8_t crossoverRate,
@@ -102,12 +98,16 @@ GeneticTunerHarness::GeneticTunerHarness(
10298

10399
void GeneticTunerHarness::run(size_t numGenerations) {
104100
for (size_t i = 0; i < numGenerations; ++i) {
105-
if (not killRequested_) {
101+
if (not stopRequested_) {
106102
runOneGeneration(i);
107103
}
108104
}
109105
}
110106

107+
void GeneticTunerHarness::stopAfterCurrentGeneration() {
108+
stopRequested_ = true;
109+
}
110+
111111
namespace {
112112

113113
std::vector<size_t> filterHigherThan(

0 commit comments

Comments
 (0)