Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are numpy's basic operations vectorized, i.e. do they use SIMD operations?

I am doing some performance analysis, and i wonder, whether numpy vectorizes its standard array operations, when the datatype is known (double).

a, b = (some numpy arrays) c = a + b #Is this vectorized? 

Edit: Is this operation vectorized, i.e. will the computation consist of SIMD operations?

like image 493
hr0m Avatar asked Jul 06 '17 09:07

hr0m


People also ask

Is NumPy using SIMD?

NumPy comes with a flexible working mechanism that allows it to harness the SIMD features that CPUs own, in order to provide faster and more stable performance on all popular platforms.

Does Python use SIMD?

simd is a C extension, that is only compatible with Python 3.

What is NumPy vectorization?

The concept of vectorized operations on NumPy allows the use of more optimal and pre-compiled functions and mathematical operations on NumPy array objects and data sequences. The Output and Operations will speed up when compared to simple non-vectorized operations. Example 1: Using vectorized sum method on NumPy array.

What do you mean by vectorized operation?

Vectorization is the process of converting an algorithm from operating on a single value at a time to operating on a set of values (vector) at one time. Modern CPUs provide direct support for vector operations where a single instruction is applied to multiple data (SIMD).


Video Answer


2 Answers

Yes, they are.

/*  * This file is for the definitions of simd vectorized operations.  *  * Currently contains sse2 functions that are built on amd64, x32 or  * non-generic builds (CFLAGS=-march=...)  * In future it may contain other instruction sets like AVX or NEON     detected  * at runtime in which case it needs to be included indirectly via a file  * compiled with special options (or use gcc target attributes) so the binary  * stays portable.  */ 

Link: Numpy simd.inc.src on github.

like image 54
henrikstroem Avatar answered Oct 09 '22 04:10

henrikstroem


i notice there is a comment from Quazi Irfan on henrikstroem's answer,which says numpy doesn't exploit vectorization and cites a blog in which the author made a "proof" by experiments.

so i go through the blog and found there is a gap may conduct a different conclusion:for numpy-array a and b,the arithmetic a*b is different with np.dot(a,b).the arithmetic(a*b) which the blog author tested is just scalar multiplication,not a matrix multiplication(np.dot(a,b)),even not a vector inner product.but the author still used a*b to compare with the original experiment which runs np.dot(a,b).the complexity of these two arithmetics are so different!

numpy certainly exploits vectorized by SIMD and BLAS,which can be found in its source code.the official numpy distribution supports a set of parallel manipulation(like np.dot),but not every functions(like np.where,np.mean).the blog author may choose an inappropriate function(a unvectorized function) to compare.

we can also see that in respect of multi-cores CPU usage.when executing numpy.dot(),all the cores are performing a high usage.Hence numpy must have vectorized(by BLAS) to avoid just using a single core because of the CPython's GIL restriction.

like image 39
23spongebob Avatar answered Oct 09 '22 03:10

23spongebob