How to use cached_function and parallel together

asked 2021-01-29 16:49:37 +0200

Bakerbakura gravatar image

updated 2021-02-01 10:00:40 +0200

I have a function f(a,b,c) which I'd like to save the output values of, so I'm currently using the @cached_function decorator to do so:

@cached_function
def f(a,b,c):
    m = g(a+b)
    n = h(c)
    out = (expensive calculation involving m and m)
    return m+n

@cached_function
def g(d):
    out = (expensive calculation involving d)
    return out

@cached_function
def h(c):
    out = (expensive calculation involving c)
    return out

I would now like to calculate the values of this function for a number of triples (a,b,c) = (a1,b1,c1), (a2,b2,c2), ..., (aN,bN,cN), and since there are many such triples I'd like to calculate them in parallel. From some Googling it seems that I should use the @parallel decorator, but

  1. How do I use the parallel decorator to calculate values of a function which has more than one input parameter?
  2. Is it possible for the calculations of these individual values of f to share memory somehow? The calculation of my function f depends on other expensive functions g and h and thus I have also used the @cached_function decorator on them; it would be preferable for g and h to not be run on the same input if multiple processes are used to perform the calculation of f.
edit retag flag offensive close merge delete