Skip to content

deepSwap #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions paddle/math/BaseMatrix.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,12 @@ void BaseMatrixT<T>::assignAtOffset(BaseMatrixT& b, int64_t columnOffset) {
}
}

DEFINE_MATRIX_BINARY_OP(DeepSwap, T tmp = a; a = b; b = tmp);
template<class T>
void BaseMatrixT<T>::deepSwap(BaseMatrixT& b) {
applyBinary(binary::DeepSwap<T>(), b);
}

template<>
void BaseMatrixT<real>::rowDotMul(size_t destCol,
BaseMatrixT& b,
Expand Down
11 changes: 11 additions & 0 deletions paddle/math/BaseMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ class BaseMatrixT {
*/
void assign(T p);

/**
* @code
* swap(this, b)
* example: swap two Matrices
* MatrixPtr cpuA = std::make_shared<CpuMatrix>(height, width);
* MatrixPtr cpuB = std::make_shared<CpuMatrix>(height, width);
* cpuA->deepSwap(*cpuB);
* @endcode
*/
void deepSwap(BaseMatrixT& b);

/**
* @code
* this = this + p
Expand Down
20 changes: 20 additions & 0 deletions paddle/math/tests/test_matrixCompare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,24 @@ void testMatrixZeroAtOffset(int height, int width) {
MatrixCheckEqual(*cpuA, *cpuTest);
}

void testMatrixDeepSwap(int height, int width) {
MatrixPtr cpuA = std::make_shared<CpuMatrix>(height, width);
MatrixPtr cpuB = std::make_shared<CpuMatrix>(height, width);
MatrixPtr cpuCopyA = std::make_shared<CpuMatrix>(height, width);
MatrixPtr cpuCopyB = std::make_shared<CpuMatrix>(height, width);

cpuA->randomizeUniform();
cpuB->randomizeUniform();
cpuCopyA->copyFrom(*cpuA);
cpuCopyB->copyFrom(*cpuB);

// swap matrix cpuA and cpuB
cpuA->deepSwap(*cpuB);

MatrixCheckEqual(*cpuA, *cpuCopyB);
MatrixCheckEqual(*cpuB, *cpuCopyA);
}

void testMatrixBinaryAdd(int height, int width) {
MatrixPtr cpuA = std::make_shared<CpuMatrix>(height, width);
MatrixPtr cpuB = std::make_shared<CpuMatrix>(height, width);
Expand Down Expand Up @@ -480,6 +498,7 @@ void testMatrixAssign(int height, int width) {
MatrixCheckEqual(*cpuA, *outputCheck);
}


void testMatrixAdd(int height, int width) {
MatrixPtr cpuA = std::make_shared<CpuMatrix>(height, width);
MatrixPtr gpuA = std::make_shared<GpuMatrix>(height, width);
Expand Down Expand Up @@ -798,6 +817,7 @@ TEST(Matrix, unary) {
testMatrixBinaryAdd(height, width);
testMatrixTanh(height, width);
testMatrixTanhDerivative(height, width);
testMatrixDeepSwap(height, width);

// applyTernary
testMatrixTernarySub(height, width);
Expand Down