简体   繁体   中英

Dot product of values from different arrays using BLAS in C++

I'm looking for fast way to do dot product and BLAS can be a option. But is there fast any way to do dot product of values from different arrays?

For example, let there are 4 different 1D array, a, b, c, d

a = {1, 2, 3, 4}
b = {5, 6, 7, 8}
c = {9, 10, 11, 12}
d = {13, 14, 15, 16}

and there is another array

k = {1, 2, 3, 4}

what I want to do is calculate

ans1 = a[0]*k[0] + b[0]*k[1] + c[0]*k[2] + d[0]*k[3];
ans2 = a[1]*k[0] + b[1]*k[1] + c[1]*k[2] + d[1]*k[3];

and so on...

Doing this same calculation lots of time, but since the values in all arrays can be changed every iteration. I'm trying to use ddot in BLAS and use pointer somehow but fail to do it. Is there any way to use ddot without assign another array and copy the value to it such as,

e[0]=a[0];    e[1]=b[0];    e[2]=c[0];    e[3]=d[0];

then,

ans = ddot(4, e, 1, k, 1); 

Also it is hard to make it as a 2D matrix because each 1D vectors(arrays) are located in different classes. Please give me an advice to do this more efficiently.

Thanks in advance.

If you store the column vectors a , b , c , and d in a 2D matrix M of the form

M = (a  b  c  d)

you can cast the problem into

ans = M k

where ans = (ans1 ans2 ans3 ans4) . This is a single matrix-vector multiplication for which the dgemv routine of BLAS is suitable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM