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
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