-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
init.cpp
214 lines (185 loc) · 4.62 KB
/
init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2009 Soeren Sonnenburg
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#include <shogun/base/init.h>
#include <shogun/lib/memory.h>
#include <shogun/lib/config.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/linalg/SGLinalg.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>
#include <shogun/base/SGObject.h>
#include <stdlib.h>
#include <string.h>
#ifdef TRACE_MEMORY_ALLOCS
#include <shogun/lib/Map.h>
shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
#endif
#ifdef HAVE_PROTOBUF
#include <google/protobuf/stubs/common.h>
#endif
namespace shogun
{
Parallel* sg_parallel=NULL;
SGIO* sg_io=NULL;
Version* sg_version=NULL;
CMath* sg_math=NULL;
CRandom* sg_rand=NULL;
std::unique_ptr<SGLinalg> sg_linalg(nullptr);
/// function called to print normal messages
void (*sg_print_message)(FILE* target, const char* str) = NULL;
/// function called to print warning messages
void (*sg_print_warning)(FILE* target, const char* str) = NULL;
/// function called to print error messages
void (*sg_print_error)(FILE* target, const char* str) = NULL;
/// function called to cancel things
void (*sg_cancel_computations)(bool &delayed, bool &immediately)=NULL;
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))
{
if (!sg_io)
sg_io = new shogun::SGIO();
if (!sg_parallel)
sg_parallel=new shogun::Parallel();
if (!sg_version)
sg_version = new shogun::Version();
if (!sg_math)
sg_math = new shogun::CMath();
if (!sg_rand)
sg_rand = new shogun::CRandom();
if (!sg_linalg)
sg_linalg = std::unique_ptr<SGLinalg>(new shogun::SGLinalg());
#ifdef TRACE_MEMORY_ALLOCS
if (!sg_mallocs)
sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
SG_REF(sg_mallocs);
#endif
SG_REF(sg_io);
SG_REF(sg_parallel);
SG_REF(sg_version);
SG_REF(sg_math);
SG_REF(sg_rand);
sg_print_message=print_message;
sg_print_warning=print_warning;
sg_print_error=print_error;
sg_cancel_computations=cancel_computations;
init_from_env();
}
void sg_global_print_default(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}
void init_shogun_with_defaults()
{
init_shogun(&sg_global_print_default, &sg_global_print_default,
&sg_global_print_default);
}
void exit_shogun()
{
#ifdef TRACE_MEMORY_ALLOCS
list_memory_allocs();
shogun::CMap<void*, shogun::MemoryBlock>* mallocs=sg_mallocs;
sg_mallocs=NULL;
SG_UNREF(mallocs);
#endif
sg_print_message=NULL;
sg_print_warning=NULL;
sg_print_error=NULL;
sg_cancel_computations=NULL;
SG_UNREF(sg_rand);
SG_UNREF(sg_math);
SG_UNREF(sg_version);
SG_UNREF(sg_parallel);
SG_UNREF(sg_io);
#ifdef HAVE_PROTOBUF
::google::protobuf::ShutdownProtobufLibrary();
#endif
}
void set_global_io(SGIO* io)
{
SG_REF(io);
SG_UNREF(sg_io);
sg_io=io;
}
SGIO* get_global_io()
{
SG_REF(sg_io);
return sg_io;
}
void set_global_parallel(Parallel* parallel)
{
SG_REF(parallel);
SG_UNREF(sg_parallel);
sg_parallel=parallel;
}
Parallel* get_global_parallel()
{
SG_REF(sg_parallel);
return sg_parallel;
}
void set_global_version(Version* version)
{
SG_REF(version);
SG_UNREF(sg_version);
sg_version=version;
}
Version* get_global_version()
{
SG_REF(sg_version);
return sg_version;
}
void set_global_math(CMath* math)
{
SG_REF(math);
SG_UNREF(sg_math);
sg_math=math;
}
CMath* get_global_math()
{
SG_REF(sg_math);
return sg_math;
}
void set_global_rand(CRandom* rand)
{
SG_REF(rand);
SG_UNREF(sg_rand);
sg_rand=rand;
}
CRandom* get_global_rand()
{
SG_REF(sg_rand);
return sg_rand;
}
#ifndef SWIG // SWIG should skip this part
SGLinalg* get_global_linalg()
{
return sg_linalg.get();
}
#endif
void init_from_env()
{
char* env_log_val = NULL;
SGIO* io = get_global_io();
env_log_val = getenv("SHOGUN_LOG_LEVEL");
if (env_log_val)
{
if(strncmp(env_log_val, "DEBUG", 5) == 0)
io->set_loglevel(MSG_DEBUG);
else if(strncmp(env_log_val, "WARN", 4) == 0)
io->set_loglevel(MSG_WARN);
else if(strncmp(env_log_val, "ERROR", 5) == 0)
io->set_loglevel(MSG_ERROR);
}
SG_UNREF(io);
}
}