Commit 87c55031 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Debuging shared pointers. Operator = does not work in tnlMeshFunction.

parent a34c1d95
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -30,8 +30,7 @@ bool tnlArrayOperations< tnlCuda >::allocateMemory( Element*& data,
                                                    const Index size )
{
#ifdef HAVE_CUDA
   if( cudaMalloc( ( void** ) &data,
                   ( size_t ) size * sizeof( Element ) ) != cudaSuccess )
   if( cudaMalloc( ( void** ) &data, ( size_t ) size * sizeof( Element ) ) != cudaSuccess )
      data = 0;
   return checkCudaDevice;
#else
@@ -200,6 +199,7 @@ bool tnlArrayOperations< tnlHost, tnlCuda >::copyMemory( DestinationElement* des
{
   tnlAssert( destination, );
   tnlAssert( source, );

   #ifdef HAVE_CUDA
   if( std::is_same< DestinationElement, SourceElement >::value )
   {
+1 −0
Original line number Diff line number Diff line
@@ -87,5 +87,6 @@ void tnlCuda::removeSmartPointer( tnlSmartPointer* pointer )
bool tnlCuda::synchronizeDevice( int deviceId )
{
    smartPointersRegister.synchronizeDevice( deviceId );
    return checkCudaDevice;
}
+5 −4
Original line number Diff line number Diff line
@@ -50,12 +50,13 @@ int tnlCuda::getDeviceId()
   return id;
}

bool tnlCuda::checkDevice( const char* file_name, int line )
bool tnlCuda::checkDevice( const char* file_name, int line, cudaError error )
{   
   cudaError error = cudaGetLastError();
   if( error == cudaSuccess )
      return true;
   cerr << "CUDA ERROR(" << error << ") at line " << line << " in " << file_name << ":" << endl;
   cerr << cudaGetErrorString( error )  << endl;   
   cerr << "Detailed description is: " << endl;
   switch( error )
   {
      // 1
@@ -424,6 +425,6 @@ bool tnlCuda::checkDevice( const char* file_name, int line )
       break;

   }
   //throw EXIT_FAILURE;
   throw EXIT_FAILURE;
   return false;
}
+12 −3
Original line number Diff line number Diff line
@@ -92,9 +92,14 @@ class tnlCuda
#endif

#ifdef HAVE_CUDA
   static bool checkDevice( const char* file_name, int line );
   /****
    * I do not know why, but it is more reliable to pass the error code instead
    * of calling cudaGetLastError() inside the method.
    * We recommend to use macro 'checkCudaDevice' defined bellow.
    */
   static bool checkDevice( const char* file_name, int line, cudaError error );
#else
   static bool checkDevice( const char* file_name, int line ) { return false;};
   static bool checkDevice() { return false;};
#endif
   
   static void configSetup( tnlConfigDescription& config, const tnlString& prefix = "" );
@@ -115,7 +120,11 @@ class tnlCuda

};

#define checkCudaDevice tnlCuda::checkDevice( __FILE__, __LINE__ )
#ifdef HAVE_CUDA
#define checkCudaDevice tnlCuda::checkDevice( __FILE__, __LINE__, cudaGetLastError() )
#else
#define checkCudaDevice tnlCuda::checkDevice()
#endif

#define tnlCudaSupportMissingMessage \
   std::cerr << "The CUDA support is missing in the source file " << __FILE__ << " at line " << __LINE__ << ". Please set WITH_CUDA=yes in the install script. " << std::endl;
+60 −31
Original line number Diff line number Diff line
@@ -37,20 +37,29 @@ class tnlSharedPointer< Object, tnlHost > : public tnlSmartPointer
      typedef tnlSharedPointer< Object, tnlHost > ThisType;
         
      explicit  tnlSharedPointer()
      : counter( new int )
      : counter( 0 ), pointer( 0 )
      {
         std::cerr << "Creating new shared pointer..." << std::endl;
         this->pointer = new Object();
         *( this->counter ) = 1;
      }
      
      tnlSharedPointer( const ThisType& pointer )
      : pointer( pointer.pointer ),
        counter( pointer.counter )
      {
         *counter++;
      }
      
      template< typename... Args >
      explicit tnlSharedPointer( const Args... args )
      : counter( new int )
      bool create( const Args... args )
      {         
         std::cerr << "Creating new shared pointer..." << std::endl;
         this->free();
         this->pointer = new Object( args... );
         this->counter = new int;
         if( ! this->pointer || ! this->counter )
            return false;
         *( this->counter ) = 1;
         return true;
         
      }
      
      const Object* operator->() const
@@ -119,12 +128,16 @@ class tnlSharedPointer< Object, tnlHost > : public tnlSmartPointer
      
      void free()
      {
         if( ! this->pointer )
            return;
         if( this->counter )
         {
            if( ! --*( this->counter ) )
            {
               delete this->pointer;
               std::cerr << "Deleting data..." << std::endl;
            }
         }

      }
      
@@ -143,32 +156,40 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
      typedef tnlSharedPointer< Object, tnlHost > ThisType;

      explicit  tnlSharedPointer()
      : modified( false ),
        counter( new int )
      : counter( 0 ), cuda_pointer( 0 ), 
        pointer( 0 ), modified( false )
      {
         std::cerr << "Creating new shared pointer..." << std::endl;
         this->pointer = new Object();
         *( this->counter )= 1;
#ifdef HAVE_CUDA         
         cudaMalloc( ( void** )  &this->cuda_pointer, sizeof( Object ) );
         cudaMemcpy( this->cuda_pointer, this->pointer, sizeof( Object ), cudaMemcpyHostToDevice );
         tnlCuda::insertSmartPointer( this );
#endif         
      }
                  
      tnlSharedPointer( const ThisType& pointer )
      : pointer( pointer.pointer ),
        cuda_pointer( pointer.cuda_pointer ),
        counter( pointer.counter ),
        modified( pointer.modified )
      {
         *counter++;
      }

      
      template< typename... Args >
      explicit tnlSharedPointer( const Args... args )
      : modified( false ),
        counter( new int )
      bool create( const Args... args )
      {
         std::cerr << "Creating new shared pointer..." << std::endl;
         this->free();
         this->modified = false;
         this->counter= new int;
         this->pointer = new Object( args... );
         if( ! this->pointer || ! this->counter )
            return false;
         *( this->counter )= 1;         
#ifdef HAVE_CUDA         
         cudaMalloc( ( void** )  &this->cuda_pointer, sizeof( Object ) );
         cudaMemcpy( this->cuda_pointer, this->pointer, sizeof( Object ), cudaMemcpyHostToDevice );
         if( ! checkCudaDevice )
            return false;
         tnlCuda::insertSmartPointer( this );
#endif
         return true;
      }
      
      const Object* operator->() const
@@ -198,6 +219,8 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
      const Object& getData() const
      {
         static_assert( std::is_same< Device, tnlHost >::value || std::is_same< Device, tnlCuda >::value, "Only tnlHost or tnlCuda devices are accepted here." );
         tnlAssert( this->pointer, );
         tnlAssert( this->cuda_pointer, );
         if( std::is_same< Device, tnlHost >::value )
            return *( this->pointer );
         if( std::is_same< Device, tnlCuda >::value )
@@ -210,7 +233,7 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
         return *( this->pointer );
      }
      
      const ThisType& operator=( ThisType&& ptr )
      /*const ThisType& operator=( ThisType&& ptr )
      {
         if( this-> pointer )
            delete this->pointer;
@@ -227,10 +250,11 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
         ptr.modified = false;
         ptr.counter = NULL;
         return *this;
      }
      }*/
      
      const ThisType& operator=( const ThisType& ptr )
      {
         std::cerr << "Op. = " << std::endl;
         this->free();
         this->pointer = ptr.pointer;
         this->cuda_pointer = ptr.cuda_pointer;
@@ -244,14 +268,16 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
      bool synchronize()
      {
#ifdef HAVE_CUDA
         if( this-> modified )
         /*if( this->modified )
         {
            std::cerr << "Synchronizing data..." << std::endl;
            cudaMemcpy( this->cuda_pointer, this->pointer, sizeof( Object ), cudaMemcpyHostToDevice );
            tnlAssert( this->pointer, );
            tnlAssert( this->cuda_pointer, );
            cudaMemcpy( this->cuda_pointer, this->pointer, sizeof( ObjectType ), cudaMemcpyHostToDevice );
            if( ! checkCudaDevice )
               return false;
            return true;
         }
         }*/
#else         
         return false;
#endif         
@@ -269,6 +295,8 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
      
      void free()
      {
         if( ! this->pointer )
            return;
         if( this->counter )
         {
            if( ! --*( this->counter ) )
@@ -278,6 +306,7 @@ class tnlSharedPointer< Object, tnlCuda > : public tnlSmartPointer
#ifdef HAVE_CUDA
               if( this->cuda_pointer )
                  cudaFree( this->cuda_pointer );
               checkCudaDevice;
#endif         
               std::cerr << "Deleting data..." << std::endl;
            }
Loading