Skip to content

Commit

Permalink
Merge branch 'feature/error-handling-staging' into feature/error-hand…
Browse files Browse the repository at this point in the history
…ling-cvodes
  • Loading branch information
balos1 committed Dec 11, 2023
2 parents 9c8153a + 9a2f675 commit 32caa4f
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 39 deletions.
1 change: 1 addition & 0 deletions include/sundials/sundials_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
\
ENTRY(SUN_ERR_CORRUPT, "Object is NULL or corrupt") \
ENTRY(SUN_ERR_FILE_OPEN, "Unable to open file") \
ENTRY(SUN_ERR_MEM_FAIL, "a memory operation failed") \
ENTRY(SUN_ERR_MALLOC_FAIL, "malloc returned NULL") \
ENTRY(SUN_ERR_DESTROY_FAIL, "a destroy function returned an error") \
ENTRY(SUN_ERR_NOT_IMPLEMENTED, \
Expand Down
6 changes: 4 additions & 2 deletions scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
# This script will use clang-tidy and clang-format to format code.
#
# Usage:
# ./format.sh <path to directory to format>
# ./format.sh <paths to directories or files to format>
#
# We require clang-format 17.0.4. Other versions may produce different styles!
# ---------------------------------------------------------------------------------

find $1 -iname '*.h' -o -iname '*.hpp' -o \
paths="$@"

find $paths -iname '*.h' -o -iname '*.hpp' -o \
-iname '*.c' -o -iname '*.cpp' -o \
-iname '*.cuh' -o -iname '*.cu' | grep -v fmod | xargs clang-format -i
2 changes: 0 additions & 2 deletions src/sundials/fmod/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 82 additions & 0 deletions src/sundials/fmod/fsundials_context_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,90 @@


#include "sundials/sundials_context.h"
#include "sundials/sundials_errors.h"
#include "sundials/sundials_profiler.h"


#include <stdlib.h>
#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 <string.h>

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 ;
Expand Down
183 changes: 183 additions & 0 deletions src/sundials/fmod/fsundials_context_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion swig/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions swig/sundials/fsundials_context_mod.i
Original file line number Diff line number Diff line change
Expand Up @@ -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"
%}

Expand All @@ -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"
34 changes: 0 additions & 34 deletions swig/sundials/fsundials_errors_mod.i

This file was deleted.

0 comments on commit 32caa4f

Please sign in to comment.