Skip to content

Commit 25b9625

Browse files
authored
Make show on Datatype functional before MPI has been initialized (#491)
Fixes #490
1 parent bd7e079 commit 25b9625

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/datatypes.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const DATATYPE_NULL = _Datatype(MPI_DATATYPE_NULL)
1919

2020
const MPI_Datatype_default = MPI_Datatype == Cint ? MPI_DATATYPE_NULL : C_NULL
2121
Datatype() = Datatype(MPI_Datatype_default)
22-
22+
2323

2424
function free(dt::Datatype)
2525
if dt.val != DATATYPE_NULL.val && !Finalized()
@@ -80,12 +80,13 @@ end
8080

8181
# names
8282
function get_name(datatype::Datatype)
83+
MPI.Initialized() || return ""
8384
buffer = Array{UInt8}(undef, MPI_MAX_OBJECT_NAME)
8485
lenref = Ref{Cint}()
8586
@mpichk ccall((:MPI_Type_get_name, libmpi), Cint,
86-
(MPI_Datatype, Ptr{UInt8}, Ptr{Cint}),
87+
(MPI_Datatype, Ptr{UInt8}, Ptr{Cint}),
8788
datatype, buffer, lenref)
88-
String(resize!(buffer, lenref[]))
89+
return String(resize!(buffer, lenref[]))
8990
end
9091

9192
# datatype attribute to store Julia type
@@ -99,11 +100,13 @@ Return the Julia type corresponding to the MPI [`Datatype`](@ref) `datatype`, or
99100
if it doesn't correspond directly.
100101
"""
101102
function to_type(datatype::Datatype)
102-
ptr = get_attr(datatype, JULIA_TYPE_PTR_ATTR[])
103-
if isnothing(ptr)
104-
return ptr
103+
if MPI.Initialized()
104+
ptr = get_attr(datatype, JULIA_TYPE_PTR_ATTR[])
105+
if !isnothing(ptr)
106+
return unsafe_pointer_to_objref(ptr)
107+
end
105108
end
106-
return unsafe_pointer_to_objref(ptr)
109+
return nothing
107110
end
108111

109112

@@ -159,9 +162,9 @@ end
159162

160163

161164
function Base.show(io::IO, datatype::Datatype)
162-
juliatype = to_type(datatype)
163165
show(io, Datatype)
164166
print(io, '(')
167+
juliatype = to_type(datatype)
165168
if isnothing(juliatype)
166169
show(io, datatype.val)
167170
else
@@ -174,7 +177,7 @@ function Base.show(io::IO, datatype::Datatype)
174177
print(io, name)
175178
end
176179
end
177-
180+
178181

179182

180183
module Types
@@ -231,7 +234,7 @@ end
231234
MPI.Types.create_vector(count::Integer, blocklength::Integer, stride::Integer, oldtype::MPI.Datatype)
232235
233236
Create a derived [`Datatype`](@ref) that replicates `oldtype` into locations that
234-
consist of equally spaced blocks.
237+
consist of equally spaced blocks.
235238
236239
Note that [`MPI.Types.commit!`](@ref) must be used before the datatype can be used for
237240
communication.
@@ -274,7 +277,7 @@ end
274277
"""
275278
MPI.Types.create_subarray(sizes, subsizes, offset, oldtype::Datatype;
276279
rowmajor=false)
277-
280+
278281
Creates a derived [`Datatype`](@ref) describing an `N`-dimensional subarray of size
279282
`subsizes` of an `N`-dimensional array of size `sizes` and element type `oldtype`, with
280283
the first element offset by `offset` (i.e. the 0-based index of the first element).
@@ -298,7 +301,7 @@ function create_subarray!(newtype::Datatype, sizes, subsizes, offset, oldtype::D
298301
sizes = sizes isa Vector{Cint} ? sizes : Cint[s for s in sizes]
299302
subsizes = subsizes isa Vector{Cint} ? subsizes : Cint[s for s in subsizes]
300303
offset = offset isa Vector{Cint} ? offset : Cint[s for s in offset]
301-
304+
302305
@mpichk ccall((:MPI_Type_create_subarray, libmpi), Cint,
303306
(Cint, Ptr{Cint}, Ptr{Cint}, Ptr{Cint}, Cint, MPI_Datatype, Ptr{MPI_Datatype}),
304307
N, sizes, subsizes, offset,

test/test_datatype.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
using Test
22
using MPI
33

4+
# issue #490
5+
@test startswith(sprint(show, MPI.Datatype(Float64)), "MPI.Datatype")
6+
47
MPI.Init()
58

9+
@test sprint(show, MPI.Datatype(Float64)) == "MPI.Datatype(Float64): MPI_DOUBLE"
10+
11+
612
comm_size = MPI.Comm_size(MPI.COMM_WORLD)
713
comm_rank = MPI.Comm_rank(MPI.COMM_WORLD)
814

0 commit comments

Comments
 (0)