SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 
18 #include <shogun/io/SGIO.h>
19 #include <shogun/base/Parallel.h>
20 #include <shogun/base/Version.h>
21 #include <shogun/base/SGObject.h>
22 
23 #include <string>
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef TRACE_MEMORY_ALLOCS
27 #include <shogun/lib/Map.h>
29 #endif
30 
31 #ifdef HAVE_PROTOBUF
32 #include <google/protobuf/stubs/common.h>
33 #endif
34 
35 namespace shogun
36 {
38  SGIO* sg_io=NULL;
40  CMath* sg_math=NULL;
42  std::unique_ptr<SGLinalg> sg_linalg(nullptr);
43 
45  void (*sg_print_message)(FILE* target, const char* str) = NULL;
46 
48  void (*sg_print_warning)(FILE* target, const char* str) = NULL;
49 
51  void (*sg_print_error)(FILE* target, const char* str) = NULL;
52 
54  void (*sg_cancel_computations)(bool &delayed, bool &immediately)=NULL;
55 
56 
57  void init_shogun(void (*print_message)(FILE* target, const char* str),
58  void (*print_warning)(FILE* target, const char* str),
59  void (*print_error)(FILE* target, const char* str),
60  void (*cancel_computations)(bool &delayed, bool &immediately))
61  {
62  if (!sg_io)
63  sg_io = new shogun::SGIO();
64  if (!sg_parallel)
65  sg_parallel=new shogun::Parallel();
66  if (!sg_version)
67  sg_version = new shogun::Version();
68  if (!sg_math)
69  sg_math = new shogun::CMath();
70  if (!sg_rand)
71  sg_rand = new shogun::CRandom();
72  if (!sg_linalg)
73  sg_linalg = std::unique_ptr<SGLinalg>(new shogun::SGLinalg());
74 
75 #ifdef TRACE_MEMORY_ALLOCS
76  if (!sg_mallocs)
77  sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
78 
79  SG_REF(sg_mallocs);
80 #endif
81  SG_REF(sg_io);
82  SG_REF(sg_parallel);
83  SG_REF(sg_version);
84  SG_REF(sg_math);
85  SG_REF(sg_rand);
86 
87  sg_print_message=print_message;
88  sg_print_warning=print_warning;
89  sg_print_error=print_error;
90  sg_cancel_computations=cancel_computations;
91 
92  init_from_env();
93  }
94 
95  void sg_global_print_default(FILE* target, const char* str)
96  {
97  fprintf(target, "%s", str);
98  }
99 
101  {
104  }
105 
106  void exit_shogun()
107  {
108 #ifdef TRACE_MEMORY_ALLOCS
109  list_memory_allocs();
110  shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
111  sg_mallocs=NULL;
112  SG_UNREF(mallocs);
113 #endif
114  sg_print_message=NULL;
115  sg_print_warning=NULL;
116  sg_print_error=NULL;
118 
119  SG_UNREF(sg_rand);
120  SG_UNREF(sg_math);
121  SG_UNREF(sg_version);
122  SG_UNREF(sg_parallel);
123  SG_UNREF(sg_io);
124 
125 #ifdef HAVE_PROTOBUF
126  ::google::protobuf::ShutdownProtobufLibrary();
127 #endif
128  }
129 
130  void set_global_io(SGIO* io)
131  {
132  SG_REF(io);
133  SG_UNREF(sg_io);
134  sg_io=io;
135  }
136 
138  {
139  SG_REF(sg_io);
140  return sg_io;
141  }
142 
144  {
145  SG_REF(parallel);
146  SG_UNREF(sg_parallel);
147  sg_parallel=parallel;
148  }
149 
151  {
152  SG_REF(sg_parallel);
153  return sg_parallel;
154  }
155 
157  {
158  SG_REF(version);
159  SG_UNREF(sg_version);
160  sg_version=version;
161  }
162 
164  {
165  SG_REF(sg_version);
166  return sg_version;
167  }
168 
169  void set_global_math(CMath* math)
170  {
171  SG_REF(math);
172  SG_UNREF(sg_math);
173  sg_math=math;
174  }
175 
177  {
178  SG_REF(sg_math);
179  return sg_math;
180  }
181 
183  {
184  SG_REF(rand);
185  SG_UNREF(sg_rand);
186  sg_rand=rand;
187  }
188 
190  {
191  SG_REF(sg_rand);
192  return sg_rand;
193  }
194 #ifndef SWIG // SWIG should skip this part
196  {
197  return sg_linalg.get();
198  }
199 #endif
201  {
202  char* env_log_val = NULL;
203  SGIO* io = get_global_io();
204  env_log_val = getenv("SHOGUN_LOG_LEVEL");
205  if (env_log_val)
206  {
207  if(strncmp(env_log_val, "DEBUG", 5) == 0)
208  io->set_loglevel(MSG_DEBUG);
209  else if(strncmp(env_log_val, "WARN", 4) == 0)
210  io->set_loglevel(MSG_WARN);
211  else if(strncmp(env_log_val, "ERROR", 5) == 0)
212  io->set_loglevel(MSG_ERROR);
213  }
214  SG_UNREF(io);
215 
216  char* env_warnings_val = NULL;
217  SGLinalg* linalg = get_global_linalg();
218  env_warnings_val = getenv("SHOGUN_GPU_WARNINGS");
219  if (env_warnings_val)
220  {
221  if (strncmp(env_warnings_val, "off", 3) == 0)
222  linalg->set_linalg_warnings(false);
223  }
224 
225 #ifdef HAVE_CXX11
226  char* env_thread_val = NULL;
227  Parallel* parallel = get_global_parallel();
228  env_thread_val = getenv("SHOGUN_NUM_THREADS");
229  if (env_thread_val)
230  {
231 
232  try {
233  int32_t num_threads = std::stoi(std::string(env_thread_val));
234  parallel->set_num_threads(num_threads);
235  } catch (...) {
236  SG_WARNING("The specified SHOGUN_NUM_THREADS environment (%s)"
237  "variable could not be parsed as integer!\n", env_thread_val);
238  }
239  }
240 #endif
241  }
242 }
void set_num_threads(int32_t n)
Definition: Parallel.cpp:86
void init_shogun(void(*print_message)(FILE *target, const char *str), void(*print_warning)(FILE *target, const char *str), void(*print_error)(FILE *target, const char *str), void(*cancel_computations)(bool &delayed, bool &immediately))
Definition: init.cpp:57
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:320
void set_global_version(Version *version)
Definition: init.cpp:156
void set_global_math(CMath *math)
Definition: init.cpp:169
void(* sg_print_warning)(FILE *target, const char *str)
function called to print warning messages
Definition: init.cpp:48
void exit_shogun()
Definition: init.cpp:106
void init_shogun_with_defaults()
Definition: init.cpp:100
CRandom * sg_rand
Definition: init.cpp:41
#define SG_REF(x)
Definition: SGObject.h:52
std::unique_ptr< SGLinalg > sg_linalg
Version * sg_version
Definition: init.cpp:39
CMath * get_global_math()
Definition: init.cpp:176
Parallel * sg_parallel
Definition: init.cpp:37
void(* sg_print_error)(FILE *target, const char *str)
function called to print error messages
Definition: init.cpp:51
SGIO * sg_io
Definition: init.cpp:38
Parallel * get_global_parallel()
Definition: init.cpp:150
SGIO * get_global_io()
Definition: init.cpp:137
void(* sg_cancel_computations)(bool &delayed, bool &immediately)
function called to cancel things
Definition: init.cpp:54
Class Version provides version information.
Definition: Version.h:28
CMath * sg_math
Definition: init.cpp:40
linalg library backend
Definition: SGLinalg.h:51
void set_global_parallel(Parallel *parallel)
Definition: init.cpp:143
void(* sg_print_message)(FILE *target, const char *str)
function called to print normal messages
Definition: init.cpp:45
: Pseudo random number geneartor
Definition: Random.h:34
SGLinalg * get_global_linalg()
Definition: init.cpp:195
void init_from_env()
Definition: init.cpp:200
#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
Version * get_global_version()
Definition: init.cpp:163
Class which collects generic mathematical functions.
Definition: Math.h:130
void set_global_rand(CRandom *rand)
Definition: init.cpp:182
void sg_global_print_default(FILE *target, const char *str)
Definition: init.cpp:95
void set_global_io(SGIO *io)
Definition: init.cpp:130
#define SG_WARNING(...)
Definition: SGIO.h:127
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:242
CRandom * get_global_rand()
Definition: init.cpp:189
void set_linalg_warnings(bool enable_warnings=true)
Definition: SGLinalg.h:107
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:37

SHOGUN Machine Learning Toolbox - Documentation