# Multiplication matrix matrix MPI

**URL:** <https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118>\
**Category:** General Usage\
**Created:** [June 9, 2023, 4:10pm UTC](https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118 "2023-06-09T16:10:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![k.kirshin123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k.kirshin123/32/50436_2.png) [@k.kirshin123](https://discourse.julialang.org/u/k.kirshin123)\
**Post date:** [June 9, 2023, 4:10pm UTC](https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118/1 "2023-06-09T16:10:05Z")

</div>

hello guys, maybe a bit strange request, but could you help me write a matrix multiplication algorithm.  
I’m quite new to parallel computing, but I have a task ahead of me with a very tight deadline.  
So for now, all I can do is ask for help.  
So far, all that I have been able to implement is the distribution of matrices across processes, but the multiplication logic itself cannot be done

```julia

using MPI

function split_count(N::Integer, n::Integer)
    q,r = divrem(N, n)
    return [i <= r ? q+1 : q for i = 1:n]
end

MPI.Init()
comm = MPI.COMM_WORLD
rank = MPI.Comm_rank(comm)
comm_size = MPI.Comm_size(comm)

root = 0

if rank == root
    N = 4

    A = rand(Float64, N, N)
    B = rand(Float64, N, N)
    println("series = ", A*B)
    result = similar(A)
    M_counts = [N for i = 1:comm_size]
    N_counts = split_count(N, comm_size)

    sizes = vcat(M_counts', N_counts')
    size_ubuf = UBuffer(sizes, 2)
    counts = vec(prod(sizes, dims=1))

    A_vbuf = VBuffer(A, counts)
    B_vbuf = VBuffer(B, counts)
    result_vbuf = VBuffer(result, counts)
else
    size_ubuf = UBuffer(nothing)
    result_vbuf = A_vbuf = VBuffer(nothing)
    B_vbuf = VBuffer(nothing)
end

local_size = MPI.Scatter(size_ubuf, NTuple{2,Int}, root, comm)
local_A = MPI.Scatterv!(A_vbuf, zeros(Float64, local_size), root, comm)
local_B = MPI.Scatterv!(B_vbuf, zeros(Float64, local_size), root, comm)

####
#multiplication 
####

println("parallel = ", comp)

MPI.Barrier(comm)
MPI.Gatherv!(comp, result_vbuf, root, comm)

if rank == root
    println()
    println("Final matrix")
    println("================")
    @show result
end 

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 9, 2023, 7:03pm UTC](https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118/2 "2023-06-09T19:03:54Z")

</div>

> [@k.kirshin123](#):
>
> hello guys, maybe a bit strange request, but could you help me write a matrix multiplication algorithm.

Have you considered just using a library like [GitHub - JuliaParallel/Elemental.jl: Julia interface to the Elemental linear algebra library.](https://github.com/JuliaParallel/Elemental.jl)?

---

<div class="post-metadata">

**Author:** ![k.kirshin123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k.kirshin123/32/50436_2.png) [@k.kirshin123](https://discourse.julialang.org/u/k.kirshin123)\
**Post date:** [June 9, 2023, 7:33pm UTC](https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118/3 "2023-06-09T19:33:47Z")

</div>

unfortunately I don’t consider such options, the task is to write a program specifically on MPI

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 9, 2023, 7:51pm UTC](https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118/4 "2023-06-09T19:51:04Z")

</div>

Is this some sort of homework problem (a task with a tight deadline for which you aren’t allowed to use existing tools out there)?

---

<div class="post-metadata">

**Author:** ![k.kirshin123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k.kirshin123/32/50436_2.png) [@k.kirshin123](https://discourse.julialang.org/u/k.kirshin123)\
**Post date:** [June 9, 2023, 8:30pm UTC](https://discourse.julialang.org/t/multiplication-matrix-matrix-mpi/100118/5 "2023-06-09T20:30:24Z")

</div>

you can say so
