Skip to content
Snippets Groups Projects
Commit cb5aafda authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added tests for allocators and updated ArrayOperationsTest to use allocators

parent b0f5de89
No related branches found
No related tags found
1 merge request!33Allocators
#include "AllocatorsTest.h"
#include "AllocatorsTest.h"
/***************************************************************************
AllocatorsTest.h - description
-------------------
begin : Jul 4, 2019
copyright : (C) 2019 by Tomas Oberhuber et al.
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#pragma once
#ifdef HAVE_GTEST
#include <TNL/Allocators/Host.h>
#include <TNL/Allocators/Cuda.h>
#include <TNL/Allocators/CudaHost.h>
#include <TNL/Allocators/CudaManaged.h>
#include <TNL/Containers/Algorithms/ArrayOperations.h>
#include "gtest/gtest.h"
using namespace TNL;
constexpr int ARRAY_TEST_SIZE = 5000;
// test fixture for typed tests
template< typename Value >
class AllocatorsTest : public ::testing::Test
{
protected:
using ValueType = Value;
};
// types for which ArrayTest is instantiated
using ValueTypes = ::testing::Types< short int, int, long, float, double >;
TYPED_TEST_SUITE( AllocatorsTest, ValueTypes );
TYPED_TEST( AllocatorsTest, Host )
{
using ValueType = typename TestFixture::ValueType;
using Allocator = Allocators::Host< ValueType >;
Allocator allocator;
ValueType* data = allocator.allocate( ARRAY_TEST_SIZE );
ASSERT_NE( data, nullptr );
// do something useful with the data
for (int i = 0; i < ARRAY_TEST_SIZE; i++) {
data[i] = 0;
EXPECT_EQ(data[i], 0);
}
allocator.deallocate( data, ARRAY_TEST_SIZE );
}
#ifdef HAVE_CUDA
TYPED_TEST( AllocatorsTest, CudaHost )
{
using ValueType = typename TestFixture::ValueType;
using Allocator = Allocators::CudaHost< ValueType >;
Allocator allocator;
ValueType* data = allocator.allocate( ARRAY_TEST_SIZE );
ASSERT_NE( data, nullptr );
// do something useful with the data
for (int i = 0; i < ARRAY_TEST_SIZE; i++) {
data[i] = 0;
EXPECT_EQ(data[i], 0);
}
allocator.deallocate( data, ARRAY_TEST_SIZE );
}
TYPED_TEST( AllocatorsTest, CudaManaged )
{
using ValueType = typename TestFixture::ValueType;
using Allocator = Allocators::CudaManaged< ValueType >;
Allocator allocator;
ValueType* data = allocator.allocate( ARRAY_TEST_SIZE );
ASSERT_NE( data, nullptr );
// set data on the device
Containers::Algorithms::ArrayOperations< Devices::Cuda >::setMemory( data, (ValueType) 0, ARRAY_TEST_SIZE );
ASSERT_NO_THROW( TNL_CHECK_CUDA_DEVICE );
// check values on the host
for (int i = 0; i < ARRAY_TEST_SIZE; i++)
EXPECT_EQ(data[i], 0);
allocator.deallocate( data, ARRAY_TEST_SIZE );
}
TYPED_TEST( AllocatorsTest, Cuda )
{
using ValueType = typename TestFixture::ValueType;
using Allocator = Allocators::CudaHost< ValueType >;
Allocator allocator;
ValueType* data = allocator.allocate( ARRAY_TEST_SIZE );
ASSERT_NE( data, nullptr );
// set data on the device
Containers::Algorithms::ArrayOperations< Devices::Cuda >::setMemory( data, (ValueType) 0, ARRAY_TEST_SIZE );
ASSERT_NO_THROW( TNL_CHECK_CUDA_DEVICE );
allocator.deallocate( data, ARRAY_TEST_SIZE );
}
#endif // HAVE_CUDA
#endif // HAVE_GTEST
#include "main.h"
......@@ -14,6 +14,15 @@ if( BUILD_CUDA )
TARGET_LINK_LIBRARIES( AssertCudaTest ${GTEST_BOTH_LIBRARIES} )
endif()
if( BUILD_CUDA )
CUDA_ADD_EXECUTABLE( AllocatorsTest AllocatorsTest.cu OPTIONS ${CXX_TESTS_FLAGS} )
TARGET_LINK_LIBRARIES( AllocatorsTest ${GTEST_BOTH_LIBRARIES} )
else()
ADD_EXECUTABLE( AllocatorsTest AllocatorsTest.cpp )
TARGET_COMPILE_OPTIONS( AllocatorsTest PRIVATE ${CXX_TESTS_FLAGS} )
TARGET_LINK_LIBRARIES( AllocatorsTest ${GTEST_BOTH_LIBRARIES} )
endif()
if( BUILD_CUDA )
CUDA_ADD_EXECUTABLE( FileTest FileTest.cu OPTIONS ${CXX_TESTS_FLAGS} )
TARGET_LINK_LIBRARIES( FileTest ${GTEST_BOTH_LIBRARIES} )
......@@ -52,6 +61,7 @@ ADD_TEST( AssertTest ${EXECUTABLE_OUTPUT_PATH}/AssertTest${CMAKE_EXECUTABLE_SUFF
if( BUILD_CUDA )
ADD_TEST( AssertCudaTest ${EXECUTABLE_OUTPUT_PATH}/AssertCudaTest${CMAKE_EXECUTABLE_SUFFIX} )
endif()
ADD_TEST( AllocatorsTest ${EXECUTABLE_OUTPUT_PATH}/AllocatorsTest${CMAKE_EXECUTABLE_SUFFIX} )
ADD_TEST( FileTest ${EXECUTABLE_OUTPUT_PATH}/FileTest${CMAKE_EXECUTABLE_SUFFIX} )
ADD_TEST( StringTest ${EXECUTABLE_OUTPUT_PATH}/StringTest${CMAKE_EXECUTABLE_SUFFIX} )
ADD_TEST( ObjectTest ${EXECUTABLE_OUTPUT_PATH}/ObjectTest${CMAKE_EXECUTABLE_SUFFIX} )
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment