Julia : BLAS.gemm!() parameters

走远了吗. 提交于 2019-12-11 03:12:46

问题


I want to use the BLAS package. To do so, the meaning of the two first parameters of the gemm() function is not evident for me. What do the parameters 'N' and 'T' represent?

 BLAS.gemm!('N', 'T', lr,  alpha, A, B, beta, C)

What is the difference between BLAS.gemm and BLAS.gemm! ?


回答1:


According to the documentation

gemm!(tA, tB, alpha, A, B, beta, C)

Update C as alpha * A * B + beta*C or the other three variants according to tA (transpose A) and tB. Returns the updated C.

Note: here, alpha and beta must be float type scalars. A, B and C are all matrices. It's up to you to make sure the matrix dimensions match.

Thus, the tA and tB parameters refer to whether you want to apply the transpose operation to A or to B before multiplying. Note that this will cost you some computation time and allocations - the transpose isn't free. (thus, if you were going to apply the multiplication many times, each time with the same transpose specification, you'd be better off storing your matrix as the transposed version from the beginning). Select N for no transpose, T for transpose. You must select one or the other.

The difference between gemm!() and gemv!() is that for gemm!() you already need to have allocated the matrix C. The ! is a "modify in place" signal. Consider the following illustration of their different uses:

A = rand(5,5)
B = rand(5,5)
C = Array(Float64, 5, 5)

BLAS.gemm!('N', 'T', 1.0, A, B, 0.0, C)
D = BLAS.gemm('N', 'T', 1.0, A, B)

julia> C == D
true

Each of these, in essence, perform the calculation C = A * B'. (Technically, gemm!() performs C = (0.0)*C + (1.0)*A * B'.)

Thus, the syntax for the modify in place gemm!() is a bit unusual in some respects (unless you've already worked with a language like C in which case it seems very intuitive). You don't have the explicit = sign like you frequently do when calling functions in assigning values in a high level object oriented language like Julia.

As the illustration above shows, the outcome of gemm!() and gemm() in this case is identical, even though the syntax and procedure to achieve that outcome is a bit different. Practically speaking, however, performance differences between the two can be significant, depending on your use case. In particular, if you are going to be performing that multiplication operation many times, replacing/updating the value of C each time, then gemm!() can be a decent bit quicker because you don't need to keep re-allocating new memory each time, which does have time costs, both in the initial memory allocation and then in the garbage collection later on.



来源:https://stackoverflow.com/questions/38481674/julia-blas-gemm-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!