monitor.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "monitor.hpp"
27 #include "function_internal.hpp"
28 namespace casadi {
29 
30  Monitor::Monitor(const MX& x, const std::string& comment) : comment_(comment) {
31  casadi_assert_dev(x.nnz()>0);
32  set_dep(x);
34  }
35 
36  std::string Monitor::disp(const std::vector<std::string>& arg) const {
37  return "monitor(" + arg.at(0) + ", " + comment_ + ")";
38  }
39 
40  void Monitor::eval_mx(const std::vector<MX>& arg, std::vector<MX>& res,
41  const std::vector<bool>& unique) const {
42  res[0] = arg[0].monitor(comment_);
43  }
44 
45  void Monitor::ad_forward(const std::vector<std::vector<MX> >& fseed,
46  std::vector<std::vector<MX> >& fsens) const {
47  for (casadi_int d=0; d<fsens.size(); ++d) {
48  std::stringstream ss;
49  ss << "fwd(" << d << ") of " << comment_;
50  fsens[d][0] = fseed[d][0].monitor(ss.str());
51  }
52  }
53 
54  void Monitor::ad_reverse(const std::vector<std::vector<MX> >& aseed,
55  std::vector<std::vector<MX> >& asens) const {
56  for (casadi_int d=0; d<aseed.size(); ++d) {
57  std::stringstream ss;
58  ss << "adj(" << d << ") of " << comment_;
59  asens[d][0] += aseed[d][0].monitor(ss.str());
60  }
61  }
62 
63  int Monitor::eval_sx(const SXElem** arg, SXElem** res, casadi_int* iw, SXElem* w) const {
64  if (arg[0]!=res[0]) {
65  std::copy(arg[0], arg[0]+nnz(), res[0]);
66  }
67  return 0;
68  }
69 
70  int Monitor::eval(const double** arg, double** res, casadi_int* iw, double* w) const {
71  // Print comment
72  uout() << comment_ << ":" << std::endl;
74  uout() << std::endl;
75  // Perform operation
76  if (arg[0]!=res[0]) {
77  std::copy(arg[0], arg[0]+nnz(), res[0]);
78  }
79  return 0;
80  }
81 
82  int Monitor::sp_forward(const bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
83  if (arg[0]!=res[0]) {
84  std::copy(arg[0], arg[0]+nnz(), res[0]);
85  }
86  return 0;
87  }
88 
89  int Monitor::sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w) const {
90  bvec_t *a = arg[0];
91  bvec_t *r = res[0];
92  casadi_int n = nnz();
93  if (a != r) {
94  for (casadi_int i=0; i<n; ++i) {
95  *a++ |= *r;
96  *r++ = 0;
97  }
98  }
99  return 0;
100  }
101 
103  const std::vector<casadi_int>& arg,
104  const std::vector<casadi_int>& res,
105  const std::vector<bool>& arg_is_ref,
106  std::vector<bool>& res_is_ref) const {
107  // Print comment
108  std::string a = g.work(arg[0], dep(0).nnz(), arg_is_ref[0]);
109  g << g.printf(comment_ + ":\\n") << "\n";
110  g << g.print_canonical(dep(0).sparsity(), a) << "\n";
111  g << g.printf("\\n") << "\n";
112  generate_copy(g, arg, res, arg_is_ref, res_is_ref, 0);
113  }
114 
117  s.pack("Monitor::comment", comment_);
118  }
119 
121  s.unpack("Monitor::comment", comment_);
122  }
123 
124 } // namespace casadi
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 print_canonical(const Sparsity &sp, const std::string &arg)
Print canonical representaion of a matrix.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
static void print_canonical(std::ostream &stream, const Sparsity &sp, const double *nz)
Print canonical representation of a numeric matrix.
casadi_int nnz() const
Get the number of (structural) non-zero elements.
Node class for MX objects.
Definition: mx_node.hpp:51
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
Definition: mx_node.cpp:460
const Sparsity & sparsity() const
Get the sparsity.
Definition: mx_node.hpp:410
casadi_int nnz(casadi_int i=0) const
Definition: mx_node.hpp:427
const MX & dep(casadi_int ind=0) const
dependencies - functions that have to be evaluated before this one
Definition: mx_node.hpp:392
virtual void serialize_body(SerializingStream &s) const
Serialize an object without type information.
Definition: mx_node.cpp:530
void set_sparsity(const Sparsity &sparsity)
Set the sparsity.
Definition: mx_node.cpp:224
void set_dep(const MX &dep)
Set unary dependency.
Definition: mx_node.cpp:228
MX - Matrix expression.
Definition: mx.hpp:92
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:612
std::string disp(const std::vector< std::string > &arg) const override
Print expression.
Definition: monitor.cpp:36
void eval_mx(const std::vector< MX > &arg, std::vector< MX > &res, const std::vector< bool > &unique={}) const override
Evaluate symbolically (MX)
Definition: monitor.cpp:40
int sp_forward(const bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity forward.
Definition: monitor.cpp:82
void ad_forward(const std::vector< std::vector< MX > > &fseed, std::vector< std::vector< MX > > &fsens) const override
Calculate forward mode directional derivatives.
Definition: monitor.cpp:45
void ad_reverse(const std::vector< std::vector< MX > > &aseed, std::vector< std::vector< MX > > &asens) const override
Calculate reverse mode directional derivatives.
Definition: monitor.cpp:54
int eval_sx(const SXElem **arg, SXElem **res, casadi_int *iw, SXElem *w) const override
Evaluate the function symbolically (SX)
Definition: monitor.cpp:63
int eval(const double **arg, double **res, casadi_int *iw, double *w) const override
Evaluate the function numerically.
Definition: monitor.cpp:70
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: monitor.cpp:115
Monitor(const MX &x, const std::string &comment)
Constructor.
Definition: monitor.cpp:30
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.
Definition: monitor.cpp:102
int sp_reverse(bvec_t **arg, bvec_t **res, casadi_int *iw, bvec_t *w) const override
Propagate sparsity backwards.
Definition: monitor.cpp:89
The basic scalar symbolic class of CasADi.
Definition: sx_elem.hpp:75
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
The casadi namespace.
Definition: archiver.cpp:28
unsigned long long bvec_t
std::ostream & uout()