SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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)
62  sg_parallel=new shogun::Parallel();
63  if (!sg_version)
64  sg_version = new shogun::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);
76  SG_REF(sg_parallel);
77  SG_REF(sg_version);
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);
115  SG_UNREF(sg_version);
116  SG_UNREF(sg_parallel);
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);
140  SG_UNREF(sg_parallel);
141  sg_parallel=parallel;
142  }
143 
145  {
146  SG_REF(sg_parallel);
147  return sg_parallel;
148  }
149 
151  {
152  SG_REF(version);
153  SG_UNREF(sg_version);
154  sg_version=version;
155  }
156 
158  {
159  SG_REF(sg_version);
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 }
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:54
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:290
void set_global_version(Version *version)
Definition: init.cpp:150
void set_global_math(CMath *math)
Definition: init.cpp:163
void(* sg_print_warning)(FILE *target, const char *str)
function called to print warning messages
Definition: init.cpp:45
void exit_shogun()
Definition: init.cpp:100
void init_shogun_with_defaults()
Definition: init.cpp:94
CRandom * sg_rand
Definition: init.cpp:39
#define SG_REF(x)
Definition: SGObject.h:54
Version * sg_version
Definition: init.cpp:37
CMath * get_global_math()
Definition: init.cpp:170
Parallel * sg_parallel
Definition: init.cpp:35
void(* sg_print_error)(FILE *target, const char *str)
function called to print error messages
Definition: init.cpp:48
SGIO * sg_io
Definition: init.cpp:36
Parallel * get_global_parallel()
Definition: init.cpp:144
SGIO * get_global_io()
Definition: init.cpp:131
void(* sg_cancel_computations)(bool &delayed, bool &immediately)
function called to cancel things
Definition: init.cpp:51
Class Version provides version information.
Definition: Version.h:28
CMath * sg_math
Definition: init.cpp:38
void set_global_parallel(Parallel *parallel)
Definition: init.cpp:137
void(* sg_print_message)(FILE *target, const char *str)
function called to print normal messages
Definition: init.cpp:42
: Pseudo random number geneartor
Definition: Random.h:34
void init_from_env()
Definition: init.cpp:189
#define SG_UNREF(x)
Definition: SGObject.h:55
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:157
Class which collects generic mathematical functions.
Definition: Math.h:134
void set_global_rand(CRandom *rand)
Definition: init.cpp:176
void sg_global_print_default(FILE *target, const char *str)
Definition: init.cpp:89
void set_global_io(SGIO *io)
Definition: init.cpp:124
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:243
CRandom * get_global_rand()
Definition: init.cpp:183
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:39

SHOGUN Machine Learning Toolbox - Documentation