0

How do I parallelize multiplication of 2 elements in a nested list. ie if my list is:

lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]

The output I want to get is:

[1, 2, 3, 2, 4, 6, 3, 6, 9]

where 1*1=1, 1*2=2, 1*3=3, 2*1=2 and so on.

I have the following code:

from itertools import product
from joblib import Parallel, delayed

lst = (list(a) for a in product([1, 2, 3], repeat=2))

results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
results

But this code is giving me an error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-60-68b5134d22bd> in <module>
----> 1 results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
      2 results

~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
    964         if hasattr(self._backend, 'start_call'):
    965             self._backend.start_call()
--> 966         iterator = iter(iterable)
    967         pre_dispatch = self.pre_dispatch
    968 

TypeError: 'function' object is not iterable

1) Where have I gone wrong?

2) How do I fix this?

Leockl
  • 1,906
  • 5
  • 18
  • 51

1 Answers1

0

This is what you need to do:

from concurrent.futures import ProcessPoolExecutor

lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]


def multiply(numbers):
    return numbers[0] * numbers[1]


with ProcessPoolExecutor() as pool:
    results = [result
               for result
               in pool.map(multiply, lst)]

print(results)
# [1, 2, 3, 2, 4, 6, 3, 6, 9]
Hrisimir Dakov
  • 557
  • 3
  • 9