From 1be608e9e4113a9062895ad5707ca16aa1d179a0 Mon Sep 17 00:00:00 2001 From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz> Date: Sun, 3 Mar 2013 18:54:31 +0100 Subject: [PATCH] Fixing the code to work without C++11 (for nvcc 5.0). --- CMakeLists.txt | 2 + src/core/tnlArray.h | 5 +++ src/core/tnlFile.h | 31 ++++++------- src/core/tnlList.h | 45 +++++++++++++------ src/core/tnlSharedArray.h | 5 +++ src/core/tnlTuple.h | 20 +++++++-- src/implementation/core/tnlArray_impl.h | 12 ++++- src/implementation/core/tnlObject.cpp | 8 ++++ src/implementation/core/tnlSharedArray_impl.h | 6 ++- .../core/tnlSharedVector_impl.h | 6 ++- src/implementation/core/tnlString.cpp | 20 ++++++++- src/solvers/linear/stationary/tnlSORSolver.h | 6 +++ 12 files changed, 130 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7176a9be64..0bd6290ce5 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ if( CMAKE_BUILD_TYPE STREQUAL "Debug") set( EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/Debug/bin ) set( debugExt -dbg ) AddCompilerFlag( "-std=gnu++0x -DTEMPLATE_EXPLICIT_INSTANTIATION" ) + #AddCompilerFlag( "-DHAVE_NOT_CXX11 -DTEMPLATE_EXPLICIT_INSTANTIATION" ) else() set( PROJECT_BUILD_PATH ${PROJECT_SOURCE_DIR}/Release/src ) set( PROJECT_TESTS_PATH ${PROJECT_SOURCE_DIR}/Release/tests ) @@ -26,6 +27,7 @@ else() #set( CXX_OPTIMIZE_FLAGS "-O3") OptimizeForArchitecture() AddCompilerFlag( "-std=gnu++0x -O3 -DNDEBUG -DTEMPLATE_EXPLICIT_INSTANTIATION" ) + #AddCompilerFlag( "-DHAVE_NOT_CXX11 -O3 -DNDEBUG -DTEMPLATE_EXPLICIT_INSTANTIATION" ) endif() ##### diff --git a/src/core/tnlArray.h b/src/core/tnlArray.h index 068fbc9d3d..5b9306a9f8 100644 --- a/src/core/tnlArray.h +++ b/src/core/tnlArray.h @@ -90,8 +90,13 @@ class tnlArray : public tnlObject * Every time one touches this grid touches * size * sizeof( Real ) bytes are added * to transfered bytes in tnlStatistics. */ +#ifdef HAVE_NOT_CXX11 + template< typename IndexType2 > + void touch( IndexType2 touches = 1 ) const; +#else template< typename IndexType2 = Index > void touch( IndexType2 touches = 1 ) const; +#endif //! Method for saving the object to a file as a binary data. bool save( tnlFile& file ) const; diff --git a/src/core/tnlFile.h b/src/core/tnlFile.h index f4f8f1e14a..cde05af778 100644 --- a/src/core/tnlFile.h +++ b/src/core/tnlFile.h @@ -104,21 +104,7 @@ class tnlFile } // TODO: this does not work for constant types -#ifdef HAVE_CXX11 - template< typename Type, typename Device = tnlHost, typename Index = int > - bool read( Type* buffer, - const Index& elements ); - - template< typename Type, typename Device = tnlHost > - bool read( Type* buffer ); - - template< typename Type, typename Device = tnlHost, typename Index = int > - bool write( const Type* buffer, - const Index elements ); - - template< typename Type, typename Device = tnlHost > - bool write( Type* buffer ); -#else +#ifdef HAVE_NOT_CXX11 template< typename Type, typename Device, typename Index > bool read( Type* buffer, const Index& elements ); @@ -132,6 +118,21 @@ class tnlFile template< typename Type, typename Device > bool write( Type* buffer ); +#else + template< typename Type, typename Device = tnlHost, typename Index = int > + bool read( Type* buffer, + const Index& elements ); + + template< typename Type, typename Device = tnlHost > + bool read( Type* buffer ); + + template< typename Type, typename Device = tnlHost, typename Index = int > + bool write( const Type* buffer, + const Index elements ); + + template< typename Type, typename Device = tnlHost > + bool write( Type* buffer ); + #endif bool close(); diff --git a/src/core/tnlList.h b/src/core/tnlList.h index 14dae6305a..a7780fd159 100644 --- a/src/core/tnlList.h +++ b/src/core/tnlList.h @@ -301,31 +301,32 @@ template< class T > class tnlList //! Save the list in binary format bool Save( tnlFile& file ) const { -#ifdef HAVE_CXX11 - file. write( &size ); +#ifdef HAVE_NOT_CXX11 + file. write< const int, tnlHost >( &size ); for( int i = 0; i < size; i ++ ) - if( ! file. write( &operator[]( i ), 1 ) ) + if( ! file. write< int, tnlHost, int >( &operator[]( i ), 1 ) ) return false; return true; #else - file. write< const int, tnlHost >( &size ); + file. write( &size ); for( int i = 0; i < size; i ++ ) - if( ! file. write< int, tnlHost, int >( &operator[]( i ), 1 ) ) + if( ! file. write( &operator[]( i ), 1 ) ) return false; return true; + #endif } //! Save the list in binary format using method save of type T bool DeepSave( tnlFile& file ) const { -#ifdef HAVE_CXX11 - file. write( &size ); +#ifdef HAVE_NOT_CXX11 + file. write< const int, tnlHost >( &size ); for( int i = 0; i < size; i ++ ) if( ! operator[]( i ). save( file ) ) return false; return true; #else - file. write< const int, tnlHost >( &size ); + file. write( &size ); for( int i = 0; i < size; i ++ ) if( ! operator[]( i ). save( file ) ) return false; return true; @@ -335,10 +336,10 @@ template< class T > class tnlList //! Load the list bool Load( tnlFile& file ) { -#ifdef HAVE_CXX11 +#ifdef HAVE_NOT_CXX11 EraseAll(); int _size; - file. read( &_size, 1 ); + file. read< int, tnlHost >( &_size ); if( _size < 0 ) { cerr << "The curve size is negative." << endl; @@ -347,7 +348,7 @@ template< class T > class tnlList T t; for( int i = 0; i < _size; i ++ ) { - if( ! file. read( &t, 1 ) ) + if( ! file. read< T, tnlHost >( &t ) ) return false; Append( t ); } @@ -355,7 +356,7 @@ template< class T > class tnlList #else EraseAll(); int _size; - file. read< int, tnlHost >( &_size ); + file. read( &_size, 1 ); if( _size < 0 ) { cerr << "The curve size is negative." << endl; @@ -364,7 +365,7 @@ template< class T > class tnlList T t; for( int i = 0; i < _size; i ++ ) { - if( ! file. read< T, tnlHost >( &t ) ) + if( ! file. read( &t, 1 ) ) return false; Append( t ); } @@ -375,6 +376,23 @@ template< class T > class tnlList //! Load the list using method Load of the type T bool DeepLoad( tnlFile& file ) { +#ifdef HAVE_NOT_CXX11 + EraseAll(); + int _size; + file. read< int, tnlHost >( &_size ); + if( _size < 0 ) + { + cerr << "The list size is negative." << endl; + return false; + } + for( int i = 0; i < _size; i ++ ) + { + T t; + if( ! t. load( file ) ) return false; + Append( t ); + } + return true; +#else EraseAll(); int _size; file. read( &_size ); @@ -390,6 +408,7 @@ template< class T > class tnlList Append( t ); } return true; +#endif }; }; diff --git a/src/core/tnlSharedArray.h b/src/core/tnlSharedArray.h index 38d9389623..e12bbf1ca5 100644 --- a/src/core/tnlSharedArray.h +++ b/src/core/tnlSharedArray.h @@ -89,8 +89,13 @@ class tnlSharedArray : public tnlObject * Every time one touches this grid touches * size * sizeof( Real ) bytes are added * to transfered bytes in tnlStatistics. */ +#ifdef HAVE_NOT_CXX11 + template< typename IndexType2 > + void touch( IndexType2 touches = 1 ) const; +#else template< typename IndexType2 = Index > void touch( IndexType2 touches = 1 ) const; +#endif //! Method for saving the object to a file as a binary data. bool save( tnlFile& file ) const; diff --git a/src/core/tnlTuple.h b/src/core/tnlTuple.h index 57fb49ec9d..11fd35fe68 100644 --- a/src/core/tnlTuple.h +++ b/src/core/tnlTuple.h @@ -550,9 +550,15 @@ template< int Size, typename Real > bool tnlTuple< Size, Real > :: save( tnlFile& file ) const { int size = Size; - if( ! file. write( &size, 1 ) || +#ifdef HAVE_NOT_CXX11 + if( ! file. write< int, tnlHost >( &size ) || + ! file. write< Real, tnlHost, int >( data, size ) ) + cerr << "Unable to write tnlTuple." << endl; +#else + if( ! file. write( &size ) || ! file. write( data, size ) ) cerr << "Unable to write tnlTuple." << endl; +#endif return true; }; @@ -560,18 +566,26 @@ template< int Size, typename Real > bool tnlTuple< Size, Real > :: load( tnlFile& file) { int size; - if( ! file. read( &size, 1 ) ) +#ifdef HAVE_NOT_CXX11 + if( ! file. read< int, tnlHost >( &size ) ) +#else + if( ! file. read( &size ) ) +#endif { cerr << "Unable to read tnlTuple." << endl; return false; - } + } if( size != Size ) { cerr << "You try to read tnlTuple with wrong size " << size << ". It should be " << Size << endl; return false; } +#ifdef HAVE_NOT_CXX11 + if( ! file. read< Real, tnlHost, int >( data, size ) ) +#else if( ! file. read( data, size ) ) +#endif { cerr << "Unable to read tnlTuple." << endl; return false; diff --git a/src/implementation/core/tnlArray_impl.h b/src/implementation/core/tnlArray_impl.h index 60b399dddf..ad0e945658 100644 --- a/src/implementation/core/tnlArray_impl.h +++ b/src/implementation/core/tnlArray_impl.h @@ -301,8 +301,13 @@ bool tnlArray< Element, Device, Index > :: save( tnlFile& file ) const cerr << "You try to save empty vector. Its name is " << this -> getName() ); if( ! tnlObject :: save( file ) ) return false; - if( ! file. write( &this -> size, 1 ) ) +#ifdef HAVE_NOT_CXX11 + if( ! file. write< const Index, tnlHost >( &this -> size ) ) return false; +#else + if( ! file. write( &this -> size ) ) + return false; +#endif if( ! file. write< Element, Device, Index >( this -> data, this -> size ) ) { cerr << "I was not able to WRITE tnlArray " << this -> getName() @@ -320,8 +325,13 @@ bool tnlArray< Element, Device, Index > :: load( tnlFile& file ) if( ! tnlObject :: load( file ) ) return false; int _size; +#ifdef HAVE_NOT_CXX11 + if( ! file. read< int, tnlHost >( &_size ) ) + return false; +#else if( ! file. read( &_size, 1 ) ) return false; +#endif if( _size <= 0 ) { cerr << "Error: The size " << _size << " of the file is not a positive number." << endl; diff --git a/src/implementation/core/tnlObject.cpp b/src/implementation/core/tnlObject.cpp index 0f69bf8c11..864624f69a 100644 --- a/src/implementation/core/tnlObject.cpp +++ b/src/implementation/core/tnlObject.cpp @@ -61,7 +61,11 @@ bool tnlObject :: save( tnlFile& file ) const { dbgFunctionName( "tnlObject", "Save" ); dbgCout( "Writing magic number." ); +#ifdef HAVE_NOT_CXX11 + if( ! file. write< const char, tnlHost, int >( magic_number, strlen( magic_number ) ) ) +#else if( ! file. write( magic_number, strlen( magic_number ) ) ) +#endif return false; dbgCout( "Writing object name " << name ); if( ! this -> getType(). save( file ) || ! name. save( file ) ) return false; @@ -126,7 +130,11 @@ bool getObjectType( tnlFile& file, tnlString& type ) dbgFunctionName( "", "getObjectType" ); dbgCout( "Checking magic number." ); char mn[ 10 ]; +#ifdef HAVE_NOT_CXX11 + if( ! file. read< char, tnlHost, int >( mn, strlen( magic_number ) ) ) +#else if( ! file. read( mn, strlen( magic_number ) ) ) +#endif { cerr << "Unable to read file " << file. getFileName() << " ... " << endl; return false; diff --git a/src/implementation/core/tnlSharedArray_impl.h b/src/implementation/core/tnlSharedArray_impl.h index ad0e8b4a5f..66c1f8a817 100644 --- a/src/implementation/core/tnlSharedArray_impl.h +++ b/src/implementation/core/tnlSharedArray_impl.h @@ -283,7 +283,11 @@ bool tnlSharedArray< Element, Device, Index > :: save( tnlFile& file ) const cerr << "You try to save empty array. Its name is " << this -> getName() ); if( ! tnlObject :: save( file ) ) return false; - if( ! file. write( &this -> size, 1 ) ) +#ifdef HAVE_NOT_CXX11 + if( ! file. write< const Index, Device >( &this -> size ) ) +#else + if( ! file. write( &this -> size ) ) +#endif return false; if( ! file. write< Element, Device, Index >( this -> data, this -> size ) ) { diff --git a/src/implementation/core/tnlSharedVector_impl.h b/src/implementation/core/tnlSharedVector_impl.h index ff0c2724be..e43fc31303 100644 --- a/src/implementation/core/tnlSharedVector_impl.h +++ b/src/implementation/core/tnlSharedVector_impl.h @@ -84,7 +84,11 @@ bool tnlSharedVector< Element, Device, Index > :: save( tnlFile& file ) const cerr << "You try to save empty vector. Its name is " << this -> getName() ); if( ! tnlObject :: save( file ) ) return false; - if( ! file. write( &this -> size, 1 ) ) +#ifdef HAVE_NOT_CXX11 + if( ! file. write< const Index, Device >( &this -> size ) ) +#else + if( ! file. write( &this -> size ) ) +#endif return false; if( ! file. write< Element, Device, Index >( this -> data, this -> size ) ) { diff --git a/src/implementation/core/tnlString.cpp b/src/implementation/core/tnlString.cpp index 7d05156953..229f9cfa90 100644 --- a/src/implementation/core/tnlString.cpp +++ b/src/implementation/core/tnlString.cpp @@ -288,9 +288,17 @@ bool tnlString :: save( tnlFile& file ) const dbgExpr( string ); int len = strlen( string ); - if( ! file. write( &len, 1 ) ) +#ifdef HAVE_NOT_CXX11 + if( ! file. write< int, tnlHost >( &len ) ) +#else + if( ! file. write( &len ) ) +#endif return false; +#ifdef HAVE_NOT_CXX11 + if( ! file. write< char, tnlHost, int >( string, len ) ) +#else if( ! file. write( string, len ) ) +#endif return false; return true; } @@ -298,7 +306,11 @@ bool tnlString :: save( tnlFile& file ) const bool tnlString :: load( tnlFile& file ) { int _length; - if( ! file. read( &_length, 1 ) ) +#ifdef HAVE_NOT_CXX11 + if( ! file. read< int, tnlHost >( &_length ) ) +#else + if( ! file. read( &_length ) ) +#endif { cerr << "I was not able to read tnlString length." << endl; return false; @@ -321,7 +333,11 @@ bool tnlString :: load( tnlFile& file ) string = new char[ length ]; } +#ifdef HAVE_NOT_CXX11 + if( ! file. read< char, tnlHost, int >( string, _length ) ) +#else if( ! file. read( string, _length ) ) +#endif { cerr << "I was not able to read a tnlString with a length " << length << "." << endl; return false; diff --git a/src/solvers/linear/stationary/tnlSORSolver.h b/src/solvers/linear/stationary/tnlSORSolver.h index 240f77ea9d..ca7295f64f 100644 --- a/src/solvers/linear/stationary/tnlSORSolver.h +++ b/src/solvers/linear/stationary/tnlSORSolver.h @@ -55,9 +55,15 @@ class tnlSORSolver : public tnlObject, void setPreconditioner( const Preconditioner& preconditioner ); +#ifdef HAVE_NOT_CXX11 + template< typename Vector, + typename ResidueGetter > + bool solve( const Vector& b, Vector& x ); +#else template< typename Vector, typename ResidueGetter = tnlLinearResidueGetter< Matrix, Vector > > bool solve( const Vector& b, Vector& x ); +#endif ~tnlSORSolver(); -- GitLab