Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing the input eigen matrix to be csc when copying it into an osqp matrix #57

Merged
merged 4 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cmake_minimum_required(VERSION 3.5)

project(OsqpEigen
VERSION 0.6.102)
VERSION 0.6.103)

# ouptut paths
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
Expand Down
20 changes: 9 additions & 11 deletions include/OsqpEigen/SparseMatrixHelper.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ bool OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(const Eigen::SparseCo
csc*& osqpSparseMatrix)

{
Eigen::SparseMatrix<typename Derived::value_type, Eigen::ColMajor> colMajorCopy; //Copying into a new sparse matrix to be sure to use a CSC matrix
colMajorCopy = eigenSparseMatrix; //This may perform merory allocation, but this is already the case for allocating the osqpSparseMatrix
// get number of row, columns and nonZeros from Eigen SparseMatrix
c_int rows = eigenSparseMatrix.rows();
c_int cols = eigenSparseMatrix.cols();
c_int numberOfNonZeroCoeff = eigenSparseMatrix.nonZeros();
c_int rows = colMajorCopy.rows();
c_int cols = colMajorCopy.cols();
c_int numberOfNonZeroCoeff = colMajorCopy.nonZeros();

// get innerr and outer index
const int* innerIndexPtr = eigenSparseMatrix.innerIndexPtr();
const int* outerIndexPtr = eigenSparseMatrix.outerIndexPtr();
const int* innerNonZerosPtr = eigenSparseMatrix.innerNonZeroPtr();

// get nonzero values
auto valuePtr = eigenSparseMatrix.valuePtr();
const int* outerIndexPtr = colMajorCopy.outerIndexPtr();
const int* innerNonZerosPtr = colMajorCopy.innerNonZeroPtr();

// instantiate csc matrix
// MEMORY ALLOCATION!!
Expand All @@ -35,7 +33,7 @@ bool OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(const Eigen::SparseCo

int innerOsqpPosition = 0;
for(int k = 0; k < cols; k++) {
if (eigenSparseMatrix.isCompressed()) {
if (colMajorCopy.isCompressed()) {
osqpSparseMatrix->p[k] = static_cast<c_int>(outerIndexPtr[k]);
} else {
if (k == 0) {
Expand All @@ -44,7 +42,7 @@ bool OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(const Eigen::SparseCo
osqpSparseMatrix->p[k] = osqpSparseMatrix->p[k-1] + innerNonZerosPtr[k-1];
}
}
for (typename Eigen::SparseCompressedBase<Derived>::InnerIterator it(eigenSparseMatrix,k); it; ++it) {
for (typename Eigen::SparseMatrix<typename Derived::value_type, Eigen::ColMajor>::InnerIterator it(colMajorCopy,k); it; ++it) {
osqpSparseMatrix->i[innerOsqpPosition] = static_cast<c_int>(it.row());
osqpSparseMatrix->x[innerOsqpPosition] = static_cast<c_float>(it.value());
innerOsqpPosition++;
Expand Down
17 changes: 16 additions & 1 deletion tests/SparseMatrixTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,29 @@
template<typename T, int n, int m>
bool computeTest(const Eigen::Matrix<T, n, m> &mEigen)
{
Eigen::SparseMatrix<T> matrix, newMatrix;
Eigen::SparseMatrix<T, Eigen::ColMajor> matrix, newMatrix, newMatrixFromCSR;
matrix = mEigen.sparseView();

csc* osqpSparseMatrix = nullptr;
//NOTE: Dynamic memory allocation
if(!OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(matrix, osqpSparseMatrix))
return false;

Eigen::SparseMatrix<T, Eigen::RowMajor> csrMatrix;
csrMatrix = matrix;
csc* otherOsqpSparseMatrix = nullptr;
if(!OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(csrMatrix, otherOsqpSparseMatrix))
return false;

if(!OsqpEigen::SparseMatrixHelper::osqpSparseMatrixToEigenSparseMatrix(osqpSparseMatrix, newMatrix))
return false;

if(!OsqpEigen::SparseMatrixHelper::osqpSparseMatrixToEigenSparseMatrix(otherOsqpSparseMatrix, newMatrixFromCSR))
return false;

if (!newMatrixFromCSR.isApprox(newMatrix))
return false;

std::vector<Eigen::Triplet<T>> tripletListCsc;
if(!OsqpEigen::SparseMatrixHelper::osqpSparseMatrixToTriplets(osqpSparseMatrix, tripletListCsc))
return false;
Expand All @@ -41,6 +54,8 @@ bool computeTest(const Eigen::Matrix<T, n, m> &mEigen)
bool outcome = matrix.isApprox(newMatrix);

csc_spfree(osqpSparseMatrix);
csc_spfree(otherOsqpSparseMatrix);


return outcome;
}
Expand Down