2016-12-08 16:58:01 +00:00
|
|
|
/*************************************************************************************
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/AlignedAllocator.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2015-03-04 05:31:44 +00:00
|
|
|
#ifndef GRID_ALIGNED_ALLOCATOR_H
|
|
|
|
#define GRID_ALIGNED_ALLOCATOR_H
|
2015-04-06 11:26:24 +01:00
|
|
|
|
2015-07-27 10:32:28 +01:00
|
|
|
#ifdef HAVE_MALLOC_MALLOC_H
|
|
|
|
#include <malloc/malloc.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
2015-05-15 11:32:11 +01:00
|
|
|
#ifdef HAVE_MM_MALLOC_H
|
|
|
|
#include <mm_malloc.h>
|
|
|
|
#endif
|
2016-03-02 15:00:00 +00:00
|
|
|
|
2015-04-03 05:29:54 +01:00
|
|
|
namespace Grid {
|
2015-03-04 05:31:44 +00:00
|
|
|
|
2016-12-08 16:58:01 +00:00
|
|
|
class PointerCache {
|
|
|
|
private:
|
|
|
|
|
|
|
|
static const int Ncache=8;
|
|
|
|
static int victim;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
void *address;
|
|
|
|
size_t bytes;
|
|
|
|
int valid;
|
|
|
|
} PointerCacheEntry;
|
|
|
|
|
|
|
|
static PointerCacheEntry Entries[Ncache];
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
|
|
static void *Insert(void *ptr,size_t bytes) ;
|
|
|
|
static void *Lookup(size_t bytes) ;
|
|
|
|
|
|
|
|
};
|
2017-09-22 14:21:18 +01:00
|
|
|
|
|
|
|
struct MemoryStats
|
|
|
|
{
|
|
|
|
size_t totalAllocated{0}, maxAllocated{0},
|
|
|
|
currentlyAllocated{0}, totalFreed{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
class MemoryProfiler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static MemoryStats *stats;
|
2017-12-06 15:50:25 +00:00
|
|
|
static bool debug;
|
2017-09-22 14:21:18 +01:00
|
|
|
};
|
2016-12-08 16:58:01 +00:00
|
|
|
|
2017-12-06 15:50:25 +00:00
|
|
|
#define profilerDebugPrint \
|
|
|
|
if (MemoryProfiler::stats)\
|
|
|
|
{\
|
|
|
|
auto s = MemoryProfiler::stats;\
|
|
|
|
std::cout << "[Memory debug] Stats " << MemoryProfiler::stats << std::endl;\
|
|
|
|
std::cout << "[Memory debug] Total : " << s->totalAllocated << "B" << std::endl;\
|
|
|
|
std::cout << "[Memory debug] Max : " << s->maxAllocated << "B" << std::endl;\
|
|
|
|
std::cout << "[Memory debug] Current: " << s->totalAllocated << "B" << std::endl;\
|
|
|
|
std::cout << "[Memory debug] Freed : " << s->totalFreed << "B" << std::endl;\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define profilerAllocate(bytes)\
|
|
|
|
if (MemoryProfiler::stats)\
|
|
|
|
{\
|
|
|
|
auto s = MemoryProfiler::stats;\
|
|
|
|
s->totalAllocated += (bytes);\
|
|
|
|
s->currentlyAllocated += (bytes);\
|
|
|
|
s->maxAllocated = std::max(s->maxAllocated, s->currentlyAllocated);\
|
|
|
|
}\
|
|
|
|
if (MemoryProfiler::debug)\
|
|
|
|
{\
|
|
|
|
std::cout << "[Memory debug] allocating " << bytes << "B" << std::endl;\
|
|
|
|
profilerDebugPrint;\
|
|
|
|
}
|
|
|
|
|
|
|
|
#define profilerFree(bytes)\
|
|
|
|
if (MemoryProfiler::stats)\
|
|
|
|
{\
|
|
|
|
auto s = MemoryProfiler::stats;\
|
|
|
|
s->totalFreed += (bytes);\
|
|
|
|
s->currentlyAllocated -= (bytes);\
|
|
|
|
}\
|
|
|
|
if (MemoryProfiler::debug)\
|
|
|
|
{\
|
|
|
|
std::cout << "[Memory debug] freeing " << bytes << "B" << std::endl;\
|
|
|
|
profilerDebugPrint;\
|
|
|
|
}
|
|
|
|
|
2017-08-25 22:36:08 +01:00
|
|
|
void check_huge_pages(void *Buf,uint64_t BYTES);
|
|
|
|
|
2015-03-04 05:31:44 +00:00
|
|
|
////////////////////////////////////////////////////////////////////
|
|
|
|
// A lattice of something, but assume the something is SIMDized.
|
|
|
|
////////////////////////////////////////////////////////////////////
|
2016-12-08 16:58:01 +00:00
|
|
|
|
2015-03-04 05:31:44 +00:00
|
|
|
template<typename _Tp>
|
|
|
|
class alignedAllocator {
|
|
|
|
public:
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef _Tp* pointer;
|
|
|
|
typedef const _Tp* const_pointer;
|
|
|
|
typedef _Tp& reference;
|
|
|
|
typedef const _Tp& const_reference;
|
|
|
|
typedef _Tp value_type;
|
|
|
|
|
|
|
|
template<typename _Tp1> struct rebind { typedef alignedAllocator<_Tp1> other; };
|
|
|
|
alignedAllocator() throw() { }
|
|
|
|
alignedAllocator(const alignedAllocator&) throw() { }
|
|
|
|
template<typename _Tp1> alignedAllocator(const alignedAllocator<_Tp1>&) throw() { }
|
|
|
|
~alignedAllocator() throw() { }
|
2015-05-11 12:43:10 +01:00
|
|
|
pointer address(reference __x) const { return &__x; }
|
2015-03-04 05:31:44 +00:00
|
|
|
size_type max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
|
2015-05-11 12:43:10 +01:00
|
|
|
|
2016-02-14 20:24:38 +00:00
|
|
|
pointer allocate(size_type __n, const void* _p= 0)
|
2015-03-04 05:31:44 +00:00
|
|
|
{
|
2016-12-08 16:58:01 +00:00
|
|
|
size_type bytes = __n*sizeof(_Tp);
|
2017-12-06 15:50:25 +00:00
|
|
|
profilerAllocate(bytes);
|
2017-09-22 14:21:18 +01:00
|
|
|
|
2016-12-08 16:58:01 +00:00
|
|
|
_Tp *ptr = (_Tp *) PointerCache::Lookup(bytes);
|
2017-08-25 09:25:54 +01:00
|
|
|
// if ( ptr != NULL )
|
|
|
|
// std::cout << "alignedAllocator "<<__n << " cache hit "<< std::hex << ptr <<std::dec <<std::endl;
|
|
|
|
|
2017-07-29 18:06:53 +01:00
|
|
|
//////////////////
|
|
|
|
// Hack 2MB align; could make option probably doesn't need configurability
|
|
|
|
//////////////////
|
|
|
|
//define GRID_ALLOC_ALIGN (128)
|
|
|
|
#define GRID_ALLOC_ALIGN (2*1024*1024)
|
2016-10-20 16:56:05 +01:00
|
|
|
#ifdef HAVE_MM_MALLOC_H
|
2017-07-29 18:06:53 +01:00
|
|
|
if ( ptr == (_Tp *) NULL ) ptr = (_Tp *) _mm_malloc(bytes,GRID_ALLOC_ALIGN);
|
2016-10-20 16:56:05 +01:00
|
|
|
#else
|
2017-07-29 18:06:53 +01:00
|
|
|
if ( ptr == (_Tp *) NULL ) ptr = (_Tp *) memalign(GRID_ALLOC_ALIGN,bytes);
|
2016-10-20 16:56:05 +01:00
|
|
|
#endif
|
2017-08-25 09:25:54 +01:00
|
|
|
// std::cout << "alignedAllocator " << std::hex << ptr <<std::dec <<std::endl;
|
2017-06-30 10:48:27 +01:00
|
|
|
// First touch optimise in threaded loop
|
|
|
|
uint8_t *cp = (uint8_t *)ptr;
|
2017-06-30 10:49:08 +01:00
|
|
|
#ifdef GRID_OMP
|
2017-06-30 10:48:27 +01:00
|
|
|
#pragma omp parallel for
|
2017-06-30 10:49:08 +01:00
|
|
|
#endif
|
2017-06-30 10:48:27 +01:00
|
|
|
for(size_type n=0;n<bytes;n+=4096){
|
|
|
|
cp[n]=0;
|
|
|
|
}
|
2016-10-20 16:56:05 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
2016-02-14 20:24:38 +00:00
|
|
|
|
2016-12-08 16:58:01 +00:00
|
|
|
void deallocate(pointer __p, size_type __n) {
|
|
|
|
size_type bytes = __n * sizeof(_Tp);
|
2017-08-25 09:25:54 +01:00
|
|
|
|
2017-12-06 15:50:25 +00:00
|
|
|
profilerFree(bytes);
|
2017-09-22 14:21:18 +01:00
|
|
|
|
2016-12-08 16:58:01 +00:00
|
|
|
pointer __freeme = (pointer)PointerCache::Insert((void *)__p,bytes);
|
|
|
|
|
2016-10-20 16:56:05 +01:00
|
|
|
#ifdef HAVE_MM_MALLOC_H
|
2016-12-08 16:58:01 +00:00
|
|
|
if ( __freeme ) _mm_free((void *)__freeme);
|
2016-10-20 16:56:05 +01:00
|
|
|
#else
|
2016-12-08 16:58:01 +00:00
|
|
|
if ( __freeme ) free((void *)__freeme);
|
2016-10-20 16:56:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void construct(pointer __p, const _Tp& __val) { };
|
|
|
|
void construct(pointer __p) { };
|
|
|
|
void destroy(pointer __p) { };
|
|
|
|
};
|
|
|
|
template<typename _Tp> inline bool operator==(const alignedAllocator<_Tp>&, const alignedAllocator<_Tp>&){ return true; }
|
|
|
|
template<typename _Tp> inline bool operator!=(const alignedAllocator<_Tp>&, const alignedAllocator<_Tp>&){ return false; }
|
2016-02-14 20:24:38 +00:00
|
|
|
|
2016-10-20 16:56:05 +01:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MPI3 : comms must use shm region
|
|
|
|
// SHMEM: comms must use symmetric heap
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifdef GRID_COMMS_SHMEM
|
|
|
|
extern "C" {
|
|
|
|
#include <mpp/shmem.h>
|
|
|
|
extern void * shmem_align(size_t, size_t);
|
|
|
|
extern void shmem_free(void *);
|
|
|
|
}
|
2016-02-14 20:24:38 +00:00
|
|
|
#define PARANOID_SYMMETRIC_HEAP
|
2016-10-20 16:56:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
template<typename _Tp>
|
|
|
|
class commAllocator {
|
|
|
|
public:
|
|
|
|
typedef std::size_t size_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef _Tp* pointer;
|
|
|
|
typedef const _Tp* const_pointer;
|
|
|
|
typedef _Tp& reference;
|
|
|
|
typedef const _Tp& const_reference;
|
|
|
|
typedef _Tp value_type;
|
|
|
|
|
|
|
|
template<typename _Tp1> struct rebind { typedef commAllocator<_Tp1> other; };
|
|
|
|
commAllocator() throw() { }
|
|
|
|
commAllocator(const commAllocator&) throw() { }
|
|
|
|
template<typename _Tp1> commAllocator(const commAllocator<_Tp1>&) throw() { }
|
|
|
|
~commAllocator() throw() { }
|
|
|
|
pointer address(reference __x) const { return &__x; }
|
|
|
|
size_type max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
|
|
|
|
|
|
|
|
#ifdef GRID_COMMS_SHMEM
|
|
|
|
pointer allocate(size_type __n, const void* _p= 0)
|
2016-10-21 09:07:20 +01:00
|
|
|
{
|
2017-09-22 14:21:18 +01:00
|
|
|
size_type bytes = __n*sizeof(_Tp);
|
|
|
|
|
2017-12-06 15:50:25 +00:00
|
|
|
profilerAllocate(bytes);
|
2016-10-21 09:07:20 +01:00
|
|
|
#ifdef CRAY
|
2017-09-22 14:21:18 +01:00
|
|
|
_Tp *ptr = (_Tp *) shmem_align(bytes,64);
|
2016-10-21 09:07:20 +01:00
|
|
|
#else
|
2017-09-22 14:21:18 +01:00
|
|
|
_Tp *ptr = (_Tp *) shmem_align(64,bytes);
|
2016-10-21 09:07:20 +01:00
|
|
|
#endif
|
2016-02-14 20:24:38 +00:00
|
|
|
#ifdef PARANOID_SYMMETRIC_HEAP
|
2016-02-11 13:37:39 +00:00
|
|
|
static void * bcast;
|
|
|
|
static long psync[_SHMEM_REDUCE_SYNC_SIZE];
|
|
|
|
|
2016-02-14 20:24:38 +00:00
|
|
|
bcast = (void *) ptr;
|
2016-02-11 13:37:39 +00:00
|
|
|
shmem_broadcast32((void *)&bcast,(void *)&bcast,sizeof(void *)/4,0,0,0,shmem_n_pes(),psync);
|
2016-02-14 20:24:38 +00:00
|
|
|
|
|
|
|
if ( bcast != ptr ) {
|
|
|
|
std::printf("inconsistent alloc pe %d %lx %lx \n",shmem_my_pe(),bcast,ptr);std::fflush(stdout);
|
2016-10-24 23:45:31 +01:00
|
|
|
// BACKTRACEFILE();
|
2016-02-14 20:24:38 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
assert( bcast == (void *) ptr);
|
|
|
|
#endif
|
2016-10-20 16:56:05 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
2017-09-22 14:21:18 +01:00
|
|
|
void deallocate(pointer __p, size_type __n) {
|
|
|
|
size_type bytes = __n*sizeof(_Tp);
|
|
|
|
|
2017-12-06 15:50:25 +00:00
|
|
|
profilerFree(bytes);
|
2016-10-20 16:56:05 +01:00
|
|
|
shmem_free((void *)__p);
|
|
|
|
}
|
2016-02-11 13:37:39 +00:00
|
|
|
#else
|
2016-10-20 17:09:40 +01:00
|
|
|
pointer allocate(size_type __n, const void* _p= 0)
|
|
|
|
{
|
2017-09-22 14:21:18 +01:00
|
|
|
size_type bytes = __n*sizeof(_Tp);
|
|
|
|
|
2017-12-06 15:50:25 +00:00
|
|
|
profilerAllocate(bytes);
|
2015-05-15 11:32:11 +01:00
|
|
|
#ifdef HAVE_MM_MALLOC_H
|
2017-09-22 14:21:18 +01:00
|
|
|
_Tp * ptr = (_Tp *) _mm_malloc(bytes, GRID_ALLOC_ALIGN);
|
2015-05-15 11:32:11 +01:00
|
|
|
#else
|
2017-09-22 14:21:18 +01:00
|
|
|
_Tp * ptr = (_Tp *) memalign(GRID_ALLOC_ALIGN, bytes);
|
2016-02-11 13:37:39 +00:00
|
|
|
#endif
|
2017-06-30 10:48:27 +01:00
|
|
|
uint8_t *cp = (uint8_t *)ptr;
|
2017-08-31 10:42:35 +01:00
|
|
|
if ( ptr ) {
|
2017-07-09 03:27:11 +01:00
|
|
|
// One touch per 4k page, static OMP loop to catch same loop order
|
|
|
|
#pragma omp parallel for schedule(static)
|
2017-08-31 10:42:35 +01:00
|
|
|
for(size_type n=0;n<bytes;n+=4096){
|
|
|
|
cp[n]=0;
|
|
|
|
}
|
2017-06-30 10:48:27 +01:00
|
|
|
}
|
2015-03-04 05:31:44 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
2017-09-22 14:21:18 +01:00
|
|
|
void deallocate(pointer __p, size_type __n) {
|
|
|
|
size_type bytes = __n*sizeof(_Tp);
|
|
|
|
|
2017-12-06 15:50:25 +00:00
|
|
|
profilerFree(bytes);
|
2015-05-15 11:32:11 +01:00
|
|
|
#ifdef HAVE_MM_MALLOC_H
|
2015-06-16 14:04:33 +01:00
|
|
|
_mm_free((void *)__p);
|
2015-05-15 11:32:11 +01:00
|
|
|
#else
|
2015-06-16 14:04:33 +01:00
|
|
|
free((void *)__p);
|
2015-05-15 11:32:11 +01:00
|
|
|
#endif
|
2015-03-04 05:31:44 +00:00
|
|
|
}
|
2016-10-20 16:56:05 +01:00
|
|
|
#endif
|
2015-03-04 05:31:44 +00:00
|
|
|
void construct(pointer __p, const _Tp& __val) { };
|
|
|
|
void construct(pointer __p) { };
|
|
|
|
void destroy(pointer __p) { };
|
|
|
|
};
|
2016-10-20 16:56:05 +01:00
|
|
|
template<typename _Tp> inline bool operator==(const commAllocator<_Tp>&, const commAllocator<_Tp>&){ return true; }
|
|
|
|
template<typename _Tp> inline bool operator!=(const commAllocator<_Tp>&, const commAllocator<_Tp>&){ return false; }
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Template typedefs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-10-20 17:09:40 +01:00
|
|
|
template<class T> using Vector = std::vector<T,alignedAllocator<T> >;
|
2016-10-20 16:56:05 +01:00
|
|
|
template<class T> using commVector = std::vector<T,commAllocator<T> >;
|
2016-10-20 17:09:40 +01:00
|
|
|
template<class T> using Matrix = std::vector<std::vector<T,alignedAllocator<T> > >;
|
2015-03-04 05:31:44 +00:00
|
|
|
|
2015-04-03 05:29:54 +01:00
|
|
|
}; // namespace Grid
|
2015-03-04 05:31:44 +00:00
|
|
|
#endif
|