Commit 93db2ce6 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Fixed UniquePointer, cleaned up SharedPointer and DevicePointer

parent 3677ba8b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -34,7 +34,8 @@ set( headers
     SystemInfo.h
     Timer.h
     TimerCPU.h
     TimerRT.h )
     TimerRT.h
     UniquePointer.h )

set( common_SOURCES 
     File.cpp
+1 −8
Original line number Diff line number Diff line
@@ -210,12 +210,10 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
      {
         this->counter = new int( 1 );
         this->pointer = &obj;
#ifdef HAVE_CUDA
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return;
         Devices::Cuda::insertSmartPointer( this );
#endif
      }

      // this is needed only to avoid the default compiler-generated constructor
@@ -402,9 +400,7 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
      ~DevicePointer()
      {
         this->free();
#ifdef HAVE_CUDA
         Devices::Cuda::removeSmartPointer( this );
#endif
      }

   protected:
@@ -417,11 +413,8 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
            {
               delete this->counter;
               this->counter = nullptr;
#ifdef HAVE_CUDA
               if( this->cuda_pointer )
                  cudaFree( this->cuda_pointer );
               checkCudaDevice;
#endif
                  Devices::Cuda::freeFromDevice( this->cuda_pointer );
            }
         }

+1 −12
Original line number Diff line number Diff line
@@ -310,7 +310,6 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         {
            this->counter = new int( 1 );
            this->pointer = new Object( args... );
#ifdef HAVE_CUDA
            this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
            if( ! this->cuda_pointer )
               return;
@@ -318,7 +317,6 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
            std::cerr << "Created shared pointer to " << demangle(typeid(ObjectType).name()) << " (cuda_pointer = " << this->cuda_pointer << ")" << std::endl;
#endif
            Devices::Cuda::insertSmartPointer( this );
#endif
         }
      }

@@ -382,12 +380,10 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         {
            this->counter = new int( 1 );
            this->pointer = new ObjectType( args... );
#ifdef HAVE_CUDA
            this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
            if( ! this->cuda_pointer )
               return false;
            Devices::Cuda::insertSmartPointer( this );
#endif
            return true;
         }
         if( *this->counter == 1 )
@@ -408,13 +404,11 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         this->pointer = new Object( args... );
         if( ! this->pointer || ! this->counter )
            return false;
#ifdef HAVE_CUDA
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         if( ! this->cuda_pointer )
            return false;
         // TODO: what if 'this' is already in the register?
         Devices::Cuda::insertSmartPointer( this );
#endif
         return true;
      }

@@ -568,9 +562,7 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
      ~SharedPointer()
      {
         this->free();
#ifdef HAVE_CUDA
         Devices::Cuda::removeSmartPointer( this );
#endif
      }

   protected:
@@ -588,11 +580,8 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
               this->counter = nullptr;
               if( this->pointer )
                  delete this->pointer;
#ifdef HAVE_CUDA
               if( this->cuda_pointer )
                  cudaFree( this->cuda_pointer );
               checkCudaDevice;
#endif
                  Devices::Cuda::freeFromDevice( this->cuda_pointer );
#ifdef TNL_DEBUG_SHARED_POINTERS
               std::cerr << "...deleted data." << std::endl;
#endif
+14 −32
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@

#pragma once

#include <utility>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
#include <TNL/SmartPointer.h>
@@ -85,7 +84,7 @@ class UniquePointer< Object, Devices::Host > : public SmartPointer
         if( this->pointer )
            delete this->pointer;
         this->pointer = ptr.pointer;
         ptr.pointer= NULL;
         ptr.pointer = nullptr;
         return *this;
      }
      
@@ -120,27 +119,13 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      typedef Devices::Host DeviceType;
      typedef UniquePointer< Object, Devices::Host > ThisType;
         
      UniquePointer()
      : modified( false )
      {
         this->pointer = new Object();
#ifdef HAVE_CUDA         
         cudaMalloc( ( void** )  &this->cuda_pointer, sizeof( Object ) );
         cudaMemcpy( this->cuda_pointer, this->pointer, sizeof( Object ), cudaMemcpyHostToDevice );
         tnlCuda::insertSmartPointer( this );
#endif         
      }
      
      template< typename... Args >
      UniquePointer( const Args... args )
      : modified( false )
      {
         this->pointer = new Object( args... );
#ifdef HAVE_CUDA         
         cudaMalloc( ( void** )  &this->cuda_pointer, sizeof( Object ) );
         cudaMemcpy( this->cuda_pointer, this->pointer, sizeof( Object ), cudaMemcpyHostToDevice );
         tnlCuda::insertSmartPointer( this );
#endif                  
         this->cuda_pointer = Devices::Cuda::passToDevice( *this->pointer );
         Devices::Cuda::insertSmartPointer( this );
      }
      
      const Object* operator->() const
@@ -168,7 +153,7 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      template< typename Device = Devices::Host >      
      const Object& getData() const
      {
         static_assert( std::is_same< Device, Devices::Host >::value || std::is_same< Device, Devices::Cuda >::value, "Only Devices::Host or tnlCuda devices are accepted here." );
         static_assert( std::is_same< Device, Devices::Host >::value || std::is_same< Device, Devices::Cuda >::value, "Only Devices::Host or Devices::Cuda devices are accepted here." );
         if( std::is_same< Device, Devices::Host >::value )
            return *( this->pointer );
         if( std::is_same< Device, Devices::Cuda >::value )
@@ -185,15 +170,13 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      {
         if( this->pointer )
            delete this->pointer;
#ifdef HAVE_CUDA
         if( this->cuda_pointer )
            cudaFree( this->cuda_pointer );
#endif                  
            Devices::Cuda::freeFromDevice( this->cuda_pointer );
         this->pointer = ptr.pointer;
         this->cuda_pointer = ptr.cuda_pointer;
         this->modified = ptr.modified;
         ptr.pointer= NULL;
         ptr.cuda_pointer = NULL;
         ptr.pointer = nullptr;
         ptr.cuda_pointer = nullptr;
         ptr.modified = false;
         return *this;
      }
@@ -213,6 +196,7 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
               return false;
            return true;
         }
         return true;
#else         
         return false;
#endif         
@@ -222,16 +206,14 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      {
         if( this->pointer )
            delete this->pointer;
#ifdef HAVE_CUDA
         if( this->cuda_pointer )
            cudaFree( this->cuda_pointer );
         tnlCuda::removeSmartPointer( this );
#endif         
            Devices::Cuda::freeFromDevice( this->cuda_pointer );
         Devices::Cuda::removeSmartPointer( this );
      }
      
   protected:
      
      Object *pointer, cuda_pointer;
      Object *pointer, *cuda_pointer;
      
      bool modified;      
};