1

I have two arrays of size n1 X n2 and I want them to be added together in parallel. I have an openMP enabled build of Python, but when I set export OMP_NUM_THREADS=4 inside of my bash shell before execution, I don't see my code being multithreaded. Is it possible to perform arr = arr + tarr in a mulithreaded way?

#!/usr/bin/env python

import numpy as np

n1 = 20000
n2 = 20000

arr = np.random.random_sample((n1,n2))

for i in range(10):
    tarr = np.random.random_sample((n1,n2))
    arr = arr+tarr
6
  • 3
    Your title is about 'vectorized', but the text is about 'threading'. Those are different issues. Commented Jan 13, 2016 at 19:53
  • 1
    Was numpy and/or its libraries (BLAS etc) built with openMP? Commented Jan 13, 2016 at 21:39
  • 1
    I don't think simple array addition is done with BLAS. Do operations like dot product or equation solving get multithreaded? Commented Jan 13, 2016 at 22:00
  • Yes, you are right about vectorization and threading. I have two versions - one with AVX specific compilation and one without. The AVX compilation does not get speed up, nor does is use multiple threads so I said both but didn't specify the difference. Apologies. Commented Jan 14, 2016 at 0:05
  • Yes, the version I am using was compiled with Intel MKL. Commented Jan 14, 2016 at 0:05

1 Answer 1

1

Your BLAS linkage would only be only relevant for linear algebra operations (matrix products, solving linear systems etc.). numpy itself does not multithread basic elementwise arithmetic operations on arrays (such as addition, non-matrix multiplication, exponentiation etc.).

One of the simplest options for multithreading that calculation would be to use numexpr:

In [1]: import numpy as np

In [2]: import numexpr as ne

In [3]: n1, n2 = 5000, 5000

In [4]: x = np.random.randn(n1, n2)

In [5]: %%timeit y = np.random.randn(n1, n2)
   ...: x + y
   ...: 
1 loops, best of 3: 245 ms per loop

In [6]: %%timeit y = np.random.randn(n1, n2)
   ...: ne.evaluate('x + y')
   ...: 
10 loops, best of 3: 83.6 ms per loop
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.