Skip to content

Commit

Permalink
address another batch of comments
Browse files Browse the repository at this point in the history
  • Loading branch information
balos1 committed Nov 28, 2023
1 parent 3214dba commit 0ce9564
Show file tree
Hide file tree
Showing 19 changed files with 273 additions and 412 deletions.
8 changes: 4 additions & 4 deletions examples/cvode/serial/cvAdvDiff_bnd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand Down
134 changes: 73 additions & 61 deletions include/sundials/impl/sundials_errors_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions include/sundials/sundials_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion include/sundials/sundials_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <sundials/sundials_config.h>
#include <sundials/sundials_context.h>
#include <sundials/sundials_direct.h>
#include <sundials/sundials_errors.h>
#include <sundials/sundials_iterative.h>
#include <sundials/sundials_logger.h>
Expand Down
1 change: 1 addition & 0 deletions include/sundials/sundials_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <sundials/sundials_core.h>
#include <sundials/sundials_context.hpp>
#include <sundials/sundials_memory.hpp>
#include <sundials/sundials_linearsolver.hpp>
#include <sundials/sundials_matrix.hpp>
#include <sundials/sundials_nonlinearsolver.hpp>
Expand Down
56 changes: 19 additions & 37 deletions include/sundials/sundials_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#include <sundials/sundials_context.h>
#include <sundials/sundials_types.h>

Expand All @@ -35,41 +34,47 @@
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") \
ENTRY(SUN_ERR_PROFILER_MAPINSERT, \
"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
* ---------------------------------------------------------------------------*/
Expand All @@ -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);

/* ----------------------------------------------------------------------------
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions include/sundials/sundials_linearsolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 0ce9564

Please sign in to comment.