# Out of memory when constructing large sparse SDP in JuMP

**URL:** <https://discourse.julialang.org/t/out-of-memory-when-constructing-large-sparse-sdp-in-jump/98332>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, memory, sdp\
**Created:** [May 4, 2023, 4:03pm UTC](https://discourse.julialang.org/t/out-of-memory-when-constructing-large-sparse-sdp-in-jump/98332 "2023-05-04T16:03:05Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 9, 2023, 8:17am UTC](https://discourse.julialang.org/t/out-of-memory-when-constructing-large-sparse-sdp-in-jump/98332/3 "2023-05-09T08:17:29Z")

</div>

Looking at `@profview_allocs`, it seems COSMO’s `decompose` takes quite a lot of memory that you can get rid of with `"decompose" => false`.

A lot of memory could be saved at the JuMP level, I fixed it in [Speed up vectorization of symmetric matrices by blegat · Pull Request #3349 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/pull/3349).

You can gain some more by the following trick:

> [@mattgiamou](#):
>
> `@constraint(model, Symmetric(C - spdiagm(y)) >= 0, PSDCone())`

This first creates the vectorized version of a nonsparse matrix. So it creates n^2/2 affine expressions. That calls `zero(JuMP.AffExpr)` for each zero entry of the sparse matrix.  
This vector of `AffExpr` is then converted into a `MOI.VectorAffineExpression` which uses a sparse datastructure for the affine terms which is much more memory efficient. You can gain a lot by creating the `MOI.VectorAffineExpression` directly as follows

```julia
func = MOI.VectorAffineFunction(
    [MOI.VectorAffineTerm(i, MOI.ScalarAffineTerm(1.0, index(y[i]))) for i in 1:n],
    JuMP.vectorize(C, SymmetricMatrixShape(n)),
)
set = MOI.PositiveSemidefiniteConeTriangle(n)
MOI.add_constraint(backend(model), func, set)

```

---

_[View the full topic](https://discourse.julialang.org/t/out-of-memory-when-constructing-large-sparse-sdp-in-jump/98332)._
