-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
init.cpp
255 lines (218 loc) · 5.5 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* 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/base/Parallel.h>
#include <shogun/base/SGObject.h>
#include <shogun/base/Version.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Signal.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/linalg/SGLinalg.h>
#include <csignal>
#include <functional>
#include <stdlib.h>
#include <string.h>
#include <string>
#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
#include <rxcpp/rx-lite.hpp>
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<CSignal> sg_signal(nullptr);
std::unique_ptr<SGLinalg> sg_linalg(nullptr);
/// function called to print normal messages
std::function<void(FILE*, const char*)> sg_print_message(nullptr);
/// function called to print warning messages
std::function<void(FILE*, const char*)> sg_print_warning(nullptr);
/// function called to print error messages
std::function<void(FILE*, const char*)> sg_print_error(nullptr);
void init_shogun(
const std::function<void(FILE*, const char*)> print_message,
const std::function<void(FILE*, const char*)> print_warning,
const std::function<void(FILE*, const char*)> print_error)
{
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());
if (!sg_signal)
sg_signal = std::unique_ptr<CSignal>(new shogun::CSignal());
#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;
// Set up signal handler
std::signal(SIGINT, sg_signal->handler);
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_UNREF(sg_rand);
SG_UNREF(sg_math);
SG_UNREF(sg_version);
SG_UNREF(sg_parallel);
SG_UNREF(sg_io);
delete CSignal::m_subscriber;
delete CSignal::m_observable;
delete CSignal::m_subject;
#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;
}
CSignal* get_global_signal()
{
return sg_signal.get();
}
#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);
char* env_warnings_val = NULL;
SGLinalg* linalg = get_global_linalg();
env_warnings_val = getenv("SHOGUN_GPU_WARNINGS");
if (env_warnings_val)
{
if (strncmp(env_warnings_val, "off", 3) == 0)
linalg->set_linalg_warnings(false);
}
#ifdef HAVE_CXX11
char* env_thread_val = NULL;
Parallel* parallel = get_global_parallel();
env_thread_val = getenv("SHOGUN_NUM_THREADS");
if (env_thread_val)
{
try {
int32_t num_threads = std::stoi(std::string(env_thread_val));
parallel->set_num_threads(num_threads);
} catch (...) {
SG_WARNING("The specified SHOGUN_NUM_THREADS environment (%s)"
"variable could not be parsed as integer!\n", env_thread_val);
}
}
#endif
}
}