Skip to content

Commit

Permalink
[PrematureStopping] Register SIGINT signal handler.
Browse files Browse the repository at this point in the history
The signal handler is set up when calling init_shogun(). It is
disabled by default (sg_signal->enable_handler() is needed).
Applied also style fixes.
  • Loading branch information
geektoni authored and vigsterkr committed Jul 12, 2017
1 parent 30ca8ad commit 07335b1
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 90 deletions.
28 changes: 23 additions & 5 deletions src/shogun/base/init.cpp
Expand Up @@ -12,17 +12,19 @@
#include <shogun/lib/memory.h>
#include <shogun/lib/config.h>

#include <shogun/base/Parallel.h>
#include <shogun/base/SGObject.h>
#include <shogun/base/Version.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Signal.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/linalg/SGLinalg.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>
#include <shogun/base/SGObject.h>

#include <string>
#include <csignal>
#include <stdlib.h>
#include <string.h>
#include <string>
#ifdef TRACE_MEMORY_ALLOCS
#include <shogun/lib/Map.h>
shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
Expand All @@ -39,6 +41,7 @@ namespace shogun
Version* sg_version=NULL;
CMath* sg_math=NULL;
CRandom* sg_rand=NULL;
CSignal* sg_signal = NULL;
std::unique_ptr<SGLinalg> sg_linalg(nullptr);

/// function called to print normal messages
Expand Down Expand Up @@ -71,6 +74,8 @@ namespace shogun
sg_rand = new shogun::CRandom();
if (!sg_linalg)
sg_linalg = std::unique_ptr<SGLinalg>(new shogun::SGLinalg());
if (!sg_signal)
sg_signal = new shogun::CSignal();

#ifdef TRACE_MEMORY_ALLOCS
if (!sg_mallocs)
Expand All @@ -83,12 +88,16 @@ namespace shogun
SG_REF(sg_version);
SG_REF(sg_math);
SG_REF(sg_rand);
SG_REF(sg_signal);

sg_print_message=print_message;
sg_print_warning=print_warning;
sg_print_error=print_error;
sg_cancel_computations=cancel_computations;

// Set up signal handler
std::signal(SIGINT, sg_signal->handler);

init_from_env();
}

Expand Down Expand Up @@ -116,6 +125,7 @@ namespace shogun
sg_print_error=NULL;
sg_cancel_computations=NULL;

SG_UNREF(sg_signal);
SG_UNREF(sg_rand);
SG_UNREF(sg_math);
SG_UNREF(sg_version);
Expand Down Expand Up @@ -191,12 +201,20 @@ namespace shogun
SG_REF(sg_rand);
return sg_rand;
}

CSignal* get_global_signal()
{
SG_REF(sg_signal);
return sg_signal;
}

#ifndef SWIG // SWIG should skip this part
SGLinalg* get_global_linalg()
{
return sg_linalg.get();
}
#endif

void init_from_env()
{
char* env_log_val = NULL;
Expand Down
181 changes: 96 additions & 85 deletions src/shogun/base/init.h
Expand Up @@ -23,91 +23,96 @@ namespace shogun
class Parallel;
class CRandom;
class SGLinalg;

/** This function must be called before libshogun is used. Usually shogun does
* not provide any output messages (neither debugging nor error; apart from
* exceptions). This function allows one to specify customized output
* callback functions and a callback function to check for exceptions:
*
* @param print_message function pointer to print a message
* @param print_warning function pointer to print a warning message
* @param print_error function pointer to print an error message (this will be
* printed before shogun throws an exception)
*
* @param cancel_computations function pointer to check for exception
*
*/
void init_shogun(void (*print_message)(FILE* target, const char* str) = NULL,
void (*print_warning)(FILE* target, const char* str) = NULL,
void (*print_error)(FILE* target, const char* str) = NULL,
void (*cancel_computations)(bool &delayed, bool &immediately)=NULL);

/** init shogun with defaults */
void init_shogun_with_defaults();

/** This function must be called when one stops using libshogun. It will
* perform a number of cleanups */
void exit_shogun();

/** set the global io object
*
* @param io io object to use
*/
void set_global_io(SGIO* io);

/** get the global io object
*
* @return io object
*/
SGIO* get_global_io();

/** set the global parallel object
*
* @param parallel parallel object to use
*/
void set_global_parallel(Parallel* parallel);

/** get the global parallel object
*
* @return parallel object
*/
Parallel* get_global_parallel();

/** set the global version object
*
* @param version version object to use
*/
void set_global_version(Version* version);

/** get the global version object
*
* @return version object
*/
Version* get_global_version();

/** set the global math object
*
* @param math math object to use
*/
void set_global_math(CMath* math);

/** get the global math object
*
* @return math object
*/
CMath* get_global_math();

/** set the global random object
*
* @param rand random object to use
*/
void set_global_rand(CRandom* rand);

/** get the global random object
*
* @return random object
*/
CRandom* get_global_rand();
class CSignal;

/** This function must be called before libshogun is used. Usually shogun
* does
* not provide any output messages (neither debugging nor error; apart from
* exceptions). This function allows one to specify customized output
* callback functions and a callback function to check for exceptions:
*
* @param print_message function pointer to print a message
* @param print_warning function pointer to print a warning message
* @param print_error function pointer to print an error message (this will
* be
* printed before shogun throws an
* exception)
*
* @param cancel_computations function pointer to check for exception
*
*/
void init_shogun(
void (*print_message)(FILE* target, const char* str) = NULL,
void (*print_warning)(FILE* target, const char* str) = NULL,
void (*print_error)(FILE* target, const char* str) = NULL,
void (*cancel_computations)(bool& delayed, bool& immediately) = NULL);

/** init shogun with defaults */
void init_shogun_with_defaults();

/** This function must be called when one stops using libshogun. It will
* perform a number of cleanups */
void exit_shogun();

/** set the global io object
*
* @param io io object to use
*/
void set_global_io(SGIO* io);

/** get the global io object
*
* @return io object
*/
SGIO* get_global_io();

/** set the global parallel object
*
* @param parallel parallel object to use
*/
void set_global_parallel(Parallel* parallel);

/** get the global parallel object
*
* @return parallel object
*/
Parallel* get_global_parallel();

/** set the global version object
*
* @param version version object to use
*/
void set_global_version(Version* version);

/** get the global version object
*
* @return version object
*/
Version* get_global_version();

/** set the global math object
*
* @param math math object to use
*/
void set_global_math(CMath* math);

/** get the global math object
*
* @return math object
*/
CMath* get_global_math();

/** set the global random object
*
* @param rand random object to use
*/
void set_global_rand(CRandom* rand);

/** get the global random object
*
* @return random object
*/
CRandom* get_global_rand();

#ifndef SWIG // SWIG should skip this part
/** get the global linalg library object
Expand All @@ -117,6 +122,12 @@ CRandom* get_global_rand();
SGLinalg* get_global_linalg();
#endif

/** get the global singnal handler object
*
* @return linalg object
*/
CSignal* get_global_signal();

/** Checks environment variables and modifies global objects
*/
void init_from_env();
Expand Down

0 comments on commit 07335b1

Please sign in to comment.