Commit 91fdf94e by xiaotong

rewrite parts of the memory pool to free the space on the fly

parent 97b41e35
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
/* the nts (NiuTrans.Tensor) namespace */ /* the nts (NiuTrans.Tensor) namespace */
namespace nts{ namespace nts{
int testxmemid = 0;
void * recordp = NULL;
XMem * GMem; XMem * GMem;
...@@ -83,6 +86,9 @@ XMem::~XMem() ...@@ -83,6 +86,9 @@ XMem::~XMem()
#endif #endif
Free(); Free();
delete[] name; delete[] name;
delete[] memIndex;
delete[] memIndex2;
delete[] minSizeIndex;
} }
/* /*
...@@ -279,15 +285,19 @@ initialize the index ...@@ -279,15 +285,19 @@ initialize the index
*/ */
void XMem::SetIndex(INT_64 indexSize, MTYPE minSizeFirst, int minSizeNum) void XMem::SetIndex(INT_64 indexSize, MTYPE minSizeFirst, int minSizeNum)
{ {
delete freeMemIndex; delete[] memIndex;
delete minSizeIndex; delete[] memIndex2;
delete[] minSizeIndex;
indexNodeNum = indexSize; nodeNum = indexSize;
indexNodeNumUsed = minSizeNum; nodeNumUsed = minSizeNum * 2;
indexEntryNum = minSizeNum; indexEntryNum = minSizeNum;
freeMemIndex = new MPieceNode[indexNodeNum]; memIndex = new MPieceNode[nodeNum];
memset(freeMemIndex, 0, sizeof(MPieceNode) * indexNodeNum); memset(memIndex, 0, sizeof(MPieceNode) * nodeNum);
memIndex2 = new MPieceNode[nodeNum];
memset(memIndex2, 0, sizeof(MPieceNode) * nodeNum);
minSizeIndex = new MTYPE[indexEntryNum]; minSizeIndex = new MTYPE[indexEntryNum];
memset(minSizeIndex, 0, sizeof(MTYPE) * indexEntryNum); memset(minSizeIndex, 0, sizeof(MTYPE) * indexEntryNum);
...@@ -496,21 +506,23 @@ void * XMem::AllocBuf(int myDevID, MTYPE mySize, int pitch) ...@@ -496,21 +506,23 @@ void * XMem::AllocBuf(int myDevID, MTYPE mySize, int pitch)
/* /*
release a piece of memory release a piece of memory
>> p - pointer to the memory piece we intend to release >> p - pointer to the memory piece we intend to release
>> size - size of the memory piece to release
*/ */
void XMem::Release(void * p) void XMem::Release(void * p, MTYPE size)
{ {
Release(devID, p); Release(devID, p, size);
} }
/* /*
release a piece of memory release a piece of memory
>> myDevID - device id >> myDevID - device id
>> p - pointer to the memory piece we intend to release >> p - pointer to the memory piece we intend to release
>> size - size of the memory piece to release
*/ */
void XMem::Release(int myDevID, void * p) void XMem::Release(int myDevID, void * p, MTYPE size)
{ {
if(mode == FREE_ON_THE_FLY) if(mode == FREE_ON_THE_FLY)
ReleaseStandard(myDevID, p); ReleaseStandard(myDevID, p, size);
} }
/* /*
...@@ -553,7 +565,7 @@ allocate a piece of memory as "malloc" ...@@ -553,7 +565,7 @@ allocate a piece of memory as "malloc"
*/ */
void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
{ {
CheckNTErrors(freeMemIndex != NULL, "The index of the memory pool is not initialized!"); CheckNTErrors(memIndex != NULL, "The index of the memory pool is not initialized!");
if(mySize <= minSizeIndex[0]) if(mySize <= minSizeIndex[0])
mySize = minSizeIndex[0]; mySize = minSizeIndex[0];
...@@ -567,20 +579,17 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -567,20 +579,17 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
/* search for the memory piece avialable for the allocation */ /* search for the memory piece avialable for the allocation */
for(int i = indexEntryNum; i > index; i--){ for(int i = indexEntryNum; i > index; i--){
if(i == indexEntryNum){ if(i == indexEntryNum){
entry = freeMemIndex + index; entry = memIndex + index;
CheckNTErrors(mySize >= minSizeIndex[index], "Wrong index!"); CheckNTErrors(mySize >= minSizeIndex[index], "Wrong index!");
} }
else else
entry = freeMemIndex + i; entry = memIndex + i;
if(entry->size == 0)
continue;
node = entry; node = entry->next;
while(node != NULL){ while(node != NULL){
if(node->size == 0){ if(node->size == 0){
MPieceNode * next = node->next; MPieceNode * next = node->next;
RemoveIndexNode(node, entry); RemoveFreeIndexNode(node, entry);
node = next; node = next;
} }
else{ else{
...@@ -598,41 +607,47 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -598,41 +607,47 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
/* if a free memory piece is found, we allocate the memory on it. */ /* if a free memory piece is found, we allocate the memory on it. */
if(hit != NULL){ if(hit != NULL){
MHeader * head = (MHeader*)hit->p; MHeader * head = &hit->head;
CheckNTErrors(head->state == 1, "Accessing the wrong memory piece!"); char * beg = (char*)GetPitchedAddress((char*)hit->p, MY_PITCH);
char * beg = (char*)GetPitchedAddress((char*)head + sizeof(MHeader) + sizeof(void*), MY_PITCH);
char * end = (char*)beg + mySize; char * end = (char*)beg + mySize;
MTYPE needed = end - (char*)head; MTYPE needed = end - (char*)hit->p;
MTYPE remaining = head->size - needed; MTYPE remaining = head->size - needed;
if(remaining >= minSizeIndex[0] + sizeof(MHeader) + sizeof(void*)){ if(remaining >= minSizeIndex[0]){
/* make a new header for the remaining segment in the memory piece */
MHeader * newHead = (MHeader*)end;
newHead->state = 1;
newHead->size = remaining;
newHead->pre = head;
newHead->next = head->next;
newHead->blockID = head->blockID;
head->next = newHead;
head->size = needed;
if(newHead->next != NULL)
newHead->next->pre = newHead;
/* make a new index node */ /* make a new index node */
MPieceNode * newNode = freeMemIndex + indexNodeNumUsed++; MPieceNode * newNode = memIndex + nodeNumUsed++;
newNode->head.indexNode = newNode;
newNode->p = end; newNode->p = end;
newNode->size = (char*)newHead + newHead->size - newNode->pReal = NULL;
(char*)GetPitchedAddress((char*)newNode->p + sizeof(MHeader) + sizeof(void*), MY_PITCH); newNode->size = (char*)end + remaining -
AddIndexNode(newNode); (char*)GetPitchedAddress((char*)end, MY_PITCH);
AddFreeIndexNode(newNode);
/* connections for headers */
MHeader &cur = hit->head;
MHeader &next = newNode->head;
next.pre = &cur;
next.next = cur.next;
cur.next = &next;
if(cur.next != NULL)
cur.next->pre = &next;
cur.size = needed;
next.state = 1;
next.size = remaining;
next.blockID = cur.blockID;
} }
head->state = 2; hit->size = mySize;
void * backPointer = (char*)beg - sizeof(void*); hit->head.state = 2;
*((MHeader**)backPointer) = head; hit->pReal = beg;
hit->size = 0; RemoveFreeIndexNode(hit);
AddAllocIndexNode(hit);
result = beg; result = beg;
} }
else{ else{
...@@ -648,7 +663,7 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -648,7 +663,7 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
for(bi = 0; bi < blockNum; bi++){ for(bi = 0; bi < blockNum; bi++){
XMemBlock * block = blocks + bi; XMemBlock * block = blocks + bi;
if(block->mem == NULL){ if(block->mem == NULL){
block->size = MAX(maxBlockSize, mySize + sizeof(MHeader) + sizeof(void*) + 2 * MY_PITCH); block->size = MAX(maxBlockSize, mySize + 2 * MY_PITCH);
if (myDevID < 0) { if (myDevID < 0) {
block->mem = new char[block->size]; block->mem = new char[block->size];
memset(block->mem, 0, block->size); memset(block->mem, 0, block->size);
...@@ -670,22 +685,26 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -670,22 +685,26 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
} }
curBlockID = MAX(curBlockID, bi); curBlockID = MAX(curBlockID, bi);
/* make a new header for the remaining segment in the memory piece */
MHeader * newHead = (MHeader*)block->mem;
newHead->state = 1;
newHead->size = block->size;
newHead->pre = NULL;
newHead->next = NULL;
newHead->blockID = bi;
/* make a new index node */ /* make a new index node */
MPieceNode * newNode = freeMemIndex + indexNodeNumUsed++; MPieceNode * newNode = memIndex + nodeNumUsed++;
newNode->p = newHead; newNode->head.indexNode = newNode;
newNode->size = (char*)newHead + newHead->size - newNode->p = block->mem;
(char*)GetPitchedAddress((char*)newHead + sizeof(MHeader) + sizeof(void*), MY_PITCH); newNode->pReal = NULL;
AddIndexNode(newNode); newNode->size = (char*)block->mem + mySize -
(char*)GetPitchedAddress(block->mem, MY_PITCH);
AddFreeIndexNode(newNode);
MHeader &header = newNode->head;
header.state = 1;
header.size = block->size;
header.pre = NULL;
header.next = NULL;
header.blockID = bi;
block->head = &header;
result = AllocStandard(myDevID, mySize, myIsRebuiltIndex); result = AllocStandard(myDevID, mySize, myIsRebuiltIndex);
break; break;
} }
...@@ -695,10 +714,13 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex) ...@@ -695,10 +714,13 @@ void * XMem::AllocStandard(int myDevID, MTYPE mySize, bool myIsRebuiltIndex)
} }
/* if all index nodes are used, we rebuild the index to release the nodes that are free */ /* if all index nodes are used, we rebuild the index to release the nodes that are free */
if(indexNodeNumUsed == indexNodeNum){ if(nodeNumUsed == nodeNum){
RebuildIndex(); RebuildIndex();
CheckNTErrors(indexNodeNumUsed < indexNodeNum, "No enough index nodes for the memory pool!"); CheckNTErrors(nodeNumUsed < nodeNum, "No enough index nodes for the memory pool!");
} }
//if(testxmemid == 106)
// recordp = result;
return result; return result;
} }
...@@ -749,61 +771,73 @@ int XMem::FindIndexEntry(MTYPE mySize) ...@@ -749,61 +771,73 @@ int XMem::FindIndexEntry(MTYPE mySize)
} }
/* /*
remove an index node remove an index node for available memory pieces
>> 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::RemoveIndexNode(MPieceNode * node, MPieceNode * entry) void XMem::RemoveFreeIndexNode(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!");
if(pre == NULL){ pre->next = next;
if(entry == NULL){ if(next != NULL)
entry = freeMemIndex + FindIndexEntry(node->size); next->pre = pre;
CheckNTErrors(entry == node, "Illegal index node!");
} node->pre = NULL;
*entry = *next; node->next = NULL;
entry->pre = NULL;
}
else{
pre->next = next;
if(next != NULL)
next->pre = pre;
}
memset(node, 0, sizeof(MPieceNode));
} }
/* /*
add an index node add an index node for available memory pieces
>> node - node to add >> node - node to add
>> entry - the entry of the list to append the node >> entry - the entry of the list to append the node
*/ */
void XMem::AddIndexNode(MPieceNode * node, MPieceNode * entry) void XMem::AddFreeIndexNode(MPieceNode * node, MPieceNode * entry)
{ {
MPieceNode * entryForMe = entry != NULL ? entry : MPieceNode * entryForMe = entry != NULL ? entry :
freeMemIndex + FindIndexEntry(node->size); memIndex + FindIndexEntry(node->size);
MPieceNode * backup = entryForMe->next;
entryForMe->next = node;
node->pre = entryForMe;
node->next = backup;
if(backup != NULL)
backup->pre = node;
if(entryForMe->size == 0){ CheckNTErrors(node != node->next, "Something wrong with the index node!");
entryForMe->size = node->size; CheckNTErrors(node != node->pre, "Something wrong with the index node!");
entryForMe->p = node->p; }
entryForMe->pre = NULL;
entryForMe->next = NULL; /*
} remove an index node for memory pieces in use
else{ >> node - node to remove
MTYPE tmpSize = entryForMe->size; >> - the entry of the list that keeps the node
void * tmpP = entryForMe->p; */
entryForMe->size = node->size; void XMem::RemoveAllocIndexNode(MPieceNode * node, MPieceNode * entry)
entryForMe->p = node->p; {
node->size = tmpSize; RemoveFreeIndexNode(node, entry);
node->p = tmpP; }
node->next = entryForMe->next;
node->pre = entryForMe;
entryForMe->next = node;
}
/*
add an index node for memory pieces in use
>> node - node to add
>> entry - the entry of the list to append the node
*/
void XMem::AddAllocIndexNode(MPieceNode * node, MPieceNode * entry)
{
MPieceNode * entryForMe = entry != NULL ? entry :
memIndex + indexEntryNum + FindIndexEntry(node->size);
MPieceNode * backup = entryForMe->next;
entryForMe->next = node;
node->pre = entryForMe;
node->next = backup;
if(backup != NULL)
backup->pre = node;
CheckNTErrors(node != node->next, "Something wrong with the index node!"); CheckNTErrors(node != node->next, "Something wrong with the index node!");
CheckNTErrors(node != node->pre, "Something wrong with the index node!"); CheckNTErrors(node != node->pre, "Something wrong with the index node!");
} }
...@@ -812,39 +846,71 @@ void XMem::AddIndexNode(MPieceNode * node, MPieceNode * entry) ...@@ -812,39 +846,71 @@ void XMem::AddIndexNode(MPieceNode * node, MPieceNode * entry)
release a piece of memory as "free" release a piece of memory as "free"
>> myDevID - device id(-1: CPU memory, >=0: GPU device ID) >> myDevID - device id(-1: CPU memory, >=0: GPU device ID)
>> p - the pointer to the address of the memory we intend to free >> p - the pointer to the address of the memory we intend to free
>> size - size of the memory piece to release
*/ */
void XMem::ReleaseStandard(int myDevID, void * p) void XMem::ReleaseStandard(int myDevID, void * p, MTYPE size)
{ {
if(p == NULL) if(p == NULL)
return; return;
void * back = (char*)p - sizeof(void*); if(size <= minSizeIndex[0])
MHeader * head = *(MHeader**)back; size = minSizeIndex[0];
CheckNTErrors(head->state == 2, "Illegal header of a memory piece!"); MPieceNode * entry = NULL;
head->state = 1; MPieceNode * node = NULL;
MPieceNode * hit = NULL;
/* make a new index node */ MPieceNode * last = NULL;
MPieceNode * newNode = freeMemIndex + indexNodeNumUsed++;
newNode->p = head; entry = memIndex + indexEntryNum + FindIndexEntry(size);
newNode->size = (char*)head + head->size -
(char*)GetPitchedAddress((char*)head + sizeof(MHeader) + sizeof(void*), MY_PITCH); last = entry;
AddIndexNode(newNode); node = entry->next;
while(node != NULL){
CheckNTErrors(node->pre == last, "Something is wrong!");
CheckNTErrors(last->next == node, "Something is wrong!");
CheckNTErrors(node->head.state == 2, "Something is wrong!");
last = node;
if(node->size == 0){
MPieceNode * next = node->next;
RemoveFreeIndexNode(node, entry);
node = next;
ShowNTErrors("Something is wrong!");
}
else{
CheckNTErrors(node->pReal != NULL, "Illegal pointer!");
if(node->pReal == p){
hit = node;
break;
}
node = node->next;
}
}
CheckNTErrors(hit != NULL, "No header is found!");
hit->head.state = 1;
RemoveAllocIndexNode(hit);
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 */
void XMem::RebuildIndex() void XMem::RebuildIndex()
{ {
indexNodeNumUsed = indexEntryNum; int nodeNumUsed2 = indexEntryNum * 2;
memset(freeMemIndex, 0, sizeof(MPieceNode) * indexEntryNum); memset(memIndex2, 0, sizeof(MPieceNode) * indexEntryNum * 2);
for(int bi = 0; bi <= curBlockID; bi++){ for(int bi = 0; bi <= curBlockID; bi++){
XMemBlock * block = blocks + bi; XMemBlock * block = blocks + bi;
if(block->mem == NULL) if(block->mem == NULL)
continue; continue;
MHeader * head = (MHeader*)block->mem; MHeader * head = block->head;
CheckNTErrors(head->size <= block->size, "Illegal memory block!"); CheckNTErrors(head->size <= block->size, "Illegal memory block!");
block->head = NULL;
/* if the block is not used, we delete it */ /* if the block is not used, we delete it */
if(head->state == 1 && head->size == block->size){ if(head->state == 1 && head->size == block->size){
...@@ -870,6 +936,7 @@ void XMem::RebuildIndex() ...@@ -870,6 +936,7 @@ void XMem::RebuildIndex()
/* if the block is in use, we build the index */ /* if the block is in use, we build the index */
int pieceCount = 0; int pieceCount = 0;
MTYPE size = 0; MTYPE size = 0;
MHeader * newLast = NULL;
while(head != NULL){ while(head != NULL){
MHeader * next = head->next; MHeader * next = head->next;
if(head->state == 1){ if(head->state == 1){
...@@ -878,21 +945,93 @@ void XMem::RebuildIndex() ...@@ -878,21 +945,93 @@ void XMem::RebuildIndex()
next = next->next; next = next->next;
} }
head->next = next; head->next = next;
/* make a new index node */
MPieceNode * newNode = freeMemIndex + indexNodeNumUsed++;
newNode->p = head;
newNode->size = (char*)head + head->size -
(char*)GetPitchedAddress((char*)head + sizeof(MHeader) + sizeof(void*), MY_PITCH);
AddIndexNode(newNode);
} }
size += head->size;
MPieceNode * node = head->indexNode;
void * p = node->p;
/* make a new index node */
MPieceNode * newNode = memIndex2 + nodeNumUsed2++;
newNode->p = p;
newNode->size = (char*)p + node->size - (char*)GetPitchedAddress((char*)p, MY_PITCH);
newNode->pre = NULL;
newNode->next = NULL;
CheckNTErrors(newNode->size > 0, "Illegal index node!");
MHeader * newHeader = &newNode->head;
newHeader->indexNode = newNode;
newHeader->pre = newLast;
newHeader->next = NULL;
newHeader->blockID = bi;
newHeader->size = head->size;
newHeader->state = head->state;
if(newLast != NULL)
newLast->next = newHeader;
newHeader->pre = newLast;
if(head->state == 1){
newNode->pReal = NULL;
MPieceNode * entry = memIndex2 + FindIndexEntry(newNode->size);
AddFreeIndexNode(newNode, entry);
}
else{
newNode->pReal = head->indexNode->pReal;
MPieceNode * entry = memIndex2 + indexEntryNum + FindIndexEntry(newNode->size);
AddAllocIndexNode(newNode, entry);
}
if(newLast == NULL)
block->head = newHeader;
pieceCount++; pieceCount++;
head = next; size += head->size;
CheckNTErrors(size <= block->size, "Illegal block size!"); CheckNTErrors(size <= block->size, "Illegal block size!");
newLast = newHeader;
head = next;
} }
} }
} }
MPieceNode * backup = memIndex2;
memIndex2 = memIndex;
memIndex = backup;
/*bool hhh = recordp != NULL ? false : true;
for(int i = 0; i < indexEntryNum; i++){
MPieceNode * entry = memIndex + indexEntryNum + i;
MPieceNode * last = entry;
MPieceNode * node = entry->next;
while(node != NULL){
CheckNTErrors(node->pre == last, "XSomething is wrong!");
CheckNTErrors(last->next == node, "XSomething is wrong!");
last = node;
if(node->pReal == recordp)
hhh = true;
if(node->size == 0){
MPieceNode * next = node->next;
node = next;
}
else{
CheckNTErrors(node->pReal != NULL, "Illegal pointer!");
node = node->next;
}
}
}
if(!hhh){
int nnn = 0;
}*/
nodeNumUsed = nodeNumUsed2;
} }
/* /*
......
...@@ -59,6 +59,8 @@ mode of runnig a memory pool ...@@ -59,6 +59,8 @@ mode of runnig a memory pool
- FREE_ON_THE_FLY: run in normal "malloc" and "free" ways - FREE_ON_THE_FLY: run in normal "malloc" and "free" ways
*/ */
enum MEMPOOL_MODE {UNI_FREE, FREE_ON_THE_FLY}; enum MEMPOOL_MODE {UNI_FREE, FREE_ON_THE_FLY};
struct MPieceNode;
/* header of a memory piece (FREE_ON_THE_FLY) */ /* header of a memory piece (FREE_ON_THE_FLY) */
struct MHeader struct MHeader
...@@ -80,6 +82,9 @@ struct MHeader ...@@ -80,6 +82,9 @@ struct MHeader
/* id of the memory block */ /* id of the memory block */
int blockID; int blockID;
/* pointer to the index node */
MPieceNode * indexNode;
}; };
/* index of memory piece */ /* index of memory piece */
...@@ -96,9 +101,12 @@ struct MPieceNode ...@@ -96,9 +101,12 @@ struct MPieceNode
/* pointer to the head of a memory piece */ /* pointer to the head of a memory piece */
void * p; void * p;
/* pointer to the head of memory that is returned back to the user */
void * pReal;
/* header of the memory piece */ /* header of the memory piece */
MHeader * head; MHeader head;
}; };
/* memory block */ /* memory block */
...@@ -115,6 +123,9 @@ struct XMemBlock ...@@ -115,6 +123,9 @@ struct XMemBlock
/* desired size of the block */ /* desired size of the block */
MTYPE sizeDesired; MTYPE sizeDesired;
/* first head of the block */
MHeader * head;
}; };
/* /*
...@@ -197,13 +208,16 @@ public: ...@@ -197,13 +208,16 @@ public:
public: public:
/* index of the free memory pieces */ /* index of the free memory pieces */
MPieceNode * freeMemIndex; MPieceNode * memIndex;
/* for double buffering */
MPieceNode * memIndex2;
/* maximum number of index nodes */ /* maximum number of index nodes */
INT_64 indexNodeNum; INT_64 nodeNum;
/* count of the used nodes */ /* count of the used nodes */
INT_64 indexNodeNumUsed; INT_64 nodeNumUsed;
/* minimal size allocation for each index entry */ /* minimal size allocation for each index entry */
MTYPE * minSizeIndex; MTYPE * minSizeIndex;
...@@ -285,10 +299,10 @@ public: ...@@ -285,10 +299,10 @@ public:
void * AllocBuf(int myDevID, MTYPE mySize, int pitch = BUF_PITCH); void * AllocBuf(int myDevID, MTYPE mySize, int pitch = BUF_PITCH);
/* release a piece of memory */ /* release a piece of memory */
void Release(void * p); void Release(void * p, MTYPE size);
/* release a piece of memory */ /* release a piece of memory */
void Release(int myDevID, void * p); void Release(int myDevID, void * p, MTYPE size);
/* release a piece of memory in the buffer */ /* release a piece of memory in the buffer */
void ReleaseBuf(int myDevID, MTYPE mySize, int pitch = BUF_PITCH); void ReleaseBuf(int myDevID, MTYPE mySize, int pitch = BUF_PITCH);
...@@ -305,14 +319,20 @@ public: ...@@ -305,14 +319,20 @@ public:
/* find the index entry for allocation query */ /* find the index entry for allocation query */
int FindIndexEntry(MTYPE mySize); int FindIndexEntry(MTYPE mySize);
/* remove an index node */ /* remove an index node for available memory pieces */
void RemoveIndexNode(MPieceNode * node, MPieceNode * entry = NULL); void RemoveFreeIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
/* add an index node */ /* add an index node for available memory pieces */
void AddIndexNode(MPieceNode * node, MPieceNode * entry = NULL); void AddFreeIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
/* remove an index node for memory pieces in use */
void RemoveAllocIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
/* add an index node for available memory pieces ∂*/
void AddAllocIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
/* release a piece of memory as "free" */ /* release a piece of memory as "free" */
void ReleaseStandard(int myDevID, void * p); void ReleaseStandard(int myDevID, void * p, MTYPE size);
/* 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 */
void RebuildIndex(); void RebuildIndex();
...@@ -382,6 +402,9 @@ public: ...@@ -382,6 +402,9 @@ public:
extern XMem * GMem; extern XMem * GMem;
extern int testxmemid;
extern void * recordp;
} /* end of the nts (NiuTrans.Tensor) namespace */ } /* end of the nts (NiuTrans.Tensor) namespace */
#endif #endif
...@@ -254,7 +254,7 @@ void XTensor::DestroyData() ...@@ -254,7 +254,7 @@ void XTensor::DestroyData()
else if(data != NULL && isInGlobalMem) else if(data != NULL && isInGlobalMem)
FreeData(this, mem); FreeData(this, mem);
else if(data != NULL) else if(data != NULL)
mem->Release(data); mem->Release(data, GetDataSizeInChar());
data = NULL; data = NULL;
if(dataHost != NULL) if(dataHost != NULL)
...@@ -1135,18 +1135,20 @@ resize a tensor with a specified tensor size ...@@ -1135,18 +1135,20 @@ resize a tensor with a specified tensor size
bool XTensor::Resize(const int myOrder, const int * myDimSize, bool XTensor::Resize(const int myOrder, const int * myDimSize,
const TENSOR_DATA_TYPE myDataType, const float myDenseRatio) const TENSOR_DATA_TYPE myDataType, const float myDenseRatio)
{ {
order = myOrder;
unitNum = 1;
unitNumNonZero = 0;
isInit = true;
/* free old mem */ /* free old mem */
if(data != NULL){ if(data != NULL){
if (mem == NULL) if (mem == NULL)
XMemFree(devID, data); XMemFree(devID, data);
else else
mem->Release(data); mem->Release(data, GetDataSizeInChar());
} }
order = myOrder;
unitNum = 1;
unitNumNonZero = 0;
isInit = true;
bool filledData = true; bool filledData = true;
bool zeroData = false; bool zeroData = false;
......
...@@ -63,6 +63,8 @@ bool TestXMemCase1() ...@@ -63,6 +63,8 @@ bool TestXMemCase1()
} }
for (int i = 0; i < testNum * scalar; i++) { for (int i = 0; i < testNum * scalar; i++) {
testxmemid++;
//fprintf(stderr, "%d\n", testxmemid);
int j = rand() % caseNum; int j = rand() % caseNum;
if (p[j] == NULL) { if (p[j] == NULL) {
...@@ -71,7 +73,7 @@ bool TestXMemCase1() ...@@ -71,7 +73,7 @@ bool TestXMemCase1()
p[j][k] = j; p[j][k] = j;
} }
else { else {
mem.ReleaseStandard(mem.devID, p[j]); mem.ReleaseStandard(mem.devID, p[j], size[j] * sizeof(int));
for (int k = 0; k < size[j]; k++) for (int k = 0; k < size[j]; k++)
p[j][k] = -1; p[j][k] = -1;
p[j] = NULL; p[j] = NULL;
...@@ -86,6 +88,61 @@ bool TestXMemCase1() ...@@ -86,6 +88,61 @@ bool TestXMemCase1()
} }
} }
} }
/*MPieceNode * entry = NULL;
MPieceNode * node = NULL;
entry = mem.memIndex + mem.indexEntryNum + mem.FindIndexEntry(112);
int cc = 0;
node = entry->next;
while(node != NULL){
fprintf(stderr, "%d ", cc++);
if(node->size == 0){
MPieceNode * next = node->next;
node = next;
}
else{
CheckNTErrors(node->pReal != NULL, "Illegal pointer!");
node = node->next;
}
}
fprintf(stderr, "\n");*/
/*int ccc = 0;
bool hhh = recordp != NULL ? false : true;
for(int i = 0; i < mem.indexEntryNum; i++){
MPieceNode * entry = mem.memIndex + mem.indexEntryNum + i;
MPieceNode * last = entry;
MPieceNode * node = entry->next;
ccc = 0;
while(node != NULL){
CheckNTErrors(node->pre == last, "XSomething is wrong!");
CheckNTErrors(last->next == node, "XSomething is wrong!");
last = node;
ccc++;
if(node->pReal == recordp){
hhh = true;
}
if(node->size == 0){
MPieceNode * next = node->next;
node = next;
}
else{
CheckNTErrors(node->pReal != NULL, "Illegal pointer!");
node = node->next;
}
}
}
if(!hhh){
int nnn = 0;
}*/
} }
delete[] p; delete[] p;
...@@ -128,4 +185,4 @@ bool TestXMem() ...@@ -128,4 +185,4 @@ bool TestXMem()
return returnFlag; return returnFlag;
} }
} // namespace nts(NiuTrans.Tensor) } // namespace nts(NiuTrans.Tensor)
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论