Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* StoppableSGObject class * add base class to swig
- Loading branch information
1 parent
bf67562
commit 986f97e
Showing
5 changed files
with
194 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* This software is distributed under BSD 3-clause license (see LICENSE file). | ||
* | ||
* Authors: Shubham Shukla | ||
*/ | ||
|
||
#include <rxcpp/rx-lite.hpp> | ||
#include <shogun/base/init.h> | ||
#include <shogun/lib/Signal.h> | ||
#include <shogun/lib/StoppableSGObject.h> | ||
|
||
using namespace shogun; | ||
|
||
#define COMPUTATION_CONTROLLERS \ | ||
if (cancel_computation()) \ | ||
continue; \ | ||
pause_computation(); | ||
|
||
CStoppableSGObject::CStoppableSGObject() : CSGObject(){}; | ||
|
||
CStoppableSGObject::~CStoppableSGObject(){}; | ||
|
||
void CStoppableSGObject::init_stoppable() | ||
{ | ||
m_cancel_computation = false; | ||
m_pause_computation_flag = false; | ||
} | ||
|
||
rxcpp::subscription CStoppableSGObject::connect_to_signal_handler() | ||
{ | ||
// Subscribe this algorithm to the signal handler | ||
auto subscriber = rxcpp::make_subscriber<int>( | ||
[this](int i) { | ||
if (i == SG_PAUSE_COMP) | ||
this->on_pause(); | ||
else | ||
this->on_next(); | ||
}, | ||
[this]() { this->on_complete(); }); | ||
return get_global_signal()->get_observable()->subscribe(subscriber); | ||
} | ||
|
||
void CStoppableSGObject::reset_computation_variables() | ||
{ | ||
m_cancel_computation = false; | ||
m_pause_computation_flag = false; | ||
} | ||
|
||
void CStoppableSGObject::on_next() | ||
{ | ||
m_cancel_computation.store(true); | ||
on_next_impl(); | ||
} | ||
|
||
void CStoppableSGObject::on_pause() | ||
{ | ||
m_pause_computation_flag.store(true); | ||
on_pause_impl(); | ||
resume_computation(); | ||
} | ||
|
||
void CStoppableSGObject::on_complete() | ||
{ | ||
on_complete_impl(); | ||
} | ||
void CStoppableSGObject::on_next_impl() | ||
{ | ||
} | ||
void CStoppableSGObject::on_pause_impl() | ||
{ | ||
} | ||
void CStoppableSGObject::on_complete_impl() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* This software is distributed under BSD 3-clause license (see LICENSE file). | ||
* | ||
* Authors: Shubham Shukla | ||
*/ | ||
|
||
#ifndef __STOPPABLESGOBJECT_H_ | ||
#define __STOPPABLESGOBJECT_H_ | ||
|
||
#include <shogun/base/SGObject.h> | ||
#include <shogun/base/init.h> | ||
|
||
#include <condition_variable> | ||
#include <mutex> | ||
|
||
namespace shogun | ||
{ | ||
#define COMPUTATION_CONTROLLERS \ | ||
if (cancel_computation()) \ | ||
continue; \ | ||
pause_computation(); | ||
|
||
/** | ||
* Class that abstracts all premature stopping code | ||
*/ | ||
class CStoppableSGObject : public CSGObject | ||
{ | ||
public: | ||
/** constructor */ | ||
CStoppableSGObject(); | ||
|
||
/** destructor */ | ||
virtual ~CStoppableSGObject(); | ||
|
||
/** init flags to false as default */ | ||
void init_stoppable(); | ||
|
||
#ifndef SWIG | ||
/** @return whether the algorithm needs to be stopped */ | ||
SG_FORCED_INLINE bool cancel_computation() const | ||
{ | ||
return m_cancel_computation.load(); | ||
} | ||
#endif | ||
|
||
#ifndef SWIG | ||
/** Pause the algorithm if the flag is set */ | ||
SG_FORCED_INLINE void pause_computation() | ||
{ | ||
if (m_pause_computation_flag.load()) | ||
{ | ||
std::unique_lock<std::mutex> lck(m_mutex); | ||
while (m_pause_computation_flag.load()) | ||
m_pause_computation.wait(lck); | ||
} | ||
} | ||
#endif | ||
|
||
#ifndef SWIG | ||
/** Resume current computation (sets the flag) */ | ||
SG_FORCED_INLINE void resume_computation() | ||
{ | ||
std::unique_lock<std::mutex> lck(m_mutex); | ||
m_pause_computation_flag = false; | ||
m_pause_computation.notify_all(); | ||
} | ||
#endif | ||
|
||
virtual const char* get_name() const | ||
{ | ||
return "StoppableSGObject"; | ||
} | ||
|
||
protected: | ||
/** connect the machine instance to the signal handler */ | ||
rxcpp::subscription connect_to_signal_handler(); | ||
|
||
/** reset the computation variables */ | ||
void reset_computation_variables(); | ||
|
||
/** sets cancel computation flag */ | ||
void on_next(); | ||
|
||
/** The action which will be done when the user decides to | ||
* premature stop the CMachine execution */ | ||
virtual void on_next_impl(); | ||
|
||
/** sets pause computation flag and resumes after action is complete */ | ||
void on_pause(); | ||
|
||
/** The action which will be done when the user decides to | ||
* pause the CMachine execution */ | ||
virtual void on_pause_impl(); | ||
|
||
/** These actions which will be done when the user decides to | ||
* return to prompt and terminate the program execution */ | ||
void on_complete(); | ||
virtual void on_complete_impl(); | ||
|
||
/** Cancel computation */ | ||
std::atomic<bool> m_cancel_computation; | ||
|
||
/** Pause computation flag */ | ||
std::atomic<bool> m_pause_computation_flag; | ||
|
||
/** Conditional variable to make threads wait */ | ||
std::condition_variable m_pause_computation; | ||
|
||
/** Mutex used to pause threads */ | ||
std::mutex m_mutex; | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters