문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top