/
external.cpp
351 lines (304 loc) · 11.3 KB
/
external.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
/*
* 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 "external_impl.hpp"
#include "std_vector_tools.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
namespace casadi {
using namespace std;
Function external(const string& name, const Importer& li,
const Dict& opts) {
return Function::create(new GenericExternal(name, li), opts);
}
Function external(const string& name, const Dict& opts) {
return external(name, "./" + name + ".so", opts);
}
Function external(const string& name, const string& bin_name,
const Dict& opts) {
return external(name, Importer(bin_name, "dll"), opts);
}
External::External(const std::string& name, const Importer& li)
: FunctionInternal(name), li_(li) {
// Increasing/decreasing reference counter
incref_ = (signal_t)li_.get_function(name_ + "_incref");
decref_ = (signal_t)li_.get_function(name_ + "_decref");
// Getting number of inputs and outputs
get_n_in_ = (getint_t)li_.get_function(name + "_n_in");
get_n_out_ = (getint_t)li_.get_function(name + "_n_out");
// Getting names of inputs and outputs
get_name_in_ = (name_t)li_.get_function(name + "_name_in");
get_name_out_ = (name_t)li_.get_function(name + "_name_out");
// Work vector sizes
work_ = (work_t)li_.get_function(name_ + "_work");
// Increase reference counter - external function memory initialized at this point
if (incref_) incref_();
}
GenericExternal::GenericExternal(const std::string& name, const Importer& li)
: External(name, li) {
// Functions for retrieving sparsities of inputs and outputs
get_sparsity_in_ = (sparsity_t)li_.get_function(name + "_sparsity_in");
get_sparsity_out_ = (sparsity_t)li_.get_function(name + "_sparsity_out");
// Memory allocation functions
alloc_mem_ = (alloc_mem_t)li_.get_function(name_ + "_alloc_mem");
init_mem_ = (init_mem_t)li_.get_function(name_ + "_init_mem");
free_mem_ = (free_mem_t)li_.get_function(name_ + "_free_mem");
// Function for numerical evaluation
eval_ = (eval_t)li_.get_function(name_);
}
External::~External() {
if (decref_) decref_();
clear_mem();
}
size_t External::get_n_in() {
if (get_n_in_) {
return get_n_in_();
} else if (li_.has_meta(name_ + "_N_IN")) {
return li_.meta_int(name_ + "_N_IN");
} else {
// Fall back to base class
return FunctionInternal::get_n_in();
}
}
size_t External::get_n_out() {
if (get_n_out_) {
return get_n_out_();
} else if (li_.has_meta(name_ + "_N_OUT")) {
return li_.meta_int(name_ + "_N_OUT");
} else {
// Fall back to base class
return FunctionInternal::get_n_out();
}
}
string External::get_name_in(int i) {
if (get_name_in_) {
// Use function pointer
const char* n = get_name_in_(i);
casadi_assert_message(n!=0, "Error querying input name");
return n;
} else if (li_.has_meta(name_ + "_NAME_IN", i)) {
// Read meta
return li_.meta_string(name_ + "_NAME_IN", i);
} else {
// Default name
return FunctionInternal::get_name_in(i);
}
}
string External::get_name_out(int i) {
if (get_name_out_) {
// Use function pointer
const char* n = get_name_out_(i);
casadi_assert_message(n!=0, "Error querying output name");
return n;
} else if (li_.has_meta(name_ + "_NAME_OUT", i)) {
// Read meta
return li_.meta_string(name_ + "_NAME_OUT", i);
} else {
// Default name
return FunctionInternal::get_name_out(i);
}
}
Sparsity GenericExternal::get_sparsity_in(int i) {
// Use sparsity retrieval function, if present
if (get_sparsity_in_) {
return Sparsity::compressed(get_sparsity_in_(i));
} else if (li_.has_meta(name_ + "_SPARSITY_IN", i)) {
return Sparsity::compressed(li_.meta_vector<int>(name_ + "_SPARSITY_IN", i));
} else {
// Fall back to base class
return FunctionInternal::get_sparsity_in(i);
}
}
Sparsity GenericExternal::get_sparsity_out(int i) {
// Use sparsity retrieval function, if present
if (get_sparsity_out_) {
return Sparsity::compressed(get_sparsity_out_(i));
} else if (li_.has_meta(name_ + "_SPARSITY_OUT", i)) {
return Sparsity::compressed(li_.meta_vector<int>(name_ + "_SPARSITY_OUT", i));
} else {
// Fall back to base class
return FunctionInternal::get_sparsity_out(i);
}
}
void* GenericExternal::alloc_mem() const {
if (alloc_mem_) {
return alloc_mem_();
} else {
return FunctionInternal::alloc_mem();
}
}
int GenericExternal::init_mem(void* mem) const {
if (init_mem_) {
return init_mem_(mem);
} else {
return FunctionInternal::init_mem(mem);
}
}
void GenericExternal::free_mem(void *mem) const {
if (free_mem_) {
return free_mem_(mem);
} else {
return FunctionInternal::free_mem(mem);
}
}
void External::init(const Dict& opts) {
// Call the initialization method of the base class
FunctionInternal::init(opts);
// Reference counting?
has_refcount_ = li_.has_function(name_ + "_incref");
casadi_assert_message(has_refcount_==li_.has_function(name_ + "_decref"),
"External functions must provide functions for both increasing "
"and decreasing the reference count, or neither.");
// Allocate work vectors
int sz_arg=0, sz_res=0, sz_iw=0, sz_w=0;
if (work_) {
int flag = work_(&sz_arg, &sz_res, &sz_iw, &sz_w);
casadi_assert_message(flag==0, "External: \"work\" failed");
} else if (li_.has_meta(name_ + "_WORK")) {
vector<int> v = li_.meta_vector<int>(name_ + "_WORK");
casadi_assert(v.size()==4);
sz_arg = v[0];
sz_res = v[1];
sz_iw = v[2];
sz_w = v[3];
}
// Get information about Jacobian sparsity, if any
sparsity_t jac_sparsity_fcn = (sparsity_t)li_.get_function("jac_" + name_ + "_sparsity_out");
if (jac_sparsity_fcn) {
set_jac_sparsity(Sparsity::compressed(jac_sparsity_fcn(0)));
} else if (li_.has_meta("JAC_" + name_ + "_SPARSITY_OUT", 0)) {
vector<int> sp = li_.meta_vector<int>("jac_" + name_ + "_SPARSITY_OUT", 0);
set_jac_sparsity(Sparsity::compressed(sp));
}
alloc_arg(sz_arg);
alloc_res(sz_res);
alloc_iw(sz_iw);
alloc_w(sz_w);
}
void GenericExternal::init(const Dict& opts) {
// Call recursively
External::init(opts);
}
void External::codegen_declarations(CodeGenerator& g) const {
if (!li_.inlined(name_)) {
g.add_external(signature(name_) + ";");
}
}
void External::codegen_body(CodeGenerator& g) const {
if (li_.inlined(name_)) {
// Function body is inlined
g << li_.body(name_) << "\n";
} else {
g << "if (" << name_ << "(arg, res, iw, w, mem)) return 1;\n";
}
}
bool External::has_jacobian() const {
if (FunctionInternal::has_jacobian()) return true;
return li_.has_function("jac_" + name_);
}
Function External
::get_jacobian(const std::string& name,
const std::vector<std::string>& inames,
const std::vector<std::string>& onames,
const Dict& opts) const {
if (has_jacobian()) {
return external(name, li_, opts);
} else {
return FunctionInternal::get_jacobian(name, inames, onames, opts);
}
}
Function External
::get_forward(int nfwd, const std::string& name,
const std::vector<std::string>& inames,
const std::vector<std::string>& onames,
const Dict& opts) const {
// Consistency check
int n=1;
while (n<nfwd) n*=2;
if (n!=nfwd || !has_forward(nfwd)) {
// Inefficient code to be replaced later
Function fwd1 = forward(1);
return fwd1.map(name, "serial", nfwd, range(n_in_+n_out_), std::vector<int>(), opts);
//casadi_error("Internal error: Refactoring needed, cf. #1055");
}
return external(name, li_, opts);
}
bool External::has_forward(int nfwd) const {
return li_.has_function("fwd" + str(nfwd) + "_" + name_);
}
Function External
::get_reverse(int nadj, const std::string& name,
const std::vector<std::string>& inames,
const std::vector<std::string>& onames,
const Dict& opts) const {
// Consistency check
int n=1;
while (n<nadj) n*=2;
if (n!=nadj || !has_reverse(nadj)) {
// Inefficient code to be replaced later
Function adj1 = reverse(1);
return adj1.map(name, "serial", nadj, range(n_in_+n_out_), std::vector<int>(), opts);
//casadi_error("Internal error: Refactoring needed, cf. #1055");
}
return external(name, li_, opts);
}
bool External::has_reverse(int nadj) const {
return li_.has_function("adj" + str(nadj) + "_" + name_);
}
Function External::factory(const std::string& name,
const std::vector<std::string>& s_in,
const std::vector<std::string>& s_out,
const Function::AuxOut& aux,
const Dict& opts) const {
// If not available, call base class function
if (!li_.has_function(name)) {
return FunctionInternal::factory(name, s_in, s_out, aux, opts);
}
// Retrieve function
Function ret = external(name, li_, opts);
// Inputs consistency checks
casadi_assert_message(s_in.size() == ret.n_in(),
"Inconsistent number of inputs. Expected " + str(s_in.size())+ " "
"(" + str(s_in) + "), got " + str(ret.n_in()) + ".");
for (int i=0; i<s_in.size(); ++i) {
string s = s_in[i];
replace(s.begin(), s.end(), ':', '_');
casadi_assert_message(s == ret.name_in(i),
"Inconsistent input name. Expected: " + str(s_in) + ", "
"got " + ret.name_in(i) + " for input " + str(i));
}
// Outputs consistency checks
casadi_assert_message(s_out.size() == ret.n_out(),
"Inconsistent number of outputs. Expected " + str(s_out.size()) + " "
"(" + str(s_out) + "), got " + str(ret.n_out()) + ".");
for (int i=0; i<s_out.size(); ++i) {
string s = s_out[i];
replace(s.begin(), s.end(), ':', '_');
casadi_assert_message(s == ret.name_out(i),
"Inconsistent output name. Expected: " + str(s_out) + ", "
"got " + ret.name_out(i) + " for output " + str(i));
}
return ret;
}
} // namespace casadi