SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
17 #include <shogun/io/SGIO.h>
18 #include <shogun/base/Parallel.h>
19 #include <shogun/base/Version.h>
20 #include <shogun/base/SGObject.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #ifdef TRACE_MEMORY_ALLOCS
25 #include <shogun/lib/Map.h>
27 #endif
28 
29 #ifdef HAVE_PROTOBUF
30 #include <google/protobuf/stubs/common.h>
31 #endif
32 
33 namespace shogun
34 {
36  SGIO* sg_io=NULL;
38  CMath* sg_math=NULL;
40 
42  void (*sg_print_message)(FILE* target, const char* str) = NULL;
43 
45  void (*sg_print_warning)(FILE* target, const char* str) = NULL;
46 
48  void (*sg_print_error)(FILE* target, const char* str) = NULL;
49 
51  void (*sg_cancel_computations)(bool &delayed, bool &immediately)=NULL;
52 
53 
54  void init_shogun(void (*print_message)(FILE* target, const char* str),
55  void (*print_warning)(FILE* target, const char* str),
56  void (*print_error)(FILE* target, const char* str),
57  void (*cancel_computations)(bool &delayed, bool &immediately))
58  {
59  if (!sg_io)
60  sg_io = new shogun::SGIO();
61  if (!sg_parallel)
63  if (!sg_version)
65  if (!sg_math)
66  sg_math = new shogun::CMath();
67  if (!sg_rand)
68  sg_rand = new shogun::CRandom();
69 #ifdef TRACE_MEMORY_ALLOCS
70  if (!sg_mallocs)
71  sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
72 
73  SG_REF(sg_mallocs);
74 #endif
75  SG_REF(sg_io);
78  SG_REF(sg_math);
79  SG_REF(sg_rand);
80 
81  sg_print_message=print_message;
82  sg_print_warning=print_warning;
83  sg_print_error=print_error;
84  sg_cancel_computations=cancel_computations;
85 
86  init_from_env();
87  }
88 
89  void sg_global_print_default(FILE* target, const char* str)
90  {
91  fprintf(target, "%s", str);
92  }
93 
95  {
98  }
99 
100  void exit_shogun()
101  {
102 #ifdef TRACE_MEMORY_ALLOCS
103  list_memory_allocs();
104  shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
105  sg_mallocs=NULL;
106  SG_UNREF(mallocs);
107 #endif
108  sg_print_message=NULL;
109  sg_print_warning=NULL;
110  sg_print_error=NULL;
112 
113  SG_UNREF(sg_rand);
114  SG_UNREF(sg_math);
117  SG_UNREF(sg_io);
118 
119 #ifdef HAVE_PROTOBUF
120  ::google::protobuf::ShutdownProtobufLibrary();
121 #endif
122  }
123 
124  void set_global_io(SGIO* io)
125  {
126  SG_REF(io);
127  SG_UNREF(sg_io);
128  sg_io=io;
129  }
130 
132  {
133  SG_REF(sg_io);
134  return sg_io;
135  }
136 
138  {
139  SG_REF(parallel);
141  sg_parallel=parallel;
142  }
143 
145  {
147  return sg_parallel;
148  }
149 
151  {
152  SG_REF(version);
154  sg_version=version;
155  }
156 
158  {
160  return sg_version;
161  }
162 
163  void set_global_math(CMath* math)
164  {
165  SG_REF(math);
166  SG_UNREF(sg_math);
167  sg_math=math;
168  }
169 
171  {
172  SG_REF(sg_math);
173  return sg_math;
174  }
175 
177  {
178  SG_REF(rand);
179  SG_UNREF(sg_rand);
180  sg_rand=rand;
181  }
182 
184  {
185  SG_REF(sg_rand);
186  return sg_rand;
187  }
188 
190  {
191  char* env_log_val = NULL;
192  SGIO* io = get_global_io();
193  env_log_val = getenv("SHOGUN_LOG_LEVEL");
194  if (env_log_val)
195  {
196  if(strncmp(env_log_val, "DEBUG", 5) == 0)
197  io->set_loglevel(MSG_DEBUG);
198  else if(strncmp(env_log_val, "WARN", 4) == 0)
199  io->set_loglevel(MSG_WARN);
200  else if(strncmp(env_log_val, "ERROR", 5) == 0)
201  io->set_loglevel(MSG_ERROR);
202  }
203  SG_UNREF(io);
204  }
205 }

SHOGUN Machine Learning Toolbox - Documentation