Skip to content

Instantly share code, notes, and snippets.

@geektoni
Last active June 24, 2017 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geektoni/701e01a1ffd06005b695a493579efe51 to your computer and use it in GitHub Desktop.
Save geektoni/701e01a1ffd06005b695a493579efe51 to your computer and use it in GitHub Desktop.
#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
// (it's a stupid method than a fake one actually)
class MockAlg : public CMachine {
public:
MockAlg() : CMachine() {}
~MockAlg() {}
protected:
virtual bool train_machine(CFeatures * feat) {
cout << "Training machine..." << endl;
#pragma omp parallel for
for(int i=0; i<10000; i++)
{
if (cancel_computation())
continue;
pause_computation();
cout << i << endl;
// We set up a delay to simulate long computation
this_thread::sleep_for(chrono::milliseconds(1000));
}
}
virtual void on_pause()
{
m_pause_computation_flag = true;
cout << "PAUSED" << endl;
/* Here you will implement your custom actions */
this_thread::sleep_for(chrono::milliseconds(5000));
resume_computation();
}
};
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.
get_global_signal()->enable_handler();
MockAlg a, b;
a.set_labels(train_labs);
a.train();
b.set_labels(train_labs);
b.train();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment