# JuMP is slow in generating block PSD constraints

**URL:** https://discourse.julialang.org/t/jump-is-slow-in-generating-block-psd-constraints/65186
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [July 23, 2021, 2:54pm UTC](https://discourse.julialang.org/t/jump-is-slow-in-generating-block-psd-constraints/65186 "2021-07-23T14:54:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)
#### Post date: [July 23, 2021, 2:54pm UTC](https://discourse.julialang.org/t/jump-is-slow-in-generating-block-psd-constraints/65186/1 "2021-07-23T14:54:47Z")

</div>

Could an expert help me speed up the following code? In particular,

- Have I used the performance tips ([https://jump.dev/JuMP.jl/stable/tutorials/Getting%20started/performance\_tips/](https://jump.dev/JuMP.jl/stable/tutorials/Getting%20started/performance_tips/)) correctly? That is only generate expressions inside macro.

- Does it help to use sparse matrix?

- Does multithreading the `for` loop with Threads.@threads help?

Note that `computeMatrix` is not a bottleneck in my case, however the bottleneck could be with the fact that `length(x)` is large.

Edit: I did a better benchmark this time and the slowdown is mainly due to `computeMatrix` as caused by having large `length(x)` so it seems this is not a JuMP problem.

```julia
for blk in blocks
        if size(blk, 1) > 1
            println("adding PSD constraint for block of size $(size(blk))")
            @constraint(m, sum(x[i] .* computeMatrix(i, blk) for i in 1:length(x)) in PSDCone())
        else
            println("adding inequality constraint")
            @constraint(m, sum(x[i] .* computeMatrix(i, blk) for i in 1:length(x)) .>= 0)
        end
    end

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [July 23, 2021, 6:38pm UTC](https://discourse.julialang.org/t/jump-is-slow-in-generating-block-psd-constraints/65186/2 "2021-07-23T18:38:28Z")

</div>

Please provide a reproducible example

---

<div class="post-metadata">

### Author: ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)
#### Post date: [July 24, 2021, 2:48pm UTC](https://discourse.julialang.org/t/jump-is-slow-in-generating-block-psd-constraints/65186/3 "2021-07-24T14:48:41Z")

</div>

Nevermind, I got it to work faster by improving the bottleneck in `computeMatrix`. Multithreading does not seem to help much with the speedup.
