-1

How to implement numpy broadcast mechanism with mkl?

  I have been confused, how to use mkl to efficiently implement the broadcast mechanism in numpy (Element wise operator "+","-","*")?

such as 2-D array sub 1-D array

[[1,2,3],              [[0,0,0],
 [4,5,6],  - [1,2,3] =  [3,3,3],
 [7,8,9]]               [6,6,6]] 

And the second operation (can be understood as a matrix multiplied by a diagonal matrix) 2-D array multiply 1-D array(Element wise multiply )

[[1,2,3],               [[1,4,9],
 [4,5,6],  *  [1,2,3] =  [4,10,18],
 [7,8,9]]                [7,16,27]] 

I tried to implement with the for loop +cblas_dscal/vdSub. But I think this is not efficient, I don't know if there is any better implementation.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • The `mkl` code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to `numpy` during setup or compilation, not something you consciously use while running interpreted code. – hpaulj Jan 04 '19 at 21:24

1 Answers1

0

You can just broadcast the second array to two dimensions with arr2[None, :]. This yields the following code:

arr1 = np.array([[1,2,3],
                 [4,5,6],
                 [7,8,9]])
arr2 = np.array([1,2,3])

print(arr1 - arr2[None, :])
# Out: [[0 0 0]
#       [3 3 3]
#       [6 6 6]]
print(arr1 * arr2[None, :])
# Out: [[ 1,  4,  9],
#       [ 4, 10, 18],
#       [ 7, 16, 27]]

If you broadcast your arrays like this, numpy will use optimizations like mkl to perform the requested operations like multiplications.
More information on broadcasting and expanding array dimensions with None or np.newaxis can be found here:

JE_Muc
  • 5,403
  • 2
  • 26
  • 41
  • You may still upvote and accept my answer, since it answered your question. :) – JE_Muc Jan 05 '19 at 17:21
  • @Scotty1- Is the element-wise multiply/add/etc in numpy automatically using MKL/BLAS/etc to parallelize these operations? – John Thompson Jan 22 '21 at 19:59
  • 1
    @JohnPeterThompsonGarcés Yes, if MKL etc. is correctly installed (which should be, if you install a scientific python package which already includes numpy and the scipy stack). – JE_Muc Jan 23 '21 at 17:46