/
oracle_function.cpp
410 lines (350 loc) · 12.3 KB
/
oracle_function.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* This file is part of CasADi.
*
* CasADi -- A symbolic framework for dynamic optimization.
* Copyright (C) 2010-2014 Joel Andersson, Joris Gillis, Moritz Diehl,
* K.U. Leuven. All rights reserved.
* Copyright (C) 2011-2014 Greg Horn
*
* CasADi is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* CasADi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with CasADi; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "oracle_function.hpp"
#include "external.hpp"
#include <iostream>
#include <iomanip>
using namespace std;
namespace casadi {
OracleFunction::OracleFunction(const std::string& name, const Function& oracle)
: FunctionInternal(name), oracle_(oracle) {
}
OracleFunction::~OracleFunction() {
}
Options OracleFunction::options_
= {{&FunctionInternal::options_},
{{"monitor",
{OT_STRINGVECTOR,
"Set of user problem functions to be monitored"}},
{"common_options",
{OT_DICT,
"Options for auto-generated functions"}},
{"specific_options",
{OT_DICT,
"Options for specific auto-generated functions,"
" overwriting the defaults from common_options. Nested dictionary."}}
}
};
void OracleFunction::init(const Dict& opts) {
FunctionInternal::init(opts);
// Read options
for (auto&& op : opts) {
if (op.first=="common_options") {
common_options_ = op.second;
} else if (op.first=="specific_options") {
specific_options_ = op.second;
for (auto&& i : specific_options_) {
casadi_assert_message(i.second.is_dict(),
"specific_option must be a nested dictionary."
" Type mismatch for entry '" + i.first+ "': "
" got type " + i.second.get_description() + ".");
}
}
}
}
void OracleFunction::finalize(const Dict& opts) {
// Default options
vector<string> monitor;
// Read options
for (auto&& op : opts) {
if (op.first=="monitor") {
monitor = op.second;
}
}
// Set corresponding monitors
for (const string& fname : monitor) {
auto it = all_functions_.find(fname);
if (it==all_functions_.end()) {
casadi_warning("Ignoring monitor '" + fname + "'."
" Available functions: " + join(get_function()) + ".");
} else {
casadi_assert_warning(!it->second.monitored, "Duplicate monitor " + fname);
it->second.monitored = true;
}
}
// Check specific options
for (auto&& i : specific_options_) {
if (all_functions_.find(i.first)==all_functions_.end())
casadi_warning("Ignoring specific_options entry '" + i.first+"'."
" Available functions: " + join(get_function()) + ".");
}
// Recursive call
FunctionInternal::finalize(opts);
}
Function OracleFunction::create_function(const std::string& fname,
const std::vector<std::string>& s_in,
const std::vector<std::string>& s_out,
const Function::AuxOut& aux) {
// Retrieve specific set of options if available
Dict specific_options;
auto it = specific_options_.find(fname);
if (it!=specific_options_.end()) specific_options = it->second;
// Combine specific and common options
Dict opt = combine(specific_options, common_options_);
// Generate the function
Function ret = oracle_.factory(fname, s_in, s_out, aux, opt);
set_function(ret, fname, true);
return ret;
}
void OracleFunction::
set_function(const Function& fcn, const std::string& fname, bool jit) {
casadi_assert_message(!has_function(fname), "Duplicate function " + fname);
RegFun& r = all_functions_[fname];
r.f = fcn;
r.jit = jit;
alloc(fcn);
}
int OracleFunction::
calc_function(OracleMemory* m, const std::string& fcn,
const double* const* arg) const {
// Respond to a possible Crl+C signals
InterruptHandler::check();
// Get function
const Function& f = get_function(fcn);
// Is the function monitored?
bool monitored = this->monitored(fcn);
// Get statistics structure
FStats& fstats = m->fstats.at(fcn);
// Number of inputs and outputs
int n_in = f.n_in(), n_out = f.n_out();
// Prepare stats, start timer
fstats.tic();
// Input buffers
if (arg) {
fill_n(m->arg, n_in, nullptr);
for (int i=0; i<n_in; ++i) m->arg[i] = *arg++;
}
// Print inputs nonzeros
if (monitored) {
userOut() << fcn << " input nonzeros: " << endl;
for (int i=0; i<n_in; ++i) {
userOut() << " " << i << " (" << f.name_in(i) << "): ";
if (m->arg[i]) {
// Print nonzeros
userOut() << "[";
for (int k=0; k<f.nnz_in(i); ++k) {
if (k!=0) userOut() << ", ";
userOut() << m->arg[i][k];
}
userOut() << "]" << endl;
} else {
// All zero input
userOut() << "0" << endl;
}
}
}
// Evaluate memory-less
try {
f(m->arg, m->res, m->iw, m->w, 0);
} catch(exception& ex) {
// Fatal error
userOut<true, PL_WARN>()
<< name() << ":" << fcn << " failed:" << ex.what() << endl;
return 1;
}
// Print output nonzeros
if (monitored) {
userOut() << fcn << " output nonzeros: " << endl;
for (int i=0; i<n_out; ++i) {
userOut() << " " << i << " (" << f.name_out(i) << "): ";
if (m->res[i]) {
// Print nonzeros
userOut() << "[";
for (int k=0; k<f.nnz_out(i); ++k) {
if (k!=0) userOut() << ", ";
userOut() << m->res[i][k];
}
userOut() << "]" << endl;
} else {
// Ignored output
userOut() << " N/A" << endl;
}
}
}
// Make sure not NaN or Inf
for (int i=0; i<n_out; ++i) {
if (!m->res[i]) continue;
if (!all_of(m->res[i], m->res[i]+f.nnz_out(i), [](double v) { return isfinite(v);})) {
userOut<true, PL_WARN>()
<< name() << ":" << fcn << " failed: NaN or Inf detected for output "
<< f.name_out(i) << endl;
return -1;
}
}
// Update stats
fstats.toc();
// Success
return 0;
}
std::string OracleFunction::
generate_dependencies(const std::string& fname, const Dict& opts) {
CodeGenerator gen(fname, opts);
gen.add(oracle_);
for (auto&& e : all_functions_) {
if (e.second.jit) gen.add(e.second.f);
}
return gen.generate();
}
void OracleFunction::jit_dependencies(const std::string& fname) {
if (verbose())
log("OracleFunction::jit_dependencies", "compiling to "+ fname+"'.");
// JIT dependent functions
compiler_ = Importer(generate_dependencies(fname, Dict()),
compilerplugin_, jit_options_);
// Replace the Oracle functions with generated functions
for (auto&& e : all_functions_) {
if (verbose())
log("OracleFunction::jit_dependencies",
"loading '" + e.second.f.name() + "' from '" + fname + "'.");
if (e.second.jit) e.second.f = external(e.second.f.name(), compiler_);
}
}
void OracleFunction::expand() {
oracle_ = oracle_.expand();
}
void OracleFunction::print_fstats(const OracleMemory* m) const {
size_t maxNameLen=0;
// Retrieve all nlp keys
std::vector<std::string> keys;
std::vector<std::string> keys_other;
for (auto &&s : m->fstats) {
maxNameLen = max(s.first.size(), maxNameLen);
if (s.first.find("nlp")!=std::string::npos) {
keys.push_back(s.first);
} else if (s.first.find("mainloop")==std::string::npos) {
keys_other.push_back(s.first);
} else {
continue;
}
}
maxNameLen = max(std::string("all previous").size(), maxNameLen);
maxNameLen = max(std::string("solver").size(), maxNameLen);
// Print header
std::stringstream s;
std::string blankName(maxNameLen, ' ');
s
<< blankName
<< " proc wall num mean mean"
<< endl << blankName
<< " time time evals proc time wall time";
userOut() << s.str() << endl;
// Sort the keys according to order
std::vector<std::string> keys_order0;
std::vector<std::string> keys_order1;
std::vector<std::string> keys_order2;
for (auto k : keys) {
if (k.find("hess")!=std::string::npos) {
keys_order2.push_back(k);
continue;
}
if (k.find("grad")!=std::string::npos ||
k.find("jac")!=std::string::npos) {
keys_order1.push_back(k);
continue;
}
keys_order0.push_back(k);
}
// Print all NLP stats
for (auto keys : {&keys_order0, &keys_order1, &keys_order2}) {
std::sort(keys->begin(), keys->end());
for (auto k : *keys) {
const FStats& fs = m->fstats.at(k);
print_stats_line(maxNameLen, k, fs.n_call, fs.t_proc, fs.t_wall);
}
}
// Sum the previously printed stats
double t_wall_all_previous = 0;
double t_proc_all_previous = 0;
for (auto k : keys) {
const FStats& fs = m->fstats.at(k);
t_proc_all_previous += fs.t_proc;
t_wall_all_previous += fs.t_wall;
}
print_stats_line(maxNameLen, "all previous", -1, t_proc_all_previous, t_wall_all_previous);
// Sort and show the remainder of keys
std::sort(keys_other.begin(), keys_other.end());
for (std::string k : keys_other) {
const FStats& fs = m->fstats.at(k);
print_stats_line(maxNameLen, k, fs.n_call, fs.t_proc, fs.t_wall);
t_proc_all_previous += fs.t_proc;
t_wall_all_previous += fs.t_wall;
}
// Show the mainloop stats
const FStats& fs_mainloop = m->fstats.at("mainloop");
if (fs_mainloop.n_call>0) {
print_stats_line(maxNameLen, "solver", -1,
fs_mainloop.t_proc-t_proc_all_previous, fs_mainloop.t_wall-t_wall_all_previous);
print_stats_line(maxNameLen, "mainloop", -1, fs_mainloop.t_proc, fs_mainloop.t_wall);
}
}
Dict OracleFunction::get_stats(void *mem) const {
auto m = static_cast<OracleMemory*>(mem);
// Add timing statistics
Dict stats;
for (auto&& s : m->fstats) {
stats["n_call_" +s.first] = s.second.n_call;
stats["t_wall_" +s.first] = s.second.t_wall;
stats["t_proc_" +s.first] = s.second.t_proc;
}
return stats;
}
void OracleFunction::init_memory(void* mem) const {
auto m = static_cast<OracleMemory*>(mem);
// Create statistics
for (auto&& e : all_functions_) {
m->fstats[e.first] = FStats();
}
}
void OracleFunction::set_temp(void* mem, const double** arg, double** res,
int* iw, double* w) const {
auto m = static_cast<OracleMemory*>(mem);
m->arg = arg;
m->res = res;
m->iw = iw;
m->w = w;
}
std::vector<std::string> OracleFunction::get_function() const {
std::vector<std::string> ret;
ret.reserve(all_functions_.size());
for (auto&& e : all_functions_) {
ret.push_back(e.first);
}
return ret;
}
const Function& OracleFunction::get_function(const std::string &name) const {
auto it = all_functions_.find(name);
casadi_assert_message(it!=all_functions_.end(),
"No function \"" + name + "\" in " + this->name());
return it->second.f;
}
bool OracleFunction::monitored(const std::string &name) const {
auto it = all_functions_.find(name);
casadi_assert_message(it!=all_functions_.end(),
"No function \"" + name + "\" in " + this->name());
return it->second.monitored;
}
bool OracleFunction::has_function(const std::string& fname) const {
return all_functions_.find(fname) != all_functions_.end();
}
} // namespace casadi