Skip to content

Commit

Permalink
bug fix: BatchLUSequenceReader would move features and labels to the …
Browse files Browse the repository at this point in the history
…CPU, causing downstream issues. Now it keeps the location, so that it works with GPU now
  • Loading branch information
frankseide committed Nov 30, 2015
1 parent e647197 commit 95bf296
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions DataReader/LUSequenceReader/LUSequenceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ bool BatchLUSequenceReader<ElemType>::GetMinibatch(std::map<std::wstring, Matrix
}
}

locObs.SetPreferredDeviceId(features.GetDeviceId()); // needed, otherwise SetValue() below will inherit CPUDEVICE a as target
features.SetValue(locObs);

lablsize = GetLabelOutput(matrices, m_labelInfo[labelInfoOut], actualmbsize);
Expand Down Expand Up @@ -917,11 +918,12 @@ size_t BatchLUSequenceReader<ElemType>::GetLabelOutput(std::map<std::wstring,
Matrix<ElemType>* labels = matrices[m_labelsName[labelInfoOut]];
if (labels == nullptr) return 0;

DEVICEID_TYPE device = labels->GetDeviceId();

labels->Resize(labelInfo.dim, actualmbsize);
labels->SetValue(0);
labels->TransferFromDeviceToDevice(device, CPUDEVICE, true);

// build it on the CPU side
DEVICEID_TYPE deviceId = labels->GetDeviceId();
labels->TransferFromDeviceToDevice(deviceId, CPUDEVICE, true);

size_t nbrLabl = 0;
for (size_t j = 0; j < actualmbsize; ++j)
Expand Down Expand Up @@ -955,6 +957,9 @@ size_t BatchLUSequenceReader<ElemType>::GetLabelOutput(std::map<std::wstring,
nbrLabl++;
}

// move it back to GPU if that's where it was before
labels->TransferFromDeviceToDevice(CPUDEVICE, deviceId, true);

return nbrLabl;
}

Expand Down
6 changes: 3 additions & 3 deletions Math/Math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ namespace Microsoft { namespace MSR { namespace CNTK {
void SetDataLocation(CurrentDataLocation location, MatrixType type=UNDETERMINED) const;

public:
MatrixType GetMatrixType() const {return m_matrixType;};
MatrixType GetMatrixType() const { return m_matrixType; }
MatrixFormat GetFormat() const { return m_baseMatrix->GetFormat(); }
bool OwnBuffer() const {return m_baseMatrix->OwnBuffer();}
bool OwnBuffer() const { return m_baseMatrix->OwnBuffer(); }
int GetDeviceId() const; //-1 if CPU, otherwise GPU CUDA device id
DEVICEID_TYPE GetPreferredDeviceId() const { return m_preferredDeviceId; }; //-1 if CPU, otherwise GPU CUDA device id
void SetPreferredDeviceId(DEVICEID_TYPE preferredDeviceId){ if (m_preferredDeviceId != preferredDeviceId) m_preferredDeviceId = preferredDeviceId; }
void SetPreferredDeviceId(DEVICEID_TYPE preferredDeviceId) { m_preferredDeviceId = preferredDeviceId; }
//Moves matrix from device id_from to device with id_to.
//If emptyTransfer=true, then no data is ever moved, just corresponding GPU/CPU matrices are deleted and then created using empty constructor
void TransferFromDeviceToDevice(int id_from, int id_to, bool ismoved = false,/*if false then keep source and set location to BOTH*/ bool emptyTransfer = false, bool updatePreferredDevice = true) const;
Expand Down

0 comments on commit 95bf296

Please sign in to comment.