commit 4f8402b4c5168751e5ad70f67d640581d1d2c0f0
parent f144cc7f3bd137431c288048a96914ee5187500f
Author: Fabian Wermelinger <fabianw@mavt.ethz.ch>
Date: Sun, 25 Sep 2016 16:38:14 +0200
fixed posix memalign issue
Diffstat:
| M | common.h | | | 112 | ++++++------------------------------------------------------------------------- |
1 file changed, 8 insertions(+), 104 deletions(-)
diff --git a/common.h b/common.h
@@ -130,32 +130,28 @@ 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;
+ size_t _N; // number of elements w/o _SS and _SE
T* _data;
- inline T* _alloc(const size_t n)
+ inline void _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);
+ _data = static_cast<T*>(pmem);
}
- else
- return _data;
}
inline void _dealloc()
{
if (_bAllocated) free(_data);
+ _N = 0;
_bAllocated = false;
_data = nullptr;
}
@@ -171,105 +167,13 @@ class LightVector
{
#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
-{
- const size_t _N;
- T* _data;
-
- inline T* _alloc(const size_t n)
- {
- void* pmem;
- posix_memalign(&pmem, _ALIGN_, n*sizeof(T));
- return (T*)pmem;
- }
-
- inline void _dealloc() { free(_data); _data=0; }
-
- 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)
_data[i] = 0.0;
}
public:
- LightVector(const int n) : _N(n), _data(_alloc(n+(_SE-_SS))) { _clear(); }
- LightVector(const LightVector& rhs) : _N(rhs._N), _data(_alloc(rhs._N+(_SE-_SS))) { _copy(rhs._data); }
+ 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(rhs._N+(_SE-_SS)); _copy(rhs._data); }
virtual ~LightVector() { _dealloc(); }
static const int SS = _SS;
@@ -310,6 +214,7 @@ public:
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(); }
@@ -320,7 +225,6 @@ public:
inline T* data() { return _data; }
inline const T * const data() const { return _data; }
};
-#endif
template <typename T>
class StateVector;