27 #include "casadi_os.hpp"
29 #include "filesystem_impl.hpp"
38 const std::string& dir,
const std::string& format,
bool verbose)
39 : base_filename_(base_filename), dir_(dir), format_(format), verbose_(verbose) {
40 casadi_assert_dev(x.
nnz()>0);
50 "Unable to create the required directory for '" + effective_dir +
"'.");
58 std::string
Dump::disp(
const std::vector<std::string>& arg)
const {
59 return "dump(" + arg.at(0) +
", " + base_filename_ +
")";
63 const std::vector<bool>& unique)
const {
65 if (!dir_.empty()) opts[
"dir"] = dir_;
66 if (!format_.empty()) opts[
"format"] = format_;
67 if (verbose_) opts[
"verbose"] =
true;
68 res[0] = arg[0].dump(base_filename_, opts);
73 std::copy(arg[0], arg[0]+
nnz(), res[0]);
78 int Dump::eval(
const double** arg,
double** res, casadi_int* iw,
double* w)
const {
81 ss << std::setfill(
'0') << std::setw(6) << dump_count_++;
82 std::string format = format_.empty() ?
"mtx" : format_;
83 std::string
filename = base_filename_ +
"." + ss.str() +
"." + format;
92 std::copy(arg[0], arg[0]+
nnz(), res[0]);
99 std::copy(arg[0], arg[0]+
nnz(), res[0]);
107 casadi_int n =
nnz();
109 for (casadi_int i=0; i<n; ++i) {
122 const std::vector<casadi_int>& arg,
123 const std::vector<casadi_int>& res,
124 const std::vector<bool>& arg_is_ref,
125 std::vector<bool>& res_is_ref)
const {
126 std::string format = format_.empty() ?
"mtx" : format_;
129 if (!effective_dir.empty()) prefix = effective_dir +
"/";
131 casadi_int buf_size = prefix.size() + base_filename_.size()
132 + 1 + 6 + 1 + format.size() + 1;
133 std::string a = g.
work(arg[0],
dep(0).
nnz(), arg_is_ref[0]);
136 g <<
"static int dump_id = 0;\n";
137 g <<
"char dump_fname[" << buf_size <<
"];\n";
138 g <<
"FILE* dump_file;\n";
139 g <<
"snprintf(dump_fname, " << buf_size <<
", \"" << prefix << base_filename_
140 <<
".%06d." << format <<
"\", dump_id++);\n";
142 g << g.
printf(
"dump -> %s\\n",
"dump_fname") <<
"\n";
144 g <<
"dump_file = fopen(dump_fname, \"w\");\n";
145 g <<
"if (dump_file) {\n";
147 g <<
"fclose(dump_file);\n";
157 s.
pack(
"Dump::base_filename", base_filename_);
158 s.
pack(
"Dump::dir", dir_);
159 s.
pack(
"Dump::format", format_);
160 s.
pack(
"Dump::verbose", verbose_);
165 s.
unpack(
"Dump::base_filename", base_filename_);
166 s.
unpack(
"Dump::dir", dir_);
167 s.
unpack(
"Dump::format", format_);
168 s.
unpack(
"Dump::verbose", verbose_);
Helper class for C code generation.
std::string work(casadi_int n, casadi_int sz, bool is_ref) const
std::string printf(const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
Printf.
std::string to_file(const std::string &f, const Sparsity &sp, const std::string &x)
Write matrix to file in MatrixMarket format.
std::string dump_dir_suffix
std::string dump_dir_prefix
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Dump(const MX &x, const std::string &base_filename, const std::string &dir, const std::string &format, bool verbose)
Constructor.
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
void reset_dump_count()
Reset the dump counter.
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
void add_dependency(CodeGenerator &g) const override
Add dependencies for code generation.
void generate(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref) const override
Generate code for the operation.
void finalize()
Validate options (called from constructors)
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
static std::string ensure_trailing_slash(const std::string &path)
static bool ensure_directory_exists(const std::string &path)
casadi_int nnz() const
Get the number of (structural) non-zero elements.
Node class for MX objects.
void generate_copy(CodeGenerator &g, const std::vector< casadi_int > &arg, const std::vector< casadi_int > &res, const std::vector< bool > &arg_is_ref, std::vector< bool > &res_is_ref, casadi_int i) const
const Sparsity & sparsity() const
Get the sparsity.
casadi_int nnz(casadi_int i=0) const
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
void set_dep(const MX &dep)
Set unary dependency.
const Sparsity & sparsity() const
Get the sparsity pattern.
void to_file(const std::string &filename, const std::string &format="") const
The basic scalar symbolic class of CasADi.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
unsigned long long bvec_t
void ensure_directory_exists(const std::string &dir)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string filename(const std::string &path)