proxsuite 0.6.7
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
qp_solve.hpp
Go to the documentation of this file.
1//
2// Copyright (c) 2023 INRIA
3//
5#ifndef PROXSUITE_PROXQP_PARALLEL_QPSOLVE_HPP
6#define PROXSUITE_PROXQP_PARALLEL_QPSOLVE_HPP
7
8// #include "proxsuite/proxqp/dense/wrapper.hpp"
12
13namespace proxsuite {
14namespace proxqp {
15namespace dense {
16
17template<typename T>
18void
20 const optional<size_t> num_threads = nullopt)
21{
22 size_t NUM_THREADS =
23 std::max((size_t)(omp_get_max_threads() / 2),
24 (size_t)(1)); // TODO(jcarpent): find optimal dispatch
25 if (num_threads != nullopt) {
26 NUM_THREADS = num_threads.value();
27 }
28 set_default_omp_options(NUM_THREADS);
29
30 typedef proxqp::dense::QP<T> qp_dense;
31 const long batch_size = long(qps.size());
32 long i = 0;
33#pragma omp parallel for schedule(dynamic)
34 for (i = 0; i < batch_size; i++) {
35 qp_dense& qp = qps[i];
36 qp.solve();
37 }
38}
39
40template<typename T>
41void
43 const optional<size_t> num_threads = nullopt)
44{
45 size_t NUM_THREADS =
46 std::max((size_t)(omp_get_max_threads() / 2), (size_t)(1));
47 if (num_threads != nullopt) {
48 NUM_THREADS = num_threads.value();
49 }
50 set_default_omp_options(NUM_THREADS);
51
52 typedef proxqp::dense::QP<T> qp_dense;
53 const long batch_size = long(qps.size());
54 long i = 0;
55#pragma omp parallel for schedule(dynamic)
56 for (i = 0; i < batch_size; i++) {
57 qp_dense& qp = qps[i];
58 qp.solve();
59 }
60}
61
62template<typename T>
63void
66{
67 if (num_threads != nullopt) {
68 set_default_omp_options(num_threads.value());
69 } else {
70 size_t NUM_THREADS =
71 std::max((size_t)(omp_get_max_threads() / 2 - 2), (size_t)(1));
72 set_default_omp_options(NUM_THREADS);
73 }
74 typedef proxqp::dense::QP<T> qp_dense;
75 const Eigen::DenseIndex batch_size = qps.size();
76 Eigen::DenseIndex i = 0;
77#pragma omp parallel for schedule(dynamic)
78 for (i = 0; i < batch_size; i++) {
79 qp_dense& qp = qps[i];
80 qp.solve();
81 }
82}
83
84template<typename T>
85void
87 optional<const size_t> num_threads,
88 std::vector<proxqp::dense::QP<T>>& qps,
89 std::vector<proxqp::dense::Vec<T>>& loss_derivatives,
90 T eps = 1.E-4,
91 T rho_new = 1.E-6,
92 T mu_new = 1.E-6)
93{
94 if (num_threads != nullopt) {
95 set_default_omp_options(num_threads.value());
96 } else {
97 size_t NUM_THREADS =
98 std::max((size_t)(omp_get_max_threads() / 2 - 2), (size_t)(1));
99 set_default_omp_options(NUM_THREADS);
100 }
101
102 typedef proxqp::dense::QP<T> qp_dense;
103 const Eigen::DenseIndex batch_size = qps.size();
104 Eigen::DenseIndex i = 0;
105#pragma omp parallel for schedule(dynamic)
106 for (i = 0; i < batch_size; i++) {
107 qp_dense& qp = qps[i];
108 dense::compute_backward<T>(qp, loss_derivatives[i], eps, rho_new, mu_new);
109 }
110}
111
112template<typename T>
113void
115 optional<const size_t> num_threads,
117 std::vector<proxqp::dense::Vec<T>>& loss_derivatives,
118 T eps = 1.E-4,
119 T rho_new = 1.E-6,
120 T mu_new = 1.E-6)
121{
122 if (num_threads != nullopt) {
123 set_default_omp_options(num_threads.value());
124 } else {
125 size_t NUM_THREADS =
126 std::max((size_t)(omp_get_max_threads() / 2 - 2), (size_t)(1));
127 set_default_omp_options(NUM_THREADS);
128 }
129
130 typedef proxqp::dense::QP<T> qp_dense;
131 const Eigen::DenseIndex batch_size = qps.size();
132 Eigen::DenseIndex i = 0;
133#pragma omp parallel for schedule(dynamic)
134 for (i = 0; i < batch_size; i++) {
135 qp_dense& qp = qps[i];
136 dense::compute_backward<T>(qp, loss_derivatives[i], eps, rho_new, mu_new);
137 }
138}
139} // namespace dense
140
141namespace sparse {
142template<typename T, typename I>
143void
145 const optional<size_t> num_threads = nullopt)
146{
147 size_t NUM_THREADS =
148 std::max((size_t)(omp_get_max_threads() / 2), (size_t)(1));
149 if (num_threads != nullopt) {
150 NUM_THREADS = num_threads.value();
151 }
152 set_default_omp_options(NUM_THREADS);
153
154 typedef proxqp::sparse::QP<T, I> qp_sparse;
155 const Eigen::DenseIndex batch_size = qps.size();
156 Eigen::DenseIndex i = 0;
157#pragma omp parallel for schedule(dynamic)
158 for (i = 0; i < batch_size; i++) {
159 qp_sparse& qp = qps[i];
160 qp.solve();
161 }
162}
163// TODO: debug
164template<typename T, typename I>
165void
167 const optional<size_t> num_threads = nullopt)
168{
169 size_t NUM_THREADS =
170 std::max((size_t)(omp_get_max_threads() / 2), (size_t)(1));
171 if (num_threads != nullopt) {
172 NUM_THREADS = num_threads.value();
173 }
174 set_default_omp_options(NUM_THREADS);
175
176 typedef proxqp::sparse::QP<T, I> qp_sparse;
177 const Eigen::DenseIndex batch_size = qps.size();
178 Eigen::DenseIndex i = 0;
179#pragma omp parallel for schedule(dynamic)
180 for (i = 0; i < batch_size; i++) {
181 qp_sparse& qp = qps[i];
182 qp.solve();
183 }
184}
185} // namespace sparse
186
187} // namespace proxqp
188} // namespace proxsuite
189
190#endif /* end of include guard PROXSUITE_PROXQP_PARALLEL_QPSOLVE_HPP */
TL_OPTIONAL_11_CONSTEXPR T & value() &
void qp_solve_in_parallel(optional< const size_t > num_threads, proxqp::dense::BatchQP< T > &qps)
Definition qp_solve.hpp:64
void qp_solve_backward_in_parallel(optional< const size_t > num_threads, std::vector< proxqp::dense::QP< T > > &qps, std::vector< proxqp::dense::Vec< T > > &loss_derivatives, T eps=1.E-4, T rho_new=1.E-6, T mu_new=1.E-6)
Definition qp_solve.hpp:86
Eigen::Matrix< T, DYN, 1 > Vec
Definition fwd.hpp:24
void solve_in_parallel(std::vector< proxqp::dense::QP< T > > &qps, const optional< size_t > num_threads=nullopt)
Definition qp_solve.hpp:19
void solve_in_parallel(proxqp::sparse::BatchQP< T, I > &qps, const optional< size_t > num_threads=nullopt)
Definition qp_solve.hpp:144
constexpr nullopt_t nullopt
Definition optional.hpp:42
void set_default_omp_options(const size_t num_threads=(size_t) omp_get_max_threads())
Definition omp.hpp:12
This class defines the API of PROXQP solver with sparse backend.
Definition wrapper.hpp:91