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

Fix compatibility of OSQP 1 when using Windows #163

Merged
merged 1 commit into from
Jun 25, 2024
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
76 changes: 76 additions & 0 deletions include/OsqpEigen/Compat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// OSQP
#include <osqp.h>

#include <cstdlib>

#ifdef OSQP_EIGEN_OSQP_IS_V1

// Re-use the same name in the global namespace as the old versions of OSQP
Expand Down Expand Up @@ -43,7 +45,81 @@ struct OSQPData
OSQPFloat* u; ///< dense array for upper bound (size m)
};

inline OSQPCscMatrix* spalloc(OSQPInt m,
OSQPInt n,
OSQPInt nzmax)
{
OSQPCscMatrix* M = static_cast<OSQPCscMatrix*>(calloc(1, sizeof(OSQPCscMatrix))); /* allocate the OSQPCscMatrix struct */
if (!M)
{
return static_cast<OSQPCscMatrix*>(OSQP_NULL);
}

OSQPInt* M_p = static_cast<OSQPInt*>(calloc(n + 1, sizeof(OSQPInt)));
if (!M_p)
{
free(M);
return static_cast<OSQPCscMatrix*>(OSQP_NULL);
}

OSQPInt* M_i = static_cast<OSQPInt*>(calloc(nzmax, sizeof(OSQPInt)));
if (!M_i)
{
free(M);
free(M_p);
return static_cast<OSQPCscMatrix*>(OSQP_NULL);
}

OSQPFloat* M_x = static_cast<OSQPFloat*>(calloc(nzmax, sizeof(OSQPFloat)));
if (!M_x)
{
free(M);
free(M_p);
free(M_i);
return static_cast<OSQPCscMatrix*>(OSQP_NULL);
}

OSQPInt M_nnz = 0;

if (nzmax >= 0)
{
M_nnz = nzmax;
}

csc_set_data(M, m, n, M_nnz, M_x, M_i, M_p);

return M;
}

inline void spfree(OSQPCscMatrix* M)
{
if (M){
if (M->p) free(M->p);
if (M->i) free(M->i);
if (M->x) free(M->x);
free(M);
}
}

} // namespace OsqpEigen

#else

namespace OsqpEigen
{

inline csc* spalloc(c_int m, c_int n, c_int nzmax)
{
return csc_spalloc(m, n, nzmax, 1, 0);
}

inline void spfree(csc* M)
{
return csc_spfree(M);
}

} // namespace OsqpEigen


#endif // OSQP_EIGEN_OSQP_IS_V1
#endif // OSQP_EIGEN_COMPAT_HPP
2 changes: 1 addition & 1 deletion include/OsqpEigen/SparseMatrixHelper.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool OsqpEigen::SparseMatrixHelper::createOsqpSparseMatrix(
return false;
}

osqpSparseMatrix = csc_spalloc(rows, cols, numberOfNonZeroCoeff, 1, 0);
osqpSparseMatrix = OsqpEigen::spalloc(rows, cols, numberOfNonZeroCoeff);

int innerOsqpPosition = 0;
for (int k = 0; k < cols; k++)
Expand Down
4 changes: 2 additions & 2 deletions src/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void OsqpEigen::Data::clearHessianMatrix()
if (m_isHessianMatrixSet)
{
m_isHessianMatrixSet = false;
csc_spfree(m_data->P);
OsqpEigen::spfree(m_data->P);
m_data->P = nullptr;
}
}
Expand All @@ -58,7 +58,7 @@ void OsqpEigen::Data::clearLinearConstraintsMatrix()
if (m_isLinearConstraintsMatrixSet)
{
m_isLinearConstraintsMatrixSet = false;
csc_spfree(m_data->A);
OsqpEigen::spfree(m_data->A);
m_data->A = nullptr;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/SparseMatrixTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ template <typename T, int n, int m> bool computeTest(const Eigen::Matrix<T, n, m
constexpr double tolerance = 1e-4;
bool outcome = matrix.isApprox(newMatrix, tolerance);

csc_spfree(osqpSparseMatrix);
csc_spfree(otherOsqpSparseMatrix);
OsqpEigen::spfree(osqpSparseMatrix);
OsqpEigen::spfree(otherOsqpSparseMatrix);

return outcome;
}
Expand Down
Loading