# Do JuMP Variable ensure contiguous memory to Gurobi?

**URL:** <https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, gurobi\
**Created:** [March 13, 2026, 7:58am UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177 "2026-03-13T07:58:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [March 13, 2026, 7:58am UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/1 "2026-03-13T07:58:21Z")

</div>

The motivation is that I notices Gurobi exposes the 3 APIs.

- [`GRBsetdblattrelement`](https://docs.gurobi.com/projects/optimizer/en/current/reference/c/attribute.html#c.GRBsetdblattrelement)
- [`GRBsetdblattrarray`](https://docs.gurobi.com/projects/optimizer/en/current/reference/c/attribute.html#c.GRBsetdblattrarray)
- [`GRBsetdblattrlist`](https://docs.gurobi.com/projects/optimizer/en/current/reference/c/attribute.html#c.GRBsetdblattrlist)

Allegedly, using `GRBsetdblattrarray` should be the fastest (than using the `list` counterpart, than using the `element` entrywise in a (julia) loop).  
But the `array` API only exposes a `start` and a `len` arg. So it’s probably only effective for variables with a contiguous index pattern.

So I wonder if JuMP.jl (based on Gurobi.jl) can ensure the contiguous pattern when creating JuMP variables? e.g.

```julia-auto
JuMP.@variable(m, x[2:3, [4]])
# some other code
y = JuMP.@variable(m, [i = 1:5, j = i:5, u = (0, 1)])

```

can I expect that the `x` and `y` has a contiguous index pattern in the Gurobi solver? (The order in julia is `x -> y`, and intra-`x` it’s `for i in x`, intra-`y` it’s `for i in y`).

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [March 13, 2026, 9:33am UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/2 "2026-03-13T09:33:08Z")

</div>

A related question is,  
considering this API

```julia-auto
function GRBsetdblattrarray(model, attrname, first, len, newvalues)
    ccall((:GRBsetdblattrarray, libgurobi), Cint, (Ptr{GRBmodel}, Ptr{Cchar}, Cint, Cint, Ptr{Cdouble}), model, attrname, first, len, newvalues)
end

```

in julia if I call the above API, I need to input a `len` (e.g. `3`).

Do I have to input `Cint(3)` (meaning that I cannot input `3` directly)? or will julia auto-convert that for me so I can input `3` directly?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 13, 2026, 2:40pm UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/3 "2026-03-13T14:40:31Z")

</div>

> <https://github.com/jump-dev/Gurobi.jl/blob/master/src/MOI_wrapper/MOI_wrapper.jl#L1291>

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [March 13, 2026, 3:32pm UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/4 "2026-03-13T15:32:04Z")

</div>

Yes, they used `0` instead of `Cint(0)`. I wonder if that’s valid action.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 13, 2026, 4:48pm UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/5 "2026-03-13T16:48:41Z")

</div>

The `GRBsetdblattrarray` function called in the code above is a Gurobi version-specific function that calls `ccall` with the appropriate types, e.g., [Gurobi.jl/src/gen100/libgrb\_api.jl at master · jump-dev/Gurobi.jl · GitHub](https://github.com/jump-dev/Gurobi.jl/blob/master/src/gen100/libgrb_api.jl#L116-L118).

---

<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:** [March 15, 2026, 10:17pm UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/6 "2026-03-15T22:17:37Z")

</div>

> can I expect that the `x` and `y` has a contiguous index pattern in the Gurobi solver?

Yes, although this is not true for every solver.

For example, CSDP does not support free variables, so `@variable(model, x)` will actually create two columns in the solver (with the substitution rule `x := x1 - x2`)

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [March 18, 2026, 5:47am UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/7 "2026-03-18T05:47:16Z")

</div>

I find it reasonably satisfying to use the Gurobi C-API shipped with Gurobi.jl. Does it suggest that Gurobi has no need to release a stand-alone julia API in the future?

---

<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:** [March 18, 2026, 6:17am UTC](https://discourse.julialang.org/t/do-jump-variable-ensure-contiguous-memory-to-gurobi/136177/8 "2026-03-18T06:17:38Z")

</div>

I personally don’t think they need a different API. But that’s a commercial decision for them to make.
