Commit 55965160 by liyinqiao

Remove dataP in XTensor and use const_cast to modify the const data.

parent 893a0938
...@@ -87,7 +87,6 @@ int MakeTensorID() ...@@ -87,7 +87,6 @@ int MakeTensorID()
XTensor::XTensor() XTensor::XTensor()
{ {
Init(); Init();
SetDataPointer();
id = MakeTensorID(); id = MakeTensorID();
isDefaultDType = true; isDefaultDType = true;
...@@ -100,7 +99,6 @@ XTensor::XTensor() ...@@ -100,7 +99,6 @@ XTensor::XTensor()
XTensor::XTensor(const XTensor* reference) XTensor::XTensor(const XTensor* reference)
{ {
Init(); Init();
SetDataPointer();
id = MakeTensorID(); id = MakeTensorID();
InitTensorV2(this, reference); InitTensorV2(this, reference);
...@@ -117,7 +115,6 @@ XTensor::XTensor(const int myOrder, int myDevID, XMem* myMem) ...@@ -117,7 +115,6 @@ XTensor::XTensor(const int myOrder, int myDevID, XMem* myMem)
CheckNTErrors((myOrder >= 0), "Illegal tensor order!"); CheckNTErrors((myOrder >= 0), "Illegal tensor order!");
Init(); Init();
SetDataPointer();
id = MakeTensorID(); id = MakeTensorID();
order = myOrder; order = myOrder;
...@@ -138,7 +135,6 @@ XTensor::XTensor(const int myOrder, const int * myDimSize, const TENSOR_DATA_TYP ...@@ -138,7 +135,6 @@ XTensor::XTensor(const int myOrder, const int * myDimSize, const TENSOR_DATA_TYP
const float myDenseRatio, int myDevID, XMem * myMem) const float myDenseRatio, int myDevID, XMem * myMem)
{ {
Init(); Init();
SetDataPointer();
id = MakeTensorID(); id = MakeTensorID();
order = myOrder; order = myOrder;
...@@ -153,7 +149,6 @@ XTensor::XTensor(const int myOrder, const int * myDimSize, const TENSOR_DATA_TYP ...@@ -153,7 +149,6 @@ XTensor::XTensor(const int myOrder, const int * myDimSize, const TENSOR_DATA_TYP
XTensor::XTensor(const XTensor& reference) XTensor::XTensor(const XTensor& reference)
{ {
Init(); Init();
SetDataPointer();
id = MakeTensorID(); id = MakeTensorID();
ShallowCopy(reference); ShallowCopy(reference);
data = NULL; data = NULL;
...@@ -164,13 +159,7 @@ XTensor::XTensor(const XTensor& reference) ...@@ -164,13 +159,7 @@ XTensor::XTensor(const XTensor& reference)
mem = reference.mem; mem = reference.mem;
data = reference.data; data = reference.data;
signature = reference.signature; signature = reference.signature;
const_cast<XTensor&>(reference).data = NULL;
/* what we really want to do is "reference.data = NULL;"
As "reference" is constant, we cannot reset "reference.data"
here. So we save the ADDRESS of "reference.data" in
"reference.dataP", and do this work by updating "*reference.dataP".
This is VERY tricky and there might be better solutions :) */
*reference.dataP = NULL;
} }
else{ else{
devID = reference.devID; devID = reference.devID;
...@@ -196,7 +185,6 @@ XTensor::XTensor(const XTensor& reference) ...@@ -196,7 +185,6 @@ XTensor::XTensor(const XTensor& reference)
XTensor::XTensor(const XTensor&& reference) XTensor::XTensor(const XTensor&& reference)
{ {
Init(); Init();
SetDataPointer();
id = MakeTensorID(); id = MakeTensorID();
ShallowCopy(reference); ShallowCopy(reference);
data = NULL; data = NULL;
...@@ -206,13 +194,7 @@ XTensor::XTensor(const XTensor&& reference) ...@@ -206,13 +194,7 @@ XTensor::XTensor(const XTensor&& reference)
mem = reference.mem; mem = reference.mem;
data = reference.data; data = reference.data;
signature = reference.signature; signature = reference.signature;
const_cast<XTensor&>(reference).data = NULL;
/* what we really want to do is "reference.data = NULL;"
As "reference" is constant, we cannot reset "reference.data"
here. So we save the ADDRESS of "reference.data" in
"reference.dataP", and do this work by updating "*reference.dataP".
This is VERY tricky and there might be better solutions :) */
*reference.dataP = NULL;
if (reference.enableGrad) { if (reference.enableGrad) {
XLink::Replace(&reference, this); XLink::Replace(&reference, this);
...@@ -268,7 +250,6 @@ void XTensor::Init() ...@@ -268,7 +250,6 @@ void XTensor::Init()
signature = 0; signature = 0;
data = NULL; data = NULL;
dataHost = NULL; dataHost = NULL;
dataP = NULL;
devID = -1; devID = -1;
order = -1; order = -1;
memset(dimSize, 0, sizeof(int) * MAX_TENSOR_DIM_NUM); memset(dimSize, 0, sizeof(int) * MAX_TENSOR_DIM_NUM);
...@@ -452,13 +433,7 @@ XTensor& XTensor::operator= (const XTensor&& tensor) ...@@ -452,13 +433,7 @@ XTensor& XTensor::operator= (const XTensor&& tensor)
mem = tensor.mem; mem = tensor.mem;
data = tensor.data; data = tensor.data;
signature = tensor.signature; signature = tensor.signature;
const_cast<XTensor&>(tensor).data = NULL;
/* what we really want to do is "reference.data = NULL;"
As "reference" is constant, we cannot reset "reference.data"
here. So we save the ADDRESS of "reference.data" in
"reference.dataP", and do this work by updating "*reference.dataP".
This is VERY tricky and there might be better solutions :) */
*tensor.dataP = NULL;
if (enableGrad) { if (enableGrad) {
XLink::Copy(&tensor, this); XLink::Copy(&tensor, this);
...@@ -909,12 +884,6 @@ void XTensor::SetDataBatchedWithValues(MTYPE* offsets, void* values, int num) ...@@ -909,12 +884,6 @@ void XTensor::SetDataBatchedWithValues(MTYPE* offsets, void* values, int num)
_SetDataWithOffsetAndValue(this, offsets, values, num); _SetDataWithOffsetAndValue(this, offsets, values, num);
} }
/* set the pointer to "data" */
void XTensor::SetDataPointer()
{
dataP = &data;
}
/* /*
get the value of a cell with the index get the value of a cell with the index
>> index - index of each dimension >> index - index of each dimension
......
...@@ -82,10 +82,6 @@ public: ...@@ -82,10 +82,6 @@ public:
when the tensor is operated on GPUs */ when the tensor is operated on GPUs */
void * dataHost; void * dataHost;
/* a pointer to data (i.e., a pointer to the address of "data".
This is for reset "data" when XTensor is used as a const variable. */
void ** dataP;
/* /*
device id device id
<0: CPU memory <0: CPU memory
...@@ -331,9 +327,6 @@ public: ...@@ -331,9 +327,6 @@ public:
/* set tensor items with an array of values */ /* set tensor items with an array of values */
void SetDataBatchedWithValues(MTYPE * offsets, void * values, int num); void SetDataBatchedWithValues(MTYPE * offsets, void * values, int num);
/* set the pointer to "data" */
void SetDataPointer();
/* get the value of a cell with the index */ /* get the value of a cell with the index */
DTYPE Get(int index[], int size = -1) const; DTYPE Get(int index[], int size = -1) const;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论