Skip to content

Commit 7de5bf3

Browse files
Balazs Nemethsimonbyrne
authored andcommitted
Add universe_size function (#280)
Exposes `MPI_UNIVERSE_SIZE` attribute of `MPI_COMM_WORLD`
1 parent 4e6ad89 commit 7de5bf3

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

deps/consts_msmpi.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const MPI_TAG_UB = reinterpret(Cint, 0x64400001)
8282
const MPI_COMM_TYPE_SHARED = Cint(1)
8383
const MPI_ORDER_C = Cint(56)
8484
const MPI_ORDER_FORTRAN = Cint(57)
85+
const MPI_UNIVERSE_SIZE = reinterpret(Cint, 0x64400009)
8586

8687
const MPI_BOTTOM = reinterpret(SentinelPtr, 0)
8788
const MPI_IN_PLACE = reinterpret(SentinelPtr, -1)

deps/gen_consts.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ MPI_Cints = [
9595
:MPI_COMM_TYPE_SHARED,
9696
:MPI_ORDER_C,
9797
:MPI_ORDER_FORTRAN,
98+
:MPI_UNIVERSE_SIZE
9899
]
99100

100101
MPI_pointers = [

src/comm.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,21 @@ function Intercomm_merge(intercomm::Comm, flag::Bool)
9595
return newcomm
9696
end
9797

98+
"""
99+
universe_size()
100+
101+
The total number of available slots, or `nothing` if it is not defined. This is determined by the `MPI_UNIVERSE_SIZE` attribute of `COMM_WORLD`.
102+
103+
This is typically dependent on the MPI implementation: for MPICH-based implementations, this is specified by the `-usize` argument. OpenMPI defines a default value based on the number of processes available.
104+
"""
105+
function universe_size()
106+
flag = Ref{Cint}()
107+
result = Ref(Ptr{Cint}(C_NULL))
108+
# int MPI_Comm_get_attr(MPI_Comm comm, int comm_keyval, void *attribute_val, int *flag)
109+
@mpichk ccall((:MPI_Comm_get_attr, libmpi), Cint,
110+
(MPI_Comm, Cint, Ptr{Cvoid}, Ptr{Cint}), MPI.COMM_WORLD, MPI_UNIVERSE_SIZE, result, flag)
111+
if flag[] == 0
112+
return nothing
113+
end
114+
Int(unsafe_load(result[]))
115+
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ function runtests()
2424
testdir = dirname(@__FILE__)
2525
istest(f) = endswith(f, ".jl") && startswith(f, "test_")
2626
testfiles = sort(filter(istest, readdir(testdir)))
27-
2827
extra_args = []
2928
@static if !Sys.iswindows()
3029
if occursin( "OpenRTE", read(`$mpiexec --version`, String))
@@ -34,6 +33,7 @@ function runtests()
3433

3534
nfail = 0
3635
printstyled("Running MPI.jl tests\n"; color=:white)
36+
3737
for f in testfiles
3838
coverage_opt = coverage_opts[Base.JLOptions().code_coverage]
3939
if f singlefiles

test/test_universe_size.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Test
2+
using MPI
3+
4+
MPI.Init()
5+
6+
usize = MPI.universe_size()
7+
@test usize === nothing || usize >= 1
8+
9+
MPI.Finalize()

0 commit comments

Comments
 (0)