QuTiP's function parallel_map provides the possibility to compute the value of a given function for several values of its argument in parallel. All the examples show cases where the first positional argument is varied, like the following:
def testFunc1(a, b):
return a, b
from qutip import parallel_map
parallel_map(testFunc1, (1, 2, 3), task_args=(4,))
This returns [(1, 4), (2, 4), (3, 4)]. Now I'm wondering if it's also possible to have a fixed value for a and a tuple for b. According to the documentation task_args can also be a dictionary, so I tried
parallel_map(testFunc1, (1, 2, 3), task_args={'a': 4})
parallel_map(testFunc1, (1, 2, 3), task_args={'a': 4, 'b': (1, 2, 3)})
but this results in TypeError: can only concatenate tuple (not "dict") to tuple.
When I try
parallel_map(testFunc1, b=(1, 2, 3), task_args={'a': 4})
I get TypeError: parallel_map() missing 1 required positional argument: 'values'.
Does somebody know how to use parallel_map for the n-th positional argument (without writing a function wrapper for each n)?
Looking into the source code of
parallel_mapreveals why it only works for the first argument of a function:In this line the parallel processes are created. The function
taskgets a tuple representing all positional arguments, which is created from 1 element of thevaluesand all the othertask_args. Since the element ofvaluesis at the first position of the combined tuple ((value,) + task_args) it is always the first argument of the function which varies between its parallel instances. TheTypeError: can only concatenate tuple (not "dict") to tupleif a dictionary is used fortask_argscomes from the fact that the+-operator is only overloaded for(tuple, tuple), not for(tuple, dict).So in the end there is no way around building a wrapper. Either one for each particular n or a generic one like:
Calling this with
returns