Commit 91fdf94e by xiaotong

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

parent 97b41e35
...@@ -60,6 +60,8 @@ mode of runnig a memory pool ...@@ -60,6 +60,8 @@ mode of runnig a memory pool
*/ */
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 */
...@@ -97,8 +102,11 @@ struct MPieceNode ...@@ -97,8 +102,11 @@ 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 for available memory pieces */
void AddFreeIndexNode(MPieceNode * node, MPieceNode * entry = NULL);
/* add an index node */ /* remove an index node for memory pieces in use */
void AddIndexNode(MPieceNode * node, MPieceNode * entry = NULL); 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,19 +1135,21 @@ resize a tensor with a specified tensor size ...@@ -1135,19 +1135,21 @@ 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;
for(int i = 0; i < order; i++){ for(int i = 0; i < order; i++){
......
...@@ -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;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论