From 0ce95646d10944bbca970c5e141459753e772df4 Mon Sep 17 00:00:00 2001 From: "Balos, Cody, J" Date: Tue, 28 Nov 2023 15:53:38 -0800 Subject: [PATCH] address another batch of comments --- examples/cvode/serial/cvAdvDiff_bnd.c | 8 +- include/sundials/impl/sundials_errors_impl.h | 134 ++++++++++-------- include/sundials/sundials_context.h | 12 +- include/sundials/sundials_core.h | 1 - include/sundials/sundials_core.hpp | 1 + include/sundials/sundials_errors.h | 56 +++----- include/sundials/sundials_linearsolver.h | 4 +- include/sundials/sundials_memory.h | 22 +-- include/sundials/sundials_mpi_types.h | 2 - include/sundials/sundials_nonlinearsolver.h | 8 +- src/sundials/fmod/fsundials_context_mod.c | 48 ++----- src/sundials/fmod/fsundials_context_mod.f90 | 56 +++----- src/sundials/fmod/fsundials_errors_mod.c | 41 ------ src/sundials/fmod/fsundials_errors_mod.f90 | 89 +----------- src/sundials/sundials_context.c | 125 +++++++++------- src/sundials/sundials_errors.c | 60 ++++---- src/sundials/sundials_logger.c | 10 +- src/sundials/sundials_nvector.c | 4 +- .../sundials/test_sundials_errors.cpp | 4 +- 19 files changed, 273 insertions(+), 412 deletions(-) diff --git a/examples/cvode/serial/cvAdvDiff_bnd.c b/examples/cvode/serial/cvAdvDiff_bnd.c index 27a96e46e6..8afb7eb0c6 100644 --- a/examples/cvode/serial/cvAdvDiff_bnd.c +++ b/examples/cvode/serial/cvAdvDiff_bnd.c @@ -145,7 +145,7 @@ int main(void) /* Create a serial vector */ u = N_VNew_Serial(NEQ, sunctx); /* Allocate u vector */ - if(check_retval(SUNGetLastErr(sunctx), "N_VNew_Serial")) return(1); + if(check_retval(SUNContext_GetLastError(sunctx), "N_VNew_Serial")) return(1); reltol = ZERO; /* Set the tolerances */ abstol = ATOL; @@ -172,7 +172,7 @@ int main(void) * Backward Differentiation Formula */ cvode_mem = CVodeCreate(CV_BDF, sunctx); - if(check_retval(SUNGetLastErr(sunctx), "CVodeCreate")) return(1); + if(check_retval(SUNContext_GetLastError(sunctx), "CVodeCreate")) return(1); /* Call CVodeInit to initialize the integrator memory and specify the * user's right hand side function in u'=f(t,u), the inital time T0, and @@ -192,11 +192,11 @@ int main(void) /* Create banded SUNMatrix for use in linear solves -- since this will be factored, set the storage bandwidth to be the sum of upper and lower bandwidths */ A = SUNBandMatrix(NEQ, MY, MY, sunctx); - if(check_retval(SUNGetLastErr(sunctx), "SUNBandMatrix")) return(1); + if(check_retval(SUNContext_GetLastError(sunctx), "SUNBandMatrix")) return(1); /* Create banded SUNLinearSolver object for use by CVode */ LS = SUNLinSol_Band(u, A, sunctx); - if(check_retval(SUNGetLastErr(sunctx), "SUNLinSol_Band")) return(1); + if(check_retval(SUNContext_GetLastError(sunctx), "SUNLinSol_Band")) return(1); /* Call CVodeSetLinearSolver to attach the matrix and linear solver to CVode */ retval = CVodeSetLinearSolver(cvode_mem, LS, A); diff --git a/include/sundials/impl/sundials_errors_impl.h b/include/sundials/impl/sundials_errors_impl.h index 09ff022a2e..3664d3f07f 100644 --- a/include/sundials/impl/sundials_errors_impl.h +++ b/include/sundials/impl/sundials_errors_impl.h @@ -33,7 +33,8 @@ struct SUNErrHandler_ void* data; }; -SUNErrHandler SUNErrHandler_Create(SUNErrHandlerFn eh_fn, void* eh_data); +SUNErrCode SUNErrHandler_Create(SUNErrHandlerFn eh_fn, void* eh_data, + SUNErrHandler* eh_out); void SUNErrHandler_Destroy(SUNErrHandler eh); @@ -111,95 +112,106 @@ static inline void SUNHandleErrWithFmtMsg(int line, const char* func, returned error code. If an error occured, then it will log the error, set the last_err value, and call the error handler. */ #if defined(SUNDIALS_ENABLE_ERROR_CHECKS) -#define SUNCheckCallNoRet(call) \ - do { \ - SUNErrCode sun_chk_call_err_code_ = call; \ - if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ - { \ - SUNHandleErr(__LINE__, __func__, __FILE__, sun_chk_call_err_code_, \ - SUNCTX_); \ - (void)SUNGetLastErr(SUNCTX_); \ - } \ - } \ +#define SUNCheckCallNoRetMsg(call, msg) \ + do { \ + SUNErrCode sun_chk_call_err_code_ = call; \ + if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ + { \ + SUNHandleErrWithMsg(__LINE__, __func__, __FILE__, msg, \ + sun_chk_call_err_code_, SUNCTX_); \ + (void)SUNContext_GetLastError(SUNCTX_); \ + } \ + } \ while (0) #else -#define SUNCheckCallNoRet(call) (void)call +#define SUNCheckCallNoRetMsg(call, msg) (void)call #endif /* Same as SUNCheckCallNoRet, but returns with the error code if an error * occured. */ #if defined(SUNDIALS_ENABLE_ERROR_CHECKS) -#define SUNCheckCall(call) \ - do { \ - SUNErrCode sun_chk_call_err_code_ = call; \ - if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ - { \ - SUNHandleErr(__LINE__, __func__, __FILE__, sun_chk_call_err_code_, \ - SUNCTX_); \ - return sun_chk_call_err_code_; \ - } \ - } \ +#define SUNCheckCallMsg(call, msg) \ + do { \ + SUNErrCode sun_chk_call_err_code_ = call; \ + if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ + { \ + SUNHandleErrWithMsg(__LINE__, __func__, __FILE__, msg, \ + sun_chk_call_err_code_, SUNCTX_); \ + return sun_chk_call_err_code_; \ + } \ + } \ while (0) #else -#define SUNCheckCall(call) (void)call +#define SUNCheckCallMsg(call, msg) (void)call #endif /* Same as SUNCheckCall, but returns with NULL. */ #if defined(SUNDIALS_ENABLE_ERROR_CHECKS) -#define SUNCheckCallNull(call) \ - do { \ - SUNErrCode sun_chk_call_err_code_ = call; \ - if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ - { \ - SUNHandleErr(__LINE__, __func__, __FILE__, sun_chk_call_err_code_, \ - SUNCTX_); \ - return NULL; \ - } \ - } \ +#define SUNCheckCallNullMsg(call, msg) \ + do { \ + SUNErrCode sun_chk_call_err_code_ = call; \ + if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ + { \ + SUNHandleErrWithMsg(__LINE__, __func__, __FILE__, msg, \ + sun_chk_call_err_code_, SUNCTX_); \ + return NULL; \ + } \ + } \ while (0) #else -#define SUNCheckCallNull(call) (void)call +#define SUNCheckCallNullMsg(call, msg) (void)call #endif /* Same as SUNCheckCall, but returns void. */ #if defined(SUNDIALS_ENABLE_ERROR_CHECKS) -#define SUNCheckCallVoid(call) \ - do { \ - SUNErrCode sun_chk_call_err_code_ = call; \ - if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ - { \ - SUNHandleErr(__LINE__, __func__, __FILE__, sun_chk_call_err_code_, \ - SUNCTX_); \ - return; \ - } \ - } \ +#define SUNCheckCallVoidMsg(call, msg) \ + do { \ + SUNErrCode sun_chk_call_err_code_ = call; \ + if (SUNHintFalse(sun_chk_call_err_code_ < 0)) \ + { \ + SUNHandleErrWithMsg(__LINE__, __func__, __FILE__, msg, \ + sun_chk_call_err_code_, SUNCTX_); \ + return; \ + } \ + } \ while (0) #else -#define SUNCheckCallNull(call) (void)call +#define SUNCheckCallVoidMsg(call, msg) (void)call #endif -/* SUNCheckLastErr checks the last_err value in the SUNContext. +/* These versions of SUNCheckCall do not take a custom message so the + default message associated with the error code will be used. */ +#define SUNCheckCallNoRet(call) SUNCheckCallNoRetMsg(call, NULL) +#define SUNCheckCall(call) SUNCheckCallMsg(call, NULL) +#define SUNCheckCallNull(call) SUNCheckCallNull(call, NULL) +#define SUNCheckCallVoid(call) SUNCheckCallVoidMsg(call, NULL) + +/* SUNCheckLastErrMoRetMsg checks the last_err value in the SUNContext. If an error occured, then it will log the error, set the last_err value, and calls the error handler. */ - #if defined(SUNDIALS_ENABLE_ERROR_CHECKS) -#define SUNCheckLastErrNoRet() SUNCheckCallNoRet(SUNGetLastErr(SUNCTX_)) - -/* Same as SUNCheckLastErrNoRet, but returns with the error code. */ -#define SUNCheckLastErr() SUNCheckCall(SUNGetLastErr(SUNCTX_)) - -/* Same as SUNCheckLastErrNoRet, but returns void. */ -#define SUNCheckLastErrVoid() SUNCheckCallVoid(SUNGetLastErr(SUNCTX_)) - -/* Same as SUNCheckLastErrNoRet, but returns NULL. */ -#define SUNCheckLastErrNull() SUNCheckCallNull(SUNGetLastErr(SUNCTX_)) +#define SUNCheckLastErrNoRetMsg(msg) \ + SUNCheckCallNoRetMsg(SUNContext_GetLastError(SUNCTX_), msg) +#define SUNCheckLastErrMsg(msg) \ + SUNCheckCallMsg(SUNContext_GetLastError(SUNCTX_), msg) +#define SUNCheckLastErrNullMsg(msg) \ + SUNCheckCallNullMsg(SUNContext_GetLastError(SUNCTX_), msg) +#define SUNCheckLastErrVoidMsg(msg) \ + SUNCheckCallVoidMsg(SUNContext_GetLastError(SUNCTX_), msg) #else -#define SUNCheckLastErrNoRet() -#define SUNCheckLastErr() -#define SUNCheckLastErrVoid() -#define SUNCheckLastErrNull() +#define SUNCheckLastErrNoRetMsg(msg) +#define SUNCheckLastErrMsg(msg) +#define SUNCheckLastErrVoidMsg(msg) +#define SUNCheckLastErrNullMsg(msg) #endif +/* These versions of SUNCheckLastErr do not take a custom message so the + default message associated with the error code will be used. */ +#define SUNCheckLastErr() SUNCheckLastErrMsg(NULL) +#define SUNCheckLastErrNoRet() SUNCheckLastErrNoRetMsg(NULL) +#define SUNCheckLastErrVoid() SUNCheckLastErrVoidMsg(NULL) +#define SUNCheckLastErrNull() SUNCheckLastErrNullMsg(NULL) + /* SUNAssert checks if an expression is true. It expands to SUNCheck when error checks are enabled. If error checks are disabled, then we try to expand it to an assumption, diff --git a/include/sundials/sundials_context.h b/include/sundials/sundials_context.h index 9c645a9112..8e22f7c032 100644 --- a/include/sundials/sundials_context.h +++ b/include/sundials/sundials_context.h @@ -29,21 +29,21 @@ SUNDIALS_EXPORT SUNErrCode SUNContext_Create(SUNComm comm, SUNContext* sunctx_out); SUNDIALS_EXPORT -SUNErrCode SUNContext_GetLastError(SUNContext sunctx, SUNErrCode* last_err); +SUNErrCode SUNContext_GetLastError(SUNContext sunctx); SUNDIALS_EXPORT -SUNErrCode SUNContext_PeekLastError(SUNContext sunctx, SUNErrCode* last_err); +SUNErrCode SUNContext_PeekLastError(SUNContext sunctx); SUNDIALS_EXPORT -SUNErrHandler SUNContext_PushErrHandler(SUNContext sunctx, - SUNErrHandlerFn err_fn, - void* err_user_data); +SUNErrCode SUNContext_PushErrHandler(SUNContext sunctx, + SUNErrHandlerFn err_fn, + void* err_user_data); SUNDIALS_EXPORT SUNErrCode SUNContext_PopErrHandler(SUNContext sunctx); SUNDIALS_EXPORT -SUNErrCode SUNContext_ClearHandlers(SUNContext sunctx); +SUNErrCode SUNContext_ClearErrHandlers(SUNContext sunctx); SUNDIALS_EXPORT SUNErrCode SUNContext_GetProfiler(SUNContext sunctx, SUNProfiler* profiler); diff --git a/include/sundials/sundials_core.h b/include/sundials/sundials_core.h index 040f83b545..a7c50b8a18 100644 --- a/include/sundials/sundials_core.h +++ b/include/sundials/sundials_core.h @@ -17,7 +17,6 @@ #include #include -#include #include #include #include diff --git a/include/sundials/sundials_core.hpp b/include/sundials/sundials_core.hpp index d80fb83e1f..9de789f179 100644 --- a/include/sundials/sundials_core.hpp +++ b/include/sundials/sundials_core.hpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include diff --git a/include/sundials/sundials_errors.h b/include/sundials/sundials_errors.h index 77df8b0280..493074521d 100644 --- a/include/sundials/sundials_errors.h +++ b/include/sundials/sundials_errors.h @@ -20,7 +20,6 @@ #include #include #include - #include #include @@ -35,17 +34,14 @@ ENTRY(SUN_ERR_ARG_OUTOFRANGE, "argument is out of the valid range") \ ENTRY(SUN_ERR_ARG_WRONGTYPE, "argument provided is not the right type") \ ENTRY(SUN_ERR_ARG_DIMSMISMATCH, "argument dimensions do not agree") \ - ENTRY(SUN_ERR_LOGGER_CORRUPT, "SUNLogger is NULL or corrupt") \ - ENTRY(SUN_ERR_LOGGER_CANNOTOPENFILE, \ - "File provided to SUNLogger could not be opened") \ + \ + ENTRY(SUN_ERR_CORRUPT, "Object is NULL or corrupt") \ + ENTRY(SUN_ERR_FILE_OPEN, "Unable to open file") \ ENTRY(SUN_ERR_MALLOC_FAIL, "malloc returned NULL") \ - ENTRY(SUN_ERR_MANYVECTOR_COMMNOTSAME, \ - "not all subvectors have the same MPI_Comm") \ - ENTRY(SUN_ERR_MANYVECTOR_COMMNULL, "MPI_Comm is NULL") \ - ENTRY(SUN_ERR_MPI_FAIL, \ - "the MPI call returned something other than MPI_SUCCESS") \ + ENTRY(SUN_ERR_DESTROY_FAIL, "a destroy function returned an error") \ ENTRY(SUN_ERR_NOT_IMPLEMENTED, \ "operation is not implemented: function pointer is NULL") \ + \ ENTRY(SUN_ERR_PROFILER_MAPFULL, \ "the number of profiler entries exceeded SUNPROFILER_MAX_ENTRIES") \ ENTRY(SUN_ERR_PROFILER_MAPGET, "unknown error getting SUNProfiler timer") \ @@ -53,23 +49,32 @@ "unknown error inserting SUNProfiler timer") \ ENTRY(SUN_ERR_PROFILER_MAPKEYNOTFOUND, "timer was not found in SUNProfiler") \ ENTRY(SUN_ERR_PROFILER_MAPSORT, "error sorting SUNProfiler map") \ + \ ENTRY(SUN_ERR_SUNCTX_CORRUPT, "SUNContext is NULL or corrupt") \ - ENTRY(SUN_ERR_GENERIC, "") \ + \ + ENTRY(SUN_ERR_MPI_FAIL, \ + "an MPI call returned something other than MPI_SUCCESS") \ + \ ENTRY(SUN_ERR_UNKNOWN, "Unknown error occured: open an issue at " \ "https://github.com/LLNL/sundials") /* Expand SUN_ERR_CODE_LIST to enum */ #define SUN_EXPAND_TO_ENUM(name, description) name, -/* SUNErrorCode range is [-1000, -2000] to avoid conflicts with package error +/* SUNErrorCode range is [-10000, -1000] to avoid conflicts with package error codes, and old/deprecated codes for matrix and (non)linear solvers. */ + +/* clang-format off */ enum { - SUN_ERR_MINIMUM = -2000, - SUN_ERR_CODE_LIST(SUN_EXPAND_TO_ENUM) SUN_ERR_MAXIMUM = -1000, + SUN_ERR_MINIMUM = -10000, + SUN_ERR_CODE_LIST(SUN_EXPAND_TO_ENUM) + SUN_ERR_MAXIMUM = -1000, SUN_SUCCESS = 0 }; +/* clang-format on */ + /* ---------------------------------------------------------------------------- * Error handler definitions * ---------------------------------------------------------------------------*/ @@ -90,7 +95,7 @@ int SUNAbortErrHandlerFn(int line, const char* func, const char* file, SUNDIALS_EXPORT int SUNAssertErrHandlerFn(int line, const char* func, const char* file, - const char* msg, SUNErrCode err_code, + const char* stmt, SUNErrCode err_code, void* err_user_data, SUNContext sunctx); /* ---------------------------------------------------------------------------- @@ -101,29 +106,6 @@ int SUNAssertErrHandlerFn(int line, const char* func, const char* file, SUNDIALS_EXPORT const char* SUNGetErrMsg(SUNErrCode code, SUNContext sunctx); -/* Alternative function to SUNContext_GetLastError that is more concise. */ -static inline SUNErrCode SUNGetLastErr(SUNContext sunctx) -{ - SUNErrCode code = SUN_SUCCESS; - (void)SUNContext_GetLastError(sunctx, &code); - return code; -} - -/* Alternative function to SUNContext_SetLastError that is more concise. */ -static inline SUNErrCode SUNSetLastErr(SUNErrCode code, SUNContext sunctx) -{ - sunctx->last_err = code; - return SUN_SUCCESS; -} - -/* Alternative function to SUNContext_PeekLastError that is more concise. */ -static inline SUNErrCode SUNPeekLastErr(SUNContext sunctx) -{ - SUNErrCode code = SUN_SUCCESS; - (void)SUNContext_PeekLastError(sunctx, &code); - return code; -} - #ifdef __cplusplus /* wrapper to enable C++ usage */ } /* extern "C" */ #endif diff --git a/include/sundials/sundials_linearsolver.h b/include/sundials/sundials_linearsolver.h index f2d94e18b3..713173eeae 100644 --- a/include/sundials/sundials_linearsolver.h +++ b/include/sundials/sundials_linearsolver.h @@ -156,8 +156,8 @@ SUNLinearSolver SUNLinSolNewEmpty(SUNContext sunctx); SUNDIALS_EXPORT void SUNLinSolFreeEmpty(SUNLinearSolver S); -SUNDIALS_EXPORT SUNLinearSolver_Type -SUNLinSolGetType(SUNLinearSolver S); +SUNDIALS_EXPORT +SUNLinearSolver_Type SUNLinSolGetType(SUNLinearSolver S); SUNDIALS_EXPORT SUNLinearSolver_ID SUNLinSolGetID(SUNLinearSolver S); diff --git a/include/sundials/sundials_memory.h b/include/sundials/sundials_memory.h index e206c2e183..fe48e3a8a8 100644 --- a/include/sundials/sundials_memory.h +++ b/include/sundials/sundials_memory.h @@ -70,21 +70,21 @@ struct _SUNMemoryHelper struct _SUNMemoryHelper_Ops { /* operations that implementations are required to provide */ - int (*alloc)(SUNMemoryHelper, SUNMemory* memptr, size_t mem_size, + SUNErrCode (*alloc)(SUNMemoryHelper, SUNMemory* memptr, size_t mem_size, SUNMemoryType mem_type, void* queue); - int (*dealloc)(SUNMemoryHelper, SUNMemory mem, void* queue); - int (*copy)(SUNMemoryHelper, SUNMemory dst, SUNMemory src, size_t mem_size, + SUNErrCode (*dealloc)(SUNMemoryHelper, SUNMemory mem, void* queue); + SUNErrCode (*copy)(SUNMemoryHelper, SUNMemory dst, SUNMemory src, size_t mem_size, void* queue); /* operations that provide default implementations */ - int (*copyasync)(SUNMemoryHelper, SUNMemory dst, SUNMemory src, + SUNErrCode (*copyasync)(SUNMemoryHelper, SUNMemory dst, SUNMemory src, size_t mem_size, void* queue); - int (*getallocstats)(SUNMemoryHelper, SUNMemoryType mem_type, + SUNErrCode (*getallocstats)(SUNMemoryHelper, SUNMemoryType mem_type, unsigned long* num_allocations, unsigned long* num_deallocations, size_t* bytes_allocated, size_t* bytes_high_watermark); SUNMemoryHelper (*clone)(SUNMemoryHelper); - int (*destroy)(SUNMemoryHelper); + SUNErrCode (*destroy)(SUNMemoryHelper); }; /* @@ -108,15 +108,15 @@ SUNMemory SUNMemoryHelper_Wrap(void* ptr, SUNMemoryType mem_type); * Required SUNMemoryHelper operations. */ -SUNDIALS_EXPORT SUNErrCode -SUNMemoryHelper_Alloc(SUNMemoryHelper, SUNMemory* memptr, size_t mem_size, - SUNMemoryType mem_type, void* queue); +SUNDIALS_EXPORT +SUNErrCode SUNMemoryHelper_Alloc(SUNMemoryHelper, SUNMemory* memptr, size_t mem_size, + SUNMemoryType mem_type, void* queue); SUNDIALS_EXPORT SUNErrCode SUNMemoryHelper_Dealloc(SUNMemoryHelper, SUNMemory mem, void* queue); -SUNDIALS_EXPORT SUNErrCode SUNMemoryHelper_Copy( - SUNMemoryHelper, SUNMemory dst, SUNMemory src, size_t mem_size, void* queue); +SUNDIALS_EXPORT +SUNErrCode SUNMemoryHelper_Copy(SUNMemoryHelper, SUNMemory dst, SUNMemory src, size_t mem_size, void* queue); /* * Optional SUNMemoryHelper operations. diff --git a/include/sundials/sundials_mpi_types.h b/include/sundials/sundials_mpi_types.h index 256f1fb7ed..bcd9bfe5b1 100644 --- a/include/sundials/sundials_mpi_types.h +++ b/include/sundials/sundials_mpi_types.h @@ -19,8 +19,6 @@ #include #include -#define SUN_AsMPIComm(comm) (*((MPI_Comm*)(comm))) - /* define MPI data types */ #if defined(SUNDIALS_SINGLE_PRECISION) diff --git a/include/sundials/sundials_nonlinearsolver.h b/include/sundials/sundials_nonlinearsolver.h index 8cc4c98b34..5f003a7e42 100644 --- a/include/sundials/sundials_nonlinearsolver.h +++ b/include/sundials/sundials_nonlinearsolver.h @@ -140,8 +140,8 @@ SUNDIALS_EXPORT void SUNNonlinSolFreeEmpty(SUNNonlinearSolver NLS); /* core functions */ -SUNDIALS_EXPORT SUNNonlinearSolver_Type -SUNNonlinSolGetType(SUNNonlinearSolver NLS); +SUNDIALS_EXPORT +SUNNonlinearSolver_Type SUNNonlinSolGetType(SUNNonlinearSolver NLS); SUNDIALS_EXPORT SUNErrCode SUNNonlinSolInitialize(SUNNonlinearSolver NLS); @@ -157,8 +157,8 @@ SUNDIALS_EXPORT SUNErrCode SUNNonlinSolFree(SUNNonlinearSolver NLS); /* set functions */ -SUNDIALS_EXPORT SUNErrCode -SUNNonlinSolSetSysFn(SUNNonlinearSolver NLS, SUNNonlinSolSysFn SysFn); +SUNDIALS_EXPORT +SUNErrCode SUNNonlinSolSetSysFn(SUNNonlinearSolver NLS, SUNNonlinSolSysFn SysFn); SUNDIALS_EXPORT SUNErrCode SUNNonlinSolSetLSetupFn(SUNNonlinearSolver NLS, diff --git a/src/sundials/fmod/fsundials_context_mod.c b/src/sundials/fmod/fsundials_context_mod.c index 5b866f9061..762fa185da 100644 --- a/src/sundials/fmod/fsundials_context_mod.c +++ b/src/sundials/fmod/fsundials_context_mod.c @@ -178,13 +178,6 @@ { printf("In " DECL ": " MSG); assert(0); RETURNNULL; } -enum { - SWIG_MEM_OWN = 0x01, - SWIG_MEM_RVALUE = 0x02, - SWIG_MEM_CONST = 0x04 -}; - - #include #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WATCOM) # ifndef snprintf @@ -213,20 +206,6 @@ enum { #include "sundials/sundials_context.h" #include "sundials/sundials_profiler.h" - -typedef struct { - void* cptr; - int cmemflags; -} SwigClassWrapper; - - -SWIGINTERN SwigClassWrapper SwigClassWrapper_uninitialized() { - SwigClassWrapper result; - result.cptr = NULL; - result.cmemflags = 0; - return result; -} - SWIGEXPORT int _wrap_FSUNContext_Create(int const *farg1, void *farg2) { int fresult ; SUNComm arg1 ; @@ -251,47 +230,42 @@ SWIGEXPORT int _wrap_FSUNContext_Create(int const *farg1, void *farg2) { } -SWIGEXPORT int _wrap_FSUNContext_GetLastError(void *farg1, int *farg2) { +SWIGEXPORT int _wrap_FSUNContext_GetLastError(void *farg1) { int fresult ; SUNContext arg1 = (SUNContext) 0 ; - SUNErrCode *arg2 = (SUNErrCode *) 0 ; SUNErrCode result; arg1 = (SUNContext)(farg1); - arg2 = (SUNErrCode *)(farg2); - result = (SUNErrCode)SUNContext_GetLastError(arg1,arg2); + result = (SUNErrCode)SUNContext_GetLastError(arg1); fresult = (SUNErrCode)(result); return fresult; } -SWIGEXPORT int _wrap_FSUNContext_PeekLastError(void *farg1, int *farg2) { +SWIGEXPORT int _wrap_FSUNContext_PeekLastError(void *farg1) { int fresult ; SUNContext arg1 = (SUNContext) 0 ; - SUNErrCode *arg2 = (SUNErrCode *) 0 ; SUNErrCode result; arg1 = (SUNContext)(farg1); - arg2 = (SUNErrCode *)(farg2); - result = (SUNErrCode)SUNContext_PeekLastError(arg1,arg2); + result = (SUNErrCode)SUNContext_PeekLastError(arg1); fresult = (SUNErrCode)(result); return fresult; } -SWIGEXPORT SwigClassWrapper _wrap_FSUNContext_PushErrHandler(void *farg1, SUNErrHandlerFn farg2, void *farg3) { - SwigClassWrapper fresult ; +SWIGEXPORT int _wrap_FSUNContext_PushErrHandler(void *farg1, SUNErrHandlerFn farg2, void *farg3) { + int fresult ; SUNContext arg1 = (SUNContext) 0 ; SUNErrHandlerFn arg2 = (SUNErrHandlerFn) 0 ; void *arg3 = (void *) 0 ; - SUNErrHandler result; + SUNErrCode result; arg1 = (SUNContext)(farg1); arg2 = (SUNErrHandlerFn)(farg2); arg3 = (void *)(farg3); - result = (SUNErrHandler)SUNContext_PushErrHandler(arg1,arg2,arg3); - fresult.cptr = result; - fresult.cmemflags = SWIG_MEM_RVALUE | (0 ? SWIG_MEM_OWN : 0); + result = (SUNErrCode)SUNContext_PushErrHandler(arg1,arg2,arg3); + fresult = (SUNErrCode)(result); return fresult; } @@ -308,13 +282,13 @@ SWIGEXPORT int _wrap_FSUNContext_PopErrHandler(void *farg1) { } -SWIGEXPORT int _wrap_FSUNContext_ClearHandlers(void *farg1) { +SWIGEXPORT int _wrap_FSUNContext_ClearErrHandlers(void *farg1) { int fresult ; SUNContext arg1 = (SUNContext) 0 ; SUNErrCode result; arg1 = (SUNContext)(farg1); - result = (SUNErrCode)SUNContext_ClearHandlers(arg1); + result = (SUNErrCode)SUNContext_ClearErrHandlers(arg1); fresult = (SUNErrCode)(result); return fresult; } diff --git a/src/sundials/fmod/fsundials_context_mod.f90 b/src/sundials/fmod/fsundials_context_mod.f90 index ac93a613b8..8d91bb7eb5 100644 --- a/src/sundials/fmod/fsundials_context_mod.f90 +++ b/src/sundials/fmod/fsundials_context_mod.f90 @@ -29,20 +29,9 @@ module fsundials_context_mod public :: FSUNContext_Create public :: FSUNContext_GetLastError public :: FSUNContext_PeekLastError - - integer, parameter :: swig_cmem_own_bit = 0 - integer, parameter :: swig_cmem_rvalue_bit = 1 - integer, parameter :: swig_cmem_const_bit = 2 - type, bind(C) :: SwigClassWrapper - type(C_PTR), public :: cptr = C_NULL_PTR - integer(C_INT), public :: cmemflags = 0 - end type - type, public :: SWIGTYPE_p_SUNErrHandler_ - type(SwigClassWrapper), public :: swigdata - end type public :: FSUNContext_PushErrHandler public :: FSUNContext_PopErrHandler - public :: FSUNContext_ClearHandlers + public :: FSUNContext_ClearErrHandlers public :: FSUNContext_GetProfiler public :: FSUNContext_SetProfiler public :: FSUNContext_GetLogger @@ -60,21 +49,19 @@ function swigc_FSUNContext_Create(farg1, farg2) & integer(C_INT) :: fresult end function -function swigc_FSUNContext_GetLastError(farg1, farg2) & +function swigc_FSUNContext_GetLastError(farg1) & bind(C, name="_wrap_FSUNContext_GetLastError") & result(fresult) use, intrinsic :: ISO_C_BINDING type(C_PTR), value :: farg1 -type(C_PTR), value :: farg2 integer(C_INT) :: fresult end function -function swigc_FSUNContext_PeekLastError(farg1, farg2) & +function swigc_FSUNContext_PeekLastError(farg1) & bind(C, name="_wrap_FSUNContext_PeekLastError") & result(fresult) use, intrinsic :: ISO_C_BINDING type(C_PTR), value :: farg1 -type(C_PTR), value :: farg2 integer(C_INT) :: fresult end function @@ -82,11 +69,10 @@ function swigc_FSUNContext_PushErrHandler(farg1, farg2, farg3) & bind(C, name="_wrap_FSUNContext_PushErrHandler") & result(fresult) use, intrinsic :: ISO_C_BINDING -import :: swigclasswrapper type(C_PTR), value :: farg1 type(C_FUNPTR), value :: farg2 type(C_PTR), value :: farg3 -type(SwigClassWrapper) :: fresult +integer(C_INT) :: fresult end function function swigc_FSUNContext_PopErrHandler(farg1) & @@ -97,8 +83,8 @@ function swigc_FSUNContext_PopErrHandler(farg1) & integer(C_INT) :: fresult end function -function swigc_FSUNContext_ClearHandlers(farg1) & -bind(C, name="_wrap_FSUNContext_ClearHandlers") & +function swigc_FSUNContext_ClearErrHandlers(farg1) & +bind(C, name="_wrap_FSUNContext_ClearErrHandlers") & result(fresult) use, intrinsic :: ISO_C_BINDING type(C_PTR), value :: farg1 @@ -154,62 +140,56 @@ function swigc_FSUNContext_Free(farg1) & contains ! MODULE SUBPROGRAMS -function FSUNContext_Create(comm, ctx) & +function FSUNContext_Create(comm, sunctx_out) & result(swig_result) use, intrinsic :: ISO_C_BINDING integer(C_INT) :: swig_result integer :: comm -type(C_PTR), target, intent(inout) :: ctx +type(C_PTR), target, intent(inout) :: sunctx_out integer(C_INT) :: fresult integer(C_INT) :: farg1 type(C_PTR) :: farg2 farg1 = int(comm, C_INT) -farg2 = c_loc(ctx) +farg2 = c_loc(sunctx_out) fresult = swigc_FSUNContext_Create(farg1, farg2) swig_result = fresult end function -function FSUNContext_GetLastError(sunctx, last_err) & +function FSUNContext_GetLastError(sunctx) & result(swig_result) use, intrinsic :: ISO_C_BINDING integer(C_INT) :: swig_result type(C_PTR) :: sunctx -integer(C_INT), dimension(*), target, intent(inout) :: last_err integer(C_INT) :: fresult type(C_PTR) :: farg1 -type(C_PTR) :: farg2 farg1 = sunctx -farg2 = c_loc(last_err(1)) -fresult = swigc_FSUNContext_GetLastError(farg1, farg2) +fresult = swigc_FSUNContext_GetLastError(farg1) swig_result = fresult end function -function FSUNContext_PeekLastError(sunctx, last_err) & +function FSUNContext_PeekLastError(sunctx) & result(swig_result) use, intrinsic :: ISO_C_BINDING integer(C_INT) :: swig_result type(C_PTR) :: sunctx -integer(C_INT), dimension(*), target, intent(inout) :: last_err integer(C_INT) :: fresult type(C_PTR) :: farg1 -type(C_PTR) :: farg2 farg1 = sunctx -farg2 = c_loc(last_err(1)) -fresult = swigc_FSUNContext_PeekLastError(farg1, farg2) +fresult = swigc_FSUNContext_PeekLastError(farg1) swig_result = fresult end function function FSUNContext_PushErrHandler(sunctx, err_fn, err_user_data) & result(swig_result) use, intrinsic :: ISO_C_BINDING -type(SWIGTYPE_p_SUNErrHandler_) :: swig_result +integer(C_INT) :: swig_result type(C_PTR) :: sunctx type(C_FUNPTR), intent(in), value :: err_fn type(C_PTR) :: err_user_data -type(SwigClassWrapper) :: fresult +integer(C_INT) :: fresult type(C_PTR) :: farg1 type(C_FUNPTR) :: farg2 type(C_PTR) :: farg3 @@ -218,7 +198,7 @@ function FSUNContext_PushErrHandler(sunctx, err_fn, err_user_data) & farg2 = err_fn farg3 = err_user_data fresult = swigc_FSUNContext_PushErrHandler(farg1, farg2, farg3) -swig_result%swigdata = fresult +swig_result = fresult end function function FSUNContext_PopErrHandler(sunctx) & @@ -234,7 +214,7 @@ function FSUNContext_PopErrHandler(sunctx) & swig_result = fresult end function -function FSUNContext_ClearHandlers(sunctx) & +function FSUNContext_ClearErrHandlers(sunctx) & result(swig_result) use, intrinsic :: ISO_C_BINDING integer(C_INT) :: swig_result @@ -243,7 +223,7 @@ function FSUNContext_ClearHandlers(sunctx) & type(C_PTR) :: farg1 farg1 = sunctx -fresult = swigc_FSUNContext_ClearHandlers(farg1) +fresult = swigc_FSUNContext_ClearErrHandlers(farg1) swig_result = fresult end function diff --git a/src/sundials/fmod/fsundials_errors_mod.c b/src/sundials/fmod/fsundials_errors_mod.c index 1db3054297..c9f46d942b 100644 --- a/src/sundials/fmod/fsundials_errors_mod.c +++ b/src/sundials/fmod/fsundials_errors_mod.c @@ -354,45 +354,4 @@ SWIGEXPORT SwigArrayWrapper _wrap_FSUNGetErrMsg(int const *farg1, SwigClassWrapp } -SWIGEXPORT int _wrap_FSUNGetLastErr(SwigClassWrapper const *farg1) { - int fresult ; - SUNContext arg1 = (SUNContext) 0 ; - SUNErrCode result; - - SWIG_check_mutable(*farg1, "SUNContext", "SWIGTYPE_p_SUNContext_", "SUNGetLastErr(SUNContext)", return 0); - arg1 = (SUNContext)(farg1->cptr); - result = (SUNErrCode)SUNGetLastErr(arg1); - fresult = (SUNErrCode)(result); - return fresult; -} - - -SWIGEXPORT int _wrap_FSUNSetLastErr(int const *farg1, SwigClassWrapper const *farg2) { - int fresult ; - SUNErrCode arg1 ; - SUNContext arg2 = (SUNContext) 0 ; - SUNErrCode result; - - arg1 = (SUNErrCode)(*farg1); - SWIG_check_mutable(*farg2, "SUNContext", "SWIGTYPE_p_SUNContext_", "SUNSetLastErr(SUNErrCode,SUNContext)", return 0); - arg2 = (SUNContext)(farg2->cptr); - result = (SUNErrCode)SUNSetLastErr(arg1,arg2); - fresult = (SUNErrCode)(result); - return fresult; -} - - -SWIGEXPORT int _wrap_FSUNPeekLastErr(SwigClassWrapper const *farg1) { - int fresult ; - SUNContext arg1 = (SUNContext) 0 ; - SUNErrCode result; - - SWIG_check_mutable(*farg1, "SUNContext", "SWIGTYPE_p_SUNContext_", "SUNPeekLastErr(SUNContext)", return 0); - arg1 = (SUNContext)(farg1->cptr); - result = (SUNErrCode)SUNPeekLastErr(arg1); - fresult = (SUNErrCode)(result); - return fresult; -} - - diff --git a/src/sundials/fmod/fsundials_errors_mod.f90 b/src/sundials/fmod/fsundials_errors_mod.f90 index 6b25bbb3d8..0678238568 100644 --- a/src/sundials/fmod/fsundials_errors_mod.f90 +++ b/src/sundials/fmod/fsundials_errors_mod.f90 @@ -26,18 +26,15 @@ module fsundials_errors_mod ! DECLARATION CONSTRUCTS enum, bind(c) - enumerator :: SUN_ERR_MINIMUM = -2000 + 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_LOGGER_CORRUPT - enumerator :: SUN_ERR_LOGGER_CANNOTOPENFILE + enumerator :: SUN_ERR_CORRUPT + enumerator :: SUN_ERR_FILE_OPEN enumerator :: SUN_ERR_MALLOC_FAIL - enumerator :: SUN_ERR_MANYVECTOR_COMMNOTSAME - enumerator :: SUN_ERR_MANYVECTOR_COMMNULL - enumerator :: SUN_ERR_MPI_FAIL enumerator :: SUN_ERR_NOT_IMPLEMENTED enumerator :: SUN_ERR_PROFILER_MAPFULL enumerator :: SUN_ERR_PROFILER_MAPGET @@ -45,16 +42,15 @@ module fsundials_errors_mod enumerator :: SUN_ERR_PROFILER_MAPKEYNOTFOUND enumerator :: SUN_ERR_PROFILER_MAPSORT enumerator :: SUN_ERR_SUNCTX_CORRUPT - enumerator :: SUN_ERR_GENERIC + enumerator :: SUN_ERR_MPI_FAIL 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_LOGGER_CORRUPT, SUN_ERR_LOGGER_CANNOTOPENFILE, SUN_ERR_MALLOC_FAIL, & - SUN_ERR_MANYVECTOR_COMMNOTSAME, SUN_ERR_MANYVECTOR_COMMNULL, SUN_ERR_MPI_FAIL, SUN_ERR_NOT_IMPLEMENTED, & + SUN_ERR_ARG_DIMSMISMATCH, SUN_ERR_CORRUPT, SUN_ERR_FILE_OPEN, SUN_ERR_MALLOC_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_GENERIC, SUN_ERR_UNKNOWN, SUN_ERR_MAXIMUM, SUN_SUCCESS + SUN_ERR_PROFILER_MAPSORT, SUN_ERR_SUNCTX_CORRUPT, SUN_ERR_MPI_FAIL, SUN_ERR_UNKNOWN, SUN_ERR_MAXIMUM, SUN_SUCCESS integer, parameter :: swig_cmem_own_bit = 0 integer, parameter :: swig_cmem_rvalue_bit = 1 @@ -74,9 +70,6 @@ module fsundials_errors_mod public :: FSUNAbortErrHandlerFn public :: FSUNAssertErrHandlerFn public :: FSUNGetErrMsg - public :: FSUNGetLastErr - public :: FSUNSetLastErr - public :: FSUNPeekLastErr ! WRAPPER DECLARATIONS interface @@ -144,34 +137,6 @@ function swigc_FSUNGetErrMsg(farg1, farg2) & type(SwigArrayWrapper) :: fresult end function -function swigc_FSUNGetLastErr(farg1) & -bind(C, name="_wrap_FSUNGetLastErr") & -result(fresult) -use, intrinsic :: ISO_C_BINDING -import :: swigclasswrapper -type(SwigClassWrapper) :: farg1 -integer(C_INT) :: fresult -end function - -function swigc_FSUNSetLastErr(farg1, farg2) & -bind(C, name="_wrap_FSUNSetLastErr") & -result(fresult) -use, intrinsic :: ISO_C_BINDING -import :: swigclasswrapper -integer(C_INT), intent(in) :: farg1 -type(SwigClassWrapper) :: farg2 -integer(C_INT) :: fresult -end function - -function swigc_FSUNPeekLastErr(farg1) & -bind(C, name="_wrap_FSUNPeekLastErr") & -result(fresult) -use, intrinsic :: ISO_C_BINDING -import :: swigclasswrapper -type(SwigClassWrapper) :: farg1 -integer(C_INT) :: fresult -end function - end interface @@ -328,47 +293,5 @@ function FSUNGetErrMsg(code, sunctx) & if (.false.) call SWIG_free(fresult%data) end function -function FSUNGetLastErr(sunctx) & -result(swig_result) -use, intrinsic :: ISO_C_BINDING -integer(C_INT) :: swig_result -class(SWIGTYPE_p_SUNContext_), intent(in) :: sunctx -integer(C_INT) :: fresult -type(SwigClassWrapper) :: farg1 - -farg1 = sunctx%swigdata -fresult = swigc_FSUNGetLastErr(farg1) -swig_result = fresult -end function - -function FSUNSetLastErr(code, sunctx) & -result(swig_result) -use, intrinsic :: ISO_C_BINDING -integer(C_INT) :: swig_result -integer(C_INT), intent(in) :: code -class(SWIGTYPE_p_SUNContext_), intent(in) :: sunctx -integer(C_INT) :: fresult -integer(C_INT) :: farg1 -type(SwigClassWrapper) :: farg2 - -farg1 = code -farg2 = sunctx%swigdata -fresult = swigc_FSUNSetLastErr(farg1, farg2) -swig_result = fresult -end function - -function FSUNPeekLastErr(sunctx) & -result(swig_result) -use, intrinsic :: ISO_C_BINDING -integer(C_INT) :: swig_result -class(SWIGTYPE_p_SUNContext_), intent(in) :: sunctx -integer(C_INT) :: fresult -type(SwigClassWrapper) :: farg1 - -farg1 = sunctx%swigdata -fresult = swigc_FSUNPeekLastErr(farg1) -swig_result = fresult -end function - end module diff --git a/src/sundials/sundials_context.c b/src/sundials/sundials_context.c index 5f3dcc4ebe..880eba58c2 100644 --- a/src/sundials/sundials_context.c +++ b/src/sundials/sundials_context.c @@ -24,6 +24,7 @@ #include #include #include +#include "sundials/sundials_errors.h" #include "sundials/sundials_types.h" #include "sundials_debug.h" @@ -32,13 +33,15 @@ void sunAdiakCollectMetadata(); #endif -int SUNContext_Create(SUNComm comm, SUNContext* sunctx_out) +SUNErrCode SUNContext_Create(SUNComm comm, SUNContext* sunctx_out) { + SUNErrCode err = SUN_SUCCESS; SUNProfiler profiler = NULL; SUNLogger logger = NULL; SUNContext sunctx = NULL; + SUNErrHandler eh = NULL; - sunctx_out = NULL; + *sunctx_out = NULL; sunctx = (SUNContext)malloc(sizeof(struct SUNContext_)); /* SUNContext_Create cannot assert or log since the SUNContext is not yet @@ -54,70 +57,83 @@ int SUNContext_Create(SUNComm comm, SUNContext* sunctx_out) sunAdiakCollectMetadata(); #endif -#if defined(SUNDIALS_BUILD_WITH_PROFILING) && !defined(SUNDIALS_CALIPER_ENABLED) - if (SUNProfiler_Create(comm, "SUNContext Default", &profiler)) return (-1); -#endif - + do { #if SUNDIALS_LOGGING_LEVEL > 0 #if SUNDIALS_MPI_ENABLED - if (SUNLogger_CreateFromEnv(comm, &logger)) { - return SUN_ERR_LOGGER_CORRUPT; - } + err = SUNLogger_CreateFromEnv(comm, &logger); + SUNCheckCallNoRet(err); if (err) { break; } #else - if (SUNLogger_CreateFromEnv(SUN_COMM_NULL, &logger)) { - return SUN_ERR_LOGGER_CORRUPT; - } + err = SUNLogger_CreateFromEnv(SUN_COMM_NULL, &logger); + SUNCheckCallNoRet(err); if (err) { break; } #endif #else - if (SUNLogger_Create(NULL, 0, &logger)) { - return SUN_ERR_LOGGER_CORRUPT; - } - SUNCheckCall(SUNLogger_SetErrorFilename(logger, "")); - SUNCheckCall(SUNLogger_SetWarningFilename(logger, "")); - SUNCheckCall(SUNLogger_SetInfoFilename(logger, "")); - SUNCheckCall(SUNLogger_SetDebugFilename(logger, "")); + err = SUNLogger_Create(SUN_COMM_NULL, 0, &logger); + SUNCheckCallNoRet(err); if (err) { break; } + err = SUNLogger_SetErrorFilename(logger, ""); + SUNCheckCallNoRet(err); if (err) { break; } + err = SUNLogger_SetWarningFilename(logger, ""); + SUNCheckCallNoRet(err); if (err) { break; } + err = SUNLogger_SetInfoFilename(logger, ""); + SUNCheckCallNoRet(err); if (err) { break; } + err = SUNLogger_SetDebugFilename(logger, ""); + SUNCheckCallNoRet(err); if (err) { break; } #endif #if defined(SUNDIALS_BUILD_WITH_PROFILING) && !defined(SUNDIALS_CALIPER_ENABLED) - SUNCheckCall(SUNProfiler_Create(comm, "SUNContext Default", &profiler)); -#endif - - sunctx->logger = logger; - sunctx->own_logger = logger != NULL; - sunctx->profiler = profiler; - sunctx->own_profiler = profiler != NULL; - sunctx->last_err = 0; - sunctx->err_handler = SUNErrHandler_Create(SUNLogErrHandlerFn, NULL); - sunctx->comm = comm; - - *sunctx_out = sunctx; + err = SUNProfiler_Create(comm, "SUNContext Default", &profiler); + SUNCheckCallNoRet(err); if (err) { break; } +#endif + + err = SUNErrHandler_Create(SUNLogErrHandlerFn, NULL, &eh); + SUNCheckCallNoRet(err); if (err) { break; } + + sunctx->logger = logger; + sunctx->own_logger = logger != NULL; + sunctx->profiler = profiler; + sunctx->own_profiler = profiler != NULL; + sunctx->last_err = 0; + sunctx->err_handler = eh; + sunctx->comm = comm; + } while (0); + + if (err) { + SUNCheckCallNoRet(SUNProfiler_Free(&profiler)); + SUNCheckCallNoRet(SUNLogger_Destroy(&logger)); + free(sunctx); + } else { + *sunctx_out = sunctx; + } - return SUN_SUCCESS; + return err; } -SUNErrCode SUNContext_GetLastError(SUNContext sunctx, SUNErrCode* last_err) +SUNErrCode SUNContext_GetLastError(SUNContext sunctx) { - *last_err = sunctx->last_err; - sunctx->last_err = SUN_SUCCESS; - return SUN_SUCCESS; + SUNFunctionBegin(sunctx); + return sunctx->last_err; } -SUNErrCode SUNContext_PeekLastError(SUNContext sunctx, SUNErrCode* last_err) +SUNErrCode SUNContext_PeekLastError(SUNContext sunctx) { - *last_err = sunctx->last_err; - return SUN_SUCCESS; + SUNFunctionBegin(sunctx); + return sunctx->last_err; } -SUNErrHandler SUNContext_PushErrHandler(SUNContext sunctx, SUNErrHandlerFn err_fn, void* err_user_data) +SUNErrCode SUNContext_PushErrHandler(SUNContext sunctx, SUNErrHandlerFn err_fn, void* err_user_data) { - SUNErrHandler new_err_handler = SUNErrHandler_Create(err_fn, err_user_data); + SUNFunctionBegin(sunctx); + SUNErrHandler new_err_handler = NULL; + if (SUNErrHandler_Create(err_fn, err_user_data, &new_err_handler)) { + return SUN_ERR_CORRUPT; + } new_err_handler->previous = sunctx->err_handler; sunctx->err_handler = new_err_handler; - return new_err_handler; + return SUN_SUCCESS; } SUNErrCode SUNContext_PopErrHandler(SUNContext sunctx) { + SUNFunctionBegin(sunctx); if (sunctx->err_handler) { SUNErrHandler eh = sunctx->err_handler; if (sunctx->err_handler->previous) { @@ -125,22 +141,25 @@ SUNErrCode SUNContext_PopErrHandler(SUNContext sunctx) } else { sunctx->err_handler = NULL; } - free(eh); + SUNErrHandler_Destroy(eh); } return SUN_SUCCESS; } -SUNErrCode SUNContext_ClearHandlers(SUNContext sunctx) +SUNErrCode SUNContext_ClearErrHandlers(SUNContext sunctx) { + SUNFunctionBegin(sunctx); while (sunctx->err_handler != NULL) { - SUNContext_PopErrHandler(sunctx); + SUNCheckCall(SUNContext_PopErrHandler(sunctx)); } return SUN_SUCCESS; } SUNErrCode SUNContext_GetProfiler(SUNContext sunctx, SUNProfiler* profiler) { + SUNFunctionBegin(sunctx); + #ifdef SUNDIALS_BUILD_WITH_PROFILING /* get profiler */ *profiler = sunctx->profiler; @@ -153,10 +172,12 @@ SUNErrCode SUNContext_GetProfiler(SUNContext sunctx, SUNProfiler* profiler) SUNErrCode SUNContext_SetProfiler(SUNContext sunctx, SUNProfiler profiler) { + SUNFunctionBegin(sunctx); + #ifdef SUNDIALS_BUILD_WITH_PROFILING /* free any existing profiler */ if (sunctx->profiler && sunctx->own_profiler) { - if (SUNProfiler_Free(&(sunctx->profiler))) return -1; + SUNCheckCall(SUNProfiler_Free(&(sunctx->profiler))); sunctx->profiler = NULL; } @@ -170,17 +191,21 @@ SUNErrCode SUNContext_SetProfiler(SUNContext sunctx, SUNProfiler profiler) SUNErrCode SUNContext_GetLogger(SUNContext sunctx, SUNLogger* logger) { + SUNFunctionBegin(sunctx); + /* get logger */ *logger = sunctx->logger; return SUN_SUCCESS; } SUNErrCode SUNContext_SetLogger(SUNContext sunctx, SUNLogger logger) -{ +{ + SUNFunctionBegin(sunctx); + /* free any existing logger */ if (sunctx->logger && sunctx->own_logger) { if (SUNLogger_Destroy(&(sunctx->logger))) { - return -1; + return SUN_ERR_DESTROY_FAIL; } sunctx->logger = NULL; } @@ -195,8 +220,8 @@ SUNErrCode SUNContext_SetLogger(SUNContext sunctx, SUNLogger logger) SUNErrCode SUNContext_Free(SUNContext* sunctx) { #if defined(SUNDIALS_BUILD_WITH_PROFILING) && !defined(SUNDIALS_CALIPER_ENABLED) - FILE* fp; - char* sunprofiler_print_env; + FILE* fp = NULL; + char* sunprofiler_print_env = NULL; #endif if (!sunctx || !(*sunctx)) { diff --git a/src/sundials/sundials_errors.c b/src/sundials/sundials_errors.c index df7b88dcfa..f410c4dd93 100644 --- a/src/sundials/sundials_errors.c +++ b/src/sundials/sundials_errors.c @@ -10,36 +10,40 @@ * SUNDIALS Copyright End * -----------------------------------------------------------------*/ +#include "sundials/sundials_errors.h" + #include #include -#include #include +#include static inline char* combineFileAndLine(int line, const char* file) { - size_t total_str_len = strlen(file) + 6; /* TODO(CJB): need to figure out width of line */ - char* file_and_line = malloc(total_str_len * sizeof(char)); + size_t total_str_len = strlen(file) + 6; + char* file_and_line = malloc(total_str_len * sizeof(char)); snprintf(file_and_line, total_str_len, "%s:%d", file, line); return file_and_line; } -SUNErrHandler SUNErrHandler_Create(SUNErrHandlerFn eh_fn, void* eh_data) +SUNErrCode SUNErrHandler_Create(SUNErrHandlerFn eh_fn, void* eh_data, + SUNErrHandler* eh_out) { SUNErrHandler eh = NULL; - eh = (SUNErrHandler)malloc(sizeof(struct SUNErrHandler_)); - eh->previous = NULL; - eh->call = eh_fn; - eh->data = eh_data; - return eh; + + eh = (SUNErrHandler)malloc(sizeof(struct SUNErrHandler_)); + if (!eh) { return SUN_ERR_MALLOC_FAIL; } + + eh->previous = NULL; + eh->call = eh_fn; + eh->data = eh_data; + + *eh_out = eh; + return SUN_SUCCESS; } void SUNErrHandler_Destroy(SUNErrHandler eh) { - while (eh != NULL) { - SUNErrHandler next_eh = eh->previous; - free(eh); - eh = next_eh; - } + free(eh); } const char* SUNGetErrMsg(SUNErrCode code, SUNContext sunctx) @@ -47,7 +51,8 @@ const char* SUNGetErrMsg(SUNErrCode code, SUNContext sunctx) #define SUN_EXPAND_TO_CASES(name, description) \ case name: return description; break; - switch (code) { + switch (code) + { SUN_ERR_CODE_LIST(SUN_EXPAND_TO_CASES) default: return "unknown error"; } @@ -55,20 +60,21 @@ const char* SUNGetErrMsg(SUNErrCode code, SUNContext sunctx) return NULL; } -int SUNLogErrHandlerFn(int line, const char* func, const char* file, const char* msg, SUNErrCode err_code, - void* err_ctx, SUNContext sunctx) +int SUNLogErrHandlerFn(int line, const char* func, const char* file, + const char* msg, SUNErrCode err_code, void* err_user_data, + SUNContext sunctx) { char* file_and_line = combineFileAndLine(line, file); - if (msg == NULL) { - msg = SUNGetErrMsg(err_code, sunctx); - } - SUNLogger_QueueMsg(sunctx->logger, SUN_LOGLEVEL_ERROR, file_and_line, func, msg); + if (msg == NULL) { msg = SUNGetErrMsg(err_code, sunctx); } + SUNLogger_QueueMsg(sunctx->logger, SUN_LOGLEVEL_ERROR, file_and_line, func, + msg); free(file_and_line); return 0; } -int SUNAbortErrHandlerFn(int line, const char* func, const char* file, const char* msg, SUNErrCode err_code, - void* err_ctx, SUNContext sunctx) +int SUNAbortErrHandlerFn(int line, const char* func, const char* file, + const char* msg, SUNErrCode err_code, void* err_user_data, + SUNContext sunctx) { char* file_and_line = combineFileAndLine(line, file); SUNLogger_QueueMsg(sunctx->logger, SUN_LOGLEVEL_ERROR, file_and_line, func, @@ -79,12 +85,14 @@ int SUNAbortErrHandlerFn(int line, const char* func, const char* file, const cha return 0; } -int SUNAssertErrHandlerFn(int line, const char* func, const char* file, const char* stmt, SUNErrCode err_code, - void* err_ctx, SUNContext sunctx) +int SUNAssertErrHandlerFn(int line, const char* func, const char* file, + const char* stmt, SUNErrCode err_code, void* err_user_data, + SUNContext sunctx) { char* file_and_line = combineFileAndLine(line, file); SUNLogger_QueueMsg(sunctx->logger, SUN_LOGLEVEL_ERROR, file_and_line, func, - "SUNAssertErrHandler: assert(%s) failed... terminating\n", stmt); + "SUNAssertErrHandler: assert(%s) failed... terminating\n", + stmt); free(file_and_line); abort(); return 0; diff --git a/src/sundials/sundials_logger.c b/src/sundials/sundials_logger.c index 887425a861..2f6f245601 100644 --- a/src/sundials/sundials_logger.c +++ b/src/sundials/sundials_logger.c @@ -216,7 +216,7 @@ SUNErrCode SUNLogger_CreateFromEnv(SUNComm comm, SUNLogger* logger) retval += SUNLogger_SetDebugFilename(*logger, debug_fname_env); retval += SUNLogger_SetInfoFilename(*logger, info_fname_env); - return (retval < 0) ? SUN_ERR_LOGGER_CORRUPT : SUN_SUCCESS; + return (retval < 0) ? SUN_ERR_CORRUPT : SUN_SUCCESS; } SUNErrCode SUNLogger_SetErrorFilename(SUNLogger logger, const char* error_filename) @@ -241,7 +241,7 @@ SUNErrCode SUNLogger_SetErrorFilename(SUNLogger logger, const char* error_filena } else { - return SUN_ERR_LOGGER_CANNOTOPENFILE; + return SUN_ERR_FILE_OPEN; } } #endif @@ -272,7 +272,7 @@ SUNErrCode SUNLogger_SetWarningFilename(SUNLogger logger, const char* warning_fi } else { - return SUN_ERR_LOGGER_CANNOTOPENFILE; + return SUN_ERR_FILE_OPEN; } } #endif @@ -303,7 +303,7 @@ SUNErrCode SUNLogger_SetInfoFilename(SUNLogger logger, const char* info_filename } else { - return SUN_ERR_LOGGER_CANNOTOPENFILE; + return SUN_ERR_FILE_OPEN; } } #endif @@ -334,7 +334,7 @@ SUNErrCode SUNLogger_SetDebugFilename(SUNLogger logger, const char* debug_filena } else { - return SUN_ERR_LOGGER_CANNOTOPENFILE; + return SUN_ERR_FILE_OPEN; } } #endif diff --git a/src/sundials/sundials_nvector.c b/src/sundials/sundials_nvector.c index 94cf9e3826..646e893da2 100644 --- a/src/sundials/sundials_nvector.c +++ b/src/sundials/sundials_nvector.c @@ -1003,7 +1003,7 @@ N_Vector* N_VCloneEmptyVectorArray(int count, N_Vector w) for (j = 0; j < count; j++) { vs[j] = N_VCloneEmpty(w); SUNCheckLastErrNoRet(); - if (SUNGetLastErr(w->sunctx) < 0) { + if (SUNContext_GetLastError(w->sunctx) < 0) { N_VDestroyVectorArray(vs, j-1); return(NULL); } @@ -1025,7 +1025,7 @@ N_Vector* N_VCloneVectorArray(int count, N_Vector w) for (j = 0; j < count; j++) { vs[j] = N_VClone(w); SUNCheckLastErrNoRet(); - if (SUNGetLastErr(w->sunctx) < 0) { + if (SUNContext_GetLastError(w->sunctx) < 0) { N_VDestroyVectorArray(vs, j-1); return(NULL); } diff --git a/test/unit_tests/sundials/test_sundials_errors.cpp b/test/unit_tests/sundials/test_sundials_errors.cpp index 0a1ea25ce3..df96d86fe9 100644 --- a/test/unit_tests/sundials/test_sundials_errors.cpp +++ b/test/unit_tests/sundials/test_sundials_errors.cpp @@ -92,7 +92,7 @@ int thirdHandler(int line, const char *func, const char *file, const char *msg, TEST_F(SUNContextErrFunctionTests, SUNContextPushErrHandlerWorks) { std::vector order = {}; - SUNContext_ClearHandlers(sunctx); + SUNContext_ClearErrHandlers(sunctx); SUNContext_PushErrHandler(sunctx, firstHandler, static_cast(&order)); SUNContext_PushErrHandler(sunctx, secondHandler, static_cast(&order)); SUNContext_PushErrHandler(sunctx, thirdHandler, static_cast(&order)); @@ -106,7 +106,7 @@ TEST_F(SUNContextErrFunctionTests, SUNContextPushErrHandlerWorks) TEST_F(SUNContextErrFunctionTests, SUNContextPopErrHandlerWorks) { std::vector order = {}; - SUNContext_ClearHandlers(sunctx); + SUNContext_ClearErrHandlers(sunctx); SUNContext_PushErrHandler(sunctx, firstHandler, static_cast(&order)); SUNContext_PushErrHandler(sunctx, secondHandler, static_cast(&order)); SUNContext_PushErrHandler(sunctx, thirdHandler, static_cast(&order));