diff --git a/doc/superbuild/source/developers/getting_started/index.rst b/doc/superbuild/source/developers/getting_started/index.rst index 2eb21fb074..f776349278 100644 --- a/doc/superbuild/source/developers/getting_started/index.rst +++ b/doc/superbuild/source/developers/getting_started/index.rst @@ -20,7 +20,7 @@ Getting Started SUNDIALS uses Git for distributed version control. If you have not setup Git on your system, see the :ref:`GitSetup` section for details on configuring Git and cloning the SUNDIALS repository. A list of helpful commands can be found in the -:ref:`GitCheatSheet`. The typical evelopment workflow is described in the +:ref:`GitCheatSheet`. The typical development workflow is described in the :ref:`Workflow` section. .. toctree:: diff --git a/doc/superbuild/source/developers/style_guide/SourceCode.rst b/doc/superbuild/source/developers/style_guide/SourceCode.rst index a86a21cdd7..9f594e20dd 100644 --- a/doc/superbuild/source/developers/style_guide/SourceCode.rst +++ b/doc/superbuild/source/developers/style_guide/SourceCode.rst @@ -262,7 +262,7 @@ Coding Conventions and Rules are the ``SUNLogger`` and ``SUNProfiler`` classes. #. All SUNDIALS functions should return a ``SUNErrCode``. Many older functions - do not do this and are exceptions to the rule for backwards compatiblilty. + do not do this and are exceptions to the rule for backwards compatibility. In addition, internal helper functions may or may-not return a ``SUNErrCode``. #. All SUNDIALS functions, with the exception of some functions @@ -385,7 +385,7 @@ Coding Conventions and Rules #. If statements and loops should always have braces even if they are one line. -#. Return statements should not unecessarily use parentheses. Prefer ``return +#. Return statements should not unnecessarily use parentheses. Prefer ``return x;`` to ``return(x);``. Note, however, lots of older SUNDIALS source code uses ``return(x);``. diff --git a/src/nvector/manyvector/nvector_manyvector.c b/src/nvector/manyvector/nvector_manyvector.c index f16622dcbd..a11f085d57 100644 --- a/src/nvector/manyvector/nvector_manyvector.c +++ b/src/nvector/manyvector/nvector_manyvector.c @@ -15,6 +15,7 @@ * of the NVECTOR package. * -----------------------------------------------------------------*/ +#include #include #include @@ -268,8 +269,14 @@ N_Vector N_VNew_MPIManyVector(sunindextype num_subvectors, N_Vector* vec_array, else { SUNCheckMPICallNoRet(MPI_Comm_compare(vcomm, comm, &comparison)); - SUNCheckNull((comparison == MPI_IDENT) || (comparison == MPI_CONGRUENT), - SUN_ERR_ARG_INCOMPATIBLE); + if ((comparison != MPI_IDENT) && (comparison != MPI_CONGRUENT)) + { + SUNHandleErrWithMsg(__LINE__, __func__, __FILE__, + "All subvectors must have the same communicator, " + "i.e., MPI_Comm_compare must return MPI_IDENT or " + "MPI_CONGRUENT.", + SUN_ERR_ARG_INCOMPATIBLE, SUNCTX_); + } } } @@ -398,6 +405,7 @@ N_Vector N_VNew_ManyVector(sunindextype num_subvectors, N_Vector* vec_array, local_length = 0; for (i = 0; i < num_subvectors; i++) { + SUNAssertNull(vec_array[i]->ops->nvgetlength, SUN_ERR_NOT_IMPLEMENTED); local_length += N_VGetLength(vec_array[i]); SUNCheckLastErrNull(); } @@ -413,7 +421,7 @@ N_Vector MVAPPEND(N_VGetSubvector)(N_Vector v, sunindextype vec_num) { SUNFunctionBegin(v->sunctx); SUNAssertNull(vec_num >= 0, SUN_ERR_ARG_OUTOFRANGE); - SUNAssertNull(vec_num <= MANYVECTOR_NUM_SUBVECS(v), SUN_ERR_ARG_OUTOFRANGE); + SUNAssertNull(vec_num < MANYVECTOR_NUM_SUBVECS(v), SUN_ERR_ARG_OUTOFRANGE); return (MANYVECTOR_SUBVEC(v, vec_num)); } @@ -426,7 +434,7 @@ sunrealtype* MVAPPEND(N_VGetSubvectorArrayPointer)(N_Vector v, { SUNFunctionBegin(v->sunctx); SUNAssertNull(vec_num >= 0, SUN_ERR_ARG_OUTOFRANGE); - SUNAssertNull(vec_num <= MANYVECTOR_NUM_SUBVECS(v), SUN_ERR_ARG_OUTOFRANGE); + SUNAssertNull(vec_num < MANYVECTOR_NUM_SUBVECS(v), SUN_ERR_ARG_OUTOFRANGE); sunrealtype* arr = NULL; if (MANYVECTOR_SUBVEC(v, vec_num)->ops->nvgetarraypointer) { @@ -445,7 +453,7 @@ SUNErrCode MVAPPEND(N_VSetSubvectorArrayPointer)(sunrealtype* v_data, N_Vector v { SUNFunctionBegin(v->sunctx); SUNAssert(vec_num >= 0, SUN_ERR_ARG_OUTOFRANGE); - SUNAssert(vec_num <= MANYVECTOR_NUM_SUBVECS(v), SUN_ERR_ARG_OUTOFRANGE); + SUNAssert(vec_num < MANYVECTOR_NUM_SUBVECS(v), SUN_ERR_ARG_OUTOFRANGE); N_VSetArrayPointer(v_data, MANYVECTOR_SUBVEC(v, vec_num)); SUNCheckLastErr(); return SUN_SUCCESS; @@ -600,7 +608,12 @@ sunindextype MVAPPEND(N_VGetLength)(N_Vector v) sunindextype MVAPPEND(N_VGetSubvectorLocalLength)(N_Vector v, sunindextype vec_num) { - return (N_VGetLocalLength(MVAPPEND(N_VGetSubvector)(v, vec_num))); + SUNFunctionBegin(v->sunctx); + N_Vector subvector = MVAPPEND(N_VGetSubvector)(v, vec_num); + SUNCheckLastErrNoRet(); + sunindextype loc_length = N_VGetLocalLength(subvector); + SUNCheckLastErrNoRet(); + return loc_length; } /* Performs the linear sum z = a*x + b*y by calling N_VLinearSum on all subvectors; @@ -1715,7 +1728,6 @@ SUNErrCode MVAPPEND(N_VWrmsNormVectorArray)(int nvec, N_Vector* X, N_Vector* W, { SUNFunctionBegin(X[0]->sunctx); sunindextype i; - int retval; SUNAssert(nvec > 0, SUN_ERR_ARG_OUTOFRANGE); @@ -1727,7 +1739,6 @@ SUNErrCode MVAPPEND(N_VWrmsNormVectorArray)(int nvec, N_Vector* X, N_Vector* W, } /* accumulate totals */ - retval = 0; #ifdef MANYVECTOR_BUILD_WITH_MPI if (MANYVECTOR_COMM(X[0]) != MPI_COMM_NULL) { @@ -1742,7 +1753,7 @@ SUNErrCode MVAPPEND(N_VWrmsNormVectorArray)(int nvec, N_Vector* X, N_Vector* W, nrm[i] = SUNRsqrt(nrm[i] / (MANYVECTOR_GLOBLENGTH(X[i]))); } - return (retval); + return SUN_SUCCESS; } /* Performs the WrmsNormMaskVectorArray operation by calling N_VWSqrSumMaskLocal and @@ -1759,8 +1770,8 @@ SUNErrCode MVAPPEND(N_VWrmsNormMaskVectorArray)(int nvec, N_Vector* X, sunrealtype* nrm) { SUNFunctionBegin(X[0]->sunctx); + sunindextype i; - int retval; SUNAssert(nvec > 0, SUN_ERR_ARG_OUTOFRANGE); @@ -1772,7 +1783,6 @@ SUNErrCode MVAPPEND(N_VWrmsNormMaskVectorArray)(int nvec, N_Vector* X, } /* accumulate totals */ - retval = 0; #ifdef MANYVECTOR_BUILD_WITH_MPI if (MANYVECTOR_COMM(X[0]) != MPI_COMM_NULL) { @@ -1787,7 +1797,7 @@ SUNErrCode MVAPPEND(N_VWrmsNormMaskVectorArray)(int nvec, N_Vector* X, nrm[i] = SUNRsqrt(nrm[i] / (MANYVECTOR_GLOBLENGTH(X[i]))); } - return (retval); + return SUN_SUCCESS; } /* Performs the BufSize operation by calling N_VBufSize for each subvector and @@ -1823,7 +1833,6 @@ SUNErrCode MVAPPEND(N_VBufPack)(N_Vector x, void* buf) sunindextype offset; /* subvector buffer offset */ sunindextype i; - SUNAssert(x, SUN_ERR_ARG_CORRUPT); SUNAssert(buf, SUN_ERR_ARG_CORRUPT); /* start at the beginning of the output buffer */ @@ -1854,7 +1863,6 @@ SUNErrCode MVAPPEND(N_VBufUnpack)(N_Vector x, void* buf) sunindextype offset; /* subvector buffer offset */ sunindextype i; - SUNAssert(x, SUN_ERR_ARG_CORRUPT); SUNAssert(buf, SUN_ERR_ARG_CORRUPT); /* start at the beginning of the input buffer */ @@ -2101,12 +2109,19 @@ static N_Vector ManyVectorClone(N_Vector w, sunbooleantype cloneempty) returns 0. If an error occurs in the call to MPI_Comm_Rank, it returns -1. */ static int SubvectorMPIRank(N_Vector x) { - int rank = 0; SUNFunctionBegin(x->sunctx); + + int rank = 0; MPI_Comm comm = N_VGetCommunicator(x); SUNCheckLastErrNoRet(); - if (comm == MPI_COMM_NULL) { return SUN_SUCCESS; } - SUNCheckMPICallNoRet(MPI_Comm_rank(comm, &rank)); + + if (comm != MPI_COMM_NULL) + { + int status = MPI_Comm_rank(comm, &rank); + SUNCheckMPICallNoRet(status); + if (status != MPI_SUCCESS) { return -1; } + } + return rank; } #endif diff --git a/src/nvector/mpiplusx/nvector_mpiplusx.c b/src/nvector/mpiplusx/nvector_mpiplusx.c index cb1e753f26..edd1d90db5 100644 --- a/src/nvector/mpiplusx/nvector_mpiplusx.c +++ b/src/nvector/mpiplusx/nvector_mpiplusx.c @@ -80,7 +80,9 @@ N_Vector N_VGetLocalVector_MPIPlusX(N_Vector v) sunindextype N_VGetLocalLength_MPIPlusX(N_Vector v) { SUNFunctionBegin(v->sunctx); - sunindextype len = N_VGetLength(N_VGetLocalVector_MPIPlusX(v)); + N_Vector local_vector = N_VGetLocalVector_MPIPlusX(v); + SUNCheckLastErrNoRet(); + sunindextype len = N_VGetLength(local_vector); SUNCheckLastErrNoRet(); return len; } diff --git a/src/nvector/openmp/nvector_openmp.c b/src/nvector/openmp/nvector_openmp.c index 456b173365..63e693688c 100644 --- a/src/nvector/openmp/nvector_openmp.c +++ b/src/nvector/openmp/nvector_openmp.c @@ -196,12 +196,15 @@ N_Vector N_VNew_OpenMP(sunindextype length, int num_threads, SUNContext sunctx) /* Create data */ data = NULL; - data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); - SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); + if (length > 0) + { + data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); + SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); - /* Attach data */ - NV_OWN_DATA_OMP(v) = SUNTRUE; - NV_DATA_OMP(v) = data; + /* Attach data */ + NV_OWN_DATA_OMP(v) = SUNTRUE; + NV_DATA_OMP(v) = data; + } return (v); } @@ -222,9 +225,12 @@ N_Vector N_VMake_OpenMP(sunindextype length, sunrealtype* v_data, v = N_VNewEmpty_OpenMP(length, num_threads, sunctx); SUNCheckLastErrNull(); - /* Attach data */ - NV_OWN_DATA_OMP(v) = SUNFALSE; - NV_DATA_OMP(v) = v_data; + if (length > 0) + { + /* Attach data */ + NV_OWN_DATA_OMP(v) = SUNFALSE; + NV_DATA_OMP(v) = v_data; + } return (v); } @@ -342,8 +348,11 @@ N_Vector N_VClone_OpenMP(N_Vector w) /* Create data */ data = NULL; - data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); - SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); + if (length > 0) + { + data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); + SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); + } /* Attach data */ NV_OWN_DATA_OMP(v) = SUNTRUE; @@ -759,7 +768,10 @@ sunrealtype N_VMaxNorm_OpenMP(N_Vector x) sunrealtype N_VWrmsNorm_OpenMP(N_Vector x, N_Vector w) { - return (SUNRsqrt(N_VWSqrSumLocal_OpenMP(x, w) / (NV_LENGTH_OMP(x)))); + SUNFunctionBegin(x->sunctx); + sunrealtype sqr_sum = N_VWSqrSumLocal_OpenMP(x, w); + SUNCheckLastErrNoRet(); + return (SUNRsqrt(sqr_sum / (NV_LENGTH_OMP(x)))); } /* ---------------------------------------------------------------------------- @@ -768,11 +780,14 @@ sunrealtype N_VWrmsNorm_OpenMP(N_Vector x, N_Vector w) sunrealtype N_VWrmsNormMask_OpenMP(N_Vector x, N_Vector w, N_Vector id) { - return (SUNRsqrt(N_VWSqrSumMaskLocal_OpenMP(x, w, id) / (NV_LENGTH_OMP(x)))); + SUNFunctionBegin(x->sunctx); + sunrealtype sqr_sum = N_VWSqrSumMaskLocal_OpenMP(x, w, id); + SUNCheckLastErrNoRet(); + return (SUNRsqrt(sqr_sum / (NV_LENGTH_OMP(x)))); } /* ---------------------------------------------------------------------------- - * Finds the minimun component of a vector + * Finds the minimum component of a vector */ sunrealtype N_VMin_OpenMP(N_Vector x) @@ -1621,7 +1636,6 @@ SUNErrCode N_VScaleAddMultiVectorArray_OpenMP(int nvec, int nsum, sunrealtype* yd = NULL; sunrealtype* zd = NULL; - int retval; N_Vector* YY; N_Vector* ZZ; @@ -1647,7 +1661,9 @@ SUNErrCode N_VScaleAddMultiVectorArray_OpenMP(int nvec, int nsum, /* should have called N_VScaleAddMulti */ YY = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(YY, SUN_ERR_MALLOC_FAIL); ZZ = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(ZZ, SUN_ERR_MALLOC_FAIL); for (j = 0; j < nsum; j++) { @@ -1655,11 +1671,11 @@ SUNErrCode N_VScaleAddMultiVectorArray_OpenMP(int nvec, int nsum, ZZ[j] = Z[j][0]; } - retval = N_VScaleAddMulti_OpenMP(nsum, a, X[0], YY, ZZ); + SUNCheckCall(N_VScaleAddMulti_OpenMP(nsum, a, X[0], YY, ZZ)); free(YY); free(ZZ); - return (retval); + return SUN_SUCCESS; } /* -------------------------- @@ -1669,8 +1685,8 @@ SUNErrCode N_VScaleAddMultiVectorArray_OpenMP(int nvec, int nsum, /* should have called N_VLinearSumVectorArray */ if (nsum == 1) { - retval = N_VLinearSumVectorArray_OpenMP(nvec, a[0], X, ONE, Y[0], Z[0]); - return (retval); + SUNCheckCall(N_VLinearSumVectorArray_OpenMP(nvec, a[0], X, ONE, Y[0], Z[0])); + return SUN_SUCCESS; } /* ---------------------------- @@ -1790,8 +1806,7 @@ SUNErrCode N_VLinearCombinationVectorArray_OpenMP(int nvec, int nsum, for (j = 0; j < nvec; j++) { ctmp[j] = c[0]; } - N_VScaleVectorArray_OpenMP(nvec, ctmp, X[0], Z); - SUNCheckLastErr(); + SUNCheckCall(N_VScaleVectorArray_OpenMP(nvec, ctmp, X[0], Z)); free(ctmp); return SUN_SUCCESS; @@ -1800,8 +1815,7 @@ SUNErrCode N_VLinearCombinationVectorArray_OpenMP(int nvec, int nsum, /* should have called N_VLinearSumVectorArray */ if (nsum == 2) { - N_VLinearSumVectorArray_OpenMP(nvec, c[0], X[0], c[1], X[1], Z); - SUNCheckLastErr(); + SUNCheckCall(N_VLinearSumVectorArray_OpenMP(nvec, c[0], X[0], c[1], X[1], Z)); return SUN_SUCCESS; } diff --git a/src/nvector/parallel/nvector_parallel.c b/src/nvector/parallel/nvector_parallel.c index f2cb70cef9..4289036d5a 100644 --- a/src/nvector/parallel/nvector_parallel.c +++ b/src/nvector/parallel/nvector_parallel.c @@ -47,20 +47,21 @@ static void Vaxpy_Parallel(sunrealtype a, N_Vector x, N_Vector y); /* y <- ax+y static void VScaleBy_Parallel(sunrealtype a, N_Vector x); /* x <- ax */ /* Private functions for special cases of vector array operations */ -static int VSumVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, - N_Vector* Z); /* Z=X+Y */ -static int VDiffVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, - N_Vector* Z); /* Z=X-Y */ -static int VScaleSumVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, - N_Vector* Y, N_Vector* Z); /* Z=c(X+Y) */ -static int VScaleDiffVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, - N_Vector* Y, N_Vector* Z); /* Z=c(X-Y) */ -static int VLin1VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, - N_Vector* Y, N_Vector* Z); /* Z=aX+Y */ -static int VLin2VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, - N_Vector* Y, N_Vector* Z); /* Z=aX-Y */ -static int VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, - N_Vector* Y); /* Y <- aX+Y */ +static void VSumVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, + N_Vector* Z); /* Z=X+Y */ +static void VDiffVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, + N_Vector* Z); /* Z=X-Y */ +static void VScaleSumVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, + N_Vector* Y, N_Vector* Z); /* Z=c(X+Y) */ +static void VScaleDiffVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, + N_Vector* Y, + N_Vector* Z); /* Z=c(X-Y) */ +static void VLin1VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, + N_Vector* Y, N_Vector* Z); /* Z=aX+Y */ +static void VLin2VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, + N_Vector* Y, N_Vector* Z); /* Z=aX-Y */ +static void VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, + N_Vector* Y); /* Y <- aX+Y */ /* * ----------------------------------------------------------------- @@ -87,7 +88,13 @@ N_Vector N_VNewEmpty_Parallel(MPI_Comm comm, sunindextype local_length, n = local_length; SUNCheckMPICallNull( MPI_Allreduce(&n, &Nsum, 1, MPI_SUNINDEXTYPE, MPI_SUM, comm)); - SUNCheckNull(Nsum == global_length, SUN_ERR_ARG_INCOMPATIBLE); + if (Nsum != global_length) + { + SUNHandleErrWithMsg(__LINE__, __func__, + __FILE__, "global_length does not equal the computed global length", + SUN_ERR_ARG_INCOMPATIBLE, SUNCTX_); + return NULL; + } /* Create an empty vector object */ v = NULL; @@ -235,19 +242,6 @@ N_Vector N_VMake_Parallel(MPI_Comm comm, sunindextype local_length, return (v); } -/* ---------------------------------------------------------------- - * Function to create an array of new parallel vectors. - */ - -N_Vector* N_VCloneVectorArray_Parallel(int count, N_Vector w) -{ - SUNFunctionBegin(w->sunctx); - SUNAssertNull(count > 0, SUN_ERR_ARG_OUTOFRANGE); - N_Vector* arr = N_VCloneVectorArray(count, w); - SUNCheckLastErrNull(); - return arr; -} - /* ---------------------------------------------------------------- * Function to return global vector length */ @@ -316,8 +310,7 @@ N_Vector N_VCloneEmpty_Parallel(N_Vector w) SUNCheckLastErrNull(); /* Attach operations */ - N_VCopyOps(w, v); - SUNCheckLastErrNull(); + SUNCheckCallNull(N_VCopyOps(w, v)); /* Create content */ content = NULL; @@ -396,11 +389,16 @@ void N_VDestroy_Parallel(N_Vector v) void N_VSpace_Parallel(N_Vector v, sunindextype* lrw, sunindextype* liw) { + SUNFunctionBegin(v->sunctx); + MPI_Comm comm; int npes; + SUNAssertVoid(lrw, SUN_ERR_ARG_CORRUPT); + SUNAssertVoid(liw, SUN_ERR_ARG_CORRUPT); + comm = NV_COMM_P(v); - MPI_Comm_size(comm, &npes); + SUNCheckMPICallVoid(MPI_Comm_size(comm, &npes)); *lrw = NV_GLOBLENGTH_P(v); *liw = 2 * npes; @@ -1224,19 +1222,22 @@ SUNErrCode N_VLinearSumVectorArray_Parallel(int nvec, sunrealtype a, /* BLAS usage: axpy y <- ax+y */ if ((b == ONE) && (Z == Y)) { - return (VaxpyVectorArray_Parallel(nvec, a, X, Y)); + VaxpyVectorArray_Parallel(nvec, a, X, Y); + return SUN_SUCCESS; } /* BLAS usage: axpy x <- by+x */ if ((a == ONE) && (Z == X)) { - return (VaxpyVectorArray_Parallel(nvec, b, Y, X)); + VaxpyVectorArray_Parallel(nvec, b, Y, X); + return SUN_SUCCESS; } /* Case: a == b == 1.0 */ if ((a == ONE) && (b == ONE)) { - return (VSumVectorArray_Parallel(nvec, X, Y, Z)); + VSumVectorArray_Parallel(nvec, X, Y, Z); + return SUN_SUCCESS; } /* Cases: */ @@ -1246,7 +1247,8 @@ SUNErrCode N_VLinearSumVectorArray_Parallel(int nvec, sunrealtype a, { V1 = test ? Y : X; V2 = test ? X : Y; - return (VDiffVectorArray_Parallel(nvec, V2, V1, Z)); + VDiffVectorArray_Parallel(nvec, V2, V1, Z); + return SUN_SUCCESS; } /* Cases: */ @@ -1258,7 +1260,8 @@ SUNErrCode N_VLinearSumVectorArray_Parallel(int nvec, sunrealtype a, c = test ? b : a; V1 = test ? Y : X; V2 = test ? X : Y; - return (VLin1VectorArray_Parallel(nvec, c, V1, V2, Z)); + VLin1VectorArray_Parallel(nvec, c, V1, V2, Z); + return SUN_SUCCESS; } /* Cases: */ @@ -1269,15 +1272,24 @@ SUNErrCode N_VLinearSumVectorArray_Parallel(int nvec, sunrealtype a, c = test ? b : a; V1 = test ? Y : X; V2 = test ? X : Y; - return (VLin2VectorArray_Parallel(nvec, c, V1, V2, Z)); + VLin2VectorArray_Parallel(nvec, c, V1, V2, Z); + return SUN_SUCCESS; } /* Case: a == b */ /* catches case both a and b are 0.0 - user should have called N_VConst */ - if (a == b) { return (VScaleSumVectorArray_Parallel(nvec, a, X, Y, Z)); } + if (a == b) + { + VScaleSumVectorArray_Parallel(nvec, a, X, Y, Z); + return SUN_SUCCESS; + } /* Case: a == -b */ - if (a == -b) { return (VScaleDiffVectorArray_Parallel(nvec, a, X, Y, Z)); } + if (a == -b) + { + VScaleDiffVectorArray_Parallel(nvec, a, X, Y, Z); + return SUN_SUCCESS; + } /* Do all cases not handled above: */ /* (1) a == other, b == 0.0 - user should have called N_VScale */ @@ -1628,6 +1640,7 @@ SUNErrCode N_VLinearCombinationVectorArray_Parallel(int nvec, int nsum, if (nsum == 1) { ctmp = (sunrealtype*)malloc(nvec * sizeof(sunrealtype)); + SUNAssert(ctmp, SUN_ERR_MALLOC_FAIL); for (j = 0; j < nvec; j++) { ctmp[j] = c[0]; } @@ -1712,7 +1725,6 @@ SUNErrCode N_VLinearCombinationVectorArray_Parallel(int nvec, int nsum, SUNErrCode N_VBufSize_Parallel(N_Vector x, sunindextype* size) { - if (x == NULL) { return (-1); } *size = NV_LOCLENGTH_P(x) * ((sunindextype)sizeof(sunrealtype)); return SUN_SUCCESS; } @@ -1724,7 +1736,7 @@ SUNErrCode N_VBufPack_Parallel(N_Vector x, void* buf) sunrealtype* xd = NULL; sunrealtype* bd = NULL; - if (x == NULL || buf == NULL) { return (-1); } + SUNAssert(buf, SUN_ERR_ARG_CORRUPT); N = NV_LOCLENGTH_P(x); xd = NV_DATA_P(x); @@ -1742,7 +1754,7 @@ SUNErrCode N_VBufUnpack_Parallel(N_Vector x, void* buf) sunrealtype* xd = NULL; sunrealtype* bd = NULL; - if (x == NULL || buf == NULL) { return (-1); } + SUNAssert(buf, SUN_ERR_ARG_CORRUPT); N = NV_LOCLENGTH_P(x); xd = NV_DATA_P(x); @@ -1942,8 +1954,8 @@ static void VScaleBy_Parallel(sunrealtype a, N_Vector x) * ----------------------------------------------------------------- */ -static int VSumVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, - N_Vector* Z) +static void VSumVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, + N_Vector* Z) { int i; sunindextype j, N; @@ -1960,12 +1972,10 @@ static int VSumVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, zd = NV_DATA_P(Z[i]); for (j = 0; j < N; j++) { zd[j] = xd[j] + yd[j]; } } - - return SUN_SUCCESS; } -static int VDiffVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, - N_Vector* Z) +static void VDiffVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, + N_Vector* Z) { int i; sunindextype j, N; @@ -1982,12 +1992,10 @@ static int VDiffVectorArray_Parallel(int nvec, N_Vector* X, N_Vector* Y, zd = NV_DATA_P(Z[i]); for (j = 0; j < N; j++) { zd[j] = xd[j] - yd[j]; } } - - return SUN_SUCCESS; } -static int VScaleSumVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, - N_Vector* Y, N_Vector* Z) +static void VScaleSumVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, + N_Vector* Y, N_Vector* Z) { int i; sunindextype j, N; @@ -2004,12 +2012,10 @@ static int VScaleSumVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, zd = NV_DATA_P(Z[i]); for (j = 0; j < N; j++) { zd[j] = c * (xd[j] + yd[j]); } } - - return SUN_SUCCESS; } -static int VScaleDiffVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, - N_Vector* Y, N_Vector* Z) +static void VScaleDiffVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, + N_Vector* Y, N_Vector* Z) { int i; sunindextype j, N; @@ -2026,12 +2032,10 @@ static int VScaleDiffVectorArray_Parallel(int nvec, sunrealtype c, N_Vector* X, zd = NV_DATA_P(Z[i]); for (j = 0; j < N; j++) { zd[j] = c * (xd[j] - yd[j]); } } - - return SUN_SUCCESS; } -static int VLin1VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, - N_Vector* Y, N_Vector* Z) +static void VLin1VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, + N_Vector* Y, N_Vector* Z) { int i; sunindextype j, N; @@ -2048,12 +2052,10 @@ static int VLin1VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, zd = NV_DATA_P(Z[i]); for (j = 0; j < N; j++) { zd[j] = (a * xd[j]) + yd[j]; } } - - return SUN_SUCCESS; } -static int VLin2VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, - N_Vector* Y, N_Vector* Z) +static void VLin2VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, + N_Vector* Y, N_Vector* Z) { int i; sunindextype j, N; @@ -2070,12 +2072,10 @@ static int VLin2VectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, zd = NV_DATA_P(Z[i]); for (j = 0; j < N; j++) { zd[j] = (a * xd[j]) - yd[j]; } } - - return SUN_SUCCESS; } -static int VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, - N_Vector* Y) +static void VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, + N_Vector* Y) { int i; sunindextype j, N; @@ -2092,8 +2092,7 @@ static int VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, yd = NV_DATA_P(Y[i]); for (j = 0; j < N; j++) { yd[j] += xd[j]; } } - - return SUN_SUCCESS; + return; } if (a == -ONE) @@ -2105,7 +2104,7 @@ static int VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, for (j = 0; j < N; j++) { yd[j] -= xd[j]; } } - return SUN_SUCCESS; + return; } for (i = 0; i < nvec; i++) @@ -2114,8 +2113,6 @@ static int VaxpyVectorArray_Parallel(int nvec, sunrealtype a, N_Vector* X, yd = NV_DATA_P(Y[i]); for (j = 0; j < N; j++) { yd[j] += a * xd[j]; } } - - return SUN_SUCCESS; } /* @@ -2128,8 +2125,6 @@ SUNErrCode N_VEnableFusedOps_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - if (tf) { /* enable all fused vector operations */ @@ -2173,8 +2168,6 @@ SUNErrCode N_VEnableLinearCombination_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvlinearcombination = N_VLinearCombination_Parallel; } else { v->ops->nvlinearcombination = NULL; } @@ -2186,8 +2179,6 @@ SUNErrCode N_VEnableScaleAddMulti_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvscaleaddmulti = N_VScaleAddMulti_Parallel; } else { v->ops->nvscaleaddmulti = NULL; } @@ -2199,8 +2190,6 @@ SUNErrCode N_VEnableDotProdMulti_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvdotprodmulti = N_VDotProdMulti_Parallel; } else { v->ops->nvdotprodmulti = NULL; } @@ -2212,8 +2201,6 @@ SUNErrCode N_VEnableLinearSumVectorArray_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvlinearsumvectorarray = N_VLinearSumVectorArray_Parallel; } else { v->ops->nvlinearsumvectorarray = NULL; } @@ -2225,8 +2212,6 @@ SUNErrCode N_VEnableScaleVectorArray_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvscalevectorarray = N_VScaleVectorArray_Parallel; } else { v->ops->nvscalevectorarray = NULL; } @@ -2238,8 +2223,6 @@ SUNErrCode N_VEnableConstVectorArray_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvconstvectorarray = N_VConstVectorArray_Parallel; } else { v->ops->nvconstvectorarray = NULL; } @@ -2251,8 +2234,6 @@ SUNErrCode N_VEnableWrmsNormVectorArray_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvwrmsnormvectorarray = N_VWrmsNormVectorArray_Parallel; } else { v->ops->nvwrmsnormvectorarray = NULL; } @@ -2264,8 +2245,6 @@ SUNErrCode N_VEnableWrmsNormMaskVectorArray_Parallel(N_Vector v, sunbooleantype { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { @@ -2281,8 +2260,6 @@ SUNErrCode N_VEnableScaleAddMultiVectorArray_Parallel(N_Vector v, { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { @@ -2298,8 +2275,6 @@ SUNErrCode N_VEnableLinearCombinationVectorArray_Parallel(N_Vector v, { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { @@ -2315,8 +2290,6 @@ SUNErrCode N_VEnableDotProdMultiLocal_Parallel(N_Vector v, sunbooleantype tf) { SUNFunctionBegin(v->sunctx); - SUNAssert(v->ops, SUN_ERR_ARG_CORRUPT); - /* enable/disable operation */ if (tf) { v->ops->nvdotprodmultilocal = N_VDotProdMultiLocal_Parallel; } else { v->ops->nvdotprodmultilocal = NULL; } diff --git a/src/nvector/pthreads/nvector_pthreads.c b/src/nvector/pthreads/nvector_pthreads.c index c5e764b161..0ade1fa1fb 100644 --- a/src/nvector/pthreads/nvector_pthreads.c +++ b/src/nvector/pthreads/nvector_pthreads.c @@ -313,18 +313,6 @@ N_Vector N_VMake_Pthreads(sunindextype length, int num_threads, return (v); } -/* ---------------------------------------------------------------------------- - * Function to create an array of new vectors. - */ - -N_Vector* N_VCloneVectorArray_Pthreads(int count, N_Vector w) -{ - SUNFunctionBegin(w->sunctx); - N_Vector* result = N_VCloneVectorArray(count, w); - SUNCheckLastErrNull(); - return result; -} - /* ---------------------------------------------------------------------------- * Function to return number of vector elements */ @@ -935,17 +923,19 @@ void N_VScale_Pthreads(sunrealtype c, N_Vector x, N_Vector z) if (z == x) { /* BLAS usage: scale x <- cx */ VScaleBy_Pthreads(c, x); - SUNCheckLastErrVoid(); return; } if (c == ONE) { VCopy_Pthreads(x, z); - SUNCheckLastErrVoid(); return; } - else if (c == -ONE) { VNeg_Pthreads(x, z); } + else if (c == -ONE) + { + VNeg_Pthreads(x, z); + return; + } else { /* allocate threads and thread data structs */ @@ -2957,6 +2947,7 @@ SUNErrCode N_VScaleVectorArray_Pthreads(int nvec, sunrealtype* c, N_Vector* X, if (nvec == 1) { N_VScale_Pthreads(c[0], X[0], Z[0]); + SUNCheckLastErr(); return SUN_SUCCESS; } @@ -3070,6 +3061,7 @@ SUNErrCode N_VConstVectorArray_Pthreads(int nvec, sunrealtype c, N_Vector* Z) if (nvec == 1) { N_VConst_Pthreads(c, Z[0]); + SUNCheckLastErr(); return SUN_SUCCESS; } @@ -3411,7 +3403,6 @@ SUNErrCode N_VScaleAddMultiVectorArray_Pthreads(int nvec, int nsum, Pthreads_Data* thread_data; pthread_attr_t attr; - int retval; N_Vector* YY; N_Vector* ZZ; @@ -3434,7 +3425,9 @@ SUNErrCode N_VScaleAddMultiVectorArray_Pthreads(int nvec, int nsum, /* should have called N_VScaleAddMulti */ YY = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(YY, SUN_ERR_MALLOC_FAIL); ZZ = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(ZZ, SUN_ERR_MALLOC_FAIL); for (j = 0; j < nsum; j++) { @@ -3442,11 +3435,11 @@ SUNErrCode N_VScaleAddMultiVectorArray_Pthreads(int nvec, int nsum, ZZ[j] = Z[j][0]; } - retval = N_VScaleAddMulti_Pthreads(nsum, a, X[0], YY, ZZ); + SUNCheckCall(N_VScaleAddMulti_Pthreads(nsum, a, X[0], YY, ZZ)); free(YY); free(ZZ); - return (retval); + return SUN_SUCCESS; } /* -------------------------- @@ -3456,8 +3449,8 @@ SUNErrCode N_VScaleAddMultiVectorArray_Pthreads(int nvec, int nsum, /* should have called N_VLinearSumVectorArray */ if (nsum == 1) { - retval = N_VLinearSumVectorArray_Pthreads(nvec, a[0], X, ONE, Y[0], Z[0]); - return (retval); + SUNCheckCall(N_VLinearSumVectorArray_Pthreads(nvec, a[0], X, ONE, Y[0], Z[0])); + return SUN_SUCCESS; } /* ---------------------------- @@ -3580,7 +3573,6 @@ SUNErrCode N_VLinearCombinationVectorArray_Pthreads(int nvec, int nsum, Pthreads_Data* thread_data; pthread_attr_t attr; - int retval; sunrealtype* ctmp; N_Vector* Y; @@ -3611,13 +3603,14 @@ SUNErrCode N_VLinearCombinationVectorArray_Pthreads(int nvec, int nsum, /* should have called N_VLinearCombination */ Y = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(Y, SUN_ERR_MALLOC_FAIL); for (i = 0; i < nsum; i++) { Y[i] = X[i][0]; } - retval = N_VLinearCombination_Pthreads(nsum, c, Y, Z[0]); + SUNCheckCall(N_VLinearCombination_Pthreads(nsum, c, Y, Z[0])); free(Y); - return (retval); + return SUN_SUCCESS; } /* -------------------------- @@ -3631,17 +3624,18 @@ SUNErrCode N_VLinearCombinationVectorArray_Pthreads(int nvec, int nsum, for (j = 0; j < nvec; j++) { ctmp[j] = c[0]; } - retval = N_VScaleVectorArray_Pthreads(nvec, ctmp, X[0], Z); + SUNCheckCall(N_VScaleVectorArray_Pthreads(nvec, ctmp, X[0], Z)); free(ctmp); - return (retval); + return SUN_SUCCESS; } /* should have called N_VLinearSumVectorArray */ if (nsum == 2) { - retval = N_VLinearSumVectorArray_Pthreads(nvec, c[0], X[0], c[1], X[1], Z); - return (retval); + SUNCheckCall( + N_VLinearSumVectorArray_Pthreads(nvec, c[0], X[0], c[1], X[1], Z)); + return SUN_SUCCESS; } /* -------------------------- @@ -3778,7 +3772,6 @@ static void* nvLinearCombinationVectorArrayPt(void* thread_data) SUNErrCode N_VBufSize_Pthreads(N_Vector x, sunindextype* size) { - if (x == NULL) { return (-1); } *size = NV_LENGTH_PT(x) * ((sunindextype)sizeof(sunrealtype)); return SUN_SUCCESS; } @@ -3797,7 +3790,7 @@ SUNErrCode N_VBufPack_Pthreads(N_Vector x, void* buf) Pthreads_Data* thread_data; pthread_attr_t attr; - if (x == NULL || buf == NULL) { return (-1); } + SUNAssert(buf, SUN_ERR_ARG_CORRUPT); /* allocate threads and thread data structs */ N = NV_LENGTH_PT(x); @@ -3878,7 +3871,7 @@ SUNErrCode N_VBufUnpack_Pthreads(N_Vector x, void* buf) Pthreads_Data* thread_data; pthread_attr_t attr; - if (x == NULL || buf == NULL) { return (-1); } + SUNAssert(buf, SUN_ERR_ARG_CORRUPT); /* allocate threads and thread data structs */ N = NV_LENGTH_PT(x); diff --git a/src/nvector/serial/nvector_serial.c b/src/nvector/serial/nvector_serial.c index dd25d8026e..6ab3915023 100644 --- a/src/nvector/serial/nvector_serial.c +++ b/src/nvector/serial/nvector_serial.c @@ -23,6 +23,8 @@ #include #include +#include "sundials/sundials_errors.h" + #define ZERO SUN_RCONST(0.0) #define HALF SUN_RCONST(0.5) #define ONE SUN_RCONST(1.0) @@ -176,8 +178,11 @@ N_Vector N_VNew_Serial(sunindextype length, SUNContext sunctx) /* Create data */ data = NULL; - data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); - SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); + if (length > 0) + { + data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); + SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); + } /* Attach data */ NV_OWN_DATA_S(v) = SUNTRUE; @@ -202,25 +207,16 @@ N_Vector N_VMake_Serial(sunindextype length, sunrealtype* v_data, v = N_VNewEmpty_Serial(length, sunctx); SUNCheckLastErrNull(); - /* Attach data */ - NV_OWN_DATA_S(v) = SUNFALSE; - NV_DATA_S(v) = v_data; + if (length > 0) + { + /* Attach data */ + NV_OWN_DATA_S(v) = SUNFALSE; + NV_DATA_S(v) = v_data; + } return (v); } -/* ---------------------------------------------------------------------------- - * Function to create an array of new serial vectors. - */ - -N_Vector* N_VCloneVectorArray_Serial(int count, N_Vector w) -{ - SUNFunctionBegin(w->sunctx); - N_Vector* result = N_VCloneVectorArray(count, w); - SUNCheckLastErrNull(); - return result; -} - /* ---------------------------------------------------------------------------- * Function to return number of vector elements */ @@ -317,12 +313,15 @@ N_Vector N_VClone_Serial(N_Vector w) /* Create data */ data = NULL; - data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); - SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); + if (length > 0) + { + data = (sunrealtype*)malloc(length * sizeof(sunrealtype)); + SUNAssertNull(data, SUN_ERR_MALLOC_FAIL); - /* Attach data */ - NV_OWN_DATA_S(v) = SUNTRUE; - NV_DATA_S(v) = data; + /* Attach data */ + NV_OWN_DATA_S(v) = SUNTRUE; + NV_DATA_S(v) = data; + } return (v); } @@ -1335,7 +1334,9 @@ SUNErrCode N_VScaleAddMultiVectorArray_Serial(int nvec, int nsum, /* should have called N_VScaleAddMulti */ YY = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(YY, SUN_ERR_MALLOC_FAIL); ZZ = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(ZZ, SUN_ERR_MALLOC_FAIL); for (j = 0; j < nsum; j++) { @@ -1443,6 +1444,7 @@ SUNErrCode N_VLinearCombinationVectorArray_Serial(int nvec, int nsum, /* should have called N_VLinearCombination */ Y = (N_Vector*)malloc(nsum * sizeof(N_Vector)); + SUNAssert(Y, SUN_ERR_MALLOC_FAIL); for (i = 0; i < nsum; i++) { Y[i] = X[i][0]; } @@ -1461,6 +1463,7 @@ SUNErrCode N_VLinearCombinationVectorArray_Serial(int nvec, int nsum, if (nsum == 1) { ctmp = (sunrealtype*)malloc(nvec * sizeof(sunrealtype)); + SUNAssert(ctmp, SUN_ERR_MALLOC_FAIL); for (j = 0; j < nvec; j++) { ctmp[j] = c[0]; }