Here is a function I made with the intent to run a function func
with arguments inputs
across all processors, one by one.
This is mostly geared towards println
by order, but not limited to.
Edit: I forgot to include the next/prev processor call, so I embedded it in. The ranks (ID's) for current, next and previous processors have been thoroughly verified.
function MPI_OneByOne(::Val{my_ID},::Val{Total_Proc},func::Function,inputs...) where {my_ID,Total_Proc}
if !Base.isequal(my_ID,zero(my_ID))
# Wait for previous processor to finish
MPI.recv(comm; source=mod( MPI.Comm_rank(comm)-1, MPI.Comm_size(comm)))
end
println(my_ID," ======================================")
func(inputs...)
if !Base.isequal(my_ID,Total_Proc-one(Total_Proc))
# All but last processor need to push the next one to act
MPI.send(nothing, comm; dest= mod(MPI.Comm_rank(comm)+1, MPI.Comm_size(comm)))
end
MPI.Barrier(comm)
end
The idea was to kickstart with the first proc, while the others wait with recv. The first finishes, trigerring a send to #2. #2 does it's thing, trigerring #3 with a send, and so on.
As you can expect, this didn't work as expected and the output of the function (especially when using some longer input than just a single value) is not controlled or by order. What am I doing wrong?