BLAS 向量乘法catlas_saxpby 无法正常工作

发布于 2024-11-18 08:45:44 字数 1112 浏览 1 评论 0原文

我试图有两个任意长度的向量(典型长度为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2024-11-25 08:45:44

正如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 computes alpha * 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.

不如归去 2024-11-25 08:45:44

Apple 文档有误。 saxpby 函数计算标量 alphabeta 以及向量 alpha*X + beta*Y 的表达式>X 和 Y

我认为没有一个函数可用于计算两个向量的逐元素乘积,因为这不是线性代数中的常见运算。您可以采用外积的对角线,但这会严重浪费精力,因为它会计算整个外积(N2 乘法而不是 N)。

The Apple documentation is mistaken. The saxpby function computes the expression alpha*X + beta*Y for scalars alpha and beta and vectors X and Y.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文