I have a function, say A(), that calls a function B(). B() creates a JuMP model (using Gurobi) and returns a mutable struct containing the model and other things. All JuMP macro calls are in B(). A() is called from the top level. However if I print a string right before calling A() and another string as the first expression in A(), I observe 5+ seconds between the two prints. I would like to understand why. I suppose it is related to macro expansion somehow but I do not understand what is going on. Maybe there is a better way to design my program? Is this normal behaviour or is it a symptom of something wrong somewhere else? Note: after the second print, the call to B() takes 3-4 seconds.
Here is an MWE:
using Random, Dates
using JuMP, Gurobi
const solverObject = GurobiSolver(Threads=1, OutputFlag=0)
struct InputData{ MI<:Matrix{Int}, VI<:Vector{Int} }
N::VI
H::VI
c::VI
d::MI
q::VI
Q::VI
end
mutable struct ModelStruct{ Mod<:Model}
model::Mod
x::JuMP.JuMPArray{JuMP.Variable}
y::JuMP.JuMPArray{JuMP.Variable}
obj::JuMP.AffExpr
end
function B(d::InputData)
m = Model(solver=solverObject)
y = @variable( m, [ d.H ], basename="y")
x = @variable( m, [ d.N, d.H ], basename="x")
@constraint( m, coverage[i=d.N], sum( x[i,j] for j in d.H ) == 1)
@constraint( m, capacity[j=d.H],
sum( x[i,j] * d.q[i] for i in d.N ) <= d.Q[j] )
@constraint( m, xylink[i=d.N, j=d.H], x[i,j] <= y[j] )
openingCost = sum( y[j] * d.c[j] for j in d.H )
assignmentCost = sum( x[i,j] * d.d[i,j] for i in d.N, j in d.H )
@objective(m, Min, openingCost + assignmentCost)
ModelStruct(m, x, y, openingCost + assignmentCost)
end
function A(d::InputData)
println(Dates.now(), " Creating model")
B(d)
end
n = 100
h = 30
c = rand(1:100, h)
d = rand(1:30, n, h)
q = rand(1:20, n)
Q = rand(1:100, h)
id = InputData( collect(1:n), collect(1:h), c, d, q, Q )
println(Dates.now(), " About to create model")
A(id)