11"""
2- Dims_create! (nnodes::Integer, [ndims::Integer] , dims)
2+ newdims = Dims_create(nnodes::Integer, dims)
33
4- Create a division of `nnodes` processes in a Cartesian grid.
4+ A convenience function for selecting a balanced Cartesian grid of a total of `nnodes`
5+ nodes, for example to use with [`MPI.Cart_create`](@ref).
6+
7+ `dims` is an array or tuple of integers specifying the number of nodes in each dimension.
8+ The function returns an array `newdims` of the same length, such that if `newdims[i] =
9+ dims[i]` if `dims[i]` is non-zero, and `prod(newdims) == nnodes`, and values `newdims` are
10+ as close to each other as possible.
11+
12+ `nnodes` should be divisible by the product of the non-zero entries of `dims`.
513
614# External links
715$(_doc_external (" MPI_Dims_create" ))
816"""
9- function Dims_create! (nnodes:: Integer , ndims :: Integer , dims:: MPIBuffertype{T} ) where {T <: Integer }
10- # int MPI_Dims_create(int nnodes, int ndims, int dims[])
17+ function Dims_create (nnodes:: Integer , dims)
18+ dims = Cint[dim for dim in dims]
1119 @mpichk ccall ((:MPI_Dims_create , libmpi), Cint,
12- (Cint, Cint, Ptr{Cint}), nnodes, ndims, dims)
20+ (Cint, Cint, Ptr{Cint}), nnodes, length (dims), dims)
21+ return dims
1322end
1423
15- function Dims_create! (nnodes:: Integer , dims:: AbstractArray{T,N} ) where {T<: Integer , N}
16- cdims = Cint .(dims[:])
17- ndims = length (cdims)
18- Dims_create! (nnodes, ndims, cdims)
19- dims[:] .= cdims
20- end
2124
2225"""
23- comm_cart = Cart_create(comm_old::Comm, [ndims::Integer], dims, periods, reorder)
26+ comm_cart = Cart_create(comm::Comm, dims; periodic=map(_->false, dims), reorder=false)
27+
28+ Create new MPI communicator with Cartesian topology information attached.
2429
25- Create new MPI communicator with Cartesian structure from an existent communicator.
30+ `dims` is an array or tuple of integers specifying the number of MPI processes in each
31+ coordinate direction, and `periodic` is an array or tuple of `Bool`s indicating the
32+ periodicity of each coordinate. `prod(dims)` must be less than or equal to the size of
33+ `comm`; if it is smaller than some processes are returned a null communicator.
34+
35+ If `reorder == false` then the rank of each process in the new group is identical to its
36+ rank in the old group, otherwise the function may reorder the processes.
37+
38+ See also [`MPI.Dims_create`](@ref).
2639
2740# External links
2841$(_doc_external (" MPI_Cart_create" ))
2942"""
30- function Cart_create (comm_old:: Comm , ndims:: Integer , dims:: MPIBuffertype{Cint} , periods:: MPIBuffertype{Cint} , reorder)
43+ function Cart_create (comm:: Comm , dims; periodic = map (_-> false , dims), reorder= false )
44+ if ! (dims isa Array{Cint})
45+ dims = Cint[dim for dim in dims]
46+ end
47+ if ! (periodic isa Array{Cint})
48+ periodic = Cint[flag for flag in periodic]
49+ end
50+ length (dims) == length (periodic) || error (" dims and periodic arguments are not the same length" )
3151 comm_cart = Comm ()
3252 # int MPI_Cart_create(MPI_Comm comm_old, int ndims, const int dims[],
3353 # const int periods[], int reorder, MPI_Comm *comm_cart)
3454 @mpichk ccall ((:MPI_Cart_create , libmpi), Cint,
3555 (MPI_Comm, Cint, Ptr{Cint}, Ptr{Cint}, Cint, Ptr{MPI_Comm}),
36- comm_old, ndims , dims, periods , reorder, comm_cart)
56+ comm, length (dims) , dims, periodic , reorder, comm_cart)
3757 if comm_cart != COMM_NULL
3858 finalizer (free, comm_cart)
3959 end
4060 comm_cart
41- end
42-
43- function Cart_create (comm_old:: Comm , dims:: AbstractArray{T,N} , periods:: Array{T,N} , reorder) where T <: Integer where N
44- cdims = Cint .(dims[:])
45- cperiods = Cint .(periods[:])
46- ndims = length (cdims)
47- Cart_create (comm_old, ndims, cdims, cperiods, reorder)
48- end
61+ end
4962
5063"""
5164 rank = Cart_rank(comm::Comm, coords)
5265
53- Determine process rank in communicator `comm` with Cartesian structure.
54- The `coords` array specifies the Cartesian coordinates of the process.
66+ Determine process rank in communicator `comm` with Cartesian structure. The `coords`
67+ array specifies the 0-based Cartesian coordinates of the process. This is the inverse of [`MPI.Cart_coords`](@ref)
5568
5669# External links
5770$(_doc_external (" MPI_Cart_rank" ))
5871"""
59- function Cart_rank (comm:: Comm , coords:: MPIBuffertype{Cint} )
72+ function Cart_rank (comm:: Comm , coords)
73+ if ! (coords isa Vector{Cint})
74+ coords = Cint[coord for coord in coords]
75+ end
6076 rank = Ref {Cint} ()
6177 # int MPI_Cart_rank(MPI_Comm comm, const int coords[], int *rank)
6278 @mpichk ccall ((:MPI_Cart_rank , libmpi), Cint,
@@ -65,10 +81,6 @@ function Cart_rank(comm::Comm, coords::MPIBuffertype{Cint})
6581 Int (rank[])
6682end
6783
68- function Cart_rank (comm:: Comm , coords:: AbstractArray{T} ) where {T <: Integer }
69- ccoords = Cint .(coords[:])
70- Cart_rank (comm, ccoords)
71- end
7284
7385"""
7486 dims, periods, coords = Cart_get(comm::Comm)
@@ -112,34 +124,24 @@ function Cartdim_get(comm::Comm)
112124end
113125
114126"""
115- Cart_coords! (comm::Comm, rank::Integer, coords )
127+ coords = Cart_coords(comm::Comm, rank::Integer=Comm_rank(comm) )
116128
117- Determine coordinates of a given process in Cartesian communicator.
129+ Determine coordinates of a process with rank `rank` in the Cartesian communicator
130+ `comm`. If no `rank` is provided, it returns the coordinates of the current process.
131+
132+ Returns an integer array of the 0-based coordinates. The inverse of [`Cart_rank`](@ref).
118133
119134# External links
120135$(_doc_external (" MPI_Cart_coords" ))
121136"""
122- function Cart_coords! (comm:: Comm , rank:: Integer , coords :: MPIBuffertype{Cint} )
137+ function Cart_coords (comm:: Comm , rank:: Integer = Comm_rank (comm) )
123138 maxdims = Cartdim_get (comm)
139+ coords = Vector {Cint} (undef, maxdims)
124140 # int MPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int coords[])
125141 @mpichk ccall ((:MPI_Cart_coords , libmpi), Cint,
126142 (MPI_Comm, Cint, Cint, Ptr{Cint}),
127143 comm, rank, maxdims, coords)
128- end
129-
130- """
131- Cart_coords(comm::Comm)
132-
133- Determine coordinates of local process in Cartesian communicator.
134-
135- See also [`Cart_coords!`](@ref).
136- """
137- function Cart_coords (comm:: Comm )
138- maxdims = Cartdim_get (comm)
139- ccoords = Vector {Cint} (undef, maxdims)
140- rank = Comm_rank (comm)
141- Cart_coords! (comm, rank, ccoords)
142- Int .(ccoords)
144+ return Int .(coords)
143145end
144146
145147"""
@@ -174,7 +176,10 @@ be kept in the generated subgrid.
174176# External links
175177$(_doc_external (" MPI_Cart_sub" ))
176178"""
177- function Cart_sub (comm:: Comm , remain_dims:: MPIBuffertype{Cint} )
179+ function Cart_sub (comm:: Comm , remain_dims)
180+ if ! (remain_dims isa Array{Cint})
181+ remain_dims = Cint[dim for dim in remain_dims]
182+ end
178183 comm_sub = Comm ()
179184 # int MPI_Cart_sub(MPI_Comm comm, const int remain_dims[], MPI_Comm *comm_new)
180185 @mpichk ccall ((:MPI_Cart_sub , libmpi), Cint,
@@ -185,8 +190,3 @@ function Cart_sub(comm::Comm, remain_dims::MPIBuffertype{Cint})
185190 end
186191 comm_sub
187192end
188-
189- function Cart_sub (comm:: Comm , remain_dims)
190- cremain_dims = [Cint (dim) for dim in remain_dims]
191- Cart_sub (comm, cremain_dims)
192- end
0 commit comments