Hi there,
In my code I am using the JLD package to serialize my results. Up to now, I worked on an ad-hoc solution where this saving/ loading is done in the functions itself. Furthermore, I use dictionaries to save all the variables of a respective function and give it to the next one, which possibly needs some of them. Have a look at the following code, which essentially mimics what my real code is doing:
using JLD
function calc_stuff(savedir, a, b)
filename = savedir*"calc_stuff_a=$(a)_b=$(b).jld"
if !isfile(filename)
println("calc_stuff is running...")
c = a + b
d = a - b
# save results
vars = dict()
vars["c"] = c
vars["d"] = d
save(filename, vars)
return vars
else
println("calc_stuff was already done, loading...")
vars = load(filename)
return vars
end
end
function calc_more_stuff(savedir, a,b,e)
filename = savedir*"calc_more_stuff_a=$(a)_b=$(b)_e=$(e).jld"
if !isfile(filename)
println("calc_more_stuff is running...")
vars = calc_stuff(savedir, a, b)
c = vars["c"]
d = vars["d"]
f = c + d + e
g = c - d - e
# save results
vars = dict()
vars["c"] = c
vars["d"] = d
vars["f"] = f
vars["g"] = g
save(filename, vars)
return vars
else
println("calc_more_stuff was already done, loading...")
vars = load(filename)
return vars
end
end
You see that there are many lines of code which will replicate over and over again, in all of the functions I have. I know that meta programming is possible in julia. However, I have the feeling that other people might have encountered this problem already, so this is why I hesitated on working it out on my own. I wanted to ask you if you know something which could help to realize something like the following:
using JLD
using MagicLibrary
MagicLibrary.savedir = "./"
function calc_stuff(a, b)
c = a + b
d = a - b
@magic return vars # dictionary all variables
end
function calc_more_stuff(a,b,e)
@find vars = calc_stuff(a,b) # if already calculated, load, otherwise, calculate
s = struct_from_dict(vars) # create a struct from this dictionary
f = s.c + s.d + e
g = s.c - s.d - e
@magic return vars # dictionary all variables
end
@find vars = calc_more_stuff(1,2,3) # if already calculated, load, otherwise, calculate