# User-defined functions with large vector inputs

**URL:** https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434
**Category:** Optimization (Mathematical)
**Created:** [December 18, 2019, 10:35am UTC](https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434 "2019-12-18T10:35:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![edljk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edljk/32/2429_2.png) [@edljk](https://discourse.julialang.org/u/edljk)
#### Post date: [December 18, 2019, 10:35am UTC](https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434/1 "2019-12-18T10:35:25Z")

</div>

JuMP documentation suggest to use splatting for user-defined functions with vector inputs. For medium size data this works pretty well but for large scale vectors I encounter a memory issue:

```julia
julia> f(x...) = sum(x[i] for i in 1:length(x))
julia> y = randn(1_000_000);

julia> f(y...)
ERROR: ReadOnlyMemoryError()
Stacktrace:
 [1] f(::Float64, ::Vararg{Float64,N} where N) at ./REPL[18]:1
 [2] top-level scope at REPL[20]:1

```

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [December 18, 2019, 1:29pm UTC](https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434/2 "2019-12-18T13:29:27Z")

</div>

Hi @edljk, unfortunately I don’t see an easy workaround for this. JuMP’s user-defined functions were designed for functions with a moderate number of arguments. We’d need substantial changes to JuMP’s AD code to support vector arguments that are too large for splatting.

---

<div class="post-metadata">

### Author: ![edljk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edljk/32/2429_2.png) [@edljk](https://discourse.julialang.org/u/edljk)
#### Post date: [December 18, 2019, 1:56pm UTC](https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434/3 "2019-12-18T13:56:18Z")

</div>

Thanks for your fast answer!

- no workaround even if the JuMP user-defined function does not use autodiff (gradient function provided)?

- do you know where does the splatting limitation come from? Size of available memory?

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [December 18, 2019, 2:06pm UTC](https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434/4 "2019-12-18T14:06:12Z")

</div>

> [@edljk](#):
>
> no workaround even if the JuMP user-defined function does not use autodiff (gradient function provided)?

Correct. The issue is with JuMP’s representation of the expression graph. All elements in JuMP’s expression graphs are scalars.

> [@edljk](#):
>
> do you know where does the splatting limitation come from? Size of available memory?

Not sure exactly. That’s more a question about the Julia compiler.

---

<div class="post-metadata">

### Author: ![edljk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/edljk/32/2429_2.png) [@edljk](https://discourse.julialang.org/u/edljk)
#### Post date: [December 18, 2019, 3:40pm UTC](https://discourse.julialang.org/t/user-defined-functions-with-large-vector-inputs/32434/5 "2019-12-18T15:40:39Z")

</div>

ok thanks!
