Commit 12f84346 by xiaotong

debugging

parent fb2bdabe
...@@ -288,7 +288,7 @@ bool T2TTrainer::Train(const char * fn, const char * validFN, const char * model ...@@ -288,7 +288,7 @@ bool T2TTrainer::Train(const char * fn, const char * validFN, const char * model
total += mem->blocks[i].size; total += mem->blocks[i].size;
} }
} }
fprintf(stderr, "mem: %lld %lld\n", used, total); fprintf(stderr, "%d %d %d %d mem: %lld %lld\n", paddingEnc.GetDim(0), paddingEnc.GetDim(1), paddingDec.GetDim(0), paddingDec.GetDim(1), used, total);
if(nStepCheckpoint > 0 && ++nStepCheck >= nStepCheckpoint){ if(nStepCheckpoint > 0 && ++nStepCheck >= nStepCheckpoint){
MakeCheckpoint(model, validFN, modelFN, "step", step); MakeCheckpoint(model, validFN, modelFN, "step", step);
......
...@@ -47,6 +47,7 @@ XMem::XMem() ...@@ -47,6 +47,7 @@ XMem::XMem()
name = new char[64]; name = new char[64];
strcpy(name, "xmem"); strcpy(name, "xmem");
signature = 0; signature = 0;
mergeFreeOTF = true;
} }
/* /*
...@@ -69,6 +70,7 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum, ...@@ -69,6 +70,7 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum,
name = new char[64]; name = new char[64];
strcpy(name, "xmem"); strcpy(name, "xmem");
signature = 0; signature = 0;
mergeFreeOTF = true;
Initialize(myDevID, myMode, myBlockSize, myBlockNum, myBufSize); Initialize(myDevID, myMode, myBlockSize, myBlockNum, myBufSize);
} }
...@@ -613,7 +615,7 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -613,7 +615,7 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
while(node != NULL){ while(node != NULL){
if(node->size == 0){ if(node->size == 0){
MPieceNode * next = node->next; MPieceNode * next = node->next;
RemoveFreeIndexNode(node, entry); RemoveIndexNode(node, entry);
node = next; node = next;
} }
else{ else{
...@@ -655,9 +657,6 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -655,9 +657,6 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
next.pre = &cur; next.pre = &cur;
next.next = cur.next; next.next = cur.next;
cur.next = &next; cur.next = &next;
if(cur.next != NULL)
cur.next->pre = &next;
cur.size = needed; cur.size = needed;
next.state = 1; next.state = 1;
...@@ -670,7 +669,7 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -670,7 +669,7 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
hit->pReal = beg; hit->pReal = beg;
blocks[hit->head.blockID].used += head->size; blocks[hit->head.blockID].used += head->size;
RemoveFreeIndexNode(hit); RemoveIndexNode(hit);
AddAllocIndexNode(hit); AddAllocIndexNode(hit);
result = beg; result = beg;
...@@ -684,6 +683,78 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -684,6 +683,78 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
} }
/* if there is still no available memory piece, we have to obtain a new block of memory. */ /* if there is still no available memory piece, we have to obtain a new block of memory. */
else{ else{
MTYPE used = 0;
MTYPE total = 0;
MTYPE free = 0;
for(int i = 0; i < blockNum; i++){
if(blocks[i].mem != NULL){
used += blocks[i].used;
total += blocks[i].size;
}
}
MPieceNode * bufNodes = new MPieceNode[MILLION];
int bufNodeCount = 0;
for(int i = 0; i <= indexEntryNum; i++){
entry = memIndex + i;
node = entry->next;
while(node != NULL){
bufNodes[bufNodeCount++] = *node;
if(node->size == 0){
MPieceNode * next = node->next;
node = next;
}
else{
if(node->size >= mySize){
fprintf(stderr, "hit!!!!!!!!!!!\n");
}
fprintf(stderr, "%d %lld %lld %lld\n", node->head.blockID, free, node->size, mySize);
free += node->size;
node = node->next;
}
}
}
for(int i = 0; i < bufNodeCount; i++){
for(int j = i + 1; j < bufNodeCount; j++){
MTYPE gap1 = (MTYPE)bufNodes[i].p - (MTYPE)bufNodes[j].p - bufNodes[j].head.size;
MTYPE gap2 = (MTYPE)bufNodes[j].p - (MTYPE)bufNodes[i].p - bufNodes[i].head.size;
if(gap1 > 0 && gap1 < 128){
int nnn = 0;
}
if(gap2 > 0 && gap2 < 128){
int nnn = 0;
}
}
}
MTYPE headSize = 0;
MTYPE headSizeUsed = 0;
for(int i = 0, j = 0; i < blockNum; i++){
XMemBlock * block = blocks + i;
if(block->mem != NULL){
MHeader * head = block->head;
while(head != NULL){
if(head->state == 1){
headSize += head->size;
//fprintf(stderr, "%d head %lld\n", j++, head->size);
}
else{
headSizeUsed += head->size;
}
head = head->next;
}
}
}
delete[] bufNodes;
fprintf(stderr, "%lld %lld\n", headSize, headSizeUsed);
fprintf(stderr, "mem: %lld %lld %lld %lld\n", used, total, free, mySize);
int bi; int bi;
for(bi = 0; bi < blockNum; bi++){ for(bi = 0; bi < blockNum; bi++){
XMemBlock * block = blocks + bi; XMemBlock * block = blocks + bi;
...@@ -856,15 +927,16 @@ int XMem::FindIndexEntry(MTYPE mySize) ...@@ -856,15 +927,16 @@ int XMem::FindIndexEntry(MTYPE mySize)
} }
/* /*
remove an index node for available memory pieces remove an index node
>> node - node to remove >> node - node to remove
>> - the entry of the list that keeps the node >> - the entry of the list that keeps the node
*/ */
void XMem::RemoveFreeIndexNode(MPieceNode * node, MPieceNode * entry) void XMem::RemoveIndexNode(MPieceNode * node, MPieceNode * entry)
{ {
MPieceNode * pre = node->pre; MPieceNode * pre = node->pre;
MPieceNode * next = node->next; MPieceNode * next = node->next;
CheckNTErrors(pre != NULL, "cannot free the entry node!"); CheckNTErrors(pre != NULL, "cannot free the entry node!");
pre->next = next; pre->next = next;
...@@ -884,6 +956,19 @@ void XMem::AddFreeIndexNode(MPieceNode * node, MPieceNode * entry) ...@@ -884,6 +956,19 @@ void XMem::AddFreeIndexNode(MPieceNode * node, MPieceNode * entry)
{ {
MPieceNode * entryForMe = entry != NULL ? entry : MPieceNode * entryForMe = entry != NULL ? entry :
memIndex + FindIndexEntry(node->size); memIndex + FindIndexEntry(node->size);
/*MPieceNode * backup = entryForMe->next;
while(backup != NULL && backup->head.size < node->head.size){
backup = backup->next;
entryForMe = entryForMe->next;
}
entryForMe->next = node;
node->pre = entryForMe;
node->next = backup;
if(backup != NULL)
backup->pre = node;*/
MPieceNode * backup = entryForMe->next; MPieceNode * backup = entryForMe->next;
entryForMe->next = node; entryForMe->next = node;
...@@ -903,7 +988,7 @@ remove an index node for memory pieces in use ...@@ -903,7 +988,7 @@ remove an index node for memory pieces in use
*/ */
void XMem::RemoveAllocIndexNode(MPieceNode * node, MPieceNode * entry) void XMem::RemoveAllocIndexNode(MPieceNode * node, MPieceNode * entry)
{ {
RemoveFreeIndexNode(node, entry); RemoveIndexNode(node, entry);
} }
/* /*
...@@ -959,7 +1044,7 @@ void XMem::ReleaseStandard(int myDevID, void * p, MTYPE size) ...@@ -959,7 +1044,7 @@ void XMem::ReleaseStandard(int myDevID, void * p, MTYPE size)
if(node->size == 0){ if(node->size == 0){
MPieceNode * next = node->next; MPieceNode * next = node->next;
RemoveFreeIndexNode(node, entry); RemoveIndexNode(node, entry);
node = next; node = next;
ShowNTErrors("Something is wrong!"); ShowNTErrors("Something is wrong!");
} }
...@@ -979,10 +1064,61 @@ void XMem::ReleaseStandard(int myDevID, void * p, MTYPE size) ...@@ -979,10 +1064,61 @@ void XMem::ReleaseStandard(int myDevID, void * p, MTYPE size)
RemoveAllocIndexNode(hit); RemoveAllocIndexNode(hit);
hit->size = (char*)hit->p + hit->head.size - (char*)GetPitchedAddress((char*)hit->p, MY_PITCH); MTYPE usedSize = (char*)hit->p + hit->head.size - (char*)GetPitchedAddress((char*)hit->p, MY_PITCH);
AddFreeIndexNode(hit); blocks[hit->head.blockID].used -= usedSize;
if(mergeFreeOTF){
MHeader * head = &hit->head;
MHeader * pre = head->pre;
MHeader * next = head->next;
bool mergeLeft = false;
bool mergeRight = false;
CheckNTErrors(head != pre, "wrong list of memory headers");
CheckNTErrors(head != next, "wrong list of memory headers");
if(pre != NULL && pre->state == 1 && pre->blockID == head->blockID){
mergeLeft = true;
head->pre = pre->pre;
if(head->pre != NULL)
head->pre->next = head;
hit->p = pre->indexNode->p;
hit->head.size += pre->size;
RemoveAllocIndexNode(pre->indexNode);
if(pre == blocks[head->blockID].head){
int nnn = 0;
}
if(head == blocks[head->blockID].head){
int nnn = 0;
}
if(pre == blocks[head->blockID].head)
blocks[head->blockID].head = head;
}
blocks[hit->head.blockID].used -= hit->head.size; if(next != NULL && next->state == 1 && next->blockID == head->blockID){
mergeRight = true;
head->next = next->next;
if(head->next != NULL)
head->next->pre = head;
hit->head.size += next->size;
RemoveAllocIndexNode(next->indexNode);
}
if(!mergeLeft && !mergeRight){
hit->size = usedSize;
}
else{
hit->size = (char*)hit->p + hit->head.size - (char*)GetPitchedAddress((char*)hit->p, MY_PITCH);
}
}
else{
hit->size = usedSize;
}
AddFreeIndexNode(hit);
} }
/* rebuild index to merge small fragments of memory and free the block with no use */ /* rebuild index to merge small fragments of memory and free the block with no use */
......
...@@ -231,6 +231,9 @@ public: ...@@ -231,6 +231,9 @@ public:
/* index offset */ /* index offset */
int indexOffset; int indexOffset;
/* indicates whether we merge free memory pieces on the fly */
bool mergeFreeOTF;
public: public:
/* constructor */ /* constructor */
...@@ -326,7 +329,7 @@ public: ...@@ -326,7 +329,7 @@ public:
int FindIndexEntry(MTYPE mySize); int FindIndexEntry(MTYPE mySize);
/* remove an index node for available memory pieces */ /* remove an index node for available memory pieces */
void RemoveFreeIndexNode(MPieceNode * node, MPieceNode * entry = NULL); void RemoveIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
/* add an index node for available memory pieces */ /* add an index node for available memory pieces */
void AddFreeIndexNode(MPieceNode * node, MPieceNode * entry = NULL); void AddFreeIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论