From 9a2f6752a54dd3f091eb09f437117b6b655a6e85 Mon Sep 17 00:00:00 2001 From: "Balos, Cody, J" Date: Mon, 11 Dec 2023 11:49:17 -0800 Subject: [PATCH] move sundials_errors_mod stuff into context mod --- src/sundials/fmod/CMakeLists.txt | 2 - src/sundials/fmod/fsundials_context_mod.c | 82 +++++++++ src/sundials/fmod/fsundials_context_mod.f90 | 183 ++++++++++++++++++++ swig/Makefile | 2 +- swig/sundials/fsundials_context_mod.i | 3 + swig/sundials/fsundials_errors_mod.i | 34 ---- 6 files changed, 269 insertions(+), 37 deletions(-) delete mode 100644 swig/sundials/fsundials_errors_mod.i diff --git a/src/sundials/fmod/CMakeLists.txt b/src/sundials/fmod/CMakeLists.txt index f5f2b26bbd..5c1a8fc3b7 100644 --- a/src/sundials/fmod/CMakeLists.txt +++ b/src/sundials/fmod/CMakeLists.txt @@ -19,8 +19,6 @@ set(sundials_SOURCES fsundials_adaptcontroller_mod.f90 fsundials_context_mod.c fsundials_context_mod.f90 - fsundials_errors_mod.c - fsundials_errors_mod.f90 fsundials_futils_mod.c fsundials_futils_mod.f90 fsundials_linearsolver_mod.c diff --git a/src/sundials/fmod/fsundials_context_mod.c b/src/sundials/fmod/fsundials_context_mod.c index be6efb29c0..83f8a2ce11 100644 --- a/src/sundials/fmod/fsundials_context_mod.c +++ b/src/sundials/fmod/fsundials_context_mod.c @@ -204,8 +204,90 @@ #include "sundials/sundials_context.h" +#include "sundials/sundials_errors.h" #include "sundials/sundials_profiler.h" + +#include +#ifdef _MSC_VER +# ifndef strtoull +# define strtoull _strtoui64 +# endif +# ifndef strtoll +# define strtoll _strtoi64 +# endif +#endif + + +typedef struct { + void* data; + size_t size; +} SwigArrayWrapper; + + +SWIGINTERN SwigArrayWrapper SwigArrayWrapper_uninitialized() { + SwigArrayWrapper result; + result.data = NULL; + result.size = 0; + return result; +} + + +#include + +SWIGEXPORT void _wrap_FSUNLogErrHandlerFn(int const *farg1, SwigArrayWrapper *farg2, SwigArrayWrapper *farg3, SwigArrayWrapper *farg4, int const *farg5, void *farg6, void *farg7) { + int arg1 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; + char *arg4 = (char *) 0 ; + SUNErrCode arg5 ; + void *arg6 = (void *) 0 ; + SUNContext arg7 = (SUNContext) 0 ; + + arg1 = (int)(*farg1); + arg2 = (char *)(farg2->data); + arg3 = (char *)(farg3->data); + arg4 = (char *)(farg4->data); + arg5 = (SUNErrCode)(*farg5); + arg6 = (void *)(farg6); + arg7 = (SUNContext)(farg7); + SUNLogErrHandlerFn(arg1,(char const *)arg2,(char const *)arg3,(char const *)arg4,arg5,arg6,arg7); +} + + +SWIGEXPORT void _wrap_FSUNAbortErrHandlerFn(int const *farg1, SwigArrayWrapper *farg2, SwigArrayWrapper *farg3, SwigArrayWrapper *farg4, int const *farg5, void *farg6, void *farg7) { + int arg1 ; + char *arg2 = (char *) 0 ; + char *arg3 = (char *) 0 ; + char *arg4 = (char *) 0 ; + SUNErrCode arg5 ; + void *arg6 = (void *) 0 ; + SUNContext arg7 = (SUNContext) 0 ; + + arg1 = (int)(*farg1); + arg2 = (char *)(farg2->data); + arg3 = (char *)(farg3->data); + arg4 = (char *)(farg4->data); + arg5 = (SUNErrCode)(*farg5); + arg6 = (void *)(farg6); + arg7 = (SUNContext)(farg7); + SUNAbortErrHandlerFn(arg1,(char const *)arg2,(char const *)arg3,(char const *)arg4,arg5,arg6,arg7); +} + + +SWIGEXPORT SwigArrayWrapper _wrap_FSUNGetErrMsg(int const *farg1) { + SwigArrayWrapper fresult ; + SUNErrCode arg1 ; + char *result = 0 ; + + arg1 = (SUNErrCode)(*farg1); + result = (char *)SUNGetErrMsg(arg1); + fresult.size = strlen((const char*)(result)); + fresult.data = (char *)(result); + return fresult; +} + + SWIGEXPORT int _wrap_FSUNContext_Create(int const *farg1, void *farg2) { int fresult ; SUNComm arg1 ; diff --git a/src/sundials/fmod/fsundials_context_mod.f90 b/src/sundials/fmod/fsundials_context_mod.f90 index 8d91bb7eb5..b0778ea9f9 100644 --- a/src/sundials/fmod/fsundials_context_mod.f90 +++ b/src/sundials/fmod/fsundials_context_mod.f90 @@ -26,6 +26,43 @@ module fsundials_context_mod private ! DECLARATION CONSTRUCTS + enum, bind(c) + enumerator :: SUN_ERR_MINIMUM = -10000 + enumerator :: SUN_ERR_ARG_CORRUPT + enumerator :: SUN_ERR_ARG_INCOMPATIBLE + enumerator :: SUN_ERR_ARG_OUTOFRANGE + enumerator :: SUN_ERR_ARG_WRONGTYPE + enumerator :: SUN_ERR_ARG_DIMSMISMATCH + enumerator :: SUN_ERR_CORRUPT + enumerator :: SUN_ERR_FILE_OPEN + enumerator :: SUN_ERR_MEM_FAIL + enumerator :: SUN_ERR_MALLOC_FAIL + enumerator :: SUN_ERR_DESTROY_FAIL + enumerator :: SUN_ERR_NOT_IMPLEMENTED + enumerator :: SUN_ERR_PROFILER_MAPFULL + enumerator :: SUN_ERR_PROFILER_MAPGET + enumerator :: SUN_ERR_PROFILER_MAPINSERT + enumerator :: SUN_ERR_PROFILER_MAPKEYNOTFOUND + enumerator :: SUN_ERR_PROFILER_MAPSORT + enumerator :: SUN_ERR_SUNCTX_CORRUPT + enumerator :: SUN_ERR_MPI_FAIL + enumerator :: SUN_ERR_UNREACHABLE + enumerator :: SUN_ERR_UNKNOWN + enumerator :: SUN_ERR_MAXIMUM = -1000 + enumerator :: SUN_SUCCESS = 0 + end enum + public :: SUN_ERR_MINIMUM, SUN_ERR_ARG_CORRUPT, SUN_ERR_ARG_INCOMPATIBLE, SUN_ERR_ARG_OUTOFRANGE, SUN_ERR_ARG_WRONGTYPE, & + SUN_ERR_ARG_DIMSMISMATCH, SUN_ERR_CORRUPT, SUN_ERR_FILE_OPEN, SUN_ERR_MEM_FAIL, SUN_ERR_MALLOC_FAIL, SUN_ERR_DESTROY_FAIL, & + SUN_ERR_NOT_IMPLEMENTED, SUN_ERR_PROFILER_MAPFULL, SUN_ERR_PROFILER_MAPGET, SUN_ERR_PROFILER_MAPINSERT, & + SUN_ERR_PROFILER_MAPKEYNOTFOUND, SUN_ERR_PROFILER_MAPSORT, SUN_ERR_SUNCTX_CORRUPT, SUN_ERR_MPI_FAIL, SUN_ERR_UNREACHABLE, & + SUN_ERR_UNKNOWN, SUN_ERR_MAXIMUM, SUN_SUCCESS + type, bind(C) :: SwigArrayWrapper + type(C_PTR), public :: data = C_NULL_PTR + integer(C_SIZE_T), public :: size = 0 + end type + public :: FSUNLogErrHandlerFn + public :: FSUNAbortErrHandlerFn + public :: FSUNGetErrMsg public :: FSUNContext_Create public :: FSUNContext_GetLastError public :: FSUNContext_PeekLastError @@ -40,6 +77,46 @@ module fsundials_context_mod ! WRAPPER DECLARATIONS interface +subroutine swigc_FSUNLogErrHandlerFn(farg1, farg2, farg3, farg4, farg5, farg6, farg7) & +bind(C, name="_wrap_FSUNLogErrHandlerFn") +use, intrinsic :: ISO_C_BINDING +import :: swigarraywrapper +integer(C_INT), intent(in) :: farg1 +type(SwigArrayWrapper) :: farg2 +type(SwigArrayWrapper) :: farg3 +type(SwigArrayWrapper) :: farg4 +integer(C_INT), intent(in) :: farg5 +type(C_PTR), value :: farg6 +type(C_PTR), value :: farg7 +end subroutine + +subroutine swigc_FSUNAbortErrHandlerFn(farg1, farg2, farg3, farg4, farg5, farg6, farg7) & +bind(C, name="_wrap_FSUNAbortErrHandlerFn") +use, intrinsic :: ISO_C_BINDING +import :: swigarraywrapper +integer(C_INT), intent(in) :: farg1 +type(SwigArrayWrapper) :: farg2 +type(SwigArrayWrapper) :: farg3 +type(SwigArrayWrapper) :: farg4 +integer(C_INT), intent(in) :: farg5 +type(C_PTR), value :: farg6 +type(C_PTR), value :: farg7 +end subroutine + + subroutine SWIG_free(cptr) & + bind(C, name="free") + use, intrinsic :: ISO_C_BINDING + type(C_PTR), value :: cptr +end subroutine +function swigc_FSUNGetErrMsg(farg1) & +bind(C, name="_wrap_FSUNGetErrMsg") & +result(fresult) +use, intrinsic :: ISO_C_BINDING +import :: swigarraywrapper +integer(C_INT), intent(in) :: farg1 +type(SwigArrayWrapper) :: fresult +end function + function swigc_FSUNContext_Create(farg1, farg2) & bind(C, name="_wrap_FSUNContext_Create") & result(fresult) @@ -140,6 +217,112 @@ function swigc_FSUNContext_Free(farg1) & contains ! MODULE SUBPROGRAMS + +subroutine SWIG_string_to_chararray(string, chars, wrap) + use, intrinsic :: ISO_C_BINDING + character(kind=C_CHAR, len=*), intent(IN) :: string + character(kind=C_CHAR), dimension(:), target, allocatable, intent(OUT) :: chars + type(SwigArrayWrapper), intent(OUT) :: wrap + integer :: i + + allocate(character(kind=C_CHAR) :: chars(len(string) + 1)) + do i=1,len(string) + chars(i) = string(i:i) + end do + i = len(string) + 1 + chars(i) = C_NULL_CHAR ! C string compatibility + wrap%data = c_loc(chars) + wrap%size = len(string) +end subroutine + +subroutine FSUNLogErrHandlerFn(line, func, file, msg, err_code, err_user_data, sunctx) +use, intrinsic :: ISO_C_BINDING +integer(C_INT), intent(in) :: line +character(kind=C_CHAR, len=*), target :: func +character(kind=C_CHAR), dimension(:), allocatable, target :: farg2_chars +character(kind=C_CHAR, len=*), target :: file +character(kind=C_CHAR), dimension(:), allocatable, target :: farg3_chars +character(kind=C_CHAR, len=*), target :: msg +character(kind=C_CHAR), dimension(:), allocatable, target :: farg4_chars +integer(C_INT), intent(in) :: err_code +type(C_PTR) :: err_user_data +type(C_PTR) :: sunctx +integer(C_INT) :: farg1 +type(SwigArrayWrapper) :: farg2 +type(SwigArrayWrapper) :: farg3 +type(SwigArrayWrapper) :: farg4 +integer(C_INT) :: farg5 +type(C_PTR) :: farg6 +type(C_PTR) :: farg7 + +farg1 = line +call SWIG_string_to_chararray(func, farg2_chars, farg2) +call SWIG_string_to_chararray(file, farg3_chars, farg3) +call SWIG_string_to_chararray(msg, farg4_chars, farg4) +farg5 = err_code +farg6 = err_user_data +farg7 = sunctx +call swigc_FSUNLogErrHandlerFn(farg1, farg2, farg3, farg4, farg5, farg6, farg7) +end subroutine + +subroutine FSUNAbortErrHandlerFn(line, func, file, msg, err_code, err_user_data, sunctx) +use, intrinsic :: ISO_C_BINDING +integer(C_INT), intent(in) :: line +character(kind=C_CHAR, len=*), target :: func +character(kind=C_CHAR), dimension(:), allocatable, target :: farg2_chars +character(kind=C_CHAR, len=*), target :: file +character(kind=C_CHAR), dimension(:), allocatable, target :: farg3_chars +character(kind=C_CHAR, len=*), target :: msg +character(kind=C_CHAR), dimension(:), allocatable, target :: farg4_chars +integer(C_INT), intent(in) :: err_code +type(C_PTR) :: err_user_data +type(C_PTR) :: sunctx +integer(C_INT) :: farg1 +type(SwigArrayWrapper) :: farg2 +type(SwigArrayWrapper) :: farg3 +type(SwigArrayWrapper) :: farg4 +integer(C_INT) :: farg5 +type(C_PTR) :: farg6 +type(C_PTR) :: farg7 + +farg1 = line +call SWIG_string_to_chararray(func, farg2_chars, farg2) +call SWIG_string_to_chararray(file, farg3_chars, farg3) +call SWIG_string_to_chararray(msg, farg4_chars, farg4) +farg5 = err_code +farg6 = err_user_data +farg7 = sunctx +call swigc_FSUNAbortErrHandlerFn(farg1, farg2, farg3, farg4, farg5, farg6, farg7) +end subroutine + + +subroutine SWIG_chararray_to_string(wrap, string) + use, intrinsic :: ISO_C_BINDING + type(SwigArrayWrapper), intent(IN) :: wrap + character(kind=C_CHAR, len=:), allocatable, intent(OUT) :: string + character(kind=C_CHAR), dimension(:), pointer :: chars + integer(kind=C_SIZE_T) :: i + call c_f_pointer(wrap%data, chars, [wrap%size]) + allocate(character(kind=C_CHAR, len=wrap%size) :: string) + do i=1, wrap%size + string(i:i) = chars(i) + end do +end subroutine + +function FSUNGetErrMsg(code) & +result(swig_result) +use, intrinsic :: ISO_C_BINDING +character(kind=C_CHAR, len=:), allocatable :: swig_result +integer(C_INT), intent(in) :: code +type(SwigArrayWrapper) :: fresult +integer(C_INT) :: farg1 + +farg1 = code +fresult = swigc_FSUNGetErrMsg(farg1) +call SWIG_chararray_to_string(fresult, swig_result) +if (.false.) call SWIG_free(fresult%data) +end function + function FSUNContext_Create(comm, sunctx_out) & result(swig_result) use, intrinsic :: ISO_C_BINDING diff --git a/swig/Makefile b/swig/Makefile index 2081acbcc7..1948ecfd74 100644 --- a/swig/Makefile +++ b/swig/Makefile @@ -20,7 +20,7 @@ CVODES=fcvodes_mod IDA=fida_mod IDAS=fidas_mod KINSOL=fkinsol_mod -GENERIC=fsundials_types_mod fsundials_nvector_mod fsundials_matrix_mod fsundials_linearsolver_mod fsundials_nonlinearsolver_mod fsundials_futils_mod fsundials_context_mod fsundials_profiler_mod fsundials_logger_mod fsundials_errors_mod fsundials_adaptcontroller_mod +GENERIC=fsundials_types_mod fsundials_nvector_mod fsundials_matrix_mod fsundials_linearsolver_mod fsundials_nonlinearsolver_mod fsundials_futils_mod fsundials_context_mod fsundials_profiler_mod fsundials_logger_mod fsundials_adaptcontroller_mod NVECTOR=openmp pthreads serial parallel manyvector mpiplusx SUNMATRIX=band dense sparse SUNLINSOL=band dense lapackdense klu spbcgs spfgmr spgmr sptfqmr pcg diff --git a/swig/sundials/fsundials_context_mod.i b/swig/sundials/fsundials_context_mod.i index a2081936de..7504b0d1af 100644 --- a/swig/sundials/fsundials_context_mod.i +++ b/swig/sundials/fsundials_context_mod.i @@ -25,6 +25,7 @@ // insert the include into the swig wrapper %{ #include "sundials/sundials_context.h" +#include "sundials/sundials_errors.h" #include "sundials/sundials_profiler.h" %} @@ -33,6 +34,8 @@ %apply void** { SUNProfiler* }; %apply void* { SUNLogger }; %apply void** { SUNLogger* }; +%apply void* { SUNErrHandler }; // Process and wrap functions in the following files +%include "sundials/sundials_errors.h" %include "sundials/sundials_context.h" diff --git a/swig/sundials/fsundials_errors_mod.i b/swig/sundials/fsundials_errors_mod.i deleted file mode 100644 index 4ad52ce1f3..0000000000 --- a/swig/sundials/fsundials_errors_mod.i +++ /dev/null @@ -1,34 +0,0 @@ -// --------------------------------------------------------------- -// Programmer: Cody J. Balos @ LLNL -// --------------------------------------------------------------- -// SUNDIALS Copyright Start -// Copyright (c) 2002-2023, Lawrence Livermore National Security -// and Southern Methodist University. -// All rights reserved. -// -// See the top-level LICENSE and NOTICE files for details. -// -// SPDX-License-Identifier: BSD-3-Clause -// SUNDIALS Copyright End -// --------------------------------------------------------------- -// Swig interface file -// --------------------------------------------------------------- - -%module fsundials_errors_mod - -// Load the typedefs and generate a "use fsundials_types_mod" statement in the module -%import "../sundials/fsundials_context_mod.i" -%import "../sundials/fsundials_types_mod.i" - -%include "../sundials/fcopyright.i" - -// Insert code into the C wrapper to check that the sizes match -%{ -#include "sundials/sundials_errors.h" -%} - -%apply void* { SUNErrHandler }; - -// Process and wrap functions in the following files -%include "sundials/sundials_errors.h" -