Commit 7c5275c8 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Implementing device objects management.

parent 36bb97c6
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -12,6 +12,9 @@ set (headers tnlAssert.h
             tnlCudaDeviceInfo.h
             tnlDataElement.h
             tnlDevice.h
             tnlDeviceObject.h
             tnlDeviceObjectBase.h
             tnlDeviceObjectsContainer.h
             tnlDynamicTypeTag.h
             tnlFeature.h
             tnlFile.h 
@@ -42,6 +45,7 @@ set (headers tnlAssert.h
SET( CURRENT_DIR ${CMAKE_SOURCE_DIR}/src/core )
set( common_SOURCES
     ${CURRENT_DIR}/tnlTimerRT.cpp
     ${CURRENT_DIR}/tnlDeviceObjectsContainer.cpp
     ${CURRENT_DIR}/tnlFile.cpp
     ${CURRENT_DIR}/tnlFlopsCounter.cpp 
     ${CURRENT_DIR}/tnlLogger.cpp 
+5 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <core/tnlDevice.h>
#include <core/tnlString.h>
#include <core/tnlAssert.h>
#include <core/tnlDeviceObjectsContainer.h>

class tnlConfigDescription;
class tnlParameterContainer;
@@ -101,6 +102,10 @@ class tnlCuda
   static bool setup( const tnlParameterContainer& parameters,
                      const tnlString& prefix = "" );
   
   static bool synchronizeObjects();
   
   static tnlDeviceObjectsContainer objectsContainer;


};

+56 −28
Original line number Diff line number Diff line
@@ -82,50 +82,76 @@ class tnlDeviceObject< Object, tnlCuda > : public tnlDeviceObjectBase
{   
   public:
      
      tnlDeviceObject( Object& object )
      tnlDeviceObject()
      : modified( false ), device_pointer( 0 )
      {
         this->host_pointer = &object;
#ifdef HAVE_CUDA         
         cudaMalloc( ( void** ) &this->device_pointer, sizeof( Object ) );
         deviceId = tnlCuda::getDeviceId();
         tnlCuda::getDeviceObjectsContainer().enregister( this );
         tnlCuda::getDeviceObjectsContainer().push( this );
#endif         
      }
      
      
      Object* operator->()
      tnlDeviceObject( Object& object )
      : modified( true ), device_pointer( 0 )
      {
         return host_pointer;
#ifdef HAVE_CUDA         
         tnlCuda::getDeviceObjectsContainer().push( this );         
#endif         
      }

      const Object* operator->() const
      bool setObject( Object& object )
      {
         return host_pointer;
#ifdef HAVE_CUDA
         this->host_pointer = &object;
         if( this->device_pointer )
            cudaFree( this->device_pointer );
         cudaMalloc( ( void** ) &this->device_pointer, sizeof( Object ) );
         if( ! checkCudaDevice )
            return false;
         deviceId = tnlCuda::getDeviceId();         
         this->modified = true;
         return true;
#else
         return false
#endif                  
      }
      
      const Object& get() const
      template< typename Device >
      const Object& object() const
      {
         return *host_pointer;
      }
         tnlAssert( std::is_same< Device, tnlHost >::value ||
                    std::is_same< Device, tnlCuda >::value, );

      Object& modify()
      {
         return *host_pointer;
         if( std::is_same< Device, tnlHost >::value )
            return *this->pointer;
         return *this->device_pointer;
      }            
      
      Object* getDevicePointer()
      Object& change()
      {
         return device_pointer;
         this->modified = true;
         return *this->pointer;
      }            
            
      const Object* getDevicePointer() const
      bool synchronize()
      {
         return device_pointer;
#ifdef HAVE_CUDA
         if( this->modified )
         {
            cudaMemcpy( device_pointer, host_pointer, sizeof( Object ), cudaMemcpyHostToDevice );
            return checkCudaDevice;
         }
#else
         return false;
#endif                  
      }
      
      bool synchronize()
      ~tnlDeviceObject()
      {
         
#ifdef HAVE_CUDA
         tnlCuda::getDeviceObjectsContainer().pull( this );
         if( this->device_pointer )
            cudaFree( this->device_pointer );
#endif         
      }
      
   protected:
@@ -133,5 +159,7 @@ class tnlDeviceObject< Object, tnlCuda > : public tnlDeviceObjectBase
      Object *host_pointer, *device_pointer;
      
      int deviceId;
      
      bool modified;
};
+42 −0
Original line number Diff line number Diff line
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/***************************************************************************
                          tnlDeviceObjectsContainer.cpp  -  description
                             -------------------
    begin                : Apr 29, 2016
    copyright            : (C) 2016 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

#include <core/tnlDeviceObjectsContainer.h>

tnlDeviceObjectsContainer::tnlDeviceObjectsContainer( int devicesCount )
{
   tnlAssert( devicesCount > 0, std::cerr << "devicesCount = " << devicesCount );
   objectsOnDevices.resize( devicesCount );
   this->devicesCount = devicesCount;
}

void tnlDeviceObjectsContainer::push( tnlDeviceObjectBase* object, int deviceId )
{
   tnlAssert( deviceId >= 0 && deviceId < this->devicesCount,
              std::cerr << "deviceId = " << deviceId << " devicesCount = " << this->devicesCount );
   objectsOnDevices[ deviceId ].push_back( object );
}

bool tnlDeviceObjectsContainer::synchronize()
{
   for( int i = 0; i < this->objectsOnDevices.size(); i++ )
      for( ListType::iterator it = objectsOnDevices[ i ].begin();
           it != objectsOnDevices[ i ].end();
           it++ )
         ( *it )->synchronize();            

}
+3 −16
Original line number Diff line number Diff line
@@ -27,24 +27,11 @@ class tnlDeviceObjectsContainer
  
   public:
   
      tnlDeviceObjectsContainer( int devicesCount = 1 )
      {
         tnlAssert( deviceCount > 0, std::cerr << "deviceCount = " << deviceCount );
         objectsOnDevices.resize( devicesCount );
         this->devicesCount = devicesCount;
      }
      tnlDeviceObjectsContainer( int devicesCount = 1 );
      
      void push( tnlDeviceObjectBase* object, int deviceId )
      {
         tnlAssert( deviceId >= 0 && deviceId < this->devicesCount,
                    std::cerr << "deviceId = " << deviceId << " devicesCount = " << this->devicesCount );
         objectsOnDevices[ deviceId ].push( object );
      }
      void push( tnlDeviceObjectBase* object, int deviceId );
      
      bool synchronize()
      {
         ....
      }
      bool synchronize();
      
   protected: