-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor how structs are passed to allow cleaner passing of OSQPInfo
- Loading branch information
Showing
7 changed files
with
317 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
#ifndef OSQP_STRUCT_H_ | ||
#define OSQP_STRUCT_H_ | ||
|
||
#include <cstring> | ||
#include <functional> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <mex.h> | ||
#include <matrix.h> | ||
|
||
#include "memory_matlab.h" | ||
#include <osqp.h> | ||
|
||
/** | ||
* Base class used to store the field types for a struct. | ||
*/ | ||
class OSQPStructFieldBase { | ||
public: | ||
OSQPStructFieldBase() {} | ||
|
||
/** | ||
* Set the field in the given Matlab struct to the value of this field | ||
*/ | ||
virtual void ToMxStruct(mxArray* aStruct) = 0; | ||
|
||
/** | ||
* Set the field in the internal struct with the data from aStruct | ||
*/ | ||
virtual void ToOSQPStruct(const mxArray* aStruct) = 0; | ||
}; | ||
|
||
/** | ||
* Class to hold a numeric struct field (e.g. float/double/int/enum, etc.). | ||
*/ | ||
template<class T> | ||
class OSQPStructField : public OSQPStructFieldBase { | ||
public: | ||
OSQPStructField(T* aStructPtr, std::string aName) : | ||
m_structPtr(aStructPtr), | ||
m_name(aName) { | ||
} | ||
|
||
void ToMxStruct(mxArray* aStruct) override { | ||
mxAddField(aStruct, m_name.data()); | ||
mxSetField(aStruct, 0, m_name.data(), mxCreateDoubleScalar(*m_structPtr)); | ||
} | ||
|
||
void ToOSQPStruct(const mxArray* aStruct) override { | ||
*(m_structPtr) = static_cast<T>(mxGetScalar(mxGetField(aStruct, 0, m_name.data()))); | ||
} | ||
|
||
private: | ||
T* m_structPtr; | ||
std::string m_name; | ||
}; | ||
|
||
/** | ||
* Class to hold a character array (actual array, not char* array) field in a struct. | ||
*/ | ||
class OSQPStructFieldCharArray : public OSQPStructFieldBase { | ||
public: | ||
OSQPStructFieldCharArray(char* aStructPtr, size_t aLength, std::string aName) : | ||
m_structPtr(aStructPtr), | ||
m_name(aName), | ||
m_length(aLength) { | ||
} | ||
|
||
void ToMxStruct(mxArray* aStruct) override { | ||
mxAddField(aStruct, m_name.data()); | ||
mxSetField(aStruct, 0, m_name.data(), mxCreateString(m_structPtr)); | ||
} | ||
|
||
void ToOSQPStruct(const mxArray* aStruct) override { | ||
mxArray* tmp = mxGetField(aStruct, 0, m_name.data()); | ||
mxGetString(tmp, m_structPtr, m_length); | ||
} | ||
|
||
private: | ||
char* m_structPtr; | ||
std::string m_name; | ||
size_t m_length; | ||
}; | ||
|
||
/** | ||
* Wrap a struct from OSQP to automatically transfer the data between OSQP and Matlab. | ||
*/ | ||
template<class T> | ||
class OSQPStructWrapper { | ||
public: | ||
/** | ||
* Initialize the wrapper using the default values. | ||
*/ | ||
OSQPStructWrapper() { | ||
// Allocate the default struct and register field handlers | ||
registerFields(); | ||
} | ||
|
||
/** | ||
* Initialize the wrapper using the values from the OSQP struct. | ||
*/ | ||
OSQPStructWrapper(const T* aStruct) { | ||
// Allocate the default struct and register field handlers | ||
registerFields(); | ||
ParseOSQPStruct(aStruct); | ||
} | ||
|
||
/** | ||
* Initialize the wrapper using the values from the Matlab struct | ||
*/ | ||
OSQPStructWrapper(const mxArray* aStruct) { | ||
// Allocate the default struct and register field handlers | ||
registerFields(); | ||
ParseMxStruct(aStruct); | ||
} | ||
|
||
~OSQPStructWrapper() { | ||
for(auto& s : m_structFields) { | ||
delete s; | ||
} | ||
|
||
c_free(m_struct); | ||
} | ||
|
||
/** | ||
* Return a Matlab struct populated with the values of the current struct | ||
* contained in this wrapper. | ||
* | ||
* @return a Matlab struct with a copy of the struct (caller owns this copy and must free it) | ||
*/ | ||
mxArray* GetMxStruct() { | ||
// No fields are added right now, they are added in the for loop when they are set | ||
mxArray* matStruct = mxCreateStructMatrix(1, 1, 0, NULL); | ||
|
||
// Copy the current struct into the struct to return | ||
for(const auto& s : m_structFields) { | ||
s->ToMxStruct(matStruct); | ||
} | ||
|
||
return matStruct; | ||
} | ||
|
||
/** | ||
* Read a Matlab struct and populate the wrapper with its values. | ||
*/ | ||
void ParseMxStruct(const mxArray* aStruct) { | ||
for(const auto& s : m_structFields) { | ||
s->ToOSQPStruct(aStruct); | ||
} | ||
} | ||
|
||
/** | ||
* Get a copy of the struct contained inside this wrapper. | ||
* | ||
* @return a copy of the struct (caller owns this copy and must free it) | ||
*/ | ||
T* GetOSQPStructCopy() { | ||
// Allocate the default struct | ||
T* ret = static_cast<T*>(c_calloc(1, sizeof(T))); | ||
|
||
// Copy the current values for their return | ||
std::memcpy(ret, m_struct, sizeof(T)); | ||
return ret; | ||
} | ||
|
||
/** | ||
* Get the pointer to the internal struct object. | ||
*/ | ||
T* GetOSQPStruct() { | ||
return m_struct; | ||
} | ||
|
||
/* | ||
* Read an existing OSQP struct object into this wrapper. | ||
* The struct elements are copied, so no ownership of the aStruct pointer is transferred. | ||
*/ | ||
void ParseOSQPStruct(const T* aStruct) { | ||
std::memcpy(m_struct, aStruct, sizeof(T)); | ||
} | ||
|
||
private: | ||
/** | ||
* Register all the fields for the wrapper. | ||
* This function should be specialized for each struct type to map the fields appropriately. | ||
*/ | ||
void registerFields(); | ||
|
||
// All struct fields | ||
std::vector<OSQPStructFieldBase*> m_structFields; | ||
|
||
// Base OSQP struct object. Owned by this wrapper. | ||
T* m_struct; | ||
}; | ||
|
||
/** | ||
* Wrapper around the OSQPSettings struct | ||
*/ | ||
typedef OSQPStructWrapper<OSQPSettings> OSQPSettingsWrapper; | ||
|
||
/** | ||
* Wrapper around the OSQPInfo struct | ||
*/ | ||
typedef OSQPStructWrapper<OSQPInfo> OSQPInfoWrapper; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <osqp.h> | ||
#include "osqp_struct.h" | ||
|
||
|
||
/* | ||
* Specialization of the struct wrapper for the OSQPInfo struct. | ||
*/ | ||
template<> | ||
void OSQPStructWrapper<OSQPInfo>::registerFields() { | ||
m_struct = static_cast<OSQPInfo*>(c_calloc(1, sizeof(OSQPInfo))); | ||
|
||
if(!m_struct) | ||
mexErrMsgTxt("Failed to allocate a OSQPInfo object."); | ||
|
||
/* | ||
* Register the mapping between struct field name and the info struct memory location | ||
*/ | ||
// Solver status | ||
m_structFields.push_back(new OSQPStructFieldCharArray(m_struct->status, 32, "status")); | ||
m_structFields.push_back(new OSQPStructField<OSQPInt>(&m_struct->status_val, "status_val")); | ||
m_structFields.push_back(new OSQPStructField<OSQPInt>(&m_struct->status_polish, "status_polish")); | ||
|
||
// Solution quality | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->obj_val, "obj_val")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->prim_res, "prim_res")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->dual_res, "dual_res")); | ||
|
||
// Algorithm information | ||
m_structFields.push_back(new OSQPStructField<OSQPInt>(&m_struct->iter, "iter")); | ||
m_structFields.push_back(new OSQPStructField<OSQPInt>(&m_struct->rho_updates, "rho_updates")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->rho_estimate, "rho_estimate")); | ||
|
||
// Timing information | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->setup_time, "setup_time")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->solve_time, "solve_time")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->update_time, "update_time")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->polish_time, "polish_time")); | ||
m_structFields.push_back(new OSQPStructField<OSQPFloat>(&m_struct->run_time, "run_time")); | ||
} | ||
|
||
|
||
// Instantiate the OSQPInfo wrapper class | ||
template class OSQPStructWrapper<OSQPInfo>; |
Oops, something went wrong.