ode-toolbox

ODE integration tools
git clone https://git.0xfab.ch/ode-toolbox.git
Log | Files | Refs | README | LICENSE

commit f144cc7f3bd137431c288048a96914ee5187500f
parent 1a027c525a55598699d5117e6fe79e6ce6d8a5ba
Author: Fabian Wermelinger <fabianw@mavt.ethz.ch>
Date:   Sun, 25 Sep 2016 15:28:00 +0200

minor

Diffstat:
Mcommon.h | 113+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 111 insertions(+), 2 deletions(-)

diff --git a/common.h b/common.h @@ -53,7 +53,7 @@ struct State s[i] = rhs.s[i]; return *this; } - inline State& operator=(const double c) + inline State& operator=(const Real c) { for (int i = 0; i < _SIZE; ++i) s[i] = c; @@ -130,6 +130,114 @@ typedef State<Real,1> State1; typedef State<Real,2> State2; +#if 1 + +template <typename T, int _SS=0, int _SE=0> +class LightVector +{ + bool _bAllocated; + size_t _N; + T* _data; + + inline T* _alloc(const size_t n) + { + if (!_bAllocated) + { + _N = n; + void* pmem; + posix_memalign(&pmem, _ALIGN_, n*sizeof(T)); + _bAllocated = true; + return static_cast<T*>(pmem); + } + else + return _data; + } + + inline void _dealloc() + { + if (_bAllocated) free(_data); + _bAllocated = false; + _data = nullptr; + } + + inline void _copy(const T* const src) + { +#pragma omp parallel for LOOPSCHED + for (size_t i = 0; i < _N+_SE-_SS; ++i) + _data[i] = src[i]; + } + + inline void _clear() + { +#pragma omp parallel for LOOPSCHED + for (size_t i = 0; i < _N+_SE-_SS; ++i) + { + std::cout << "i = " << i << std::endl; + std::cout << "_N = " << _N << std::endl; + T& d = _data[i]; + std::cout << "data[i] = " << d[0] << std::endl; + + _data[i] = 0.0; + } + } + +public: + // LightVector() : _bAllocated(false), _N(0), _data(nullptr) {} + LightVector(const int n) : _bAllocated(false), _N(n), _data(nullptr) { _alloc(_N+(_SE-_SS)); _clear(); } + LightVector(const LightVector& rhs) : _bAllocated(false), _N(rhs._N), _data(nullptr) { _alloc(_N+(_SE-_SS)); _copy(rhs._data); } + ~LightVector() { _dealloc(); } + + static const int SS = _SS; + static const int SE = _SE; + using DataType = T; + + LightVector& operator=(const LightVector& rhs) + { + if (this != &rhs) + { + assert(_N+_SE-_SS == rhs._N+_SE-_SS); + _copy(rhs._data); + } + return *this; + } + + LightVector& operator=(const T& c) + { +#pragma omp parallel for LOOPSCHED + for (size_t i = 0; i < _N+_SE-_SS; ++i) + _data[i] = c; + return *this; + } + + LightVector& operator*=(const T& c) + { +#pragma omp parallel for LOOPSCHED + for (size_t i = 0; i < _N+_SE-_SS; ++i) + _data[i] *= c; + return *this; + } + + LightVector& operator+=(const LightVector& c) + { +#pragma omp parallel for LOOPSCHED + for (size_t i = 0; i < _N+_SE-_SS; ++i) + _data[i] += c._data[i]; + return *this; + } + + inline void allocate(const size_t n) { _N=n; _alloc(n+(_SE-_SS)); _clear(); } + inline size_t size() const { return _N; } + inline size_t size_all() const { return _N+_SE-_SS; } + inline void clear() { _clear(); } + inline T& operator[](const size_t i) { assert(i<_N+_SE-_SS); return _data[i]; } + inline const T& operator[](const size_t i) const { assert(i<_N+_SE-_SS); return _data[i]; } + inline T& operator()(const int ix) { assert((ix-_SS >= 0)&&(ix-_SS < static_cast<int>(_N)+_SE-_SS)); return _data[ix-_SS]; } + inline const T& operator()(const int ix) const { assert((ix-_SS >= 0)&&(ix-_SS < static_cast<int>(_N)+_SE-_SS)); return _data[ix-_SS]; } + inline T* data() { return _data; } + inline const T * const data() const { return _data; } +}; + +#else template <typename T, int _SS=0, int _SE=0> class LightVector { @@ -212,6 +320,7 @@ public: inline T* data() { return _data; } inline const T * const data() const { return _data; } }; +#endif template <typename T> class StateVector; @@ -255,7 +364,7 @@ public: if (this != &rhs) _copy(rhs); return *this; } - StateVector& operator=(double const rhs) + StateVector& operator=(Real const rhs) { for (size_t i = 0; i < m_state.size(); ++i) m_state[i] = rhs;