7static char print_buffer [ MAXLINE ];
9#define mprintf(...) snprintf (print_buffer,MAXLINE, __VA_ARGS__ ); std::cout << GridLogMemory << print_buffer << std::endl;
10#define dprintf(...) snprintf (print_buffer,MAXLINE, __VA_ARGS__ ); std::cout << GridLogDebug << print_buffer << std::endl;
42#define Consistent (0x2)
44#define EvictNext (0x8)
53 auto count =
AccViewTable.count(CpuPtr); assert((count==0)||(count==1));
61 AccCache.AccPtr = (uint64_t)NULL;
62 AccCache.bytes = bytes;
63 AccCache.state = CpuDirty;
75 return AccCacheIterator;
84 assert(AccCache.LRU_valid==0);
85 if (AccCache.transient) {
86 LRU.push_back(AccCache.CpuPtr);
87 AccCache.LRU_entry = --
LRU.end();
89 LRU.push_front(AccCache.CpuPtr);
90 AccCache.LRU_entry =
LRU.begin();
92 AccCache.LRU_valid = 1;
97 assert(AccCache.LRU_valid==1);
98 LRU.erase(AccCache.LRU_entry);
99 AccCache.LRU_valid = 0;
111 assert(AccCache.state!=Empty);
113 dprintf(
"MemoryManager: Discard(%lx) %lx",(uint64_t)AccCache.CpuPtr,(uint64_t)AccCache.AccPtr);
114 assert(AccCache.accLock==0);
115 assert(AccCache.cpuLock==0);
116 assert(AccCache.CpuPtr!=(uint64_t)NULL);
117 if(AccCache.AccPtr) {
122 AccCache.AccPtr=(uint64_t) NULL;
125 uint64_t CpuPtr = AccCache.CpuPtr;
141 assert(AccCache.state!=Empty);
143 mprintf(
"MemoryManager: Evict CpuPtr %lx AccPtr %lx cpuLock %ld accLock %ld",
144 (uint64_t)AccCache.CpuPtr,(uint64_t)AccCache.AccPtr,
145 (uint64_t)AccCache.cpuLock,(uint64_t)AccCache.accLock);
146 if (AccCache.accLock!=0)
return;
147 if (AccCache.cpuLock!=0)
return;
148 if(AccCache.state==AccDirty) {
151 if(AccCache.AccPtr) {
154 AccCache.AccPtr=(uint64_t)NULL;
155 AccCache.state=CpuDirty;
157 dprintf(
"MemoryManager: Free(AccPtr %lx) footprint now %ld ",(uint64_t)AccCache.AccPtr,
DeviceBytes);
165 assert(AccCache.state==AccDirty);
166 assert(AccCache.cpuLock==0);
167 assert(AccCache.accLock==0);
168 assert(AccCache.AccPtr!=(uint64_t)NULL);
169 assert(AccCache.CpuPtr!=(uint64_t)NULL);
171 mprintf(
"MemoryManager: acceleratorCopyFromDevice Flush size %ld AccPtr %lx -> CpuPtr %lx",(uint64_t)AccCache.bytes,(uint64_t)AccCache.AccPtr,(uint64_t)AccCache.CpuPtr); fflush(stdout);
174 AccCache.state=Consistent;
178 assert(AccCache.state==CpuDirty);
179 assert(AccCache.cpuLock==0);
180 assert(AccCache.accLock==0);
181 assert(AccCache.CpuPtr!=(uint64_t)NULL);
182 if(AccCache.AccPtr==(uint64_t)NULL){
186 mprintf(
"MemoryManager: acceleratorCopyToDevice Clone size %ld AccPtr %lx <- CpuPtr %lx",
187 (uint64_t)AccCache.bytes,
188 (uint64_t)AccCache.AccPtr,(uint64_t)AccCache.CpuPtr); fflush(stdout);
192 AccCache.state=Consistent;
197 assert(AccCache.state!=Empty);
198 assert(AccCache.cpuLock==0);
199 assert(AccCache.accLock==0);
200 assert(AccCache.CpuPtr!=(uint64_t)NULL);
201 if(AccCache.AccPtr==(uint64_t)NULL){
205 AccCache.state=AccDirty;
214 dprintf(
"AcceleratorViewClose %lx",(uint64_t)Ptr);
224 uint64_t CpuPtr = (uint64_t)_CpuPtr;
226 dprintf(
"AcceleratorViewOpen %lx",(uint64_t)CpuPtr);
229 return (
void *)
CpuViewOpen(CpuPtr,bytes,mode,hint);
238 printf(
"EvictVictims bytes %ld DeviceMaxBytes %ld\n",bytes,
DeviceMaxBytes);
243 assert(
LRU.size()>0);
244 uint64_t victim =
LRU.back();
246 auto & AccCache = AccCacheIterator->second;
263 auto & AccCache = AccCacheIterator->second;
264 if (!AccCache.AccPtr) {
269 assert(AccCache.cpuLock==0);
271 if(AccCache.state!=Empty) {
272 dprintf(
"ViewOpen found entry %lx %lx : sizes %ld %ld accLock %ld",
273 (uint64_t)AccCache.CpuPtr,
275 (uint64_t)AccCache.bytes,
277 (uint64_t)AccCache.accLock);
278 assert(AccCache.CpuPtr == CpuPtr);
279 assert(AccCache.bytes ==bytes);
295 if(AccCache.state==Empty) {
296 assert(AccCache.LRU_valid==0);
297 AccCache.CpuPtr = CpuPtr;
298 AccCache.AccPtr = (uint64_t)NULL;
299 AccCache.bytes = bytes;
300 AccCache.state = CpuDirty;
303 AccCache.state = AccDirty;
306 AccCache.state = AccDirty;
309 AccCache.state = Consistent;
312 dprintf(
"Copied Empty entry into device accLock= %d",AccCache.accLock);
313 }
else if(AccCache.state==CpuDirty ){
316 AccCache.state = AccDirty;
319 AccCache.state = AccDirty;
322 AccCache.state = Consistent;
325 dprintf(
"CpuDirty entry into device ++accLock= %d",AccCache.accLock);
326 }
else if(AccCache.state==Consistent) {
328 AccCache.state = AccDirty;
330 AccCache.state = Consistent;
332 dprintf(
"Consistent entry into device ++accLock= %d",AccCache.accLock);
333 }
else if(AccCache.state==AccDirty) {
335 AccCache.state = AccDirty;
337 AccCache.state = AccDirty;
339 dprintf(
"AccDirty entry ++accLock= %d",AccCache.accLock);
344 assert(AccCache.accLock>0);
346 if(AccCache.LRU_valid==1){
348 dprintf(
"AccCache entry removed from LRU ");
353 AccCache.transient= transient? EvictNext : 0;
355 return AccCache.AccPtr;
363 auto & AccCache = AccCacheIterator->second;
365 assert(AccCache.cpuLock==0);
366 assert(AccCache.accLock>0);
370 if(AccCache.accLock==0) {
371 dprintf(
"AccleratorViewClose %lx AccLock decremented to %ld move to LRU queue",(uint64_t)CpuPtr,(uint64_t)AccCache.accLock);
374 dprintf(
"AccleratorViewClose %lx AccLock decremented to %ld",(uint64_t)CpuPtr,(uint64_t)AccCache.accLock);
380 auto & AccCache = AccCacheIterator->second;
382 assert(AccCache.cpuLock>0);
383 assert(AccCache.accLock==0);
409 auto & AccCache = AccCacheIterator->second;
417 assert(AccCache.accLock==0);
419 if(AccCache.state!=Empty) {
420 assert(AccCache.CpuPtr == CpuPtr);
421 assert(AccCache.bytes==bytes);
424 if(AccCache.state==Empty) {
425 AccCache.CpuPtr = CpuPtr;
426 AccCache.AccPtr = (uint64_t)NULL;
427 AccCache.bytes = bytes;
428 AccCache.state = CpuDirty;
431 }
else if(AccCache.state==CpuDirty ){
433 AccCache.state = CpuDirty;
435 }
else if(AccCache.state==Consistent) {
436 assert(AccCache.AccPtr != (uint64_t)NULL);
438 AccCache.state = CpuDirty;
440 AccCache.state = Consistent;
442 }
else if(AccCache.state==AccDirty) {
443 assert(AccCache.AccPtr != (uint64_t)NULL);
445 if(mode==
CpuWrite) AccCache.state = CpuDirty;
446 else AccCache.state = Consistent;
452 AccCache.transient= transient? EvictNext : 0;
454 return AccCache.CpuPtr;
459 uint64_t ptr = (uint64_t)_ptr;
468 std::cout <<
GridLogMessage <<
"--------------------------------------------" << std::endl;
470 std::cout <<
GridLogMessage <<
"--------------------------------------------" << std::endl;
482 std::cout <<
GridLogMessage <<
"--------------------------------------------" << std::endl;
488 std::cout <<
GridLogMessage <<
"--------------------------------------------" << std::endl;
489 std::cout <<
GridLogMessage <<
"CpuAddr\t\tAccAddr\t\tState\t\tcpuLock\taccLock\tLRU_valid "<<std::endl;
490 std::cout <<
GridLogMessage <<
"--------------------------------------------" << std::endl;
492 auto &AccCache = it->second;
495 if ( AccCache.state==Empty ) str = std::string(
"Empty");
496 if ( AccCache.state==CpuDirty ) str = std::string(
"CpuDirty");
497 if ( AccCache.state==AccDirty ) str = std::string(
"AccDirty");
498 if ( AccCache.state==Consistent)str = std::string(
"Consistent");
500 std::cout <<
GridLogMessage <<
"0x"<<std::hex<<AccCache.CpuPtr<<std::dec
501 <<
"\t0x"<<std::hex<<AccCache.AccPtr<<std::dec<<
"\t" <<str
502 <<
"\t" << AccCache.cpuLock
503 <<
"\t" << AccCache.accLock
504 <<
"\t" << AccCache.LRU_valid<<std::endl;
506 std::cout <<
GridLogMessage <<
"--------------------------------------------" << std::endl;
511 uint64_t CpuPtr = (uint64_t)_CpuPtr;
514 auto & AccCache = AccCacheIterator->second;
515 return AccCache.cpuLock+AccCache.accLock;
524 uint64_t LruBytes1=0;
525 uint64_t LruBytes2=0;
528 std::cout <<
" Memory Manager::Audit() from "<<s<<std::endl;
529 for(
auto it=
LRU.begin();it!=
LRU.end();it++){
530 uint64_t cpuPtr = *it;
533 auto & AccCache = AccCacheIterator->second;
534 LruBytes2+=AccCache.bytes;
535 assert(AccCache.LRU_valid==1);
536 assert(AccCache.LRU_entry==it);
538 std::cout <<
" Memory Manager::Audit() LRU queue matches table entries "<<std::endl;
541 auto &AccCache = it->second;
544 if ( AccCache.state==Empty ) str = std::string(
"Empty");
545 if ( AccCache.state==CpuDirty ) str = std::string(
"CpuDirty");
546 if ( AccCache.state==AccDirty ) str = std::string(
"AccDirty");
547 if ( AccCache.state==Consistent)str = std::string(
"Consistent");
549 CpuBytes+=AccCache.bytes;
550 if( AccCache.AccPtr ) AccBytes+=AccCache.bytes;
551 if( AccCache.LRU_valid ) LruBytes1+=AccCache.bytes;
552 if( AccCache.LRU_valid ) LruCnt++;
554 if ( AccCache.cpuLock || AccCache.accLock ) {
555 assert(AccCache.LRU_valid==0);
557 std::cout <<
GridLogError << s<<
"\n\t 0x"<<std::hex<<AccCache.CpuPtr<<std::dec
558 <<
"\t0x"<<std::hex<<AccCache.AccPtr<<std::dec<<
"\t" <<str
559 <<
"\t cpuLock " << AccCache.cpuLock
560 <<
"\t accLock " << AccCache.accLock
561 <<
"\t LRUvalid " << AccCache.LRU_valid<<std::endl;
564 assert( AccCache.cpuLock== 0 ) ;
565 assert( AccCache.accLock== 0 ) ;
567 std::cout <<
" Memory Manager::Audit() no locked table entries "<<std::endl;
568 assert(LruBytes1==LruBytes2);
570 std::cout <<
" Memory Manager::Audit() evictable bytes matches sum over table "<<std::endl;
572 std::cout <<
" Memory Manager::Audit() device bytes matches sum over table "<<std::endl;
573 assert(LruCnt ==
LRU.size());
574 std::cout <<
" Memory Manager::Audit() LRU entry count matches "<<std::endl;
580 uint64_t CpuPtr = (uint64_t)_CpuPtr;
584 auto & AccCache = AccCacheIterator->second;
586 if ( AccCache.state==Empty ) str = std::string(
"Empty");
587 if ( AccCache.state==CpuDirty ) str = std::string(
"CpuDirty");
588 if ( AccCache.state==AccDirty ) str = std::string(
"AccDirty");
589 if ( AccCache.state==Consistent)str = std::string(
"Consistent");
590 if ( AccCache.state==EvictNext) str = std::string(
"EvictNext");
592 std::cout <<
GridLogMessage <<
"CpuAddr\t\tAccAddr\t\tState\t\tcpuLock\taccLock\tLRU_valid "<<std::endl;
593 std::cout <<
GridLogMessage <<
"\tx"<<std::hex<<AccCache.CpuPtr<<std::dec
594 <<
"\tx"<<std::hex<<AccCache.AccPtr<<std::dec<<
"\t" <<str
595 <<
"\t" << AccCache.cpuLock
596 <<
"\t" << AccCache.accLock
597 <<
"\t" << AccCache.LRU_valid<<std::endl;
600 std::cout <<
GridLogMessage <<
"No Entry in AccCache table." << std::endl;
void acceleratorMem(void)
void acceleratorCopyToDevice(void *from, void *to, size_t bytes)
void acceleratorCopyFromDevice(void *from, void *to, size_t bytes)
GridLogger GridLogError(1, "Error", GridLogColours, "RED")
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
@ AcceleratorWriteDiscard
#define NAMESPACE_BEGIN(A)
static AccViewTable_t AccViewTable
static void LRUinsert(AcceleratorViewEntry &AccCache)
static uint64_t DeviceDestroy
static AccViewTableIterator EntryLookup(uint64_t CpuPtr)
static void CpuDiscard(AcceleratorViewEntry &AccCache)
static uint64_t DeviceLRUBytes
static void EvictVictims(uint64_t bytes)
static void PrintState(void *CpuPtr)
static void * ViewOpen(void *CpuPtr, size_t bytes, ViewMode mode, ViewAdvise hint)
static void Flush(AcceleratorViewEntry &AccCache)
static void PrintBytes(void)
static uint64_t DeviceBytes
static uint64_t DeviceMaxBytes
std::unordered_map< uint64_t, AcceleratorViewEntry > AccViewTable_t
static void ViewClose(void *CpuPtr, ViewMode mode)
static void EntryCreate(uint64_t CpuPtr, size_t bytes, ViewMode mode, ViewAdvise hint)
static void NotifyDeletion(void *CpuPtr)
static void PrintAll(void)
static uint64_t DeviceToHostBytes
static void Audit(std::string s)
static uint64_t DeviceEvictions
static void EntryErase(uint64_t CpuPtr)
static uint64_t AcceleratorViewOpen(uint64_t CpuPtr, size_t bytes, ViewMode mode, ViewAdvise hint)
static void AcceleratorViewClose(uint64_t AccPtr)
static uint64_t DeviceToHostXfer
static void CpuViewClose(uint64_t Ptr)
static void LRUremove(AcceleratorViewEntry &AccCache)
static uint64_t CpuViewOpen(uint64_t CpuPtr, size_t bytes, ViewMode mode, ViewAdvise hint)
static void Evict(AcceleratorViewEntry &AccCache)
AccViewTable_t::iterator AccViewTableIterator
static void * AcceleratorAllocate(size_t bytes)
static uint64_t HostToDeviceXfer
static uint64_t HostToDeviceBytes
static int isOpen(void *CpuPtr)
static void Clone(AcceleratorViewEntry &AccCache)
static void AccDiscard(AcceleratorViewEntry &AccCache)
static void AcceleratorFree(void *ptr, size_t bytes)
static int EntryPresent(uint64_t CpuPtr)
std::list< uint64_t > LRU_t