Commit 36bb97c6 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Adding tnlDeviceObject.

parent 1d9a7a66
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -41,6 +41,15 @@ bool tnlCuda::setup( const tnlParameterContainer& parameters,
}
*/

int tnlCuda::getDeviceId()
{
   int id( 0 );
#ifdef HAVE_CUDA
   cudaGetDevice( &id );
#endif
   return id;
}

bool tnlCuda::checkDevice( const char* file_name, int line )
{
   cudaError error = cudaGetLastError();
+3 −1
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ class tnlCuda
   __cuda_callable__ static inline int getWarpSize();

#ifdef HAVE_CUDA
   static int getDeviceId();
   
   template< typename Index >
   __device__ static Index getGlobalThreadIdx( const Index gridIdx = 0 );
#endif
+137 −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.                                   *
 *                                                                         *
 ***************************************************************************/

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

#pragma once

#include <core/tnlHost.h>
#include <core/tnlCuda.h>
#include <core/tnlDeviceObjectBase.h>

template< typename Object,
          typename Device >
class tnlDeviceObject
{   
};

template< typename Object >
class tnlDeviceObject< Object, tnlHost > : public tnlDeviceObjectBase
{   
   public:
      
      tnlDeviceObject( Object& object )
      {
         this->pointer = &object;
      }
      
      Object* operator->()
      {
         return this->pointer;
      }
      
      const Object* operator->() const
      {
         return this->pointer;
      }
      
      const Object& get() const
      {
         return *this->pointer;
      }
      
      Object& modify()
      {
         return *this->pointer;
      }
      
      Object* getDevicePointer()
      {
         return this->pointer;
      }
      
      const Object* getDevicePointer() const
      {
         return this->pointer;
      }
      
      bool synchronize()
      {
         return true;
      }
            
   protected:
      
      Object* pointer;
};

template< typename Object >
class tnlDeviceObject< Object, tnlCuda > : public tnlDeviceObjectBase
{   
   public:
      
      tnlDeviceObject( Object& object )
      {
         this->host_pointer = &object;
#ifdef HAVE_CUDA
         cudaMalloc( ( void** ) &this->device_pointer, sizeof( Object ) );
         deviceId = tnlCuda::getDeviceId();
         tnlCuda::getDeviceObjectsContainer().enregister( this );
#endif         
      }

      
      Object* operator->()
      {
         return host_pointer;
      }
      
      const Object* operator->() const
      {
         return host_pointer;
      }
      
      const Object& get() const
      {
         return *host_pointer;
      }
      
      Object& modify()
      {
         return *host_pointer;
      }
      
      Object* getDevicePointer()
      {
         return device_pointer;
      }
      
      const Object* getDevicePointer() const
      {
         return device_pointer;
      }

      bool synchronize()
      {
         
      }
      
   protected:
      
      Object *host_pointer, *device_pointer;
      
      int deviceId;
};
+27 −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.                                   *
 *                                                                         *
 ***************************************************************************/

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

#pragma once

class tnlDeviceObjectBase
{
   public:
   
      virtual bool synchronize() = 0;
};

+57 −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.h  -  description
                             -------------------
    begin                : Apr 29, 2016
    copyright            : (C) 2016 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

#pragma once

#include <vector>
#include <list>
#include <core/tnlDeviceObjectBase.h>
#include <core/tnlAssert.h>

class tnlDeviceObjectsContainer
{   
  
   public:
   
      tnlDeviceObjectsContainer( int devicesCount = 1 )
      {
         tnlAssert( deviceCount > 0, std::cerr << "deviceCount = " << deviceCount );
         objectsOnDevices.resize( devicesCount );
         this->devicesCount = devicesCount;
      }
      
      void push( tnlDeviceObjectBase* object, int deviceId )
      {
         tnlAssert( deviceId >= 0 && deviceId < this->devicesCount,
                    std::cerr << "deviceId = " << deviceId << " devicesCount = " << this->devicesCount );
         objectsOnDevices[ deviceId ].push( object );
      }
      
      bool synchronize()
      {
         ....
      }
      
   protected:
      
      typedef std::list< tnlDeviceObjectBase* > ListType;   
      
      std::vector< ListType > objectsOnDevices;
      
      int devicesCount;
};