4
votes

Im starting to use BLAS functions in c++ (specifically intel MKL) to create faster versions of some of my old Matlab code.

Its been working out well so far, but I cant figure out how to perform elementwise multiplication on 2 matrices (A .* B in Matlab).

I know gemv does something similar between a matrix and a vector, so should I just break one of my matrices into vectprs and call gemv repeatedly? I think this would work, but I feel like there should be aomething built in for this operation.


1
Not sure BLAS supports this. Maybe this question is related if you can just treat the matrix data as linear. See also this thread. - horchler
Support for this was apparently added to MKLDNN, but I don't think MKL itself supports it directly. I don't have a code-fragment showing how it's used. github.com/oneapi-src/oneDNN/issues/255 - Max Barraclough

1 Answers

2
votes

Use the Hadamard product. In MKL it's v?MUL. E.g. for doubles:

vdMul( n, a, b, y );

in Matlab notation it performs:

y[1:n] = a[1:n] .* b[1:n]

In your case you can treat matrices as vectors.