BLAS 向量乘法catlas_saxpby 无法正常工作
我试图有两个任意长度的向量(典型长度为 2048)并逐个元素相乘。因此对于所有 n,Z[n] = X[n] * Y[n]。
我设置来测试的代码相当基本:
float inputX[4] = { 2, 4, 8, 16 };
float inputY[4] = { 2, 4, 8, 16 };
catlas_saxpby(4, 1, inputX, 1, 1, inputY, 1);
结果进入 inputY,结果是
4.000000, 8.000000, 16.000000, 32.000000
Which,如果它们相乘,它应该是 4, 16, 64, 256。但它看起来像是相加。
所以这没有达到我的预期,并且文档没有给我足够的信息来弄清楚它在做什么。
有什么想法吗?
Apple's documentation for BLAS says this:
Computes the product of two vectors, scaling each one separately (single-precision).
void catlas_saxpby (
const int N,
const float alpha,
const float *X,
const int incX,
const float beta,
float *Y,
const int incY
);
Parameters
N
Number of elements in the vector.
alpha
Scaling factor for X.
X
Input vector X.
incX
Stride within X. For example, if incX is 7, every 7th element is used.
beta
Scaling factor for Y.
Y
Input vector Y.
incY
Stride within Y. For example, if incY is 7, every 7th element is used.
Discussion
On return, the contents of vector Y are replaced with the result.
I am trying to have two arbitrary length vectors (typical length will be 2048) and multiply element by element. So Z[n] = X[n] * Y[n] for all n.
The code I have setup to test is fairly basic:
float inputX[4] = { 2, 4, 8, 16 };
float inputY[4] = { 2, 4, 8, 16 };
catlas_saxpby(4, 1, inputX, 1, 1, inputY, 1);
The result goes into inputY, and the result is
4.000000, 8.000000, 16.000000, 32.000000
Which if they were multiplying it should be 4, 16, 64, 256. But it looks like it is adding.
So this is not doing what I expect, and the documentation doesn't give me enough information to figure what it is doing.
Any ideas?
Apple's documentation for BLAS says this:
Computes the product of two vectors, scaling each one separately (single-precision).
void catlas_saxpby (
const int N,
const float alpha,
const float *X,
const int incX,
const float beta,
float *Y,
const int incY
);
Parameters
N
Number of elements in the vector.
alpha
Scaling factor for X.
X
Input vector X.
incX
Stride within X. For example, if incX is 7, every 7th element is used.
beta
Scaling factor for Y.
Y
Input vector Y.
incY
Stride within Y. For example, if incY is 7, every 7th element is used.
Discussion
On return, the contents of vector Y are replaced with the result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如Adam Rosenfield所说,文档是不正确的。请提交错误。
除此之外,他的答案的其余部分还有一些更正。首先,
saxpby
计算alpha * X + beta * Y
。其次,对您更有用:BLAS 中没有函数可以执行您想要的操作,但 vDSP 中确实有这样的函数,它也是 Accelerate.framework 的一部分: vDSP_vmul。As Adam Rosenfield said, the documentation is incorrect. Please file a bug.
That aside, there are some corrections to the rest of his answer. First,
saxpby
computesalpha * X + beta * Y
. Second, and more useful for you: there is no function in BLAS that does what you want, but there is exactly such a function in vDSP, which is also part of the Accelerate.framework: vDSP_vmul.Apple 文档有误。
saxpby
函数计算标量alpha
和beta
以及向量alpha*X + beta*Y
的表达式>X 和Y
。我认为没有一个函数可用于计算两个向量的逐元素乘积,因为这不是线性代数中的常见运算。您可以采用外积的对角线,但这会严重浪费精力,因为它会计算整个外积(N2 乘法而不是 N)。
The Apple documentation is mistaken. The
saxpby
function computes the expressionalpha*X + beta*Y
for scalarsalpha
andbeta
and vectorsX
andY
.I don't think there's a function available to compute the element-wise product of two vectors, since that's not a common operation in linear algebra. You could take the diagonal of the outer product, but that's a severe waste of effort since it computes the entire outer product (N2 multiplications instead of N).