Skip to content

Commit

Permalink
inherit stoppable sgobject code in CMachineEvaluation (#4291)
Browse files Browse the repository at this point in the history
* remove premature stopping code from cmachine evaluation
* remove extra includes
  • Loading branch information
shubham808 authored and karlnapf committed May 25, 2018
1 parent 5d65aa6 commit 024e465
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 106 deletions.
4 changes: 2 additions & 2 deletions src/shogun/evaluation/CrossValidation.cpp
Expand Up @@ -164,7 +164,7 @@ float64_t CCrossValidation::evaluate_one_run(
/* do actual cross-validation */
for (index_t i = 0; i < num_subsets; ++i)
{
EVALUATION_CONTROLLERS
COMPUTATION_CONTROLLERS

/* evtl. update xvalidation output class */
CrossValidationFoldStorage* fold = new CrossValidationFoldStorage();
Expand Down Expand Up @@ -236,7 +236,7 @@ float64_t CCrossValidation::evaluate_one_run(
//#pragma omp parallel for
for (index_t i = 0; i < num_subsets; ++i)
{
EVALUATION_CONTROLLERS
COMPUTATION_CONTROLLERS

CrossValidationFoldStorage* fold = new CrossValidationFoldStorage();
SG_REF(fold)
Expand Down
14 changes: 0 additions & 14 deletions src/shogun/evaluation/MachineEvaluation.cpp
Expand Up @@ -139,17 +139,3 @@ EEvaluationDirection CMachineEvaluation::get_evaluation_direction()
{
return m_evaluation_criterion->get_evaluation_direction();
}

rxcpp::subscription CMachineEvaluation::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);
}
90 changes: 3 additions & 87 deletions src/shogun/evaluation/MachineEvaluation.h
Expand Up @@ -8,24 +8,14 @@
#ifndef CMACHINEEVALUATION_H_
#define CMACHINEEVALUATION_H_

#include <shogun/lib/config.h>

#include <shogun/base/SGObject.h>
#include <shogun/evaluation/Evaluation.h>
#include <shogun/evaluation/EvaluationResult.h>
#include <shogun/evaluation/MachineEvaluation.h>

#include <condition_variable>
#include <mutex>
#include <shogun/lib/StoppableSGObject.h>
#include <shogun/lib/config.h>

namespace shogun
{

#define EVALUATION_CONTROLLERS \
if (cancel_evaluation()) \
continue; \
pause_evaluation();

class CMachine;
class CFeatures;
class CLabels;
Expand All @@ -36,7 +26,7 @@ namespace shogun
* that evaluates a machine according to some criterion.
*
*/
class CMachineEvaluation : public CSGObject
class CMachineEvaluation : public CStoppableSGObject
{

public:
Expand Down Expand Up @@ -93,37 +83,6 @@ namespace shogun
m_autolock = autolock;
}

#ifndef SWIG
/** @return whether the evaluation needs to be stopped */
SG_FORCED_INLINE bool cancel_evaluation() const
{
return m_cancel_computation.load();
}
#endif

#ifndef SWIG
/** Pause the evaluation f the flag is set */
SG_FORCED_INLINE void pause_evaluation()
{
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 evaluation (sets the flag) */
SG_FORCED_INLINE void resume_evaluation()
{
std::unique_lock<std::mutex> lck(m_mutex);
m_pause_computation_flag = false;
m_pause_computation.notify_all();
}
#endif

protected:
/** Initialize Object */
virtual void init();
Expand All @@ -137,37 +96,6 @@ namespace shogun
virtual CEvaluationResult* evaluate_impl() = 0;

/** connect the machine instance to the signal handler */
rxcpp::subscription connect_to_signal_handler();

/** reset the computation variables */
void reset_computation_variables()
{
m_cancel_computation = false;
m_pause_computation_flag = false;
}

/** The action which will be done when the user decides to
* premature stop the CMachineEvaluation execution */
virtual void on_next()
{
m_cancel_computation.store(true);
}

/** The action which will be done when the user decides to
* pause the CMachineEvaluation execution */
virtual void on_pause()
{
m_pause_computation_flag.store(true);
/* Here there should be the actual code*/
resume_evaluation();
}

/** The action which will be done when the user decides to
* return to prompt and terminate the program execution */
virtual void on_complete()
{
}

protected:
/** Machine to be Evaluated */
CMachine* m_machine;
Expand All @@ -189,18 +117,6 @@ namespace shogun

/** whether machine should be unlocked after evaluation */
bool m_do_unlock;

/** Cancel evaluation */
std::atomic<bool> m_cancel_computation;

/** Pause evaluation 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;
};

} /* namespace shogun */
Expand Down
3 changes: 0 additions & 3 deletions src/shogun/machine/Machine.h
Expand Up @@ -22,9 +22,6 @@
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>

#include <condition_variable>
#include <mutex>

namespace shogun
{

Expand Down

0 comments on commit 024e465

Please sign in to comment.