#ifndef __ma_allocator__ #define __ma_allocator__ #include #include #include #include #include #include #define MIB_SIZE 1024*1024 // 1MiB #define WARN_SIZE MIB_SIZE*100 // 100 MiB namespace MultiArrayHelper { template struct Allocator { typedef T value_type; static constexpr size_t type_size = sizeof(value_type); static constexpr size_t N = 32; struct VX { alignas(N) char x[N]; }; Allocator() = default; template Allocator(const Allocator& x) {} T* allocate(size_t n) { const size_t nn = n*type_size; if(nn >= WARN_SIZE){ std::cout << __func__ << ": WARNING: allocating " << nn/(MIB_SIZE) << " MiB" << std::endl; } const size_t off = nn%N; const size_t nnx = (off == 0) ? nn : nn + N - off; const size_t nnd = nnx/N; VX* vx = new VX[nnd]; return reinterpret_cast(vx); } void deallocate(T* p, size_t n) { VX* vx = reinterpret_cast(p); delete [] vx; } }; template bool operator==(const Allocator& a, const Allocator& b) { return true; } template bool operator!=(const Allocator& a, const Allocator& b) { return false; } } // namespace MultiArrayHelper namespace MultiArrayTools { template using vector = std::vector>; template inline std::vector toStdVec(const vector& v) { return std::vector(v.begin(), v.end()); } template inline vector toMatVec(const std::vector& v) { return vector(v.begin(), v.end()); } } // namespace MultiArrayTools #endif