SHOGUN  6.1.3
init.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/base/init.h>
12 #include <shogun/lib/memory.h>
13 #include <shogun/lib/config.h>
14 
15 #include <shogun/base/Parallel.h>
16 #include <shogun/base/SGObject.h>
17 #include <shogun/base/Version.h>
18 #include <shogun/io/SGIO.h>
19 #include <shogun/lib/Signal.h>
20 
21 #include <rxcpp/rx-lite.hpp>
22 
26 
27 #include <csignal>
28 #include <functional>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <string>
32 #ifdef TRACE_MEMORY_ALLOCS
33 #include <shogun/lib/Map.h>
35 #endif
36 
37 #ifdef HAVE_PROTOBUF
38 #include <google/protobuf/stubs/common.h>
39 #endif
40 
41 namespace shogun
42 {
44  SGIO* sg_io=NULL;
46  CMath* sg_math=NULL;
48  std::unique_ptr<CSignal> sg_signal(nullptr);
49  std::unique_ptr<SGLinalg> sg_linalg(nullptr);
50 
52  std::function<void(FILE*, const char*)> sg_print_message(nullptr);
53 
55  std::function<void(FILE*, const char*)> sg_print_warning(nullptr);
56 
58  std::function<void(FILE*, const char*)> sg_print_error(nullptr);
59 
61  const std::function<void(FILE*, const char*)> print_message,
62  const std::function<void(FILE*, const char*)> print_warning,
63  const std::function<void(FILE*, const char*)> print_error)
64  {
65  if (!sg_io)
66  sg_io = new shogun::SGIO();
67  if (!sg_parallel)
68  sg_parallel=new shogun::Parallel();
69  if (!sg_version)
70  sg_version = new shogun::Version();
71  if (!sg_math)
72  sg_math = new shogun::CMath();
73  if (!sg_rand)
74  sg_rand = new shogun::CRandom();
75  if (!sg_linalg)
76  sg_linalg = std::unique_ptr<SGLinalg>(new shogun::SGLinalg());
77  if (!sg_signal)
78  sg_signal = std::unique_ptr<CSignal>(new shogun::CSignal());
79 
80 #ifdef TRACE_MEMORY_ALLOCS
81  if (!sg_mallocs)
82  sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
83 
84  SG_REF(sg_mallocs);
85 #endif
86  SG_REF(sg_io);
87  SG_REF(sg_parallel);
88  SG_REF(sg_version);
89  SG_REF(sg_math);
90  SG_REF(sg_rand);
91 
92  sg_print_message=print_message;
93  sg_print_warning=print_warning;
94  sg_print_error=print_error;
95 
96  // Set up signal handler
97  std::signal(SIGINT, sg_signal->handler);
98 
99  init_from_env();
100  }
101 
102  void sg_global_print_default(FILE* target, const char* str)
103  {
104  fprintf(target, "%s", str);
105  }
106 
108  {
111  }
112 
113  void exit_shogun()
114  {
115 #ifdef TRACE_MEMORY_ALLOCS
116  list_memory_allocs();
117  shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
118  sg_mallocs=NULL;
119  SG_UNREF(mallocs);
120 #endif
121 
122  SG_UNREF(sg_rand);
123  SG_UNREF(sg_math);
124  SG_UNREF(sg_version);
125  SG_UNREF(sg_parallel);
126  SG_UNREF(sg_io);
127 
128  delete CSignal::m_subscriber;
129  delete CSignal::m_observable;
130  delete CSignal::m_subject;
131 
132 #ifdef HAVE_PROTOBUF
133  ::google::protobuf::ShutdownProtobufLibrary();
134 #endif
135  }
136 
137  void set_global_io(SGIO* io)
138  {
139  SG_REF(io);
140  SG_UNREF(sg_io);
141  sg_io=io;
142  }
143 
145  {
146  SG_REF(sg_io);
147  return sg_io;
148  }
149 
151  {
152  SG_REF(parallel);
153  SG_UNREF(sg_parallel);
154  sg_parallel=parallel;
155  }
156 
158  {
159  SG_REF(sg_parallel);
160  return sg_parallel;
161  }
162 
164  {
165  SG_REF(version);
166  SG_UNREF(sg_version);
167  sg_version=version;
168  }
169 
171  {
172  SG_REF(sg_version);
173  return sg_version;
174  }
175 
176  void set_global_math(CMath* math)
177  {
178  SG_REF(math);
179  SG_UNREF(sg_math);
180  sg_math=math;
181  }
182 
184  {
185  SG_REF(sg_math);
186  return sg_math;
187  }
188 
190  {
191  SG_REF(rand);
192  SG_UNREF(sg_rand);
193  sg_rand=rand;
194  }
195 
197  {
198  SG_REF(sg_rand);
199  return sg_rand;
200  }
201 
203  {
204  return sg_signal.get();
205  }
206 
207 #ifndef SWIG // SWIG should skip this part
209  {
210  return sg_linalg.get();
211  }
212 #endif
213 
215  {
216  char* env_log_val = NULL;
217  SGIO* io = get_global_io();
218  env_log_val = getenv("SHOGUN_LOG_LEVEL");
219  if (env_log_val)
220  {
221  if(strncmp(env_log_val, "DEBUG", 5) == 0)
222  io->set_loglevel(MSG_DEBUG);
223  else if(strncmp(env_log_val, "WARN", 4) == 0)
224  io->set_loglevel(MSG_WARN);
225  else if(strncmp(env_log_val, "ERROR", 5) == 0)
226  io->set_loglevel(MSG_ERROR);
227  }
228  SG_UNREF(io);
229 
230  char* env_warnings_val = NULL;
231  SGLinalg* linalg = get_global_linalg();
232  env_warnings_val = getenv("SHOGUN_GPU_WARNINGS");
233  if (env_warnings_val)
234  {
235  if (strncmp(env_warnings_val, "off", 3) == 0)
236  linalg->set_linalg_warnings(false);
237  }
238 
239 #ifdef HAVE_CXX11
240  char* env_thread_val = NULL;
241  Parallel* parallel = get_global_parallel();
242  env_thread_val = getenv("SHOGUN_NUM_THREADS");
243  if (env_thread_val)
244  {
245 
246  try {
247  int32_t num_threads = std::stoi(std::string(env_thread_val));
248  parallel->set_num_threads(num_threads);
249  } catch (...) {
250  SG_WARNING("The specified SHOGUN_NUM_THREADS environment (%s)"
251  "variable could not be parsed as integer!\n", env_thread_val);
252  }
253  }
254 #endif
255  }
256 }
void set_num_threads(int32_t n)
Definition: Parallel.cpp:86
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:194
std::function< void(FILE *, const char *)> sg_print_message(nullptr)
function called to print normal messages
Definition: init.cpp:52
void set_global_version(Version *version)
Definition: init.cpp:163
void set_global_math(CMath *math)
Definition: init.cpp:176
void exit_shogun()
Definition: init.cpp:113
void init_shogun_with_defaults()
Definition: init.cpp:107
std::function< void(FILE *, const char *)> sg_print_warning(nullptr)
function called to print warning messages
Definition: init.cpp:55
CRandom * sg_rand
Definition: init.cpp:47
#define SG_REF(x)
Definition: SGObject.h:52
std::function< void(FILE *, const char *)> sg_print_error(nullptr)
function called to print error messages
Definition: init.cpp:58
std::unique_ptr< SGLinalg > sg_linalg
static SGSubjectS * m_subject
Definition: Signal.h:98
Version * sg_version
Definition: init.cpp:45
void init_shogun(const std::function< void(FILE *, const char *)> print_message, const std::function< void(FILE *, const char *)> print_warning, const std::function< void(FILE *, const char *)> print_error)
Definition: init.cpp:60
CMath * get_global_math()
Definition: init.cpp:183
Parallel * sg_parallel
Definition: init.cpp:43
CSignal * get_global_signal()
Definition: init.cpp:202
SGIO * sg_io
Definition: init.cpp:44
Parallel * get_global_parallel()
Definition: init.cpp:157
SGIO * get_global_io()
Definition: init.cpp:144
std::unique_ptr< CSignal > sg_signal(nullptr)
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:42
Class Version provides version information.
Definition: Version.h:28
CMath * sg_math
Definition: init.cpp:46
linalg library backend
Definition: SGLinalg.h:51
static SGObservableS * m_observable
Definition: Signal.h:99
void set_global_parallel(Parallel *parallel)
Definition: init.cpp:150
: Pseudo random number geneartor
Definition: Random.h:34
SGLinalg * get_global_linalg()
Definition: init.cpp:208
void init_from_env()
Definition: init.cpp:214
#define SG_UNREF(x)
Definition: SGObject.h:53
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static SGSubscriberS * m_subscriber
Definition: Signal.h:100
Version * get_global_version()
Definition: init.cpp:170
Class Signal implements signal handling to e.g. allow CTRL+C to cancel a long running process...
Definition: Signal.h:37
Class which collects generic mathematical functions.
Definition: Math.h:114
void set_global_rand(CRandom *rand)
Definition: init.cpp:189
void sg_global_print_default(FILE *target, const char *str)
Definition: init.cpp:102
void set_global_io(SGIO *io)
Definition: init.cpp:137
#define SG_WARNING(...)
Definition: SGIO.h:127
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:218
CRandom * get_global_rand()
Definition: init.cpp:196
void set_linalg_warnings(bool enable_warnings=true)
Definition: SGLinalg.h:108

SHOGUN Machine Learning Toolbox - Documentation