Skip to content

Commit

Permalink
Moved GPU matrix tests to boost and addressed CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pkranen committed Nov 6, 2015
1 parent c215f8c commit b7206b4
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 206 deletions.
22 changes: 5 additions & 17 deletions Tests/UnitTests/MathTests/CPUMatrixTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
// </copyright>
//
#include "stdafx.h"
#include "..\..\..\Math\Math\CPUMatrix.h"

#include <array>
#include <boost/test/unit_test.hpp>
#include "../../../Math/Math/CPUMatrix.h"

using namespace Microsoft::MSR::CNTK;

Expand All @@ -31,7 +28,6 @@ namespace Microsoft

m.Resize(2, 3);
BOOST_CHECK(!m.IsEmpty());

BOOST_CHECK_EQUAL(m.GetNumRows(), 2);
BOOST_CHECK_EQUAL(m.GetNumCols(), 3);
BOOST_CHECK_EQUAL(m.GetNumElements(), 6);
Expand All @@ -47,12 +43,8 @@ namespace Microsoft

BOOST_AUTO_TEST_CASE(CPUMatrixConstructorFlagNormal)
{
std::array<float, 6> tmp = { 1, 2, 3, 4, 5, 6 };

float* array = new float[6];
std::copy(tmp.begin(), tmp.end(), array);

SMatrix m(2, 3, array, matrixFlagNormal);
std::array<float, 6> array = { 1, 2, 3, 4, 5, 6 };
SMatrix m(2, 3, array.data(), matrixFlagNormal);
BOOST_CHECK_EQUAL(m(0, 0), 1);
BOOST_CHECK_EQUAL(m(0, 1), 3);
BOOST_CHECK_EQUAL(m(0, 2), 5);
Expand All @@ -63,12 +55,8 @@ namespace Microsoft

BOOST_AUTO_TEST_CASE(CPUMatrixConstructorFormatRowMajor)
{
std::array<double, 6> tmp = { 7, 8, 9, 10, 11, 12 };

double *array = new double[6];
std::copy(tmp.begin(), tmp.end(), array);

DMatrix m(2, 3, array, matrixFormatRowMajor);
std::array<double, 6> array = { 7, 8, 9, 10, 11, 12 };
DMatrix m(2, 3, array.data(), matrixFormatRowMajor);
BOOST_CHECK_EQUAL(m(0, 0), 7);
BOOST_CHECK_EQUAL(m(0, 1), 8);
BOOST_CHECK_EQUAL(m(0, 2), 9);
Expand Down
26 changes: 10 additions & 16 deletions Tests/UnitTests/MathTests/GPUMatrixCudaBlasTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// GPUMatrix CUDA BLAS library tests should go here
//
#include "stdafx.h"
#include <boost/test/unit_test.hpp>
#include "../../../Math/Math/GPUMatrix.h"

using namespace Microsoft::MSR::CNTK;
Expand All @@ -29,11 +28,11 @@ namespace Microsoft
const float alpha = 2.0f;
const float beta = 0.42f;
GPUMatrix<float> m0(12, 5, deviceId);
m0.SetValue(1.0f);
m0.SetValue(1);
GPUMatrix<float> m1(5, 11, deviceId);
m1.SetValue(1.0f);
m1.SetValue(1);
GPUMatrix<float> m2(12, 11, deviceId);
m2.SetValue(1.0f);
m2.SetValue(1);

// m2 = alpha * m0 * m1 + beta * m2
GPUMatrix<float>::MultiplyAndWeightedAdd(alpha, m0, false, m1, false, beta, m2);
Expand All @@ -57,25 +56,20 @@ namespace Microsoft

BOOST_AUTO_TEST_CASE(GPUBlasInnerProduct)
{
float *arr = new float[100];
for (int i = 0; i < 100; i++)
{
arr[i] = 2.0f;
}

GPUMatrix<float> m0(10, 10, deviceId, arr, matrixFlagNormal);
GPUMatrix<float> m1(10, 10, deviceId, arr, matrixFlagNormal);
GPUMatrix<float> m2(1, 10, deviceId, arr, matrixFlagNormal);
GPUMatrix<float> m0(10, 10, deviceId);
GPUMatrix<float> m1(10, 10, deviceId);
GPUMatrix<float> m2(1, 10, deviceId);
m0.SetValue(2);
m1.SetValue(2);
m2.SetValue(2);

GPUMatrix<float>::InnerProduct(m0, m1, m2, true);
GPUMatrix<float> mr(1, 10, deviceId);
mr.SetValue(40.0f);
mr.SetValue(40);
BOOST_CHECK(m2.IsEqualTo(mr, epsilon));

GPUMatrix<float>::InnerProduct(m0, m1, m2, false);
BOOST_CHECK(m2.IsEqualTo(mr.Transpose(), epsilon));

delete[] arr;
}

// TODO: add tests for other CUDA BLAS methods?
Expand Down
Loading

0 comments on commit b7206b4

Please sign in to comment.