Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <shogun/base/init.h>
#include <shogun/base/some.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/lib/SGVector.h>
#include <shogun/lib/Signal.h>
#include <shogun/machine/Machine.h>
#include <iostream>
#include <thread>
using namespace shogun;
using namespace std;
// Mock algorithm which implements a fake train_machine method
class MockAlg : public CMachine {
public:
MockAlg() : m_counter(0) {}
~MockAlg() {}
protected:
virtual bool train_machine(CFeatures * feat) {
cout << "Training machine..." << endl;
for(; m_counter<10000; m_counter++)
{
COMPUTATION_CONTROLLERS
// We set up a delay to simulate long computation
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
// on_next() method which implements a custom behavior.
virtual void on_next()
{
m_cancel_computation.store(true);
cout << "We have done " << m_counter << " iterations" << endl;
}
private:
int m_counter;
};
int main() {
init_shogun_with_defaults();
// Set up binary labels
int * labs = new int[2];
labs[0] = -1;
labs[1] = 1;
SGVector<int32_t> labs_v {labs, 2};
auto train_labs = some<CBinaryLabels>(labs_v);
// We enable the signal handler. This way when pressing
// CTRL+C, it will be Shogun handler to catch the SIGINT
// message.
get_global_signal()->enable_handler();
MockAlg a, b;
a.set_labels(train_labs);
b.set_labels(train_labs);
// The MockAlg instance is registered into the signal
// handler by the train() method. The() train method
// is also responsible for unregistering the algorithm when
// the training is ended and also to reset all the
// flags used (so that subsequent calls to the train()
// method can be done without problems).
a.train();
b.train();
return 0;
}
uriel@uriel:Playground$ ./a.out
Training machine...
^C
[ShogunSignalHandler] Immediately return to prompt / Prematurely finish computations / Pause current computation / Do nothing (I/C/P/D)? C
[ShogunSignalHandler] Terminating prematurely current algorithm...
We have done 11 iterations
Training machine...
^C
[ShogunSignalHandler] Immediately return to prompt / Prematurely finish computations / Pause current computation / Do nothing (I/C/P/D)? C
[ShogunSignalHandler] Terminating prematurely current algorithm...
We have done 4 iterations
uriel@uriel:Playground$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.