Skip to content

Commit

Permalink
Applied CR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gaizkan committed Nov 20, 2015
1 parent ac15ceb commit 1d9cb75
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions Tests/UnitTests/MathTests/MatrixDataSynchronizationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// </copyright>
//
#include "stdafx.h"
#include "..\..\..\Math\Math\Matrix.h"
#include "..\..\..\Math\Math\Helpers.h"
#include "../../../Math/Math/Matrix.h"
#include "../../../Math/Math/Helpers.h"

using namespace Microsoft::MSR::CNTK;

Expand All @@ -19,37 +19,37 @@ namespace Microsoft
{
BOOST_AUTO_TEST_SUITE(GPUMatrixSuite)

// This tests defaulting to GPU. Your machine has to have CUDA5 GPU to pass it
BOOST_AUTO_TEST_CASE(MatrixDataSynchronization_DefaultBehaviorTestForConstructors)
// Requires GPU
BOOST_AUTO_TEST_CASE(MatrixDataSynchronization_DefaultBehaviorTestForConstructors)
{
SingleMatrix matrixA;
BOOST_CHECK(CurrentDataLocation::GPU == matrixA.GetCurrentMatrixLocation());
const SingleMatrix matrixA;
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixA.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(0, matrixA.GetNumCols());
BOOST_CHECK_EQUAL(0, matrixA.GetNumRows());

SingleMatrix matrixA1(AUTOPLACEMATRIX);
BOOST_CHECK(CurrentDataLocation::GPU == matrixA1.GetCurrentMatrixLocation());
const SingleMatrix matrixA1(AUTOPLACEMATRIX);
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixA1.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(0, matrixA1.GetNumCols());
BOOST_CHECK_EQUAL(0, matrixA1.GetNumRows());

SingleMatrix matrixA2(static_cast<size_t>(13), static_cast<size_t>(12));
BOOST_CHECK(CurrentDataLocation::GPU == matrixA2.GetCurrentMatrixLocation());
const SingleMatrix matrixA2(static_cast<size_t>(13), static_cast<size_t>(12));
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixA2.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(12, matrixA2.GetNumCols());
BOOST_CHECK_EQUAL(13, matrixA2.GetNumRows());

float arr[255];
SingleMatrix matrixA3(5, 45, arr, matrixFlagNormal);
float arr[5 * 45];
const SingleMatrix matrixA3(5, 45, arr, matrixFlagNormal);
BOOST_CHECK(CurrentDataLocation::GPU == matrixA3.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(45, matrixA3.GetNumCols());
BOOST_CHECK_EQUAL(5, matrixA3.GetNumRows());
}

// This tests defaulting to GPU and transfering. Your machine has to have CUDA5 GPU to pass it
// Requires GPU
BOOST_AUTO_TEST_CASE(MatrixDataSynchronization_AccessPatternAndTransferTest)
{
float arr[255];
const SingleMatrix matrixA(5, 45, &arr[0], matrixFlagNormal);
BOOST_CHECK(CurrentDataLocation::GPU == matrixA.GetCurrentMatrixLocation());
float arr[5 * 45];
const SingleMatrix matrixA(5, 45, arr, matrixFlagNormal);
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixA.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(45, matrixA.GetNumCols());
BOOST_CHECK_EQUAL(5, matrixA.GetNumRows());

Expand All @@ -62,59 +62,56 @@ namespace Microsoft
}

SingleMatrix matrixB(15, 15, arr, matrixFlagNormal);
BOOST_CHECK(CurrentDataLocation::GPU == matrixB.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixB.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(15, matrixB.GetNumCols());
BOOST_CHECK_EQUAL(15, matrixB.GetNumRows());

x = matrixB(1, 1);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixB.GetCurrentMatrixLocation());
matrixB(4, 2) = x;
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixB.GetCurrentMatrixLocation());
foreach_coord(i, j, matrixB)
{
x = matrixB(i, j);
matrixB(j, i) = x;
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixB.GetCurrentMatrixLocation());
}

matrixB.TransferFromDeviceToDevice(-1, 0, false);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::BOTH);
BOOST_CHECK_EQUAL(CurrentDataLocation::BOTH, matrixB.GetCurrentMatrixLocation());
matrixB.TransferFromDeviceToDevice(0, -1, false);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::BOTH);
BOOST_CHECK_EQUAL(CurrentDataLocation::BOTH, matrixB.GetCurrentMatrixLocation());
matrixB.TransferFromDeviceToDevice(-1, 0, true);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::GPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixB.GetCurrentMatrixLocation());
matrixB.TransferFromDeviceToDevice(0, -1, true);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixB.GetCurrentMatrixLocation());

SingleMatrix matrixC(-1);
BOOST_CHECK(matrixC.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
const SingleMatrix matrixC(-1);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixC.GetCurrentMatrixLocation());

SingleMatrix matrixD(15, 15, arr, matrixFlagNormal, -1);
BOOST_CHECK(matrixD.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
const SingleMatrix matrixD(15, 15, arr, matrixFlagNormal, -1);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixD.GetCurrentMatrixLocation());
}

// This tests defaulting to GPU, transfering and gravitating towards preferred device.
// Your machine has to have CUDA5 GPU to pass it
// Requires GPU
BOOST_AUTO_TEST_CASE(MatrixDataSynchronization_GravitatingTowardsPreferredDevice)
{
SingleMatrix matrixA = SingleMatrix::RandomGaussian(64, 23, 0, 2);
SingleMatrix matrixB = SingleMatrix::Eye(23);

BOOST_CHECK(matrixA.GetCurrentMatrixLocation() == CurrentDataLocation::GPU);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::GPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixA.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixB.GetCurrentMatrixLocation());

// Set the current matrix location by reading a value of the matrix
float x = matrixA(1, 1);
x;
BOOST_CHECK(matrixA.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixA.GetCurrentMatrixLocation());
x = matrixB(1, 1);
x;
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::CPU);
BOOST_CHECK_EQUAL(CurrentDataLocation::CPU, matrixB.GetCurrentMatrixLocation());

SingleMatrix matrixC = matrixA * matrixB;
BOOST_CHECK(matrixA.GetCurrentMatrixLocation() == CurrentDataLocation::GPU);
BOOST_CHECK(matrixB.GetCurrentMatrixLocation() == CurrentDataLocation::GPU);
BOOST_CHECK(matrixC.GetCurrentMatrixLocation() == CurrentDataLocation::GPU);
const SingleMatrix matrixC = matrixA * matrixB;
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixA.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixB.GetCurrentMatrixLocation());
BOOST_CHECK_EQUAL(CurrentDataLocation::GPU, matrixC.GetCurrentMatrixLocation());
}

BOOST_AUTO_TEST_SUITE_END()
Expand Down

0 comments on commit 1d9cb75

Please sign in to comment.