# Implement time out when creating the model

**URL:** <https://discourse.julialang.org/t/implement-time-out-when-creating-the-model/89451>\
**Category:** Optimization (Mathematical)\
**Created:** [October 28, 2022, 12:58pm UTC](https://discourse.julialang.org/t/implement-time-out-when-creating-the-model/89451 "2022-10-28T12:58:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dourouc05](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dourouc05/32/2646_2.png) [@dourouc05](https://discourse.julialang.org/u/dourouc05)\
**Post date:** [October 28, 2022, 12:58pm UTC](https://discourse.julialang.org/t/implement-time-out-when-creating-the-model/89451/1 "2022-10-28T12:58:00Z")

</div>

Hi,

I’m comparing several ways of solving the same problem. One of these approaches create extremely large models, so large that `@variable(model, variable[e in edges(graph), d in edges(graph2), v in vertices(graph)])` takes roughly five minutes (yes, just creating the variables). If you want to reproduce (maybe this is a performance bug in JuMP?), here are the sizes:

```julia
length(edges(graph)) = 386
length(demands(graph2)) = 596
length(vertices(graph)) = 149

```

I don’t believe there is a way to limit the time this operation takes (by cancelling it if it takes too long): I’d like to have something like `@variable(model, variable[e in edges(graph), d in edges(graph2), v in vertices(graph)], time_limit=Minute(5))`, [with `Minute` coming from `Dates`](https://github.com/JuliaLang/julia/blob/master/stdlib/Dates/src/periods.jl). I wouldn’t care if that threw an exception, for instance, to simplify error management.

By the way, I’m using Julia 1.8.2 on Windows 11 x64 and JuMP 1.3.1.

```julia
julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf26 (2022-09-29 15:21 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 12 × Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 1 on 12 virtual cores

(@v1.8) pkg> status JuMP
Status `C:\Users\Thibaut\.julia\environments\v1.8\Project.toml`
  [4076af6c] JuMP v1.3.1

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 28, 2022, 1:28pm UTC](https://discourse.julialang.org/t/implement-time-out-when-creating-the-model/89451/2 "2022-10-28T13:28:03Z")

</div>

It seems you want to change the API to (optionally) terminate. That’s always possible for some code. A lot of code has timeouts, and you could make a PR to JuMP, though I’m not sure if it’s wanted, too much overhead? [The user couldn’t add timeouts from the calling code (unless your calling code that korks a new Julia process).]

If it’s just a hack, to not take up resources (e.g. on a cluster), you can stop any process after some time, with the `timeout` command (I guess e.g. Windows might have similar, or could do in WSL2):

> <https://serverfault.com/questions/166034/kill-all-processes-that-are-running-for-more-than-5-minutes-by-a-given-user-in-l/776531#776531>

but you got me interested in knowing if you can stop some code from within the program, and my first thought was start it as a thread, and if there’s a way to time-limit it. That doesn’t seem to be the case in most languages, at least not Julia (safely, is it still possible, if you basically want to terminate after?) or C (likely possible in Elixir/Erlang, and Java?):

> [@How to kill thread?](https://discourse.julialang.org/t/how-to-kill-thread/34236):
>
> Is there a way to kill a thread spawned by Threads.@spawn macro?

> <https://stackoverflow.com/questions/70234333/c-kill-long-running-thread-by-sigint>

> **[How to stop/kill long running Java Thread at runtime? timed-out -\>...](https://crunchify.com/java-thread-futures-timedout-cancelled-interrupted/)**
>
> Have you ever wondered how to kill long running Java thread? Do you have any of below questions? Kill/Stop a thread after certain period of time Killing

---

<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:** [October 28, 2022, 8:12pm UTC](https://discourse.julialang.org/t/implement-time-out-when-creating-the-model/89451/3 "2022-10-28T20:12:40Z")

</div>

This is likely the same problem as [Document cost of named variables and constraints in Performance Tips · Issue #2973 · jump-dev/JuMP.jl · GitHub](https://github.com/jump-dev/JuMP.jl/issues/2973), which led to [https://github.com/jump-dev/JuMP.jl/pull/2978](https://github.com/jump-dev/JuMP.jl/pull/2978).

```julia
julia> using JuMP

julia> function main(flag)
           model = Model()
           set_string_names_on_creation(model, flag)
           @variable(model, x[1:386, 1:596, 1:149])
           return model
       end
main (generic function with 1 method)

julia> @time main(false)
  6.122188 seconds (68.56 M allocations: 4.652 GiB, 22.75% gc time)
A JuMP Model
Feasibility problem with:
Variables: 34278344
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x

julia> @time main(true)
ERROR: InterruptException:
# [... I didn't want to wait ...] 

```

---

<div class="post-metadata">

**Author:** ![dourouc05](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dourouc05/32/2646_2.png) [@dourouc05](https://discourse.julialang.org/u/dourouc05)\
**Post date:** [October 28, 2022, 11:36pm UTC](https://discourse.julialang.org/t/implement-time-out-when-creating-the-model/89451/4 "2022-10-28T23:36:49Z")

</div>

Thanks, I was not aware of that parameter!
