# Parallelization of Sum in JuMP

**URL:** <https://discourse.julialang.org/t/parallelization-of-sum-in-jump/82979>\
**Category:** Optimization (Mathematical)\
**Created:** [June 18, 2022, 5:40pm UTC](https://discourse.julialang.org/t/parallelization-of-sum-in-jump/82979 "2022-06-18T17:40:16Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [June 19, 2022, 3:51am UTC](https://discourse.julialang.org/t/parallelization-of-sum-in-jump/82979/2 "2022-06-19T03:51:44Z")

</div>

> Btw, this is my first post here

Welcome! It’s a little easier to provide feedback if you can produce a minimal working example: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757). Without one, we have to make some guesses. What are `a` and `b`? What is `term_idxs`? Are there lots of `(i, j)` with the same `i`? How many terms are we talking about?

> I did not really find any approaches to parallelizing a sum in JuMP models.

There is no way to use parallelism to make problem construction faster.

One suggestion I would try is to factor out the terms that do not depend on `j`. Assuming I didn’t make a mistake (I didn’t run the code because I don’t have the data):

```Julia
I = unique(first.(term_idxs))
@expression(model, term_i[i=I], sum(x[(i, j)] for j in dict_j[i]))
@expression(model, sum((a[i] - b[i] * term_i[i]) * term_i[i] for i in I))

```

This should greatly reduce the number of loop iterations. Otherwise you’re looping over all `term_idxs` and for each term you’re looping over `dict_j[I]`.

You should also read the Julia performance tips if you haven’t already: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html). I assume you aren’t using global variables, etc?

---

_[View the full topic](https://discourse.julialang.org/t/parallelization-of-sum-in-jump/82979)._
