我正在做一些性能分析,我想知道,当数据类型已知(double)时,numpy
是否将其标准数组操作矢量化。
a, b = (some numpy arrays)
c = a + b #Is this vectorized?
编辑:此操作是矢量化的吗,即计算是否由SIMD操作组成?
发布于 2017-08-21 21:10:23
是的,他们是。
/*
* 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.
*/
发布于 2021-02-18 17:52:39
我注意到Quazi Irfan对henrikstroem的回答有一条评论,说numpy没有利用矢量化,并引用了作者通过实验做了一个“证明”的博客。
所以我通过博客发现有一个差距可能会进行不同的结论:对于数值数组a和b,算法a*b与np.dot(a,b).the算法( a*b )不同,博客作者测试的只是标量乘法,而不是矩阵乘法( np.dot(a,b)),甚至不是向量内部的product.but作者仍然使用a*b来比较运行np.dot(a,b)的原始实验,这两种算法的.the复杂度是如此的不同!
numpy肯定利用了SIMD和BLAS的矢量化,可以在其源代码中找到官方的numpy发行版支持一组并行操作(如np.dot),但不是每个函数(如np.where,np.mean).the博客作者可能会选择一个不合适的函数(未矢量化的函数)进行比较。
我们还可以看到,对于多核CPU usage.when执行numpy.dot(),所有的核心都在执行一个很高的usage.Hence数值,必须(通过BLAS)向量化,以避免由于CPython的GIL限制而只使用单核。
发布于 2017-07-06 17:12:21
看一下基本示例
import numpy as np
x = np.array([1, 2, 3], np.int32)
print (type(x))
y = np.array([6, 7, 8], np.int32)
print (type(y))
现在我们将这两个数组相加
z=x+y
print (z)
print (type(z))
因此,我们有了
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
[ 7 9 11]
<class 'numpy.ndarray'>
向量化,是的,他们are.But术语向量在数学和物理中有不同的含义,我们使用数组作为数学抽象。
https://stackoverflow.com/questions/44944367
复制相似问题