在 BLAS 中将三个矩阵相乘,中间一个是对角矩阵

发布于 2024-09-15 18:35:30 字数 433 浏览 3 评论 0原文

A 是一个 MxK 矩阵,B 是大小为 K 的向量,C > 是一个 KxN 矩阵。我应该使用哪组 BLAS 运算符来计算下面的矩阵?

M = A*diag(B)*C

实现这一点的一种方法是使用三个 for 循环,如下所示

for (int i=0; i<M; ++i)
    for (int j=0; j<N; ++j)
        for (int k=0; k<K; ++k)
            M(i,j) = A(i,k)*B(k)*C(k,j);

为了获得更好的速度效率,是否真的值得在 BLAS 中实现这一点?

A is an MxK matrix, B is a vector of size K, and C is a KxN matrix. What set of BLAS operators should I use to compute the matrix below?

M = A*diag(B)*C

One way to implement this would be using three for loops like below

for (int i=0; i<M; ++i)
    for (int j=0; j<N; ++j)
        for (int k=0; k<K; ++k)
            M(i,j) = A(i,k)*B(k)*C(k,j);

Is it actually worth implementing this in BLAS in order to gain better speed efficiency?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

思慕 2024-09-22 18:35:30

首先计算 D = diag(B)*C,然后使用适当的 BLAS 矩阵乘法来计算 A*D

您可以使用对 B 元素进行循环并调用适当的 BLAS 标量乘法例程来实现 diag(B)*C

First compute D = diag(B)*C, then use the appropriate BLAS matrix-multiply to compute A*D.

You can implement diag(B)*C using a loop over elements of B and calling to the appropriate BLAS scalar-multiplication routine.

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