Skip to content

Commit 72882a4

Browse files
authored
Merge pull request #367 from Simple-Robotics/alloc-integration
Integrate `mimalloc` memory resource as default resource for `SolverProxDDP`, move `blk-matrix.hpp` header to `aligator/core`
2 parents 59b4711 + 6147a37 commit 72882a4

File tree

20 files changed

+141
-112
lines changed

20 files changed

+141
-112
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
### Changed
2121

22+
- move header `<aligator/gar/blk-matrix.hpp>` to `<aligator/core/blk-matrix>`
23+
- solver-proxddp : make new `mimalloc` memory resource the allocator resource for ProxDDP solver (https://github.com/Simple-Robotics/aligator/pull/367)
2224
- tests/gar/block-matrix.cpp : test against BunchKaufman factorization
2325
- move headers `allocator.hpp` and `arena-matrix.hpp` to `aligator/core` dir (https://github.com/Simple-Robotics/aligator/pull/362)
2426
- python: aligator now requires eigenpy version 3.10.3 at least

bench/gar-riccati.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <benchmark/benchmark.h>
66

7+
#include "aligator/core/mimalloc-resource.hpp"
78
#include "aligator/gar/proximal-riccati.hpp"
89
#include "aligator/gar/parallel-solver.hpp"
910
#include "aligator/gar/dense-riccati.hpp"
@@ -22,11 +23,24 @@ static constexpr double mueq = 1e-11;
2223
static std::mt19937 rng;
2324
static normal_unary_op normal_op{rng};
2425

26+
static aligator::mimalloc_resource mim_resource;
27+
28+
auto get_allocator(int64_t ID) -> aligator::polymorphic_allocator {
29+
static std::pmr::memory_resource *RESOURCES[2] = {
30+
std::pmr::get_default_resource(), &mim_resource};
31+
assert(ID < 2);
32+
return aligator::polymorphic_allocator{RESOURCES[ID]};
33+
}
34+
35+
#define GET_PROBLEM(state) \
36+
auto allocator = get_allocator(state.range(1)); \
37+
uint horz = (uint)state.range(0); \
38+
VectorXs x0 = VectorXs::NullaryExpr(nx, normal_op); \
39+
LqrProblemTpl<double> problem = \
40+
generateLqProblem(rng, x0, horz, nx, nu, 0, nc, true, allocator)
41+
2542
static void BM_serial(benchmark::State &state) {
26-
uint horz = (uint)state.range(0);
27-
VectorXs x0 = VectorXs::NullaryExpr(nx, normal_op);
28-
const LqrProblemTpl<double> problem =
29-
generateLqProblem(rng, x0, horz, nx, nu, 0, nc);
43+
GET_PROBLEM(state);
3044
ProximalRiccatiSolver<double> solver(problem);
3145
auto [xs, us, vs, lbdas] = lqrInitializeSolution(problem);
3246
for (auto _ : state) {
@@ -37,10 +51,7 @@ static void BM_serial(benchmark::State &state) {
3751

3852
#ifdef ALIGATOR_MULTITHREADING
3953
template <uint NPROC> static void BM_parallel(benchmark::State &state) {
40-
uint horz = (uint)state.range(0);
41-
VectorXs x0 = VectorXs::NullaryExpr(nx, normal_op);
42-
LqrProblemTpl<double> problem =
43-
generateLqProblem(rng, x0, horz, nx, nu, 0, nc);
54+
GET_PROBLEM(state);
4455
ParallelRiccatiSolver<double> solver(problem, NPROC);
4556
auto [xs, us, vs, lbdas] = lqrInitializeSolution(problem);
4657
for (auto _ : state) {
@@ -51,10 +62,7 @@ template <uint NPROC> static void BM_parallel(benchmark::State &state) {
5162
#endif
5263

5364
static void BM_stagedense(benchmark::State &state) {
54-
uint horz = (uint)state.range(0);
55-
VectorXs x0 = VectorXs::NullaryExpr(nx, normal_op);
56-
LqrProblemTpl<double> problem =
57-
generateLqProblem(rng, x0, horz, nx, nu, 0, nc);
65+
GET_PROBLEM(state);
5866
RiccatiSolverDense<double> solver(problem);
5967
auto [xs, us, vs, lbdas] = lqrInitializeSolution(problem);
6068
for (auto _ : state) {
@@ -65,8 +73,10 @@ static void BM_stagedense(benchmark::State &state) {
6573

6674
static void customArgs(benchmark::internal::Benchmark *b) {
6775
for (uint e = 4; e <= 10; e++) {
68-
b->Arg(1 << e);
76+
b->Args({1 << e, 0});
77+
b->Args({1 << e, 1});
6978
}
79+
b->ArgNames({"N", "alloc"});
7080
b->Unit(benchmark::kMillisecond);
7181
b->UseRealTime();
7282
}

bindings/python/include/aligator/python/blk-matrix.hpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
1+
/// @copyright Copyright (C) 2023-2024 LAAS-CNRS, 2023-2025 INRIA
2+
/// @author Wilson Jallet
13
#include "fwd.hpp"
2-
#include "aligator/gar/blk-matrix.hpp"
4+
#include "aligator/core/blk-matrix.hpp"
35

46
namespace aligator {
57
namespace python {
68
namespace bp = boost::python;
79

10+
template <typename T> struct PrintableVisitor;
11+
812
template <typename BlockMatrixType> struct BlkMatrixPythonVisitor;
913

1014
template <typename MatrixType, int N, int M>
1115
struct BlkMatrixPythonVisitor<BlkMatrix<MatrixType, N, M>>
1216
: bp::def_visitor<BlkMatrixPythonVisitor<BlkMatrix<MatrixType, N, M>>> {
1317
using BlockMatrixType = BlkMatrix<MatrixType, N, M>;
1418
using RefType = Eigen::Ref<MatrixType>;
19+
static constexpr bool IsVector = BlockMatrixType::IsVectorAtCompileTime;
1520

1621
using Self = BlkMatrixPythonVisitor<BlockMatrixType>;
1722

1823
static RefType get_block(BlockMatrixType &bmt, size_t i, size_t j) {
1924
return bmt(i, j);
2025
}
2126

27+
static RefType get_block2(BlockMatrixType &bmt, size_t i) { return bmt[i]; }
28+
2229
static RefType blockRow(BlockMatrixType &mat, size_t i) {
2330
if (i >= mat.rowDims().size()) {
2431
PyErr_SetString(PyExc_IndexError, "Index out of range.");
@@ -27,19 +34,34 @@ struct BlkMatrixPythonVisitor<BlkMatrix<MatrixType, N, M>>
2734
return mat.blockRow(i);
2835
}
2936

37+
static RefType blockCol(BlockMatrixType &mat, size_t i) {
38+
if (i >= mat.rowDims().size()) {
39+
PyErr_SetString(PyExc_IndexError, "Index out of range.");
40+
bp::throw_error_already_set();
41+
}
42+
return mat.blockCol(i);
43+
}
44+
3045
template <class... Args> void visit(bp::class_<Args...> &obj) const {
3146
obj.add_property(
3247
"matrix", +[](BlockMatrixType &m) -> RefType { return m.matrix(); })
33-
.def_readonly("rows", &BlockMatrixType::rows)
34-
.def_readonly("cols", &BlockMatrixType::cols)
48+
.add_property("rows", &BlockMatrixType::rows)
49+
.add_property("cols", &BlockMatrixType::cols)
3550
.add_property("rowDims",
3651
bp::make_function(&BlockMatrixType::rowDims,
3752
bp::return_internal_reference<>()))
3853
.add_property("colDims",
3954
bp::make_function(&BlockMatrixType::colDims,
4055
bp::return_internal_reference<>()))
4156
.def("blockRow", blockRow, "Get a block row by index.")
42-
.def("__call__", get_block, ("self"_a, "i", "j"));
57+
.def("blockCol", blockCol, "Get a block row by index.")
58+
.def("setZero", &BlockMatrixType::setZero, ("self"_a),
59+
"Set all coefficients to zero.")
60+
.def("__call__", get_block, ("self"_a, "i", "j"))
61+
.def(PrintableVisitor<BlockMatrixType>{});
62+
if constexpr (BlockMatrixType::IsVectorAtCompileTime) {
63+
obj.def("__call__", get_block2, ("self"_a, "i"));
64+
}
4365
}
4466

4567
static void expose(const char *name) {

examples/clqr.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "aligator/modelling/constraints.hpp"
88
#include <aligator/fmt-eigen.hpp>
99
#include <iostream>
10-
#include <random>
10+
#include "../tests/test_util.hpp"
1111

1212
using namespace aligator;
1313

@@ -23,20 +23,15 @@ using context::StageModel;
2323
using context::TrajOptProblem;
2424
using context::VectorXs;
2525

26-
static std::mt19937_64 urng{42};
27-
struct NormalGen {
28-
double operator()() const { return norm(urng); }
29-
mutable std::normal_distribution<double> norm;
30-
};
31-
3226
int main() {
27+
std::mt19937 rng{42};
3328
std::srand(42);
3429
const size_t nsteps = 100;
3530
const auto nx = 4;
3631
const auto nu = 2;
3732
const auto space = Space(nx);
3833

39-
NormalGen norm_gen;
34+
normal_unary_op norm_gen{rng};
4035
MatrixXs A;
4136
// clang-format off
4237
A.setIdentity(nx, nx);

examples/talos_arms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Args(ArgsBase):
8383

8484

8585
TOL = 1e-5
86-
mu_init = 1e-3
86+
mu_init = 1e-2
8787
max_iters = 200
8888
verbose = aligator.VerboseLevel.VERBOSE
8989
solver = aligator.SolverProxDDP(TOL, mu_init, verbose=verbose)

include/aligator/gar/blk-matrix.hpp renamed to include/aligator/core/blk-matrix.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// @copyright Copyright (C) 2023-2024 LAAS-CNRS, INRIA
1+
/// @copyright Copyright (C) 2023-2024 LAAS-CNRS, 2023-2025 INRIA
22
/// @author Wilson Jallet
33
#pragma once
44

@@ -22,7 +22,9 @@ template <typename _MatrixType, int _N, int _M> class BlkMatrix {
2222
using PlainObject = typename MatrixType::PlainObject;
2323
using Scalar = typename MatrixType::Scalar;
2424
using Index = Eigen::Index;
25-
enum { N = _N, M = _M, Options = PlainObject::Options };
25+
static constexpr int N = _N;
26+
static constexpr int M = _M;
27+
static constexpr int Options = PlainObject::Options;
2628
static constexpr bool IsVectorAtCompileTime =
2729
MatrixType::IsVectorAtCompileTime;
2830

include/aligator/core/lagrangian.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "aligator/core/stage-data.hpp"
66
#include "aligator/core/explicit-dynamics.hpp"
77
#include "aligator/core/cost-abstract.hpp"
8-
#include "aligator/gar/blk-matrix.hpp"
8+
#include "aligator/core/blk-matrix.hpp"
99
#include "aligator/tracy.hpp"
1010

1111
namespace aligator {

include/aligator/gar/block-tridiagonal.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/// @author Wilson Jallet
33
#pragma once
44

5-
#include "aligator/gar/blk-matrix.hpp"
5+
#include "aligator/core/blk-matrix.hpp"
66
#include "aligator/utils/exceptions.hpp"
77
#include "aligator/tracy.hpp"
88

include/aligator/gar/dense-kernel.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
#pragma once
44

55
#include "aligator/core/bunchkaufman.hpp"
6-
7-
#include "blk-matrix.hpp"
6+
#include "aligator/core/blk-matrix.hpp"
87
#include "lqr-problem.hpp"
8+
99
#include <boost/core/span.hpp>
1010

1111
namespace aligator::gar {

include/aligator/gar/riccati-kernel.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
/// @author Wilson Jallet
33
#pragma once
44

5-
#include "aligator/context.hpp"
6-
#include "lqr-problem.hpp"
7-
#include "blk-matrix.hpp"
85
#include "aligator/core/bunchkaufman.hpp"
6+
#include "aligator/core/blk-matrix.hpp"
7+
#include "lqr-problem.hpp"
98

109
#include <boost/core/make_span.hpp>
11-
1210
#include <optional>
1311

1412
namespace aligator {

0 commit comments

Comments
 (0)