Skip to content

Commit 57c3af3

Browse files
committed
add CComm type for interoperability with the C API (see also #59)
1 parent d91eef8 commit 57c3af3

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

deps/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ endif(MPI_Fortran_LINK_FLAGS)
135135

136136
target_link_libraries(juliampi ${MPI_Fortran_LIBRARIES})
137137

138+
include(CheckFunctionExists)
139+
set(CMAKE_REQUIRED_LIBRARIES ${MPI_C_LIBRARIES})
140+
check_function_exists(MPI_Comm_c2f HAVE_MPI_COMM_C2F)
141+
unset(CMAKE_REQUIRED_LIBRARIES)
142+
if(${HAVE_MPI_COMM_C2F})
143+
set(HAVE_MPI_COMM_C2F_BOOL "true")
144+
else()
145+
set(HAVE_MPI_COMM_C2F_BOOL "false")
146+
endif()
147+
138148
add_executable(gen_functions gen_functions.c)
139149
add_dependencies(gen_functions version)
140150

@@ -150,6 +160,7 @@ file(APPEND \${DST} \${FTWOC})
150160
file(APPEND \${DST} \"\n\")
151161
file(APPEND \${DST} \${FCONS})
152162
file(APPEND \${DST} \"\n\")
163+
file(APPEND \${DST} \"const HAVE_MPI_COMM_C2F = ${HAVE_MPI_COMM_C2F_BOOL}\n\")
153164
")
154165

155166
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/compile-time.jl

deps/gen_functions.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include "jlmpi_f2c.h"
3+
#include "mpi.h"
34
#include "version.h"
45

56
#define STRING1(s) #s
@@ -58,5 +59,6 @@ int main(int argc, char *argv[])
5859
printf("const MPI_WAITANY = Libdl.dlsym(libmpi, \"%s\")\n", STRING(MPI_WAITANY));
5960
printf("const MPI_WTIME = Libdl.dlsym(libmpi, \"%s\")\n", STRING(MPI_WTIME));
6061

62+
printf("bitstype %lu CComm\n", sizeof(MPI_Comm) * 8);
6163
return 0;
6264
}

src/mpi-base.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const datatypes = Dict{DataType, Cint}(
4444

4545
type Comm
4646
val::Cint
47+
Comm(val::Integer) = new(val)
4748
end
4849
const COMM_NULL = Comm(MPI_COMM_NULL)
4950
const COMM_SELF = Comm(MPI_COMM_SELF)
@@ -526,3 +527,20 @@ function ExScan{T<:MPIDatatype}(object::T, op::Op, comm::Comm)
526527
sendbuf = T[object]
527528
ExScan(sendbuf,1,op,comm)
528529
end
530+
531+
# Conversion between C and Fortran Comm handles:
532+
533+
if HAVE_MPI_COMM_C2F
534+
# use MPI_Comm_f2c and MPI_Comm_c2f
535+
const MPI_COMM_F2C = Libdl.dlsym(libmpi, "MPI_Comm_f2c")
536+
const MPI_COMM_C2F = Libdl.dlsym(libmpi, "MPI_Comm_c2f")
537+
Base.convert(::Type{CComm}, comm::Comm) = ccall(MPI_COMM_F2C, CComm, (Cint,), comm.val)
538+
Base.convert(::Type{Comm}, ccomm::CComm) = Comm(ccall(MPI_COMM_C2F, Cint, (CComm,), ccomm))
539+
elseif sizeof(CComm) == sizeof(Cint)
540+
# in MPICH, both C and Fortran use identical Cint comm handles
541+
# and MPI_Comm_c2f is not provided.
542+
Base.convert(::Type{CComm}, comm::Comm) = reinterpret(CComm, comm.val)
543+
Base.convert(::Type{Comm}, ccomm::CComm) = Comm(reinterpret(Cint, ccomm))
544+
else
545+
warn("No MPI_Comm_c2f found - conversion to/from MPI.CComm will not work")
546+
end

test/test_basic.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using MPI
55
MPI.Init()
66
@test MPI.Initialized()
77

8+
@test MPI.Comm(MPI.CComm(MPI.COMM_WORLD)).val == MPI.COMM_WORLD.val
9+
810
@test !MPI.Finalized()
911
MPI.Finalize()
1012
@test MPI.Finalized()

0 commit comments

Comments
 (0)