'Mosek Vectorization, Slow Adding Constraints
I am trying to solve a semi-definite program in Mosek with C++ Fusion with constraints of the form
$$ \sum_j A_{i,j} M_j - \vec{c}_i^T \vec{y} + x \le b_i \forall i, $$ (links to picture of equation)
where each M_j is a positive semi-definite matrix. Currently I am creating the constraints using a for loop to sum over A_{i,j} M_j and another for loop to add all of the constraints. However, the number of constraints is fairly large and using M->constraint(...) that many times is by far the slowest part of my program. I was looking through the Mosek documentation, and it seems like vectorization could speed up the program. However, I'm struggling with the vectorization for summing A_{i,j} M_j. The data type for each variable are:
A: vector < vector < Matrix::t > > 
M: vector < Variable::t >
c: vector < shared_ptr < ndarray < int,1 > > >
y: Variable::t
x: Variable::t
b: vector < int >  
I've tried using new_array_ptr on both A and M, and then using Expr::add and Expr::dot, but neither of those worked. Any help either with the vectorization or speeding up M->constraint(...) would be greatly appreciated!
Solution 1:[1]
It is a bit tricky. Since each of your constraints involves a product of matrices, vectorizing it further would require the ability to multiply higher-dimensional objects (tensors?) and Fusion does not have an interface for that.
One thing you could do is to make sure your A are represented as sparse matrices, if they indeed are sparse.
Another solution is to rewrite everything using the C API. It seems your data is already in a format that would allow to do the translation relatively easily.
Last but not least, if you could package your code into a reproducible example and send to MOSEK support (email here https://www.mosek.com/support/) then we [I work there] can check more concretely if there is some inefficiency in Mosek or if anything can be improved in your code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Michal Adamaszek | 
