Commit cb9c88cb by xiaotong

add signature to XMem and XTensor

parent 0e2bd767
...@@ -46,6 +46,7 @@ XMem::XMem() ...@@ -46,6 +46,7 @@ XMem::XMem()
indexOffset = -1; indexOffset = -1;
name = new char[64]; name = new char[64];
strcpy(name, "xmem"); strcpy(name, "xmem");
signature = 0;
} }
/* /*
...@@ -67,6 +68,7 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum, ...@@ -67,6 +68,7 @@ XMem::XMem(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int myBlockNum,
indexOffset = -1; indexOffset = -1;
name = new char[64]; name = new char[64];
strcpy(name, "xmem"); strcpy(name, "xmem");
signature = 0;
Initialize(myDevID, myMode, myBlockSize, myBlockNum, myBufSize); Initialize(myDevID, myMode, myBlockSize, myBlockNum, myBufSize);
} }
...@@ -153,6 +155,8 @@ void XMem::Initialize(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int m ...@@ -153,6 +155,8 @@ void XMem::Initialize(int myDevID, MEMPOOL_MODE myMode, MTYPE myBlockSize, int m
if (myMode == FREE_ON_THE_FLY) if (myMode == FREE_ON_THE_FLY)
SetIndex(MILLION); SetIndex(MILLION);
signature++;
} }
/* free memory */ /* free memory */
...@@ -189,11 +193,16 @@ void XMem::Free(int myDevID, void * mem) ...@@ -189,11 +193,16 @@ void XMem::Free(int myDevID, void * mem)
/* on GPUs */ /* on GPUs */
else{ else{
#ifdef USE_CUDA #ifdef USE_CUDA
int devIDBackup = -1;
cudaGetDevice(&devIDBackup);
SetDevice(myDevID); SetDevice(myDevID);
cudaError_t error = cudaFree((char*)mem); cudaError_t error = cudaFree((char*)mem);
if(error != cudaSuccess){ if(error != cudaSuccess){
ShowNTErrors("Cannot free the memory."); ShowNTErrors("Cannot free the memory.");
} }
SetDevice(devIDBackup);
#else #else
ShowNTErrors("Please specify USE_CUDA for compiling this program."); ShowNTErrors("Please specify USE_CUDA for compiling this program.");
#endif #endif
...@@ -201,6 +210,15 @@ void XMem::Free(int myDevID, void * mem) ...@@ -201,6 +210,15 @@ void XMem::Free(int myDevID, void * mem)
} }
/* /*
get signature
<< return - return the signature
*/
MTYPE XMem::GetSignature()
{
return signature;
}
/*
use string as the name of the memory pool use string as the name of the memory pool
>> myName - name of the memory pool >> myName - name of the memory pool
*/ */
...@@ -510,10 +528,12 @@ void * XMem::AllocBuf(int myDevID, MTYPE mySize, int pitch) ...@@ -510,10 +528,12 @@ 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 >> size - size of the memory piece to release
>> code - code the memory
*/ */
void XMem::Release(void * p, MTYPE size) void XMem::Release(void * p, MTYPE size, MTYPE code)
{ {
Release(devID, p, size); if(code == signature)
Release(devID, p, size);
} }
/* /*
...@@ -998,39 +1018,7 @@ void XMem::RebuildIndex() ...@@ -998,39 +1018,7 @@ void XMem::RebuildIndex()
MPieceNode * backup = memIndex2; MPieceNode * backup = memIndex2;
memIndex2 = memIndex; memIndex2 = memIndex;
memIndex = backup; 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; nodeNumUsed = nodeNumUsed2;
} }
...@@ -1103,10 +1091,32 @@ void * XMem::GetAddress() ...@@ -1103,10 +1091,32 @@ void * XMem::GetAddress()
/* clear it */ /* clear it */
void XMem::Clear() void XMem::Clear()
{ {
for(int i = 0; i < blockNum; i++) if (mode == UNI_FREE) {
blocks[i].used = 0; for (int i = 0; i < blockNum; i++)
curBlock = blocks; blocks[i].used = 0;
curBlockID = 0; curBlock = blocks;
curBlockID = 0;
}
else if (mode == FREE_ON_THE_FLY) {
nodeNumUsed = indexEntryNum * 2;
memset(memIndex, 0, sizeof(MPieceNode) * indexEntryNum * 2);
for (int i = 0; i <= curBlockID; i++) {
blocks[i].head = NULL;
blocks[i].used = 0;
if (i > 0) {
blocks[i].size = blocks[i].sizeDesired;
Free(devID, blocks[i].mem);
blocks[i].mem = NULL;
}
}
curBlock = blocks;
curBlockID = 0;
}
else {
ShowNTErrors("Something is wrong!");
}
signature++;
} }
/* clear the buffer */ /* clear the buffer */
......
...@@ -152,6 +152,9 @@ public: ...@@ -152,6 +152,9 @@ public:
/* mode of running the memory pool */ /* mode of running the memory pool */
MEMPOOL_MODE mode; MEMPOOL_MODE mode;
/* signature */
MTYPE signature;
/* indicates whether the memory allocation is static */ /* indicates whether the memory allocation is static */
bool isStatic; bool isStatic;
...@@ -252,6 +255,9 @@ public: ...@@ -252,6 +255,9 @@ public:
/* free a piece of memory */ /* free a piece of memory */
void Free(int myDevID, void * mem); void Free(int myDevID, void * mem);
/* get signature */
MTYPE GetSignature();
/* use string as the name of the memory pool */ /* use string as the name of the memory pool */
void SetName(const char * myName); void SetName(const char * myName);
...@@ -299,7 +305,7 @@ public: ...@@ -299,7 +305,7 @@ 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, MTYPE size); void Release(void * p, MTYPE size, MTYPE code);
/* release a piece of memory */ /* release a piece of memory */
void Release(int myDevID, void * p, MTYPE size); void Release(int myDevID, void * p, MTYPE size);
......
...@@ -221,7 +221,8 @@ XTensor::~XTensor() ...@@ -221,7 +221,8 @@ XTensor::~XTensor()
void XTensor::Init() void XTensor::Init()
{ {
id = -1; id = -1;
mem = NULL;; mem = NULL;
signature = 0;
data = NULL; data = NULL;
dataHost = NULL; dataHost = NULL;
dataP = NULL; dataP = NULL;
...@@ -254,7 +255,7 @@ void XTensor::DestroyData() ...@@ -254,7 +255,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, GetDataSizeInChar()); mem->Release(data, GetDataSizeInChar(), signature);
data = NULL; data = NULL;
if(dataHost != NULL) if(dataHost != NULL)
...@@ -1135,15 +1136,15 @@ resize a tensor with a specified tensor size ...@@ -1135,15 +1136,15 @@ 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)
{ {
/* 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, GetDataSizeInChar()); mem->Release(data, GetDataSizeInChar(), signature);
} }
signature = mem != NULL ? mem->GetSignature() : 0;
order = myOrder; order = myOrder;
unitNum = 1; unitNum = 1;
...@@ -1245,56 +1246,6 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize, ...@@ -1245,56 +1246,6 @@ bool XTensor::Resize(const int myOrder, const int * myDimSize,
} }
/* /*
resize a tensor with a specified tensor size (with no data filled)
>> myOrder - order of the tensor
>> myDimSize - the size of each dimension
>> myDataType - unit size (e.g., int, float, and double)
>> myDenseRatio - how often an element has non-zero value
<< return - succeeded or not
*/
bool XTensor::ResizeWithNoData(const int myOrder, const int * myDimSize,
const TENSOR_DATA_TYPE myDataType, const float myDenseRatio)
{
order = myOrder;
unitNum = 1;
unitNumNonZero = 0;
/* free old mem */
if(data != NULL && mem == NULL)
delete[] (char*)data;
bool filledData = true;
bool zeroData = false;
for(int i = 0; i < order; i++){
dimSize[i] = abs(myDimSize[i]);
dimSizeRDI[order - i - 1] = dimSize[i];
if(myDimSize[i] < 0)
filledData = false;
if(myDimSize[i] == 0)
zeroData = true;
unitNum *= dimSize[i];
}
data = NULL;
denseRatio = myDenseRatio;
isSparse = denseRatio < 1.0F ? true : false;
dataType = myDataType;
unitSize = GetUnitSize(dataType);
if(myDataType != DEFAULT_DTYPE)
isDefaultDType = false;
else
isDefaultDType = true;
if(zeroData){
unitNum = 0;
return false;
}
return true;
}
/*
resize a tensor by another one resize a tensor by another one
>> myTensor - tensor for reference >> myTensor - tensor for reference
*/ */
...@@ -1672,6 +1623,8 @@ void XTensor::AllocateData(XTensor * tensor, XMem * myMem, bool useBuf) ...@@ -1672,6 +1623,8 @@ void XTensor::AllocateData(XTensor * tensor, XMem * myMem, bool useBuf)
tensor->isInGlobalMem = true; tensor->isInGlobalMem = true;
} }
} }
tensor->signature = 0;
} }
/* /*
......
...@@ -51,7 +51,6 @@ struct XLink; ...@@ -51,7 +51,6 @@ struct XLink;
#define MIN_TENSOR_SPLIT_LIST_NUM 1024 #define MIN_TENSOR_SPLIT_LIST_NUM 1024
#define MIN_TENSOR_CAT_NUM 8 #define MIN_TENSOR_CAT_NUM 8
/* computation flags */ /* computation flags */
#define UNSAFE_BUT_FAST_MEM #define UNSAFE_BUT_FAST_MEM
#define FAST_MATRIX #define FAST_MATRIX
...@@ -66,6 +65,9 @@ public: ...@@ -66,6 +65,9 @@ public:
/* memory pool */ /* memory pool */
XMem * mem; XMem * mem;
/* signature of the memory pool */
MTYPE signature;
/* data array to keep the elements */ /* data array to keep the elements */
void * data; void * data;
...@@ -327,11 +329,6 @@ public: ...@@ -327,11 +329,6 @@ public:
const TENSOR_DATA_TYPE myDataType = DEFAULT_DTYPE, const TENSOR_DATA_TYPE myDataType = DEFAULT_DTYPE,
const float myDenseRatio = 1.0F); const float myDenseRatio = 1.0F);
/* resize a matrix with a specified matrix size (with no data filled) */
bool ResizeWithNoData(const int myOrder, const int * myDimSize,
const TENSOR_DATA_TYPE myDataType = DEFAULT_DTYPE,
const float myDenseRatio = 1.0F);
/* resize a matrix by another one */ /* resize a matrix by another one */
bool Resize(const XTensor * myTensor); bool Resize(const XTensor * myTensor);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论