Skip to content

Commit

Permalink
fix handling of SUN_COMM_NULL in Fortran
Browse files Browse the repository at this point in the history
  • Loading branch information
balos1 committed Nov 30, 2023
1 parent 2b07808 commit 413bfb4
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 19 deletions.
8 changes: 8 additions & 0 deletions include/sundials/sundials_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,19 @@ typedef int (*SUNErrHandlerFn)(int line, const char* func, const char* file,
*------------------------------------------------------------------
*/

/* We don't define SUN_COMM_NULL when SWIG is processing the header
because we manually insert the wrapper code for SUN_COMM_NULL
(and %ignoring it in the SWIG code doesn't seem to work). */

#if SUNDIALS_MPI_ENABLED
#ifndef SWIG
#define SUN_COMM_NULL MPI_COMM_NULL
#endif
typedef MPI_Comm SUNComm;
#else
#ifndef SWIG
#define SUN_COMM_NULL 0
#endif
typedef int SUNComm;
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/nvector/manyvector/fmod/fnvector_mpimanyvector_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ SWIGEXPORT N_Vector _wrap_FN_VMake_MPIManyVector(int const *farg1, int64_t const
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down
2 changes: 1 addition & 1 deletion src/nvector/mpiplusx/fmod/fnvector_mpiplusx_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ SWIGEXPORT N_Vector _wrap_FN_VMake_MPIPlusX(int const *farg1, N_Vector farg2, vo
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down
6 changes: 3 additions & 3 deletions src/nvector/parallel/fmod/fnvector_parallel_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ SWIGEXPORT N_Vector _wrap_FN_VNew_Parallel(int const *farg1, int64_t const *farg
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down Expand Up @@ -250,7 +250,7 @@ SWIGEXPORT N_Vector _wrap_FN_VNewEmpty_Parallel(int const *farg1, int64_t const
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down Expand Up @@ -279,7 +279,7 @@ SWIGEXPORT N_Vector _wrap_FN_VMake_Parallel(int const *farg1, int64_t const *far
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down
2 changes: 1 addition & 1 deletion src/sundials/fmod/fsundials_context_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ SWIGEXPORT int _wrap_FSUNContext_Create(int const *farg1, void *farg2) {
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down
4 changes: 2 additions & 2 deletions src/sundials/fmod/fsundials_logger_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ SWIGEXPORT int _wrap_FSUNLogger_Create(int const *farg1, int const *farg2, void
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand All @@ -271,7 +271,7 @@ SWIGEXPORT int _wrap_FSUNLogger_CreateFromEnv(int const *farg1, void *farg2) {
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down
6 changes: 3 additions & 3 deletions src/sundials/fmod/fsundials_nonlinearsolver_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ SWIGEXPORT int _wrap_FSUNNonlinSolSetup(SUNNonlinearSolver farg1, N_Vector farg2
SUNNonlinearSolver arg1 = (SUNNonlinearSolver) 0 ;
N_Vector arg2 = (N_Vector) 0 ;
void *arg3 = (void *) 0 ;
SUNErrCode result;
int result;

arg1 = (SUNNonlinearSolver)(farg1);
arg2 = (N_Vector)(farg2);
arg3 = (void *)(farg3);
result = (SUNErrCode)SUNNonlinSolSetup(arg1,arg2,arg3);
fresult = (SUNErrCode)(result);
result = (int)SUNNonlinSolSetup(arg1,arg2,arg3);
fresult = (int)(result);
return fresult;
}

Expand Down
2 changes: 1 addition & 1 deletion src/sundials/fmod/fsundials_profiler_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ SWIGEXPORT int _wrap_FSUNProfiler_Create(int const *farg1, SwigArrayWrapper *far
if(flag) {
arg1 = MPI_Comm_f2c((MPI_Fint)(*farg1));
} else {
arg1 = 0;
arg1 = SUN_COMM_NULL;
}
#else
arg1 = *farg1;
Expand Down
5 changes: 3 additions & 2 deletions src/sundials/fmod/fsundials_types_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,11 @@
#error "The Fortran bindings are only targeted at 64-bit indices"
#endif


SWIGEXPORT SWIGEXTERN int const _wrap_SUN_COMM_NULL = (int)(0);

SWIGEXPORT SWIGEXTERN int const _wrap_SUNFALSE = (int)(0);

SWIGEXPORT SWIGEXTERN int const _wrap_SUNTRUE = (int)(1);

SWIGEXPORT SWIGEXTERN int const _wrap_SUN_COMM_NULL = (int)(0);


11 changes: 9 additions & 2 deletions src/sundials/fmod/fsundials_types_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ module fsundials_types_mod
private

! DECLARATION CONSTRUCTS

#if SUNDIALS_MPI_ENABLED
include "mpif.h"
integer(C_INT), protected, public :: SUN_COMM_NULL = MPI_COMM_NULL
#else
integer(C_INT), protected, public, &
bind(C, name="_wrap_SUN_COMM_NULL") :: SUN_COMM_NULL
#endif

integer(C_INT), protected, public, &
bind(C, name="_wrap_SUNFALSE") :: SUNFALSE
integer(C_INT), protected, public, &
Expand All @@ -35,7 +44,5 @@ module fsundials_types_mod
end enum
integer, parameter, public :: SUNOutputFormat = kind(SUN_OUTPUTFORMAT_TABLE)
public :: SUN_OUTPUTFORMAT_TABLE, SUN_OUTPUTFORMAT_CSV
integer(C_INT), protected, public, &
bind(C, name="_wrap_SUN_COMM_NULL") :: SUN_COMM_NULL

end module
1 change: 1 addition & 0 deletions src/sundials/sundials_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ SUNErrCode SUNContext_Create(SUNComm comm, SUNContext* sunctx_out)
do {
#if SUNDIALS_LOGGING_LEVEL > 0
#if SUNDIALS_MPI_ENABLED
printf("SUNContext_Create: comm=%p, MPI_COMM_NULL=%p\n", (void*)comm, (void*)MPI_COMM_NULL);
err = SUNLogger_CreateFromEnv(comm, &logger);
SUNCheckCallNoRet(err); if (err) { break; }
#else
Expand Down
1 change: 1 addition & 0 deletions src/sundials/sundials_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ SUNErrCode SUNLogger_Create(SUNComm comm, int output_rank, SUNLogger* logger_ptr
/* Attach the comm, duplicating it if MPI is used. */
#if SUNDIALS_MPI_ENABLED
logger->comm = SUN_COMM_NULL;
printf("SUNLogger_Create: comm=%p, MPI_COMM_NULL=%p\n", (void*)comm, (void*)MPI_COMM_NULL);
if (comm != SUN_COMM_NULL)
{
MPI_Comm_dup(comm, &logger->comm);
Expand Down
23 changes: 20 additions & 3 deletions swig/sundials/fsundials_types_mod.i
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@

%apply MPI_Comm { SUNComm };


// Insert code into the C wrapper to check that the sizes match
%{
#include "sundials/sundials_types.h"
Expand All @@ -82,8 +81,23 @@
#endif
%}

// Process and wrap functions in the following files
%include "sundials/sundials_types.h"
// We insert the binding code for SUN_COMM_NULL ourselves because
// (1) SWIG expands SUN_COMM_NULL to its value
// (2) We need it to be equivalent to MPI_COMM_NULL when MPI is enabled

%insert("wrapper") %{
SWIGEXPORT SWIGEXTERN int const _wrap_SUN_COMM_NULL = (int)(0);
%}

%insert("fdecl") %{
#if SUNDIALS_MPI_ENABLED
include "mpif.h"
integer(C_INT), protected, public :: SUN_COMM_NULL = MPI_COMM_NULL
#else
integer(C_INT), protected, public, &
bind(C, name="_wrap_SUN_COMM_NULL") :: SUN_COMM_NULL
#endif
%}

// Insert SUNDIALS copyright into generated C files.
%insert(begin)
Expand Down Expand Up @@ -119,3 +133,6 @@
! SUNDIALS Copyright End
! ---------------------------------------------------------------
%}

// Process and wrap functions in the following files
%include "sundials/sundials_types.h"

0 comments on commit 413bfb4

Please sign in to comment.