casadi 3.6.7__cp38-none-manylinux2014_aarch64.whl → 3.7.1__cp38-none-manylinux2014_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. casadi/_casadi.so +0 -0
  2. casadi/casadi-cli +0 -0
  3. casadi/casadi.py +6531 -3039
  4. casadi/cmake/casadi-config-version.cmake +1 -1
  5. casadi/cmake/casadi-config.cmake +1 -1
  6. casadi/cmake/casadi-targets-release.cmake +5 -5
  7. casadi/cmake/casadi-targets.cmake +5 -5
  8. casadi/{lib/cmake/tinyxml2/tinyxml2-config-version.cmake → cmake/ghc_filesystem/ghc_filesystem-config-version.cmake} +30 -10
  9. casadi/cmake/ghc_filesystem/ghc_filesystem-config.cmake +30 -0
  10. casadi/cmake/ghc_filesystem/ghc_filesystem-targets.cmake +107 -0
  11. casadi/cmake/libzip/libzip-config-version.cmake +43 -0
  12. casadi/cmake/libzip/libzip-config.cmake +69 -0
  13. casadi/cmake/libzip/libzip-targets-release.cmake +19 -0
  14. casadi/{lib/cmake/tinyxml2/tinyxml2-static-targets.cmake → cmake/libzip/libzip-targets.cmake} +11 -7
  15. casadi/cmake/libzip/modules/FindMbedTLS.cmake +141 -0
  16. casadi/cmake/libzip/modules/FindNettle.cmake +141 -0
  17. casadi/cmake/libzip/modules/Findzstd.cmake +186 -0
  18. casadi/include/casadi/casadi.i +276 -47
  19. casadi/include/casadi/config.h +11 -11
  20. casadi/include/casadi/core/archiver.hpp +58 -0
  21. casadi/include/casadi/core/blazing_spline.hpp +47 -0
  22. casadi/include/casadi/core/calculus.hpp +57 -2
  23. casadi/include/casadi/core/callback.hpp +9 -0
  24. casadi/include/casadi/core/casadi_common.hpp +37 -0
  25. casadi/include/casadi/core/casadi_meta.hpp +15 -0
  26. casadi/include/casadi/core/casadi_misc.hpp +21 -0
  27. casadi/include/casadi/core/code_generator.hpp +115 -19
  28. casadi/include/casadi/core/core.hpp +5 -0
  29. casadi/include/casadi/core/dae_builder.hpp +303 -141
  30. casadi/include/casadi/core/dm.hpp +3 -0
  31. casadi/include/casadi/core/filesystem.hpp +58 -0
  32. casadi/include/casadi/core/fmu.hpp +62 -16
  33. casadi/include/casadi/core/function.hpp +24 -0
  34. casadi/include/casadi/core/generic_matrix.hpp +214 -7
  35. casadi/include/casadi/core/generic_shared.hpp +395 -0
  36. casadi/include/casadi/core/generic_shared_impl.hpp +218 -0
  37. casadi/include/casadi/core/generic_shared_internal.hpp +215 -0
  38. casadi/include/casadi/core/generic_type.hpp +3 -0
  39. casadi/include/casadi/core/global_options.hpp +10 -0
  40. casadi/include/casadi/core/integrator.hpp +41 -7
  41. casadi/include/casadi/core/matrix_decl.hpp +71 -0
  42. casadi/include/casadi/core/mx.hpp +63 -2
  43. casadi/include/casadi/core/nlp_builder.hpp +2 -1
  44. casadi/include/casadi/core/options.hpp +6 -3
  45. casadi/include/casadi/core/optistack.hpp +43 -9
  46. casadi/include/casadi/core/printable.hpp +8 -0
  47. casadi/include/casadi/core/resource.hpp +107 -0
  48. casadi/include/casadi/core/runtime/casadi_blazing_1d_boor_eval.hpp +112 -0
  49. casadi/include/casadi/core/runtime/casadi_blazing_2d_boor_eval.hpp +311 -0
  50. casadi/include/casadi/core/runtime/casadi_blazing_3d_boor_eval.hpp +645 -0
  51. casadi/include/casadi/core/runtime/casadi_blazing_de_boor.hpp +101 -0
  52. casadi/include/casadi/core/runtime/casadi_finite_diff.hpp +1 -1
  53. casadi/include/casadi/core/runtime/casadi_nlp.hpp +8 -2
  54. casadi/include/casadi/core/runtime/casadi_print_canonical.hpp +55 -0
  55. casadi/include/casadi/core/runtime/casadi_print_scalar.hpp +25 -0
  56. casadi/include/casadi/core/runtime/casadi_print_vector.hpp +32 -0
  57. casadi/include/casadi/core/runtime/casadi_printme.hpp +26 -0
  58. casadi/include/casadi/core/serializer.hpp +13 -5
  59. casadi/include/casadi/core/serializing_stream.hpp +9 -2
  60. casadi/include/casadi/core/shared_object.hpp +73 -161
  61. casadi/include/casadi/core/sparsity.hpp +13 -1
  62. casadi/include/casadi/core/sparsity_interface.hpp +19 -1
  63. casadi/include/casadi/core/sx.hpp +41 -0
  64. casadi/include/casadi/core/sx_elem.hpp +25 -0
  65. casadi/include/casadi/core/xml_node.hpp +5 -0
  66. casadi/include/casadi/doc.i +10026 -6513
  67. casadi/include/casadi/doc_merged.i +6744 -4449
  68. casadi/include/casadi/valgrind-casadi.supp +138 -0
  69. casadi/include/casadi/valgrind-python.supp +2470 -0
  70. casadi/include/fatrop/blasfeo_wrapper/LinearAlgebraBlasfeo.hpp +4 -1
  71. casadi/include/ghc/filesystem.hpp +6083 -0
  72. casadi/include/ghc/fs_fwd.hpp +38 -0
  73. casadi/include/ghc/fs_impl.hpp +35 -0
  74. casadi/include/ghc/fs_std.hpp +60 -0
  75. casadi/include/ghc/fs_std_fwd.hpp +63 -0
  76. casadi/include/ghc/fs_std_impl.hpp +46 -0
  77. casadi/include/licenses/ghc-external/LICENSE +19 -0
  78. casadi/include/licenses/libz-external/LICENSE +22 -0
  79. casadi/include/licenses/libz-external/contrib/dotzlib/LICENSE_1_0.txt +23 -0
  80. casadi/include/licenses/libzip-external/LICENSE +31 -0
  81. casadi/include/zconf.h +545 -0
  82. casadi/include/zip.h +528 -0
  83. casadi/include/zipconf.h +48 -0
  84. casadi/include/zlib.h +1938 -0
  85. casadi/libcasadi-tp-openblas.so +0 -0
  86. casadi/libcasadi-tp-openblas.so.0 +0 -0
  87. casadi/libcasadi-tp-openblas.so.0.3 +0 -0
  88. casadi/libcasadi.so +0 -0
  89. casadi/libcasadi.so.3.7 +0 -0
  90. casadi/libcasadi_archiver_libzip.so +0 -0
  91. casadi/libcasadi_archiver_libzip.so.3.7 +0 -0
  92. casadi/libcasadi_conic_cplex.so +0 -0
  93. casadi/libcasadi_conic_cplex.so.3.7 +0 -0
  94. casadi/libcasadi_conic_daqp.so +0 -0
  95. casadi/libcasadi_conic_daqp.so.3.7 +0 -0
  96. casadi/libcasadi_conic_fatrop.so +0 -0
  97. casadi/libcasadi_conic_fatrop.so.3.7 +0 -0
  98. casadi/libcasadi_conic_gurobi.so +0 -0
  99. casadi/libcasadi_conic_gurobi.so.3.7 +0 -0
  100. casadi/libcasadi_conic_hpipm.so +0 -0
  101. casadi/libcasadi_conic_hpipm.so.3.7 +0 -0
  102. casadi/libcasadi_conic_ipqp.so +0 -0
  103. casadi/libcasadi_conic_ipqp.so.3.7 +0 -0
  104. casadi/libcasadi_conic_nlpsol.so +0 -0
  105. casadi/libcasadi_conic_nlpsol.so.3.7 +0 -0
  106. casadi/libcasadi_conic_osqp.so +0 -0
  107. casadi/libcasadi_conic_osqp.so.3.7 +0 -0
  108. casadi/libcasadi_conic_proxqp.so +0 -0
  109. casadi/libcasadi_conic_proxqp.so.3.7 +0 -0
  110. casadi/libcasadi_conic_qpoases.so +0 -0
  111. casadi/libcasadi_conic_qpoases.so.3.7 +0 -0
  112. casadi/libcasadi_conic_qrqp.so +0 -0
  113. casadi/libcasadi_conic_qrqp.so.3.7 +0 -0
  114. casadi/libcasadi_conic_superscs.so +0 -0
  115. casadi/libcasadi_conic_superscs.so.3.7 +0 -0
  116. casadi/libcasadi_filesystem_ghc.so +0 -0
  117. casadi/libcasadi_filesystem_ghc.so.3.7 +0 -0
  118. casadi/libcasadi_importer_shell.so +0 -0
  119. casadi/libcasadi_importer_shell.so.3.7 +0 -0
  120. casadi/libcasadi_integrator_collocation.so +0 -0
  121. casadi/libcasadi_integrator_collocation.so.3.7 +0 -0
  122. casadi/libcasadi_integrator_cvodes.so +0 -0
  123. casadi/libcasadi_integrator_cvodes.so.3.7 +0 -0
  124. casadi/libcasadi_integrator_idas.so +0 -0
  125. casadi/libcasadi_integrator_idas.so.3.7 +0 -0
  126. casadi/libcasadi_integrator_rk.so +0 -0
  127. casadi/libcasadi_integrator_rk.so.3.7 +0 -0
  128. casadi/libcasadi_interpolant_bspline.so +0 -0
  129. casadi/libcasadi_interpolant_bspline.so.3.7 +0 -0
  130. casadi/libcasadi_interpolant_linear.so +0 -0
  131. casadi/libcasadi_interpolant_linear.so.3.7 +0 -0
  132. casadi/libcasadi_linsol_csparse.so +0 -0
  133. casadi/libcasadi_linsol_csparse.so.3.7 +0 -0
  134. casadi/libcasadi_linsol_csparsecholesky.so +0 -0
  135. casadi/libcasadi_linsol_csparsecholesky.so.3.7 +0 -0
  136. casadi/libcasadi_linsol_lapacklu.so +0 -0
  137. casadi/libcasadi_linsol_lapacklu.so.3.7 +0 -0
  138. casadi/libcasadi_linsol_lapackqr.so +0 -0
  139. casadi/libcasadi_linsol_lapackqr.so.3.7 +0 -0
  140. casadi/libcasadi_linsol_ldl.so +0 -0
  141. casadi/libcasadi_linsol_ldl.so.3.7 +0 -0
  142. casadi/libcasadi_linsol_lsqr.so +0 -0
  143. casadi/libcasadi_linsol_lsqr.so.3.7 +0 -0
  144. casadi/libcasadi_linsol_ma27.so +0 -0
  145. casadi/libcasadi_linsol_ma27.so.3.7 +0 -0
  146. casadi/libcasadi_linsol_mumps.so +0 -0
  147. casadi/libcasadi_linsol_mumps.so.3.7 +0 -0
  148. casadi/libcasadi_linsol_qr.so +0 -0
  149. casadi/libcasadi_linsol_qr.so.3.7 +0 -0
  150. casadi/libcasadi_linsol_symbolicqr.so +0 -0
  151. casadi/libcasadi_linsol_symbolicqr.so.3.7 +0 -0
  152. casadi/libcasadi_linsol_tridiag.so +0 -0
  153. casadi/libcasadi_linsol_tridiag.so.3.7 +0 -0
  154. casadi/libcasadi_nlpsol_alpaqa.so +0 -0
  155. casadi/libcasadi_nlpsol_alpaqa.so.3.7 +0 -0
  156. casadi/libcasadi_nlpsol_ampl.so +0 -0
  157. casadi/libcasadi_nlpsol_ampl.so.3.7 +0 -0
  158. casadi/libcasadi_nlpsol_blocksqp.so +0 -0
  159. casadi/libcasadi_nlpsol_blocksqp.so.3.7 +0 -0
  160. casadi/libcasadi_nlpsol_fatrop.so +0 -0
  161. casadi/libcasadi_nlpsol_fatrop.so.3.7 +0 -0
  162. casadi/libcasadi_nlpsol_feasiblesqpmethod.so +0 -0
  163. casadi/libcasadi_nlpsol_feasiblesqpmethod.so.3.7 +0 -0
  164. casadi/libcasadi_nlpsol_ipopt.so +0 -0
  165. casadi/libcasadi_nlpsol_ipopt.so.3.7 +0 -0
  166. casadi/libcasadi_nlpsol_knitro.so +0 -0
  167. casadi/libcasadi_nlpsol_knitro.so.3.7 +0 -0
  168. casadi/libcasadi_nlpsol_madnlp.so +0 -0
  169. casadi/libcasadi_nlpsol_madnlp.so.3.7 +0 -0
  170. casadi/libcasadi_nlpsol_qrsqp.so +0 -0
  171. casadi/libcasadi_nlpsol_qrsqp.so.3.7 +0 -0
  172. casadi/libcasadi_nlpsol_scpgen.so +0 -0
  173. casadi/libcasadi_nlpsol_scpgen.so.3.7 +0 -0
  174. casadi/libcasadi_nlpsol_snopt.so +0 -0
  175. casadi/libcasadi_nlpsol_snopt.so.3.7 +0 -0
  176. casadi/libcasadi_nlpsol_sqpmethod.so +0 -0
  177. casadi/libcasadi_nlpsol_sqpmethod.so.3.7 +0 -0
  178. casadi/libcasadi_nlpsol_worhp.so +0 -0
  179. casadi/libcasadi_nlpsol_worhp.so.3.7 +0 -0
  180. casadi/libcasadi_rootfinder_fast_newton.so +0 -0
  181. casadi/libcasadi_rootfinder_fast_newton.so.3.7 +0 -0
  182. casadi/libcasadi_rootfinder_kinsol.so +0 -0
  183. casadi/libcasadi_rootfinder_kinsol.so.3.7 +0 -0
  184. casadi/libcasadi_rootfinder_newton.so +0 -0
  185. casadi/libcasadi_rootfinder_newton.so.3.7 +0 -0
  186. casadi/libcasadi_rootfinder_nlpsol.so +0 -0
  187. casadi/libcasadi_rootfinder_nlpsol.so.3.7 +0 -0
  188. casadi/libcasadi_sundials_common.so +0 -0
  189. casadi/libcasadi_sundials_common.so.3.7 +0 -0
  190. casadi/libcasadi_xmlfile_tinyxml.so +0 -0
  191. casadi/libcasadi_xmlfile_tinyxml.so.3.7 +0 -0
  192. casadi/libcoinmumps.so +0 -0
  193. casadi/libcoinmumps.so.3 +0 -0
  194. casadi/libcoinmumps.so.3.0.1 +0 -0
  195. casadi/libfatrop.so +0 -0
  196. casadi/libgfortran-9121747c.so.5.0.0 +0 -0
  197. casadi/libipopt.so +0 -0
  198. casadi/libipopt.so.3 +0 -0
  199. casadi/libipopt.so.3.14.11 +0 -0
  200. casadi/libsipopt.so +0 -0
  201. casadi/libsipopt.so.3 +0 -0
  202. casadi/libsipopt.so.3.14.11 +0 -0
  203. casadi/libz.a +0 -0
  204. casadi/libz.so +0 -0
  205. casadi/libz.so.1 +0 -0
  206. casadi/libz.so.1.3.1 +0 -0
  207. casadi/libzip.a +0 -0
  208. casadi/pkgconfig/casadi.pc +1 -1
  209. casadi/pkgconfig/libzip.pc +14 -0
  210. casadi/tools/__init__.py +3 -1
  211. casadi/tools/graph/graph.py +1 -1
  212. casadi/tools/structure3.py +7 -7
  213. {casadi-3.6.7.dist-info → casadi-3.7.1.dist-info}/METADATA +1 -1
  214. {casadi-3.6.7.dist-info → casadi-3.7.1.dist-info}/RECORD +215 -173
  215. casadi/include/tinyxml2.h +0 -2380
  216. casadi/lib/cmake/tinyxml2/tinyxml2-config.cmake +0 -57
  217. casadi/lib/cmake/tinyxml2/tinyxml2-static-targets-release.cmake +0 -19
  218. casadi/lib/libtinyxml2.a +0 -0
  219. casadi/lib/pkgconfig/tinyxml2.pc +0 -10
  220. casadi/tools/structure.py +0 -1446
  221. {casadi-3.6.7.dist-info → casadi-3.7.1.dist-info}/WHEEL +0 -0
@@ -0,0 +1,395 @@
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
+ #ifndef CASADI_GENERIC_SHARED_HPP
27
+ #define CASADI_GENERIC_SHARED_HPP
28
+
29
+ #include "casadi_common.hpp"
30
+ #include "exception.hpp"
31
+ #include <unordered_map>
32
+ #include <vector>
33
+ #include <cstdint>
34
+ #ifdef CASADI_WITH_THREAD
35
+ #ifdef CASADI_WITH_THREAD_MINGW
36
+ #include <mingw.mutex.h>
37
+ #else // CASADI_WITH_THREAD_MINGW
38
+ #include <mutex>
39
+ #endif // CASADI_WITH_THREAD_MINGW
40
+ #endif //CASADI_WITH_THREAD
41
+
42
+ #include <memory>
43
+
44
+ namespace casadi {
45
+
46
+ // Forward declaration of weak reference class
47
+ template<typename Shared, typename Internal>
48
+ class GenericWeakRef;
49
+
50
+ /// \cond INTERNAL
51
+ // Forward declaration of internal classes
52
+ template<typename Shared, typename Internal>
53
+ class GenericSharedInternal;
54
+
55
+ template<typename Shared, typename Internal>
56
+ class GenericWeakRefInternal;
57
+ /// \endcond
58
+
59
+ template<typename Shared, typename Internal>
60
+ class CASADI_EXPORT GenericShared {
61
+ #ifndef SWIG
62
+ template<class B, class S> friend B shared_cast(S& A);
63
+ template<class B, class S> friend const B shared_cast(const S& A);
64
+ #endif // SWIG
65
+
66
+ public:
67
+ #ifndef SWIG
68
+ /// Default constructor
69
+ GenericShared() {
70
+ node = nullptr;
71
+ }
72
+
73
+ /// Copy constructor (shallow copy)
74
+ GenericShared(const GenericShared& ref) {
75
+ node = ref.node;
76
+ count_up();
77
+ }
78
+
79
+ /// Destructor
80
+ ~GenericShared() {
81
+ count_down();
82
+ }
83
+
84
+ /// Assignment operator
85
+ GenericShared& operator=(const GenericShared& ref);
86
+
87
+ /// \cond INTERNAL
88
+ /// Assign the node to a node class pointer (or null)
89
+ void own(Internal* node);
90
+
91
+ /** \brief Assign the node to a node class pointer without reference counting
92
+ *
93
+ * improper use will cause memory leaks!
94
+
95
+ \identifier{at} */
96
+ void assign(Internal* node);
97
+
98
+ /// Get a const pointer to the node
99
+ Internal* get() const;
100
+
101
+ /// Get the reference count
102
+ casadi_int getCount() const;
103
+
104
+ /// Swap content with another instance
105
+ void swap(GenericShared& other);
106
+
107
+ /// Access a member function or object
108
+ Internal* operator->() const;
109
+ /// \endcond
110
+ #endif // SWIG
111
+
112
+ std::string debug_repr() const;
113
+
114
+
115
+ /// Is a null pointer?
116
+ bool is_null() const;
117
+
118
+ /** \brief Returns a number that is unique for a given Node.
119
+
120
+ * If the Object does not point to any node, "0" is returned.
121
+
122
+ \identifier{av} */
123
+ casadi_int __hash__() const;
124
+
125
+ /// \cond INTERNAL
126
+ #ifndef SWIG
127
+ /** \brief Get a weak reference to the object
128
+
129
+ \identifier{aw} */
130
+ GenericWeakRef<Shared, Internal>* weak();
131
+ protected:
132
+ void count_up(); // increase counter of the node
133
+ void count_down(); // decrease counter of the node
134
+ private:
135
+ Internal *node;
136
+ #endif // SWIG
137
+ /// \endcond
138
+ };
139
+
140
+ template<typename Shared, typename Internal>
141
+ class CASADI_EXPORT GenericWeakRef : public GenericShared<Shared, Internal> {
142
+ public:
143
+ friend class GenericSharedInternal<Shared, Internal>;
144
+
145
+ using GenericShared<Shared, Internal>::is_null;
146
+
147
+ /** \brief Default constructor
148
+
149
+ \identifier{ay} */
150
+ GenericWeakRef(int dummy=0);
151
+
152
+ /** \brief Construct from a shared object (also implicit type conversion)
153
+
154
+ \identifier{az} */
155
+ GenericWeakRef(Shared shared);
156
+
157
+ /** \brief Get a shared (owning) reference
158
+
159
+ \identifier{b0} */
160
+ Shared shared() const;
161
+
162
+ /** \brief Check if alive
163
+
164
+ \identifier{b1} */
165
+ bool alive() const;
166
+
167
+ /** \brief Thread-safe alternative to alive()/shared()
168
+
169
+ \identifier{29i} */
170
+ bool shared_if_alive(Shared& shared) const;
171
+
172
+ /** \brief Access functions of the node
173
+
174
+ \identifier{b2} */
175
+ GenericWeakRefInternal<Shared, Internal>* operator->();
176
+
177
+ /** \brief Const access functions of the node
178
+
179
+ \identifier{b3} */
180
+ const GenericWeakRefInternal<Shared, Internal>* operator->() const;
181
+
182
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
183
+ std::shared_ptr<std::mutex> get_mutex() const;
184
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
185
+
186
+ #ifndef SWIG
187
+ private:
188
+ /** \brief Construct from a shared object (internal)
189
+
190
+ \identifier{b4} */
191
+ explicit GenericWeakRef(Internal* raw);
192
+
193
+ /** \brief The shared object has been deleted
194
+
195
+ \identifier{b5} */
196
+ void kill();
197
+ #endif // SWIG
198
+ };
199
+
200
+ #ifndef SWIG
201
+
202
+ /** \brief Typecast a shared object to a base class to a shared object to a derived class,
203
+
204
+ * cf. dynamic_cast
205
+
206
+ \identifier{b6} */
207
+ template<class B, class S>
208
+ B shared_cast(S& A) {
209
+
210
+ /// Get a pointer to the node
211
+ typename S::internal_base_type* ptr = A.get();
212
+
213
+ /// Create a return object
214
+ B ret;
215
+
216
+ /// Quick return if not allowed
217
+ if (!B::test_cast(ptr)) return ret;
218
+
219
+ /// Assign node of B and return
220
+ ret.own(ptr);
221
+ return ret;
222
+ }
223
+
224
+ /** \brief Typecast a shared object to a base class to a shared object to a derived class,
225
+
226
+ * cf. dynamic_cast (const)
227
+
228
+ \identifier{b7} */
229
+ template<class B, class S>
230
+ const B shared_cast(const S& A) {
231
+ S A_copy = A;
232
+ return shared_cast<B, S>(A_copy);
233
+ }
234
+
235
+ #endif // SWIG
236
+
237
+ /**
238
+ * Key is stored as a regular ref
239
+ * Value is stored as weakref
240
+ */
241
+ template<typename K, typename T>
242
+ class CASADI_EXPORT WeakCache {
243
+ public:
244
+ void tocache(const K& key, const T& f, bool needs_lock=true) {
245
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
246
+ // Safe access to cache_
247
+ casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
248
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
249
+ // Add to cache
250
+ cache_.insert(std::make_pair(key, f));
251
+ // Remove a lost reference, if any, to prevent uncontrolled growth
252
+ for (auto it = cache_.begin(); it!=cache_.end(); ++it) {
253
+ if (!it->second.alive()) {
254
+ cache_.erase(it);
255
+ break; // just one dead reference is enough
256
+ }
257
+ }
258
+ }
259
+ /* \brief Thread-safe unique caching
260
+ * While an incache/tocache pair in multi-threaded context is safe
261
+ * it may lead to fresh cache entries being overwritten.
262
+ *
263
+ * A mutex lock_guard on the scope of an incache/tocache pair
264
+ * may lead to deadlocks.
265
+ *
266
+ */
267
+ void tocache_if_missing(const K& key, T& f) {
268
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
269
+ // Safe access to cache_
270
+ std::lock_guard<std::mutex> lock(mtx_);
271
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
272
+ if (!incache(key, f, false)) {
273
+ tocache(key, f, false);
274
+ }
275
+ }
276
+ bool incache(const K& key, T& f, bool needs_lock=true) const {
277
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
278
+ // Safe access to cache_
279
+ casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
280
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
281
+ auto it = cache_.find(key);
282
+ typename T::base_type temp;
283
+ if (it!=cache_.end() && it->second.shared_if_alive(temp)) {
284
+ f = shared_cast<T>(temp);
285
+ return true;
286
+ } else {
287
+ return false;
288
+ }
289
+ }
290
+ void cache(std::vector<K>& keys, std::vector<T>& entries) const {
291
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
292
+ // Safe access to cache_
293
+ std::lock_guard<std::mutex> lock(mtx_);
294
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
295
+ keys.clear();
296
+ entries.clear();
297
+ // Add all entries that haven't been deleted
298
+ for (auto&& cf : cache_) {
299
+ typename T::base_type temp;
300
+ if (cf.second.shared_if_alive(temp)) {
301
+ keys.push_back(cf.first);
302
+ entries.push_back(shared_cast<T>(temp));
303
+ }
304
+ }
305
+ }
306
+ private:
307
+ std::unordered_map<K,
308
+ GenericWeakRef<typename T::base_type, typename T::internal_base_type>
309
+ > cache_;
310
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
311
+ mutable std::mutex mtx_;
312
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
313
+ };
314
+
315
+ // gcc-12 is overzealous about use-after-free warnings
316
+ // <12 or >12 works fine
317
+ #pragma GCC diagnostic push
318
+ #if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ == 12)
319
+ #pragma GCC diagnostic ignored "-Wuse-after-free"
320
+ #endif
321
+
322
+ /**
323
+ * Key is stored as a weakref
324
+ * Value is stored as regular ref
325
+ */
326
+ template<typename K, typename T>
327
+ class CASADI_EXPORT RevWeakCache {
328
+ public:
329
+ void tocache(const K& key, const T& f, bool needs_lock=true) {
330
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
331
+ // Safe access to cache_
332
+ casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
333
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
334
+ // Add to cache
335
+ const void* k = key.get();
336
+ pre_cache_.insert(std::make_pair(k, key));
337
+ cache_.insert(std::make_pair(k, f));
338
+ // Remove a lost reference, if any, to prevent uncontrolled growth
339
+ for (auto it = pre_cache_.begin(); it!=pre_cache_.end(); ++it) {
340
+ if (!it->second.alive()) {
341
+ pre_cache_.erase(it);
342
+ cache_.erase(it->first);
343
+ break; // just one dead reference is enough
344
+ }
345
+ }
346
+ }
347
+ /* \brief Thread-safe unique caching
348
+ * While an incache/tocache pair in multi-threaded context is safe
349
+ * it may lead to fresh cache entries being overwritten.
350
+ *
351
+ * A mutex lock_guard on the scope of an incache/tocache pair
352
+ * may lead to deadlocks.
353
+ *
354
+ */
355
+ void tocache_if_missing(const K& key, T& f) {
356
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
357
+ // Safe access to cache_
358
+ std::lock_guard<std::mutex> lock(mtx_);
359
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
360
+ if (!incache(key, f, false)) {
361
+ tocache(key, f, false);
362
+ }
363
+ }
364
+ bool incache(const K& key, T& f, bool needs_lock=true) const {
365
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
366
+ // Safe access to cache_
367
+ casadi::conditional_lock_guard<std::mutex> lock(mtx_, needs_lock);
368
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
369
+ const void* k = key.get();
370
+ auto it = pre_cache_.find(k);
371
+ K temp;
372
+ if (it!=pre_cache_.end() && it->second.shared_if_alive(temp)) {
373
+ auto it2 = cache_.find(k);
374
+ f = it2->second;
375
+ return true;
376
+ } else {
377
+ return false;
378
+ }
379
+ }
380
+ private:
381
+ std::unordered_map<const void*,
382
+ GenericWeakRef<typename K::base_type, typename K::internal_base_type>
383
+ > pre_cache_;
384
+ std::unordered_map<const void*, T> cache_;
385
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
386
+ mutable std::mutex mtx_;
387
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
388
+ };
389
+
390
+ #pragma GCC diagnostic pop
391
+
392
+ } // namespace casadi
393
+
394
+
395
+ #endif // CASADI_GENERIC_SHARED_HPP
@@ -0,0 +1,218 @@
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
+ #ifndef CASADI_GENERIC_SHARED_IMPL_HPP
27
+ #define CASADI_GENERIC_SHARED_IMPL_HPP
28
+
29
+ #ifdef WITH_EXTRA_CHECKS
30
+ #include "function.hpp"
31
+ #endif // WITH_EXTRA_CHECKS
32
+
33
+ namespace casadi {
34
+
35
+ template<typename Shared, typename Internal>
36
+ void GenericShared<Shared, Internal>::count_up() {
37
+ #ifdef WITH_EXTRA_CHECKS
38
+ casadi_assert_dev(Function::call_depth_==0);
39
+ #endif // WITH_EXTRA_CHECKS
40
+
41
+ if (node) static_cast<Internal*>(node)->count++;
42
+
43
+ }
44
+
45
+ template<typename Shared, typename Internal>
46
+ void GenericShared<Shared, Internal>::count_down() {
47
+ #ifdef WITH_EXTRA_CHECKS
48
+ casadi_assert_dev(Function::call_depth_==0);
49
+ #endif // WITH_EXTRA_CHECKS
50
+ if (!node) return;
51
+ if (node->weak_ref_) {
52
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
53
+ auto mutex = node->weak_ref_->get_mutex();
54
+ // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
55
+ std::lock_guard<std::mutex> lock(*mutex);
56
+ // Could it be that this mutex is destroyed when the lock goes out of scope?
57
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
58
+
59
+ if (--static_cast<Internal*>(node)->count == 0) {
60
+ delete node;
61
+ node = nullptr;
62
+ }
63
+ } else {
64
+ if (--static_cast<Internal*>(node)->count == 0) {
65
+ delete node;
66
+ node = nullptr;
67
+ }
68
+ }
69
+ }
70
+
71
+ template<typename Shared, typename Internal>
72
+ void GenericShared<Shared, Internal>::own(Internal* node_) {
73
+ count_down();
74
+ node = node_;
75
+ count_up();
76
+ }
77
+
78
+ template<typename Shared, typename Internal>
79
+ void GenericShared<Shared, Internal>::assign(Internal* node_) {
80
+ node = node_;
81
+ }
82
+
83
+ template<typename Shared, typename Internal>
84
+ std::string GenericShared<Shared, Internal>::debug_repr() const {
85
+ if (node) {
86
+ return node->debug_repr(node);
87
+ } else {
88
+ return "NULL";
89
+ }
90
+ }
91
+
92
+ template<typename Shared, typename Internal>
93
+ GenericShared<Shared, Internal>&
94
+ GenericShared<Shared, Internal>::operator=(const GenericShared& ref) {
95
+ // quick return if the old and new pointers point to the same object
96
+ if (node == ref.node) return *this;
97
+
98
+ // decrease the counter and delete if this was the last pointer
99
+ count_down();
100
+
101
+ // save the new pointer
102
+ node = ref.node;
103
+ count_up();
104
+ return *this;
105
+ }
106
+
107
+ template<typename Shared, typename Internal>
108
+ Internal* GenericShared<Shared, Internal>::get() const {
109
+ return node;
110
+ }
111
+
112
+ template<typename Shared, typename Internal>
113
+ bool GenericShared<Shared, Internal>::is_null() const {
114
+ return node==nullptr;
115
+ }
116
+
117
+ template<typename Shared, typename Internal>
118
+ Internal* GenericShared<Shared, Internal>::operator->() const {
119
+ casadi_assert_dev(!is_null());
120
+ return node;
121
+ }
122
+
123
+ template<typename Shared, typename Internal>
124
+ void GenericShared<Shared, Internal>::swap(GenericShared& other) {
125
+ GenericShared<Shared, Internal> temp = *this;
126
+ *this = other;
127
+ other = temp;
128
+ }
129
+
130
+ template<typename Shared, typename Internal>
131
+ casadi_int GenericShared<Shared, Internal>::getCount() const {
132
+ return (*this)->getCount();
133
+ }
134
+
135
+ template<typename Shared, typename Internal>
136
+ GenericWeakRef<Shared, Internal>* GenericShared<Shared, Internal>::weak() {
137
+ return (*this)->weak();
138
+ }
139
+
140
+ template<typename Shared, typename Internal>
141
+ casadi_int GenericShared<Shared, Internal>::__hash__() const {
142
+ return reinterpret_cast<casadi_int>(get());
143
+ }
144
+
145
+ template<typename Shared, typename Internal>
146
+ GenericWeakRef<Shared, Internal>::GenericWeakRef(int dummy) {
147
+ casadi_assert_dev(dummy==0);
148
+ }
149
+
150
+ template<typename Shared, typename Internal>
151
+ bool GenericWeakRef<Shared, Internal>::alive() const {
152
+ return !is_null() && (*this)->raw_ != nullptr;
153
+ }
154
+
155
+ template<typename Shared, typename Internal>
156
+ Shared GenericWeakRef<Shared, Internal>::shared() const {
157
+ Shared ret;
158
+ if (alive()) {
159
+ ret.own((*this)->raw_);
160
+ }
161
+ return ret;
162
+ }
163
+
164
+ template<typename Shared, typename Internal>
165
+ bool GenericWeakRef<Shared, Internal>::shared_if_alive(Shared& shared) const {
166
+ if (is_null()) return false;
167
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
168
+ // Safe access to ...
169
+ std::lock_guard<std::mutex> lock(*(*this)->mutex_);
170
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
171
+ if (alive()) {
172
+ shared.own((*this)->raw_);
173
+ return true;
174
+ }
175
+ return false;
176
+ }
177
+
178
+ template<typename Shared, typename Internal>
179
+ const GenericWeakRefInternal<Shared, Internal>*
180
+ GenericWeakRef<Shared, Internal>::operator->() const {
181
+ return static_cast<const GenericWeakRefInternal<Shared, Internal>*>(
182
+ GenericShared<Shared, Internal>::operator->());
183
+ }
184
+
185
+ template<typename Shared, typename Internal>
186
+ GenericWeakRefInternal<Shared, Internal>*
187
+ GenericWeakRef<Shared, Internal>::operator->() {
188
+ return static_cast<GenericWeakRefInternal<Shared, Internal>*>(
189
+ GenericShared<Shared, Internal>::operator->());
190
+ }
191
+
192
+ template<typename Shared, typename Internal>
193
+ GenericWeakRef<Shared, Internal>::GenericWeakRef(Shared shared) {
194
+ this->own(shared.weak()->get());
195
+ }
196
+
197
+ template<typename Shared, typename Internal>
198
+ GenericWeakRef<Shared, Internal>::GenericWeakRef(Internal* raw) {
199
+ this->own(new typename Internal::weak_ref_type(raw));
200
+ }
201
+
202
+ template<typename Shared, typename Internal>
203
+ void GenericWeakRef<Shared, Internal>::kill() {
204
+ casadi_assert_dev((*this)->raw_);
205
+ (*this)->raw_ = nullptr;
206
+ }
207
+
208
+ #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
209
+ template<typename Shared, typename Internal>
210
+ std::shared_ptr<std::mutex> GenericWeakRef<Shared, Internal>::get_mutex() const {
211
+ return (*this)->mutex_;
212
+ }
213
+ #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
214
+
215
+ } // namespace casadi
216
+
217
+
218
+ #endif // CASADI_GENERIC_SHARED_IMPL_HPP