0

I am trying to use multiprocessing inside a cost function called by scipy.optimize's minimize function. Inside the cost function I have a loop I want to compute in parallel.

A simplified version of the code is as follows:

import numpy as np
from scipy.optimize import minimize
import multiprocessing as mp


def cost_fun(Z, arg1, arg2):
    f = 0
    shift_args = []
    for k in range(8):
        shift_args.append((arg1[ :, k], arg2[ :, k]))
    p = mp.Pool(mp.cpu_count())
    R = p.starmap(calc_R, shift_args)
    p.close()
    f = f + sum([s**2 for s in R])*Z

    return f

def calc_R(x, y):
    print('You are here')
    return x[0] * y[3]


tmp = minimize(fun=cost_fun, x0=1, method='BFGS', options={'disp': False, 'gtol': 1e-10}, args = (np.random.rand(10,8), np.random.rand(10,8)))

The code freezes at the R = p.starmap(calc_R, shift_args) line.

How can the multiprocessing be done?

Any help is highly appreciated.

  • Does this answer your question? [Parallel optimizations in SciPy](https://stackoverflow.com/questions/12874756/parallel-optimizations-in-scipy) – quamrana Mar 15 '20 at 13:19
  • No. In that question he tries to do the optimization in parallel, and I just need the computations inside the optimization's cost function to be run in parallel. – Guest_audio Mar 15 '20 at 13:37
  • Can you provide a minimal working example? Also, your calc_R function that you want to parallelize seems so simple that spawning a separate process for each call might be more expensive than the actual computation. – Merlin1896 Mar 15 '20 at 13:49
  • What do you mean by a "working example"? And the calc_R function is just an example - the actual function is much more complex. – Guest_audio Mar 15 '20 at 15:00
  • I've understood. Editing with a simple "dumb" example. The code stucks at the `R = p.starmap...` line. – Guest_audio Mar 15 '20 at 15:34
  • Does `cost_fun(1, np.random.rand(10,8), np.random.rand(10,8))` by itself have this problem? – hpaulj Mar 15 '20 at 16:06
  • You realize, don't you, that `sum([s**2 for s in R])` evaluates the same for each `Z`? So for this toy example there's no point in calculating this repeatedly in the `cost_fun`. `Z` is the only thing that `minimize` varies. – hpaulj Mar 15 '20 at 16:10
  • I don't have any problems running your `const_fn` by itself. I haven't tried it in a `minimize` but don't see why that should be any more of a problem. – hpaulj Mar 15 '20 at 16:48
  • Thanks for the answers. This is just an example, the "real" code is much more complex and with many notations etc. so I don't want to post it here. However - `cost_fun(1, np.random.rand(10,8), np.random.rand(10,8))` indeed works (when attaching `if __name__ == '__main__':` before the line. When using minimize - it does not work. What could be the problem? – Guest_audio Mar 15 '20 at 17:10

0 Answers0