# Julia Cuda  Matrix multiplication

**URL:** https://discourse.julialang.org/t/julia-cuda-matrix-multiplication/21741
**Category:** General Usage
**Tags:** cudanative, cuda
**Created:** [March 11, 2019, 1:57pm UTC](https://discourse.julialang.org/t/julia-cuda-matrix-multiplication/21741 "2019-03-11T13:57:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Noobie76](https://avatars.discourse-cdn.com/v4/letter/n/ee59a6/32.png) [@Noobie76](https://discourse.julialang.org/u/Noobie76)
#### Post date: [March 11, 2019, 1:57pm UTC](https://discourse.julialang.org/t/julia-cuda-matrix-multiplication/21741/1 "2019-03-11T13:57:52Z")

</div>

Hi,

I’m relatively new to Julia and want to implement a numerical method using the CUDA libraries for Julia. I worked myself through the introduction files on GitHub and gained all the basic knowledge to write my own code so far.

The thing is that I want to create at least a not completely inefficient code. Therefore my aim is to avoid unnecessary overhead communication between the GPU and CPU, or their memories. And here is my, maybe stupid, question.

Imagine I have some data

A = rand(ComplexF64, (N,N)) ,  
B = rand(ComplexF64, (N,N)) ,

where N is some fixed integer, and upload my data to the Nvidia GPU using

A\_gpu = CuArrays.cu(A),  
B\_gpu = CuArrays.cu(B).

And the thing I asking myself is, when I’m performing a simple a simple matrix multiplication

A\_gpu\*B\_gpu

does this calculation take place at the GPU? I mean is this a standard implemented feature of Julia when one is multiplying CuArrays, or need I to write an extra kernel function for a “parallel matrix multiplication” and call it with @cuda…?

I would be great if some expert on Julia can answer this question for me.

Thanks

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 11, 2019, 2:05pm UTC](https://discourse.julialang.org/t/julia-cuda-matrix-multiplication/21741/2 "2019-03-11T14:05:57Z")

</div>

> [@Noobie76](#):
>
> I mean is this a standard implemented feature of Julia when one is multiplying CuArrays,

It will indeed take place on the GPU. This is not really a “standard implemented feature of Julia” it is just that `*` can be overloaded and the guys writing CuArrays overloaded `*` between two `CuArrays` (CuMatrices specifically) to call the CUBLAS version of matrix multiply.

---

<div class="post-metadata">

### Author: ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)
#### Post date: [March 11, 2019, 3:19pm UTC](https://discourse.julialang.org/t/julia-cuda-matrix-multiplication/21741/3 "2019-03-11T15:19:24Z")

</div>

> [@kristoffer.carlsson](#):
>
> This is not really a “standard implemented feature of Julia” it is just that `*` can be overloaded and the guys writing CuArrays overloaded `*` between two `CuArrays` (CuMatrices specifically) to call the CUBLAS version of matrix multiply.

Specifically, this is where the CUBLAS-implementations are dispatched to: [https://github.com/JuliaGPU/CuArrays.jl/blob/cee6253edeca2029d8d0522a46e2cdbb638e0a50/src/blas/highlevel.jl#L90-L145](https://github.com/JuliaGPU/CuArrays.jl/blob/cee6253edeca2029d8d0522a46e2cdbb638e0a50/src/blas/highlevel.jl#L90-L145)

And this is the fallback generically-typed implementation (e.g. for use with Dual numbers or other types that are not supported by CUBLAS): [https://github.com/JuliaGPU/CuArrays.jl/blob/cee6253edeca2029d8d0522a46e2cdbb638e0a50/src/matmul.jl#L4-L50](https://github.com/JuliaGPU/CuArrays.jl/blob/cee6253edeca2029d8d0522a46e2cdbb638e0a50/src/matmul.jl#L4-L50)

---

<div class="post-metadata">

### Author: ![JosePereiraUA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiraua/32/37640_2.png) [@JosePereiraUA](https://discourse.julialang.org/u/JosePereiraUA)
#### Post date: [February 24, 2021, 6:52pm UTC](https://discourse.julialang.org/t/julia-cuda-matrix-multiplication/21741/4 "2021-02-24T18:52:38Z")

</div>

This helps. Can I ask if this is the absolute most efficient way of multiplying two matrices, or is there any “trick” one might employ to speed up calculation even more?
