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>
14 #include <shogun/lib/common.h>
15 #include <shogun/lib/Map.h>
16 #include <shogun/base/Parallel.h>
17 #include <shogun/base/Version.h>
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #ifdef TRACE_MEMORY_ALLOCS
23 #endif
24 
25 namespace shogun
26 {
28  SGIO* sg_io=NULL;
30  CMath* sg_math=NULL;
32 
34  void (*sg_print_message)(FILE* target, const char* str) = NULL;
35 
37  void (*sg_print_warning)(FILE* target, const char* str) = NULL;
38 
40  void (*sg_print_error)(FILE* target, const char* str) = NULL;
41 
43  void (*sg_cancel_computations)(bool &delayed, bool &immediately)=NULL;
44 
45 
46  void init_shogun(void (*print_message)(FILE* target, const char* str),
47  void (*print_warning)(FILE* target, const char* str),
48  void (*print_error)(FILE* target, const char* str),
49  void (*cancel_computations)(bool &delayed, bool &immediately))
50  {
51  if (!sg_io)
52  sg_io = new shogun::SGIO();
53  if (!sg_parallel)
55  if (!sg_version)
57  if (!sg_math)
58  sg_math = new shogun::CMath();
59  if (!sg_rand)
60  sg_rand = new shogun::CRandom();
61 #ifdef TRACE_MEMORY_ALLOCS
62  if (!sg_mallocs)
63  sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
64 
65  SG_REF(sg_mallocs);
66 #endif
67  SG_REF(sg_io);
70  SG_REF(sg_math);
71  SG_REF(sg_rand);
72 
73  sg_print_message=print_message;
74  sg_print_warning=print_warning;
75  sg_print_error=print_error;
76  sg_cancel_computations=cancel_computations;
77 
78  init_from_env();
79  }
80 
81  void sg_global_print_default(FILE* target, const char* str)
82  {
83  fprintf(target, "%s", str);
84  }
85 
87  {
90  }
91 
92  void exit_shogun()
93  {
94 #ifdef TRACE_MEMORY_ALLOCS
95  list_memory_allocs();
96  shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
97  sg_mallocs=NULL;
98  SG_UNREF(mallocs);
99 #endif
100  sg_print_message=NULL;
101  sg_print_warning=NULL;
102  sg_print_error=NULL;
104 
105  SG_UNREF(sg_rand);
106  SG_UNREF(sg_math);
109  SG_UNREF(sg_io);
110 
111  }
112 
113  void set_global_io(SGIO* io)
114  {
115  SG_REF(io);
116  SG_UNREF(sg_io);
117  sg_io=io;
118  }
119 
121  {
122  SG_REF(sg_io);
123  return sg_io;
124  }
125 
127  {
128  SG_REF(parallel);
130  sg_parallel=parallel;
131  }
132 
134  {
136  return sg_parallel;
137  }
138 
140  {
141  SG_REF(version);
143  sg_version=version;
144  }
145 
147  {
149  return sg_version;
150  }
151 
152  void set_global_math(CMath* math)
153  {
154  SG_REF(math);
155  SG_UNREF(sg_math);
156  sg_math=math;
157  }
158 
160  {
161  SG_REF(sg_math);
162  return sg_math;
163  }
164 
166  {
167  SG_REF(rand);
168  SG_UNREF(sg_rand);
169  sg_rand=rand;
170  }
171 
173  {
174  SG_REF(sg_rand);
175  return sg_rand;
176  }
177 
179  {
180  char* env_log_val = NULL;
181  SGIO* io = get_global_io();
182  env_log_val = getenv("SHOGUN_LOG_LEVEL");
183  if (env_log_val)
184  {
185  if(strncmp(env_log_val, "DEBUG", 5) == 0)
186  io->set_loglevel(MSG_DEBUG);
187  else if(strncmp(env_log_val, "WARN", 4) == 0)
188  io->set_loglevel(MSG_WARN);
189  else if(strncmp(env_log_val, "ERROR", 5) == 0)
190  io->set_loglevel(MSG_ERROR);
191  }
192  SG_UNREF(io);
193  }
194 }
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:46
void set_loglevel(EMessageType level)
Definition: SGIO.cpp:290
void set_global_version(Version *version)
Definition: init.cpp:139
void set_global_math(CMath *math)
Definition: init.cpp:152
void(* sg_print_warning)(FILE *target, const char *str)
function called to print warning messages
Definition: init.cpp:37
#define SG_UNREF(x)
Definition: SGRefObject.h:35
void exit_shogun()
Definition: init.cpp:92
void init_shogun_with_defaults()
Definition: init.cpp:86
CRandom * sg_rand
Definition: init.cpp:31
Version * sg_version
Definition: init.cpp:29
CMath * get_global_math()
Definition: init.cpp:159
Parallel * sg_parallel
Definition: init.cpp:27
void(* sg_print_error)(FILE *target, const char *str)
function called to print error messages
Definition: init.cpp:40
SGIO * sg_io
Definition: init.cpp:28
Parallel * get_global_parallel()
Definition: init.cpp:133
SGIO * get_global_io()
Definition: init.cpp:120
void(* sg_cancel_computations)(bool &delayed, bool &immediately)
function called to cancel things
Definition: init.cpp:43
#define SG_REF(x)
Definition: SGRefObject.h:34
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:40
Class Version provides version information.
Definition: Version.h:32
CMath * sg_math
Definition: init.cpp:30
void set_global_parallel(Parallel *parallel)
Definition: init.cpp:126
void(* sg_print_message)(FILE *target, const char *str)
function called to print normal messages
Definition: init.cpp:34
: Pseudo random number geneartor
Definition: Random.h:32
void init_from_env()
Definition: init.cpp:178
Class Parallel provides helper functions for multithreading.
Definition: Parallel.h:27
Version * get_global_version()
Definition: init.cpp:146
Class which collects generic mathematical functions.
Definition: Math.h:133
void set_global_rand(CRandom *rand)
Definition: init.cpp:165
void sg_global_print_default(FILE *target, const char *str)
Definition: init.cpp:81
void set_global_io(SGIO *io)
Definition: init.cpp:113
Class SGIO, used to do input output operations throughout shogun.
Definition: SGIO.h:245
CRandom * get_global_rand()
Definition: init.cpp:172

SHOGUN Machine Learning Toolbox - Documentation