From 16506aeac46fa61aec3e53f342450a7090c94829 Mon Sep 17 00:00:00 2001 From: Harry Moffat Date: Sat, 20 Dec 2008 00:00:29 +0000 Subject: [PATCH] Added some fast zero and fast copy functions for doubles. --- Cantera/src/base/vec_functions.h | 218 +++++++++++++++++++------------ 1 file changed, 137 insertions(+), 81 deletions(-) diff --git a/Cantera/src/base/vec_functions.h b/Cantera/src/base/vec_functions.h index 02a0484e6..83f0ebd23 100755 --- a/Cantera/src/base/vec_functions.h +++ b/Cantera/src/base/vec_functions.h @@ -1,10 +1,8 @@ /** - * * @file vec_functions.h - * * Templates for operations on vector-like objects. - * - * $Author$ + */ +/* * $Date$ * $Revision$ * @@ -21,94 +19,152 @@ #include #include +#include + namespace Cantera { - /** - * Copy the first n entries from x to y. Both x and y must have - * size greater than or equal to n. - */ - template - inline void copyn(size_t n, const T& x, T& y) { - std::copy(x.begin(), x.begin() + n, y.begin()); - } + /** + * Copy the first n entries from x to y. Both x and y must have + * size greater than or equal to n. + */ + template + inline void copyn(size_t n, const T& x, T& y) { + std::copy(x.begin(), x.begin() + n, y.begin()); + } - /** - * Divide each element of x by the corresponding element of y. - * This function replaces x[n] by x[n]/y[n], for 0 <= n < x.size() - */ - template - inline void divide_each(T& x, const T& y) { - std::transform(x.begin(), x.end(), y.begin(), - x.begin(), std::divides()); - } + /** + * Divide each element of x by the corresponding element of y. + * This function replaces x[n] by x[n]/y[n], for 0 <= n < x.size() + */ + template + inline void divide_each(T& x, const T& y) { + std::transform(x.begin(), x.end(), y.begin(), + x.begin(), std::divides()); + } - /** - * multiply each element of x by the corresponding element of y. - * This function replaces x[n] by x[n]*y[n], for 0 <= n < x.size() - */ - template - inline void multiply_each(T& x, const T& y) { - std::transform(x.begin(), x.end(), y.begin(), - x.begin(), std::multiplies()); - } + /** + * multiply each element of x by the corresponding element of y. + * This function replaces x[n] by x[n]*y[n], for 0 <= n < x.size() + */ + template + inline void multiply_each(T& x, const T& y) { + std::transform(x.begin(), x.end(), y.begin(), + x.begin(), std::multiplies()); + } - /** - * Multiply each element of x by scale_factor. - */ - template - inline void scale(T& x, S scale_factor) { - scale(x.begin(), x.end(), x.begin(), scale_factor); - } + /** + * Multiply each element of x by scale_factor. + */ + template + inline void scale(T& x, S scale_factor) { + scale(x.begin(), x.end(), x.begin(), scale_factor); + } - /** - * Returns the sum of x[n]*y[n], for 0 <= n < x.size(). - */ - template - inline doublereal dot_product(const T& x, const T& y) { - return std::inner_product(x.begin(), x.end(), y.begin(), 0.0); - } + /** + * Returns the sum of x[n]*y[n], for 0 <= n < x.size(). + */ + template + inline doublereal dot_product(const T& x, const T& y) { + return std::inner_product(x.begin(), x.end(), y.begin(), 0.0); + } - /** - * Returns the sum of x[n]/y[n], for 0 <= n < x.size(). - */ - template - inline doublereal dot_ratio(const T& x, const T& y) { - return _dot_ratio(x.begin(), x.end(), y.begin(), 0.0); - } + /** + * Returns the sum of x[n]/y[n], for 0 <= n < x.size(). + */ + template + inline doublereal dot_ratio(const T& x, const T& y) { + return _dot_ratio(x.begin(), x.end(), y.begin(), 0.0); + } - /** - * Replaces x[n] by x[n] + y[n] for 0 <= n < x.size() - */ - template - inline void add_each(T& x, const T& y) { - std::transform(x.begin(), x.end(), y.begin(), - x.begin(), std::plus()); - } + /** + * Replaces x[n] by x[n] + y[n] for 0 <= n < x.size() + */ + template + inline void add_each(T& x, const T& y) { + std::transform(x.begin(), x.end(), y.begin(), + x.begin(), std::plus()); + } - template - inline doublereal _dot_ratio(InputIter x_begin, InputIter x_end, - InputIter y_begin, S start_value) { - for (; x_begin != x_end; ++x_begin, ++y_begin) - start_value += *x_begin / *y_begin; - return start_value; - } + template + inline doublereal _dot_ratio(InputIter x_begin, InputIter x_end, + InputIter y_begin, S start_value) { + for (; x_begin != x_end; ++x_begin, ++y_begin) + start_value += *x_begin / *y_begin; + return start_value; + } - /** - * Finds the entry in a vector with maximum absolute - * value, and return this value. - */ - template - inline T absmax(const std::vector& v) { - int n = v.size(); - T val; - T maxval = 0.0; - for (int i = 0; i < n; i++) { - val = v[i]; - if (val < 0) val = -val; - if (val > maxval) maxval = val; - } - return maxval; + /** + * Finds the entry in a vector with maximum absolute + * value, and return this value. + */ + template + inline T absmax(const std::vector& v) { + int n = v.size(); + T val; + T maxval = 0.0; + for (int i = 0; i < n; i++) { + val = v[i]; + if (val < 0) val = -val; + if (val > maxval) maxval = val; } + return maxval; + } + + //! Copy a vector of doubles in an efficient, fast manner. + /*! + * No checking is done other that to check that len is greater than 0 + * + * @param copyTo Vector to receive the copy + * @param copyFrom Vector from which the copy is coming + * @param len Length of the copy + */ + inline void fbo_copy_dbl_1(doublereal * const copyTo, const doublereal * const copyFrom, + const int len) { + if (len > 0) { + (void) memcpy((void *)copyTo, (const void *)copyFrom, len * sizeof(doublereal)); + } + } + + //! Copy a vector in an efficient, fast manner. + /*! + * No checking is done other that to check that len is greater than 0 + * + * @param copyTo Vector to receive the copy + * @param copyFrom Vector from which the copy is coming + * @param len Length of the copy + */ + inline void fvo_copy_dbl_1(std::vector ©To, const std::vector ©From, + const int len) { + if (len > 0) { + (void) memcpy((void *)(©To[0]), (const void *)(©From[0]), len * sizeof(doublereal)); + } + } + + //! Zero a double vector in an efficient, fast manner. + /*! + * No checking is done other that to check that len is greater than 0 + * + * @param v Vector to be zeroed + * @param len Length of the copy + */ + inline void fbo_zero_dbl_1(doublereal * const v, const int len) { + if (len > 0) { + (void) memset((void *)v, 0, len * sizeof(doublereal)); + } + } + + //! Zero a vector in an efficient, fast manner. + /*! + * No checking is done other that to check that len is greater than 0 + * + * @param v Vector to be zeroed + * @param len Length of the copy + */ + inline void fvo_zero_dbl_1(std::vector &v, const int len) { + if (len > 0) { + (void) memset((void *)(&v[0]), 0, len * sizeof(doublereal)); + } + } }