Commit f2042755 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Optimized smart pointers

The original method for detecting changes to the wrapped objects (using
the 'modified' bool flag) is used to avoid unnecessary calls to
std::memcmp.
parent a3298c46
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -270,6 +270,7 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer

      Object* operator->()
      {
         this->pd->maybe_modified = true;
         return this->pointer;
      }

@@ -280,6 +281,7 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer

      Object& operator *()
      {
         this->pd->maybe_modified = true;
         return *( this->pointer );
      }

@@ -311,7 +313,10 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
         Assert( this->pd, );
         Assert( this->cuda_pointer, );
         if( std::is_same< Device, Devices::Host >::value )
         {
            this->pd->maybe_modified = true;
            return *( this->pointer );
         }
         if( std::is_same< Device, Devices::Cuda >::value )
            return *( this->cuda_pointer );
      }
@@ -402,6 +407,7 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
      {
         char data_image[ sizeof(Object) ];
         int counter = 1;
         bool maybe_modified = false;
      };

      bool allocate( ObjectType& obj )
@@ -425,12 +431,16 @@ class DevicePointer< Object, Devices::Cuda > : public SmartPointer
         Assert( this->pointer, );
         Assert( this->pd, );
         std::memcpy( (void*) &this->pd->data_image, (void*) this->pointer, sizeof( Object ) );
         this->pd->maybe_modified = false;
      }

      bool modified()
      {
         Assert( this->pointer, );
         Assert( this->pd, );
         // optimization: skip bitwise comparison if we're sure that the data is the same
         if( ! this->pd->maybe_modified )
            return false;
         return std::memcmp( (void*) &this->pd->data_image, (void*) this->pointer, sizeof( Object ) ) != 0;
      }

+12 −1
Original line number Diff line number Diff line
@@ -387,6 +387,7 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer

      Object* operator->()
      {
         this->pd->maybe_modified = true;
         return &this->pd->data;
      }

@@ -397,6 +398,7 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer

      Object& operator *()
      {
         this->pd->maybe_modified = true;
         return this->pd->data;
      }

@@ -426,7 +428,10 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         Assert( this->pd, );
         Assert( this->cuda_pointer, );
         if( std::is_same< Device, Devices::Host >::value )
         {
            this->pd->maybe_modified = true;
            return this->pd->data;
         }
         if( std::is_same< Device, Devices::Cuda >::value )
            return *( this->cuda_pointer );
      }
@@ -527,11 +532,13 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
         Object data;
         char data_image[ sizeof(Object) ];
         int counter;
         bool maybe_modified;

         template< typename... Args >
         explicit PointerData( Args... args )
         : data( args... ),
           counter( 1 )
           counter( 1 ),
           maybe_modified( false )
         {}
      };

@@ -558,11 +565,15 @@ class SharedPointer< Object, Devices::Cuda, lazy > : public SmartPointer
      {
         Assert( this->pd, );
         std::memcpy( (void*) &this->pd->data_image, (void*) &this->pd->data, sizeof( Object ) );
         this->pd->maybe_modified = false;
      }

      bool modified()
      {
         Assert( this->pd, );
         // optimization: skip bitwise comparison if we're sure that the data is the same
         if( ! this->pd->maybe_modified )
            return false;
         return std::memcmp( (void*) &this->pd->data_image, (void*) &this->pd->data, sizeof( Object ) ) != 0;
      }

+12 −1
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      
      Object* operator->()
      {
         this->pd->maybe_modified = true;
         return &this->pd->data;
      }
      
@@ -148,6 +149,7 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      
      Object& operator *()
      {
         this->pd->maybe_modified = true;
         return this->pd->data;
      }
      
@@ -175,7 +177,10 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
         Assert( this->pd, );
         Assert( this->cuda_pointer, );
         if( std::is_same< Device, Devices::Host >::value )
         {
            this->pd->maybe_modified = true;
            return this->pd->data;
         }
         if( std::is_same< Device, Devices::Cuda >::value )
            return *( this->cuda_pointer );
      }
@@ -226,10 +231,12 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      {
         Object data;
         char data_image[ sizeof(Object) ];
         bool maybe_modified;

         template< typename... Args >
         explicit PointerData( Args... args )
         : data( args... )
         : data( args... ),
           maybe_modified( false )
         {}
      };

@@ -253,11 +260,15 @@ class UniquePointer< Object, Devices::Cuda > : public SmartPointer
      {
         Assert( this->pd, );
         std::memcpy( (void*) &this->pd->data_image, (void*) &this->pd->data, sizeof( ObjectType ) );
         this->pd->maybe_modified = false;
      }

      bool modified()
      {
         Assert( this->pd, );
         // optimization: skip bitwise comparison if we're sure that the data is the same
         if( ! this->pd->maybe_modified )
            return false;
         return std::memcmp( (void*) &this->pd->data_image, (void*) &this->pd->data, sizeof( ObjectType ) ) != 0;
      }