Commit 04c68239 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

CUDA kernel tests rewritten as templates. (Tests to not pass.)

parent 1591cf3c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ libcore_tests_sources = tnlStringTester.cpp \
                        tnlObjectTester.cpp \
                        tnlObjectTester.h \
                        tnlLongVectorCUDATester.cu \
                        tnlLongVectorCUDATester.cu.h \
                        tnlLongVectorCUDATester.h
                        

+2 −0
Original line number Diff line number Diff line
@@ -125,6 +125,7 @@ template< class T > class tnlLongVectorCUDA : public tnlObject
   {
      assert( long_vector. GetSize() == GetSize() );
      cudaMemcpy( data, long_vector. Data(), GetSize() * sizeof( T ), cudaMemcpyHostToDevice );
      return true;
   }

   virtual
@@ -148,6 +149,7 @@ template< class T > bool tnlLongVector< T > :: copyFrom( const tnlLongVectorCUDA
{
   assert( cuda_vector. GetSize() == GetSize() );
   cudaMemcpy( data, cuda_vector. Data(), GetSize() * sizeof( T ), cudaMemcpyDeviceToHost );
   return true;
}

#endif
+7 −32
Original line number Diff line number Diff line
#include <cuda.h> 
#include <core/tnlString.h>
#include <tnlLongVectorCUDATester.cu.h>

#include <core/tnlLongVector.h>
#include <core/tnlLongVectorCUDA.h>
#include <core/tnlLongVectorCUDATester.h>

__global__ void setNumber( float* A, float c )
void testKernelStarter( const float& number, const int size )
{
   int i = threadIdx. x;
   A[ i ] = c;
   testKernel( number, size );  
}

void testKernel()
{
   tnlLongVectorCUDA< float > device_vector( 500 );
   tnlLongVector< float > host_vector( 500 );
   float* data = device_vector. Data();
   setNumber<<< 1, 500 >>>( data, 0.0 );
   host_vector. copyFrom( device_vector );
   int errors( 0 );
   for( int i = 0; i < 500; i ++ )
   {
      if( host_vector[ i ] != 0.0 ) errors ++;
      cout << host_vector[ i ] << "-";
   }
   CPPUNIT_ASSERT( ! errors );
   setNumber<<< 1, 500 >>>( data, 1.0 );
   host_vector. copyFrom( device_vector );
   errors = 0;
   for( int i = 0; i < 500; i ++ )
void testKernelStarter( const double& number, const int size )
{
      if( host_vector[ i ] != 1.0 ) errors ++;
      cout << host_vector[ i ] << "-";
   }
   CPPUNIT_ASSERT( ! errors );
   testKernel( number, size );
}
+33 −0
Original line number Diff line number Diff line
#include <cuda.h>
#include <cppunit/extensions/HelperMacros.h>
#include <core/tnlString.h>
#include <core/tnlLongVector.h>
#include <core/tnlLongVectorCUDA.h>
#include <core/tnlLongVectorCUDATester.h>

#ifndef tnlLongVectorCUDATester_cu_h
#define tnlLongVectorCUDATester_cu_h

template< class T > __global__ void setNumber( T* A, T c )
{
   int i = threadIdx. x;
   A[ i ] = c;
};

template< class T > void testKernel( const T& number, const int size )
{
   tnlLongVectorCUDA< T > device_vector( size );
   tnlLongVector< T > host_vector( size );
   T* data = device_vector. Data();
   setNumber<<< 1, size >>>( data, number );
   host_vector. copyFrom( device_vector );
   int errors( 0 );
   for( int i = 0; i < size; i ++ )
   {
	   cout << host_vector[ i ] << "-";
       if( host_vector[ i ] != number ) errors ++;
   }
   CPPUNIT_ASSERT( ! errors );
};

#endif
+9 −3
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@
#include <core/tnlLongVector.h>

#ifdef HAVE_CUDA
void testKernel();
void testKernelStarter( const float& number, const int size );
void testKernelStarter( const double& number, const int size );
#endif


@@ -54,16 +55,21 @@ template< class T > class tnlLongVectorCUDATester : public CppUnit :: TestCase
                               & tnlLongVectorCUDATester< float > :: testCopying )
                             );
      suiteOfTests -> addTest( new CppUnit :: TestCaller< tnlLongVectorCUDATester< float > >(
                                     "testKernel",
                                     "testKernelFloat",
                                     & tnlLongVectorCUDATester< float > :: testKernel )
                                   );
      /*suiteOfTests -> addTest( new CppUnit :: TestCaller< tnlLongVectorCUDATester< double > >(
                                           "testKernelDouble",
                                           & tnlLongVectorCUDATester< double > :: testKernel )
                                         );*/
      return suiteOfTests;
   }

   void testKernel()
   {
#ifdef HAVE_CUDA
      :: testKernel();
	  for( int i = 0; i < 10; i ++ )
		  :: testKernelStarter( ( T ) i, 1000 );
#else
      cout << "CUDA is not supported." << endl;
      CPPUNIT_ASSERT( true );
Loading