1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-14 01:35:36 +00:00

Split allocator cache into two pools of different sizes

This commit is contained in:
Peter Boyle 2020-05-09 22:27:56 -04:00
parent 384da487bd
commit efe5bc6a3c
2 changed files with 44 additions and 42 deletions

View File

@ -6,21 +6,19 @@ NAMESPACE_BEGIN(Grid);
MemoryStats *MemoryProfiler::stats = nullptr; MemoryStats *MemoryProfiler::stats = nullptr;
bool MemoryProfiler::debug = false; bool MemoryProfiler::debug = false;
#ifdef GRID_NVCC int PointerCache::Victim;
#define SMALL_LIMIT (0) int PointerCache::VictimSmall;
#else
#define SMALL_LIMIT (4096)
#endif
#ifdef POINTER_CACHE
int PointerCache::victim;
PointerCache::PointerCacheEntry PointerCache::Entries[PointerCache::Ncache]; PointerCache::PointerCacheEntry PointerCache::Entries[PointerCache::Ncache];
PointerCache::PointerCacheEntry PointerCache::EntriesSmall[PointerCache::NcacheSmall];
void *PointerCache::Insert(void *ptr,size_t bytes) { void *PointerCache::Insert(void *ptr,size_t bytes)
{
if (bytes < SMALL_LIMIT ) return ptr; if (bytes < GRID_ALLOC_SMALL_LIMIT )
return Insert(ptr,bytes,EntriesSmall,NcacheSmall,VictimSmall);
return Insert(ptr,bytes,Entries,Ncache,Victim);
}
void *PointerCache::Insert(void *ptr,size_t bytes,PointerCacheEntry *entries,int ncache,int &victim)
{
#ifdef GRID_OMP #ifdef GRID_OMP
assert(omp_in_parallel()==0); assert(omp_in_parallel()==0);
#endif #endif
@ -28,8 +26,8 @@ void *PointerCache::Insert(void *ptr,size_t bytes) {
void * ret = NULL; void * ret = NULL;
int v = -1; int v = -1;
for(int e=0;e<Ncache;e++) { for(int e=0;e<ncache;e++) {
if ( Entries[e].valid==0 ) { if ( entries[e].valid==0 ) {
v=e; v=e;
break; break;
} }
@ -37,40 +35,43 @@ void *PointerCache::Insert(void *ptr,size_t bytes) {
if ( v==-1 ) { if ( v==-1 ) {
v=victim; v=victim;
victim = (victim+1)%Ncache; victim = (victim+1)%ncache;
} }
if ( Entries[v].valid ) { if ( entries[v].valid ) {
ret = Entries[v].address; ret = entries[v].address;
Entries[v].valid = 0; entries[v].valid = 0;
Entries[v].address = NULL; entries[v].address = NULL;
Entries[v].bytes = 0; entries[v].bytes = 0;
} }
Entries[v].address=ptr; entries[v].address=ptr;
Entries[v].bytes =bytes; entries[v].bytes =bytes;
Entries[v].valid =1; entries[v].valid =1;
return ret; return ret;
} }
void *PointerCache::Lookup(size_t bytes) { void *PointerCache::Lookup(size_t bytes)
{
if (bytes < SMALL_LIMIT ) return NULL; if (bytes < GRID_ALLOC_SMALL_LIMIT )
return Lookup(bytes,EntriesSmall,NcacheSmall);
return Lookup(bytes,Entries,Ncache);
}
void *PointerCache::Lookup(size_t bytes,PointerCacheEntry *entries,int ncache)
{
#ifdef GRID_OMP #ifdef GRID_OMP
assert(omp_in_parallel()==0); assert(omp_in_parallel()==0);
#endif #endif
for(int e=0;e<ncache;e++){
for(int e=0;e<Ncache;e++){ if ( entries[e].valid && ( entries[e].bytes == bytes ) ) {
if ( Entries[e].valid && ( Entries[e].bytes == bytes ) ) { entries[e].valid = 0;
Entries[e].valid = 0; return entries[e].address;
return Entries[e].address;
} }
} }
return NULL; return NULL;
} }
#endif
void check_huge_pages(void *Buf,uint64_t BYTES) void check_huge_pages(void *Buf,uint64_t BYTES)
{ {

View File

@ -42,21 +42,19 @@ Author: Peter Boyle <paboyle@ph.ed.ac.uk>
#define POINTER_CACHE #define POINTER_CACHE
#define GRID_ALLOC_ALIGN (2*1024*1024) #define GRID_ALLOC_ALIGN (2*1024*1024)
#define GRID_ALLOC_SMALL_LIMIT (4096)
NAMESPACE_BEGIN(Grid); NAMESPACE_BEGIN(Grid);
// Move control to configure.ac and Config.h? // Move control to configure.ac and Config.h?
#ifdef POINTER_CACHE
class PointerCache { class PointerCache {
private: private:
/*Pinning pages is costly*/ /*Pinning pages is costly*/
/*Could maintain separate large and small allocation caches*/ /*Could maintain separate large and small allocation caches*/
#ifdef GRID_NVCC /* Could make these configurable, perhaps up to a max size*/
static const int Ncache=128; static const int NcacheSmall=128;
#else
static const int Ncache=8; static const int Ncache=8;
#endif
static int victim;
typedef struct { typedef struct {
void *address; void *address;
@ -65,14 +63,17 @@ private:
} PointerCacheEntry; } PointerCacheEntry;
static PointerCacheEntry Entries[Ncache]; static PointerCacheEntry Entries[Ncache];
static int Victim;
static PointerCacheEntry EntriesSmall[NcacheSmall];
static int VictimSmall;
public: public:
static void *Insert(void *ptr,size_t bytes) ; static void *Insert(void *ptr,size_t bytes) ;
static void *Insert(void *ptr,size_t bytes,PointerCacheEntry *entries,int ncache,int &victim) ;
static void *Lookup(size_t bytes) ; static void *Lookup(size_t bytes) ;
static void *Lookup(size_t bytes,PointerCacheEntry *entries,int ncache) ;
}; };
#endif
std::string sizeString(size_t bytes); std::string sizeString(size_t bytes);