Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add active_comm field #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ext/MPIExt/MPIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct MPIParallelizer <: TaijaParallel.AbstractParallelizer
n_proc::Int
n_each::Union{Nothing,Int}
threaded::Bool
active_comm::MPI.Comm
end

"""
Expand All @@ -26,9 +27,11 @@ function TaijaParallel.MPIParallelizer(
comm::MPI.Comm;
n_each::Union{Nothing,Int} = nothing,
threaded::Bool = false,
active_comm::Union{Nothing,MPI.Comm} = comm
)
rank = MPI.Comm_rank(comm) # Rank of this process in the world 🌍
n_proc = MPI.Comm_size(comm) # Number of processes in the world 🌍
rank = MPI.Comm_rank(comm) # Rank of this process in the world 🌍
n_proc = MPI.Comm_size(comm) # Number of processes in the world 🌍
active_comm = isnothing(active_comm) ? comm : active_comm # Active communication channel (if specified)

if rank == 0
@info "Using `MPI.jl` for multi-processing."
Expand All @@ -42,7 +45,7 @@ function TaijaParallel.MPIParallelizer(
end
end

return MPIParallelizer(comm, rank, n_proc, n_each, threaded)
return MPIParallelizer(comm, rank, n_proc, n_each, threaded, active_comm)
end

include("generate_counterfactual.jl")
Expand Down
10 changes: 5 additions & 5 deletions ext/MPIExt/evaluate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ function TaijaBase.parallelize(
kwargs...,
)
end
MPI.Barrier(parallelizer.comm)
MPI.Barrier(parallelizer.active_comm)

# Collect output from all processe in rank 0:
collected_output = MPI.gather(output, parallelizer.comm)
if parallelizer.rank == 0
output = vcat(collected_output...)
Serialization.serialize(joinpath(storage_path, "output_$i.jls"), output)
end
MPI.Barrier(parallelizer.comm)
MPI.Barrier(parallelizer.active_comm)
end

# Collect all chunks in rank 0:
MPI.Barrier(parallelizer.comm)
MPI.Barrier(parallelizer.active_comm)

# Load output from rank 0:
if parallelizer.rank == 0
Expand All @@ -101,8 +101,8 @@ function TaijaBase.parallelize(
end

# Broadcast output to all processes:
final_output = MPI.bcast(output, parallelizer.comm; root = 0)
MPI.Barrier(parallelizer.comm)
final_output = MPI.bcast(output, parallelizer.active_comm; root = 0)
MPI.Barrier(parallelizer.active_comm)

return final_output
end
10 changes: 5 additions & 5 deletions ext/MPIExt/generate_counterfactual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ function TaijaBase.parallelize(
kwargs...,
)
end
MPI.Barrier(parallelizer.comm)
MPI.Barrier(parallelizer.active_comm)

# Collect output from all processe in rank 0:
collected_output = MPI.gather(output, parallelizer.comm)
if parallelizer.rank == 0
output = vcat(collected_output...)
Serialization.serialize(joinpath(storage_path, "output_$i.jls"), output)
end
MPI.Barrier(parallelizer.comm)
MPI.Barrier(parallelizer.active_comm)
end

# Collect all chunks in rank 0:
MPI.Barrier(parallelizer.comm)
MPI.Barrier(parallelizer.active_comm)

# Load output from rank 0:
if parallelizer.rank == 0
Expand All @@ -101,8 +101,8 @@ function TaijaBase.parallelize(
end

# Broadcast output to all processes:
final_output = MPI.bcast(output, parallelizer.comm; root = 0)
MPI.Barrier(parallelizer.comm)
final_output = MPI.bcast(output, parallelizer.active_comm; root = 0)
MPI.Barrier(parallelizer.active_comm)

return final_output
end
9 changes: 9 additions & 0 deletions src/extensions/MPIExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ Exposes the `MPIParallelizer` function from the `MPIExt` extension.
"""
function MPIParallelizer end
export MPIParallelizer

global _active_comm = nothing

function set_active_comm(comm)
global _active_comm = comm
return _active_comm
end

get_active_comm() = _active_comm
Loading