Skip to content

Commit

Permalink
extract classes into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
RSchwan committed Oct 31, 2024
1 parent 6b512a8 commit 533190e
Show file tree
Hide file tree
Showing 7 changed files with 675 additions and 524 deletions.
28 changes: 28 additions & 0 deletions include/piqp/sparse/blocksparse/block_info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This file is part of PIQP.
//
// Copyright (c) 2024 EPFL
//
// This source code is licensed under the BSD 2-Clause License found in the
// LICENSE file in the root directory of this source tree.

#ifndef PIQP_SPARSE_BLOCKSPARSE_BLOCK_INFO_HPP
#define PIQP_SPARSE_BLOCKSPARSE_BLOCK_INFO_HPP

namespace piqp
{

namespace sparse
{

template<typename I>
struct BlockInfo
{
I start;
I width;
};

} // namespace sparse

} // namespace piqp

#endif //PIQP_SPARSE_BLOCKSPARSE_BLOCK_INFO_HPP
111 changes: 111 additions & 0 deletions include/piqp/sparse/blocksparse/block_kkt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// This file is part of PIQP.
//
// Copyright (c) 2024 EPFL
//
// This source code is licensed under the BSD 2-Clause License found in the
// LICENSE file in the root directory of this source tree.

#ifndef PIQP_SPARSE_BLOCKSPARSE_BLOCK_KKT_HPP
#define PIQP_SPARSE_BLOCKSPARSE_BLOCK_KKT_HPP

#include <memory>

#include "piqp/utils/blasfeo_mat.hpp"

namespace piqp
{

namespace sparse
{

// stores the lower triangular data of an arrow KKT structure
struct BlockKKT
{
// [D_1 ]
// [B_1 D_2 ]
// [ B_2 D_3 ]
// [ ... ]
// [ B_N-3 D_N-2 ]
// [ B_N-2 D_N-1 ]
// [E_1 E_2 E_3 ... E_N-3 E_N-2 E_N-1 D_N]

std::vector<std::unique_ptr<BlasfeoMat>> D; // lower triangular diagonal
std::vector<std::unique_ptr<BlasfeoMat>> B; // off diagonal
std::vector<std::unique_ptr<BlasfeoMat>> E; // arrow block

BlockKKT() = default;

BlockKKT(BlockKKT&&) = default;

BlockKKT(const BlockKKT& other)
{
D.resize(other.D.size());
B.resize(other.B.size());
E.resize(other.E.size());

for (std::size_t i = 0; i < other.D.size(); i++) {
if (other.D[i]) {
D[i] = std::make_unique<BlasfeoMat>(*other.D[i]);
}
}

for (std::size_t i = 0; i < other.B.size(); i++) {
if (other.B[i]) {
B[i] = std::make_unique<BlasfeoMat>(*other.B[i]);
}
}

for (std::size_t i = 0; i < other.E.size(); i++) {
if (other.E[i]) {
E[i] = std::make_unique<BlasfeoMat>(*other.E[i]);
}
}
}

BlockKKT& operator=(BlockKKT&&) = default;

BlockKKT& operator=(const BlockKKT& other)
{
D.resize(other.D.size());
B.resize(other.B.size());
E.resize(other.E.size());

for (std::size_t i = 0; i < other.D.size(); i++) {
if (other.D[i] && !D[i]) {
D[i] = std::make_unique<BlasfeoMat>(*other.D[i]);
} else if (other.D[i] && D[i]) {
*D[i] = *other.D[i];
} else {
D[i] = nullptr;
}
}

for (std::size_t i = 0; i < other.B.size(); i++) {
if (other.B[i] && !B[i]) {
B[i] = std::make_unique<BlasfeoMat>(*other.B[i]);
} else if (other.B[i] && B[i]) {
*B[i] = *other.B[i];
} else {
B[i] = nullptr;
}
}

for (std::size_t i = 0; i < other.E.size(); i++) {
if (other.E[i] && !E[i]) {
E[i] = std::make_unique<BlasfeoMat>(*other.E[i]);
} else if (other.E[i] && E[i]) {
*E[i] = *other.E[i];
} else {
E[i] = nullptr;
}
}

return *this;
}
};

} // namespace sparse

} // namespace piqp

#endif //PIQP_SPARSE_BLOCKSPARSE_BLOCK_KKT_HPP
127 changes: 127 additions & 0 deletions include/piqp/sparse/blocksparse/block_mat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// This file is part of PIQP.
//
// Copyright (c) 2024 EPFL
//
// This source code is licensed under the BSD 2-Clause License found in the
// LICENSE file in the root directory of this source tree.

#ifndef PIQP_SPARSE_BLOCKSPARSE_BLOCK_MAT_HPP
#define PIQP_SPARSE_BLOCKSPARSE_BLOCK_MAT_HPP

#include <memory>

#include "piqp/fwd.hpp"
#include "piqp/typedefs.hpp"
#include "piqp/utils/blasfeo_mat.hpp"

namespace piqp
{

namespace sparse
{

template<typename I>
struct BlockMat
{
// [A_{1,1} A_{1,2} A_{1,N} ]
// [ A_{2,2} A_{2,3} A_{2,N} ]
// [ A_{3,3} A_{3,4} A_{3,N} ]
// [ ... ... ]
// [ A_{N-2,N-2} A_{N-2,N-1} A_{N-2,N}]

// Row permutation from original matrix to block matrix.
// For example, if the original matrix b = A * x, then
// b_perm = A_block * x, where b_perm[i] = b[perm[i]] and
// b = A_block^T * x_perm, where x_perm[i] = x[perm_inv[i]].
Vec<I> perm;
Vec<I> perm_inv;
Vec<I> block_row_sizes; // the row size of each block
Vec<I> tmp; // temporary matrix for internal calculations
std::vector<std::unique_ptr<BlasfeoMat>> D; // A_{i,i}
std::vector<std::unique_ptr<BlasfeoMat>> B; // A_{i,i+1}
std::vector<std::unique_ptr<BlasfeoMat>> E; // A_{i,N}

BlockMat() = default;

BlockMat(BlockMat&&) = default;

BlockMat(const BlockMat& other)
{
perm = other.perm;
perm_inv = other.perm_inv;
block_row_sizes = other.block_row_sizes;

D.resize(other.D.size());
B.resize(other.B.size());
E.resize(other.E.size());

for (std::size_t i = 0; i < other.D.size(); i++) {
if (other.D[i]) {
D[i] = std::make_unique<BlasfeoMat>(*other.D[i]);
}
}

for (std::size_t i = 0; i < other.B.size(); i++) {
if (other.B[i]) {
B[i] = std::make_unique<BlasfeoMat>(*other.B[i]);
}
}

for (std::size_t i = 0; i < other.E.size(); i++) {
if (other.E[i]) {
E[i] = std::make_unique<BlasfeoMat>(*other.E[i]);
}
}
}

BlockMat& operator=(BlockMat&&) = default;

BlockMat& operator=(const BlockMat& other)
{
perm = other.perm;
perm_inv = other.perm_inv;
block_row_sizes = other.block_row_sizes;

D.resize(other.D.size());
B.resize(other.B.size());
E.resize(other.E.size());

for (std::size_t i = 0; i < other.D.size(); i++) {
if (other.D[i] && !D[i]) {
D[i] = std::make_unique<BlasfeoMat>(*other.D[i]);
} else if (other.D[i] && D[i]) {
*D[i] = *other.D[i];
} else {
D[i] = nullptr;
}
}

for (std::size_t i = 0; i < other.B.size(); i++) {
if (other.B[i] && !B[i]) {
B[i] = std::make_unique<BlasfeoMat>(*other.B[i]);
} else if (other.B[i] && B[i]) {
*B[i] = *other.B[i];
} else {
B[i] = nullptr;
}
}

for (std::size_t i = 0; i < other.E.size(); i++) {
if (other.E[i] && !E[i]) {
E[i] = std::make_unique<BlasfeoMat>(*other.E[i]);
} else if (other.E[i] && E[i]) {
*E[i] = *other.E[i];
} else {
E[i] = nullptr;
}
}

return *this;
}
};

} // namespace sparse

} // namespace piqp

#endif //PIQP_SPARSE_BLOCKSPARSE_BLOCK_MAT_HPP
Loading

0 comments on commit 533190e

Please sign in to comment.