@@ -111,21 +111,27 @@ std::vector<MathematicalProgramResult> SolveInParallel(
111
111
const SolverOptions* solver_options,
112
112
const std::optional<SolverId>& solver_id, const Parallelism parallelism,
113
113
const bool dynamic_schedule) {
114
- // Broadcast the option and id arguments into vectors (if given).
115
- std::optional<std::vector<const SolverOptions*>> broadcast_options;
116
- std::optional<std::vector<std::optional<SolverId>>> broadcast_ids;
117
- if (solver_options != nullptr ) {
118
- broadcast_options.emplace (progs.size (), solver_options);
119
- }
120
- if (solver_id.has_value ()) {
121
- broadcast_ids.emplace (progs.size (), solver_id);
122
- }
123
- // Delegate to the primary overload.
124
- return SolveInParallel (
125
- progs, initial_guesses,
126
- broadcast_options.has_value () ? &(*broadcast_options) : nullptr ,
127
- broadcast_ids.has_value () ? &(*broadcast_ids) : nullptr , // BR
128
- parallelism, dynamic_schedule);
114
+ // TODO(Alexandre.Amice) is there a way around this clone?
115
+ auto prog_generator = [&progs](const int thread_num, const int64_t i) {
116
+ unused (thread_num);
117
+ return progs[i]->Clone ();
118
+ };
119
+
120
+ auto initial_guess_generator =
121
+ [&initial_guesses](int64_t thread_num,
122
+ int64_t i) -> std::optional<Eigen::VectorXd> {
123
+ unused (thread_num);
124
+ if (initial_guesses != nullptr && initial_guesses->at (i) != nullptr ) {
125
+ return *(initial_guesses->at (i));
126
+ } else {
127
+ return std::nullopt;
128
+ }
129
+ };
130
+
131
+ return SolveInParallel (prog_generator, initial_guess_generator,
132
+ static_cast <int64_t >(0 ),
133
+ static_cast <int64_t >(progs.size ()), solver_options,
134
+ solver_id, parallelism, dynamic_schedule);
129
135
}
130
136
131
137
std::vector<MathematicalProgramResult> SolveInParallel (
@@ -257,9 +263,35 @@ std::vector<MathematicalProgramResult> SolveInParallel(
257
263
solve_ith_serial (i);
258
264
}
259
265
}
260
-
261
266
return results;
262
267
}
263
268
269
+ std::vector<MathematicalProgramResult> SolveInParallel (
270
+ const std::function<std::unique_ptr<MathematicalProgram>(int64_t , int64_t )>&
271
+ prog_generator,
272
+ const std::function<std::optional<Eigen::VectorXd>(int64_t , int64_t )>&
273
+ initial_guesses_generator,
274
+ const int64_t range_start, const int64_t range_end,
275
+ const SolverOptions* solver_options,
276
+ const std::optional<SolverId>& solver_id, Parallelism parallelism,
277
+ bool dynamic_schedule) {
278
+ auto solver_options_generator =
279
+ [&solver_options](int64_t thread_num,
280
+ int64_t i) -> std::optional<SolverOptions> {
281
+ unused (thread_num, i);
282
+ return solver_options == nullptr
283
+ ? std::nullopt
284
+ : std::optional<SolverOptions>{*solver_options};
285
+ };
286
+ auto solver_id_generator =
287
+ [&solver_id](int64_t thread_num, int64_t i) -> std::optional<SolverId> {
288
+ unused (thread_num, i);
289
+ return solver_id;
290
+ };
291
+ return SolveInParallel (prog_generator, initial_guesses_generator,
292
+ solver_options_generator, solver_id_generator,
293
+ range_start, range_end, parallelism, dynamic_schedule);
294
+ }
295
+
264
296
} // namespace solvers
265
297
} // namespace drake
0 commit comments