move some test from test_matrixCompare.cpp to test_BaseMatrix.cpp and test_Matrix.cpp

avx_docs
hedaoyuan 9 years ago
parent 1873945dc7
commit f70fc4a439

@ -1578,11 +1578,6 @@ void BaseMatrixT<real>::minRows(BaseMatrixT& b) {
applyRow(aggregate::min(), b); applyRow(aggregate::min(), b);
} }
template<>
void BaseMatrixT<real>::sumCols(BaseMatrixT& b) {
applyCol(aggregate::sum(), b);
}
template<> template<>
void BaseMatrixT<real>::maxCols(BaseMatrixT& b) { void BaseMatrixT<real>::maxCols(BaseMatrixT& b) {
applyCol(aggregate::max(), b); applyCol(aggregate::max(), b);

@ -1007,8 +1007,6 @@ public:
/// calculate the minimum value of each row of the matrix b. /// calculate the minimum value of each row of the matrix b.
void minRows(BaseMatrixT& b); void minRows(BaseMatrixT& b);
/// calculate the sum of each column of the matrix b.
void sumCols(BaseMatrixT& b);
/// calculate the maximum value of each column of the matrix b. /// calculate the maximum value of each column of the matrix b.
void maxCols(BaseMatrixT& b); void maxCols(BaseMatrixT& b);
/// calculate the minimum value of each column of the matrix b. /// calculate the minimum value of each column of the matrix b.

@ -110,4 +110,10 @@ void TensorCheck(AssertEq compare, real args1, real args2) {
<< ", args2 = " << args2; << ", args2 = " << args2;
} }
template <typename AssertEq>
void TensorCheck(AssertEq compare, size_t args1, size_t args2) {
EXPECT_EQ(args1, args2) << "[Test error] args1 = " << args1
<< ", args2 = " << args2;
}
} // namespace autotest } // namespace autotest

@ -65,15 +65,24 @@ public:
// construct a argument // construct a argument
template <typename T> template <typename T>
T construct(int height, int width); T construct(int height, int width);
template <> template <>
float construct(int height, int width) { float construct(int height, int width) {
return 0.0; return 0.0;
} }
template <>
size_t construct(int height, int width) {
size_t offset = std::rand() % (height < width ? height : width);
return offset;
}
template <> template <>
CpuMatrix construct(int height, int width) { CpuMatrix construct(int height, int width) {
CpuMatrix a(height, width); CpuMatrix a(height, width);
return a; return a;
} }
template <> template <>
GpuMatrix construct(int height, int width) { GpuMatrix construct(int height, int width) {
GpuMatrix a(height, width); GpuMatrix a(height, width);
@ -83,14 +92,22 @@ GpuMatrix construct(int height, int width) {
// init a argument // init a argument
template <typename T> template <typename T>
void init(T& v); void init(T& v);
template <> template <>
void init(float& v) { void init(float& v) {
v = 0.5; v = 0.5;
} }
template <>
void init(size_t& v) {
return;
}
template <> template <>
void init(CpuMatrix& v) { void init(CpuMatrix& v) {
v.randomizeUniform(); v.randomizeUniform();
} }
template <> template <>
void init(GpuMatrix& v) { void init(GpuMatrix& v) {
v.randomizeUniform(); v.randomizeUniform();
@ -111,10 +128,17 @@ template <std::size_t I = 0, typename... Args>
// copy a argument, copy src to dest // copy a argument, copy src to dest
template <typename T1, typename T2> template <typename T1, typename T2>
void copy(T1& dest, T2& src); void copy(T1& dest, T2& src);
template <> template <>
void copy(float& dest, float& src) { void copy(float& dest, float& src) {
dest = src; dest = src;
} }
template <>
void copy(size_t& dest, size_t& src) {
dest = src;
}
template <> template <>
void copy(GpuMatrix& dest, CpuMatrix& src) { void copy(GpuMatrix& dest, CpuMatrix& src) {
dest.copyFrom(src); dest.copyFrom(src);
@ -165,8 +189,8 @@ R call(C& obj, R (FC::*f)(FArgs...), Args&&... args) {
return (obj.*f)(args...); return (obj.*f)(args...);
} }
template <bool ApplyRow, template <bool AsRowVector,
bool ApplyCol, bool AsColVector,
std::size_t... I, std::size_t... I,
typename C, typename C,
typename R, typename R,
@ -177,8 +201,8 @@ void BaseMatrixCompare(R (C::*f)(Args...),
bool checkArgs = false) { bool checkArgs = false) {
for (auto height : {1, 11, 73, 128, 200, 330}) { for (auto height : {1, 11, 73, 128, 200, 330}) {
for (auto width : {1, 3, 32, 100, 512, 1000}) { for (auto width : {1, 3, 32, 100, 512, 1000}) {
CpuMatrix obj1(ApplyCol ? 1 : height, ApplyRow ? 1 : width); CpuMatrix obj1(AsRowVector ? 1 : height, AsColVector ? 1 : width);
GpuMatrix obj2(ApplyCol ? 1 : height, ApplyRow ? 1 : width); GpuMatrix obj2(AsRowVector ? 1 : height, AsColVector ? 1 : width);
init(obj1); init(obj1);
copy(obj2, obj1); copy(obj2, obj1);
@ -227,7 +251,7 @@ void BaseMatrixCompare(R (C::*f)(Args...), bool checkArgs = false) {
} }
template <std::size_t... I, typename C, typename R, typename... Args> template <std::size_t... I, typename C, typename R, typename... Args>
void BaseMatrixApplyRow(R (C::*f)(Args...)) { void BaseMatrixAsColVector(R (C::*f)(Args...)) {
static_assert(sizeof...(I) == sizeof...(Args), static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal"); "size of parameter packs are not equal");
@ -237,11 +261,11 @@ void BaseMatrixApplyRow(R (C::*f)(Args...)) {
autotest::AssertEqual compare(1e-8); autotest::AssertEqual compare(1e-8);
#endif #endif
autotest::BaseMatrixCompare<true, false, I...>(f, compare); autotest::BaseMatrixCompare<false, true, I...>(f, compare);
} }
template <std::size_t... I, typename C, typename R, typename... Args> template <std::size_t... I, typename C, typename R, typename... Args>
void BaseMatrixApplyCol(R (C::*f)(Args...)) { void BaseMatrixAsRowVector(R (C::*f)(Args...)) {
static_assert(sizeof...(I) == sizeof...(Args), static_assert(sizeof...(I) == sizeof...(Args),
"size of parameter packs are not equal"); "size of parameter packs are not equal");
@ -250,5 +274,5 @@ void BaseMatrixApplyCol(R (C::*f)(Args...)) {
#else #else
autotest::AssertEqual compare(1e-8); autotest::AssertEqual compare(1e-8);
#endif #endif
autotest::BaseMatrixCompare<false, true, I...>(f, compare); autotest::BaseMatrixCompare<true, false, I...>(f, compare);
} }

@ -24,7 +24,6 @@ limitations under the License. */
#include "TestUtils.h" #include "TestUtils.h"
using namespace paddle; // NOLINT using namespace paddle; // NOLINT
using namespace std; // NOLINT
/** /**
* Test member functions which prototype is * Test member functions which prototype is
@ -32,8 +31,8 @@ using namespace std; // NOLINT
*/ */
TEST(BaseMatrix, void) { TEST(BaseMatrix, void) {
typedef void (BaseMatrix::*FunctionProto)(); typedef void (BaseMatrix::*FunctionProto)();
#define BASEMATRIXCOMPARE(function) \ #define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare(static_cast<FunctionProto>(&BaseMatrix::function)); BaseMatrixCompare(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(neg); BASEMATRIXCOMPARE(neg);
BASEMATRIXCOMPARE(exp); BASEMATRIXCOMPARE(exp);
@ -46,7 +45,7 @@ TEST(BaseMatrix, void) {
BASEMATRIXCOMPARE(zero); BASEMATRIXCOMPARE(zero);
BASEMATRIXCOMPARE(one); BASEMATRIXCOMPARE(one);
#undef BASEMATRIXCOMPARE #undef BASEMATRIXCOMPARE
} }
/** /**
@ -55,8 +54,8 @@ TEST(BaseMatrix, void) {
*/ */
TEST(BaseMatrix, real) { TEST(BaseMatrix, real) {
typedef void (BaseMatrix::*FunctionProto)(real); typedef void (BaseMatrix::*FunctionProto)(real);
#define BASEMATRIXCOMPARE(function) \ #define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function)); BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(pow); BASEMATRIXCOMPARE(pow);
BASEMATRIXCOMPARE(subScalar); BASEMATRIXCOMPARE(subScalar);
@ -67,7 +66,7 @@ TEST(BaseMatrix, real) {
BASEMATRIXCOMPARE(biggerThanScalar); BASEMATRIXCOMPARE(biggerThanScalar);
BASEMATRIXCOMPARE(downClip); BASEMATRIXCOMPARE(downClip);
#undef BASEMATRIXCOMPARE #undef BASEMATRIXCOMPARE
} }
/** /**
@ -76,13 +75,13 @@ TEST(BaseMatrix, real) {
*/ */
TEST(BaseMatrix, real_real) { TEST(BaseMatrix, real_real) {
typedef void (BaseMatrix::*FunctionProto)(real, real); typedef void (BaseMatrix::*FunctionProto)(real, real);
#define BASEMATRIXCOMPARE(function) \ #define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function)); BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(add); BASEMATRIXCOMPARE(add);
BASEMATRIXCOMPARE(clip); BASEMATRIXCOMPARE(clip);
#undef BASEMATRIXCOMPARE #undef BASEMATRIXCOMPARE
} }
/** /**
@ -91,8 +90,8 @@ TEST(BaseMatrix, real_real) {
*/ */
TEST(BaseMatrix, BaseMatrix) { TEST(BaseMatrix, BaseMatrix) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&); typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&);
#define BASEMATRIXCOMPARE(function) \ #define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function)); BaseMatrixCompare<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(assign); BASEMATRIXCOMPARE(assign);
BASEMATRIXCOMPARE(add); BASEMATRIXCOMPARE(add);
@ -129,7 +128,7 @@ TEST(BaseMatrix, BaseMatrix) {
BASEMATRIXCOMPARE(addP2P); BASEMATRIXCOMPARE(addP2P);
BASEMATRIXCOMPARE(invSqrt); BASEMATRIXCOMPARE(invSqrt);
#undef BASEMATRIXCOMPARE #undef BASEMATRIXCOMPARE
} }
/** /**
@ -138,8 +137,8 @@ TEST(BaseMatrix, BaseMatrix) {
*/ */
TEST(BaseMatrix, BaseMatrix_real) { TEST(BaseMatrix, BaseMatrix_real) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real); typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real);
#define BASEMATRIXCOMPARE(function) \ #define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function)); BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(addBias); BASEMATRIXCOMPARE(addBias);
BASEMATRIXCOMPARE(add); BASEMATRIXCOMPARE(add);
@ -154,7 +153,7 @@ TEST(BaseMatrix, BaseMatrix_real) {
BASEMATRIXCOMPARE(isEqualTo); BASEMATRIXCOMPARE(isEqualTo);
#undef BASEMATRIXCOMPARE #undef BASEMATRIXCOMPARE
} }
/** /**
@ -163,8 +162,8 @@ TEST(BaseMatrix, BaseMatrix_real) {
*/ */
TEST(BaseMatrix, BaseMatrix_BaseMatrix) { TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&); typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&);
#define BASEMATRIXCOMPARE(function) \ #define BASEMATRIXCOMPARE(function) \
BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function)); BaseMatrixCompare<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXCOMPARE(softCrossEntropy); BASEMATRIXCOMPARE(softCrossEntropy);
BASEMATRIXCOMPARE(softCrossEntropyBp); BASEMATRIXCOMPARE(softCrossEntropyBp);
@ -181,69 +180,25 @@ TEST(BaseMatrix, BaseMatrix_BaseMatrix) {
BASEMATRIXCOMPARE(dotMulSquare); BASEMATRIXCOMPARE(dotMulSquare);
BASEMATRIXCOMPARE(dotSquareSquare); BASEMATRIXCOMPARE(dotSquareSquare);
#undef BASEMATRIXCOMPARE #undef BASEMATRIXCOMPARE
} }
/** // member function without overloaded
* Test aggregate member functions which prototype is TEST(BaseMatrix, Other) {
* void (BaseMatrix::*)(BaseMatrix&). BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowScale);
*/ BaseMatrixCompare<0, 1, 2>(&BaseMatrix::rowDotMul);
TEST(Aggregate, BaseMatrix) { BaseMatrixCompare<0, 1, 2, 3>(&BaseMatrix::binaryClassificationError);
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&);
#define BASEMATRIXAPPLYROW(function) \
BaseMatrixApplyRow<0>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXAPPLYROW(maxRows);
BASEMATRIXAPPLYROW(minRows);
BASEMATRIXAPPLYCOL(sumCols);
BASEMATRIXAPPLYCOL(maxCols);
BASEMATRIXAPPLYCOL(minCols);
#undef BASEMATRIXAPPLYROW
#undef BASEMATRIXAPPLYCOL
} }
/** TEST(BaseMatrix, Aggregate) {
* Test aggregate member functions which prototype is BaseMatrixAsColVector<0>(&BaseMatrix::maxRows);
* void (BaseMatrix::*)(BaseMatrix&, BaseMatrix&). BaseMatrixAsColVector<0>(&BaseMatrix::minRows);
*/ BaseMatrixAsColVector<0, 1, 2>(&BaseMatrix::sumRows);
TEST(Aggregate, BaseMatrix_BaseMatrix) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, BaseMatrix&);
#define BASEMATRIXAPPLYROW(function) \
BaseMatrixApplyRow<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0, 1>(static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXAPPLYCOL(addDotMulVMM);
#undef BASEMATRIXAPPLYROW
#undef BASEMATRIXAPPLYCOL
}
/**
* Test aggregate member functions which prototype is
* void (BaseMatrix::*)(BaseMatrix&, real, real).
*/
TEST(Aggregate, BaseMatrix_real_real) {
typedef void (BaseMatrix::*FunctionProto)(BaseMatrix&, real, real);
#define BASEMATRIXAPPLYROW(function) \
BaseMatrixApplyRow<0, 1, 2>(\
static_cast<FunctionProto>(&BaseMatrix::function));
#define BASEMATRIXAPPLYCOL(function) \
BaseMatrixApplyCol<0, 1, 2>(\
static_cast<FunctionProto>(&BaseMatrix::function));
BASEMATRIXAPPLYROW(sumRows);
BASEMATRIXAPPLYCOL(sumCols);
#undef BASEMATRIXAPPLYROW BaseMatrixAsRowVector<0>(&BaseMatrix::maxCols);
#undef BASEMATRIXAPPLYCOL BaseMatrixAsRowVector<0>(&BaseMatrix::minCols);
BaseMatrixAsRowVector<0, 1>(&BaseMatrix::addDotMulVMM);
BaseMatrixAsRowVector<0, 1, 2>(&BaseMatrix::sumCols);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {

@ -19,25 +19,20 @@ limitations under the License. */
*/ */
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "paddle/utils/Util.h"
#include "paddle/math/BaseMatrix.h"
#include "TestUtils.h" #include "TestUtils.h"
using namespace paddle; // NOLINT using namespace paddle; // NOLINT
using namespace std; // NOLINT
/** TEST(Matrix, Matrix) {
* Test member functions which prototype is BaseMatrixCompare<0>(&Matrix::softmax, true);
* void (Matrix::*)(Matrix&). BaseMatrixCompare<0, 1>(&Matrix::sumOfSquaresBp);
*/ }
TEST(BaseMatrix, real) {
typedef void (Matrix::*FunctionProto)(Matrix&);
#define MATRIXCOMPARE(function) \
BaseMatrixCompare<0>(static_cast<FunctionProto>(&Matrix::function), true);
MATRIXCOMPARE(softmax); TEST(Matrix, Aggregate) {
BaseMatrixAsRowVector<0, 1>(
static_cast<void (Matrix::*)(Matrix&, real)>(&Matrix::collectBias));
#undef MATRIXCOMPARE BaseMatrixAsColVector<0, 1>(&Matrix::sumOfSquares);
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save