This commit is contained in:
Clemens Marschner 2016-09-09 23:47:44 +02:00
Родитель 7c5fb2d7d8
Коммит a0844b2a35
5 изменённых файлов: 31 добавлений и 36 удалений

Просмотреть файл

@ -164,11 +164,20 @@ CPUMatrix<ElemType>& CPUMatrix<ElemType>::operator=(const CPUMatrix<ElemType>& d
//move constructor, shallow copy
template <class ElemType>
CPUMatrix<ElemType>::CPUMatrix(CPUMatrix<ElemType>&& moveFrom)
: Base(/* shallow */ true)
{
ShallowCopyFrom(moveFrom);
moveFrom.ZeroValues();
}
// Shortcut of default constructor + shallow copy, to avoid one initialization
template <class ElemType>
CPUMatrix<ElemType>::CPUMatrix(const CPUMatrix<ElemType>& shallowCopyFrom, bool shallow)
: Base(shallow)
{
ShallowCopyFrom(shallowCopyFrom);
}
//move assignment operator, shallow copy
template <class ElemType>
CPUMatrix<ElemType>& CPUMatrix<ElemType>::operator=(CPUMatrix<ElemType>&& moveFrom)
@ -182,12 +191,6 @@ CPUMatrix<ElemType>& CPUMatrix<ElemType>::operator=(CPUMatrix<ElemType>&& moveFr
return *this;
}
template <class ElemType>
CPUMatrix<ElemType>::~CPUMatrix()
{
Clear();
}
template <class ElemType>
void CPUMatrix<ElemType>::Clear()
{
@ -204,9 +207,7 @@ CPUMatrix<ElemType> CPUMatrix<ElemType>::ColumnSlice(size_t startColumn, size_t
if (startColumn + numCols > m_numCols)
InvalidArgument("The slice (%d+%d) is out of range of the source matrix (%d).", (int) startColumn, (int) numCols, (int) m_numCols);
CPUMatrix<ElemType> slice;
slice.ShallowCopyFrom(*this);
CPUMatrix<ElemType> slice(*this, /* shallow= */ true);
slice.m_numCols = numCols;
slice.m_sliceViewOffset = m_sliceViewOffset + startColumn * m_numRows;

Просмотреть файл

@ -63,6 +63,7 @@ public:
public:
CPUMatrix();
CPUMatrix(const CPUMatrix<ElemType>& shallowCopyFrom, bool shallow); // copy constructor, shallow
CPUMatrix(const size_t numRows, const size_t numCols);
CPUMatrix(const size_t numRows, const size_t numCols, ElemType* pArray, const size_t matrixFlags = matrixFlagNormal);
CPUMatrix(const CPUMatrix<ElemType>& deepCopyFrom); // copy constructor, deep copy
@ -70,8 +71,6 @@ public:
CPUMatrix(CPUMatrix<ElemType>&& moveFrom); // move constructor, shallow copy
CPUMatrix<ElemType>& operator=(CPUMatrix<ElemType>&& moveFrom); // move assignment operator, shallow copy
~CPUMatrix();
public:
size_t BufferSize() const
{

Просмотреть файл

@ -407,10 +407,12 @@ class MATH_API BaseMatrix
{
public:
BaseMatrix()
BaseMatrix(bool doNotInitializeFields = false)
{
if (!doNotInitializeFields)
ZeroInit();
}
virtual ~BaseMatrix()
{
ZeroValues();

Просмотреть файл

@ -152,20 +152,12 @@ MatrixBase::~MatrixBase() { }
// { GPU code },
// ...
// Initialize all members over virgin memory.
//This function will only initialize default bland matrix. The actual matrices need to allocated
//after calling this function and flags need to set correctly by calling SetDataLocation.
// This clears out the entire object and brings it into destructable state.
// Note: Keep this in sync with member definition and ShallowCopyFrom().
// Initialize members other than the shared_ptrs.
// This method is only called from constructors. shared_ptrs are initialized with nullptr by their default constructor.
// Thus, they are not initialized a second time. If they need to be reset, a call to ReleaseMemory() is necessary.
template <class ElemType>
void Matrix<ElemType>::Init(DEVICEID_TYPE deviceId)
void Matrix<ElemType>::InitFields(DEVICEID_TYPE deviceId)
{
m_baseMatrix = nullptr;
m_GPUMatrix = nullptr;
m_CPUMatrix = nullptr;
m_GPUSparseMatrix = nullptr;
m_CPUSparseMatrix = nullptr;
m_matrixType = MatrixType::UNDETERMINED;
m_currentDataLocation = CurrentDataLocation::NONE;
@ -281,7 +273,7 @@ void Matrix<ElemType>::SetDataLocation(CurrentDataLocation location, MatrixType
template <class ElemType>
Matrix<ElemType>::Matrix(const MatrixFlags matrixFlags, const MatrixType matrixType, const MatrixFormat matrixFormat, DEVICEID_TYPE deviceID)
{
Init(deviceID);
InitFields(deviceID);
if (!(matrixFlags & matrixFlagDontOwnBuffer))
SwitchToMatrixType(matrixType, matrixFormat, false);
@ -291,7 +283,7 @@ Matrix<ElemType>::Matrix(const MatrixFlags matrixFlags, const MatrixType matrixT
template <class ElemType>
Matrix<ElemType>::Matrix(const MatrixFlags matrixFlags, const MatrixType matrixType, DEVICEID_TYPE deviceID)
{
Init(deviceID);
InitFields(deviceID);
if (!(matrixFlags & matrixFlagDontOwnBuffer))
SwitchToMatrixType(matrixType, matrixType == MatrixType::DENSE ? MatrixFormat::matrixFormatDense : MatrixFormat::matrixFormatSparseCSC, false);
@ -301,7 +293,7 @@ Matrix<ElemType>::Matrix(const MatrixFlags matrixFlags, const MatrixType matrixT
template <class ElemType>
Matrix<ElemType>::Matrix(const MatrixFlags matrixFlags, DEVICEID_TYPE deviceID)
{
Init(deviceID);
InitFields(deviceID);
if (!(matrixFlags & matrixFlagDontOwnBuffer))
SwitchToMatrixType(MatrixType::DENSE, MatrixFormat::matrixFormatDense, false);
@ -310,7 +302,7 @@ Matrix<ElemType>::Matrix(const MatrixFlags matrixFlags, DEVICEID_TYPE deviceID)
template <class ElemType>
Matrix<ElemType>::Matrix(DEVICEID_TYPE deviceID)
{
Init(deviceID);
InitFields(deviceID);
SwitchToMatrixType(MatrixType::DENSE, MatrixFormat::matrixFormatDense, false);
}
@ -324,7 +316,7 @@ Matrix<ElemType>::Matrix(DEVICEID_TYPE deviceID)
template <class ElemType>
Matrix<ElemType>::Matrix(shared_ptr<BaseMatrix<ElemType>> baseMatrix, ElemType* pArray, DEVICEID_TYPE deviceId) // constructor for setting Matrix from a base matrix
{
Init(deviceId);
InitFields(deviceId);
if (baseMatrix->GetFormat() & matrixFormatSparse)
{
@ -360,7 +352,7 @@ Matrix<ElemType>::Matrix(shared_ptr<BaseMatrix<ElemType>> baseMatrix, ElemType*
template <class ElemType>
Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, DEVICEID_TYPE deviceId, const MatrixType matrixType, const MatrixFormat matrixFormat)
{
Init(deviceId);
InitFields(deviceId);
if (matrixType == MatrixType::SPARSE)
{
@ -400,7 +392,7 @@ Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, DEVICEID_TY
template <class ElemType>
Matrix<ElemType>::Matrix(const size_t numRows, const size_t numCols, ElemType* pArray, DEVICEID_TYPE deviceId, const size_t matrixFlags, const size_t nnz)
{
Init(deviceId);
InitFields(deviceId);
if (m_preferredDeviceId == CPUDEVICE)
{
@ -451,7 +443,7 @@ Matrix<ElemType>::Matrix(const Matrix<ElemType>& deepCopyFrom, DEVICEID_TYPE dev
{
int origCopyFromDeviceId = deepCopyFrom.GetDeviceId();
Init(deviceId); // will set m_preferredDeviceId
InitFields(deviceId); // will set m_preferredDeviceId
deepCopyFrom._transferToDevice(m_preferredDeviceId, true);
@ -473,7 +465,7 @@ Matrix<ElemType>::Matrix(const Matrix<ElemType>& deepCopyFrom, DEVICEID_TYPE dev
template <class ElemType>
Matrix<ElemType>::Matrix(Matrix<ElemType>&& moveFrom)
{
Init((DEVICEID_TYPE) moveFrom.GetDeviceId());
InitFields((DEVICEID_TYPE)moveFrom.GetDeviceId());
#if 1
operator=(move(moveFrom));
@ -499,7 +491,8 @@ Matrix<ElemType>& Matrix<ElemType>::operator=(Matrix<ElemType>&& moveFrom)
// shallow-copy all members
ShallowCopyFrom(moveFrom);
// virgin-init the source
moveFrom.Init(CPUDEVICE);
moveFrom.ReleaseMemory();
moveFrom.InitFields(CPUDEVICE);
#else
m_preferredDeviceId = moveFrom.m_preferredDeviceId;

Просмотреть файл

@ -131,7 +131,7 @@ private:
Matrix(const MatrixFlags matrixFlags, const MatrixType matrixType, const MatrixFormat matrixFormat, DEVICEID_TYPE deviceID); // only used internally to initialize a blank matrix
Matrix(const MatrixFlags matrixFlags, const MatrixType matrixType, DEVICEID_TYPE deviceID); // only used internally to initialize a blank matrix
Matrix(const MatrixFlags matrixFlags, DEVICEID_TYPE deviceID); // only used internally to initialize a blank matrix
void Init(DEVICEID_TYPE deviceID); // only used internally to initialize a blank matrix
void InitFields(DEVICEID_TYPE deviceID);
void SetDataLocation(CurrentDataLocation location, MatrixType type = UNDETERMINED) const;
void ShallowCopyFrom(const Matrix<ElemType>& other);