cmtkDeviceMemory.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 2010 SRI International
00004 //
00005 //  This file is part of the Computational Morphometry Toolkit.
00006 //
00007 //  http://www.nitrc.org/projects/cmtk/
00008 //
00009 //  The Computational Morphometry Toolkit is free software: you can
00010 //  redistribute it and/or modify it under the terms of the GNU General Public
00011 //  License as published by the Free Software Foundation, either version 3 of
00012 //  the License, or (at your option) any later version.
00013 //
00014 //  The Computational Morphometry Toolkit is distributed in the hope that it
00015 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00016 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU General Public License along
00020 //  with the Computational Morphometry Toolkit.  If not, see
00021 //  <http://www.gnu.org/licenses/>.
00022 //
00023 //  $Revision: 2184 $
00024 //
00025 //  $LastChangedDate: 2010-08-06 16:03:41 -0700 (Fri, 06 Aug 2010) $
00026 //
00027 //  $LastChangedBy: torstenrohlfing $
00028 //
00029 */
00030 
00031 #ifndef __cmtkDeviceMemory_h_included_
00032 #define __cmtkDeviceMemory_h_included_
00033 
00034 #include <cmtkconfig.h>
00035 
00036 #ifdef CMTK_USE_CUDA
00037 #  include "cmtkDeviceMemoryCUDA.h"
00038 #else
00039 #  include "cmtkDeviceMemoryCL.h"
00040 #endif
00041 
00042 namespace
00043 cmtk
00044 {
00045 
00048 
00050 #ifdef CMTK_USE_CUDA
00051 template<typename T,class DeviceMemoryGPU = DeviceMemoryCUDA>
00052 #else
00053 template<typename T,class DeviceMemoryGPU = DeviceMemoryCL>
00054 #endif
00055 class DeviceMemory :
00057     private DeviceMemoryGPU
00058 {
00059 public:
00061   typedef DeviceMemory<T,DeviceMemoryGPU> Self;
00062   
00064   typedef SmartConstPointer<Self> SmartConstPtr;
00065 
00067   typedef SmartPointer<Self> SmartPtr;
00068 
00070   typedef DeviceMemoryGPU Superclass;
00071 
00073   DeviceMemory( const size_t n,  const size_t padToMultiple = 1 ) 
00074     : DeviceMemoryGPU( n * sizeof( T ), padToMultiple * sizeof( T ) ),
00075       m_NumberOfItems( n )
00076   {}
00077 
00079   static typename Self::SmartPtr Create( const size_t nItems, 
00080                                          const size_t padToMultiple = 1   )
00081   {
00082     return typename Self::SmartPtr( new Self( nItems, padToMultiple ) );
00083   }
00084   
00086   static typename Self::SmartPtr Create( const size_t nItems, 
00087                                          const T* initFrom, 
00088                                          const size_t padToMultiple = 1   )
00089   {
00090     Self* newObject = new Self( nItems, padToMultiple );
00091     newObject->CopyToDevice( initFrom, nItems );
00092     return typename Self::SmartPtr( newObject );
00093   }
00094   
00096   const T* Ptr() const
00097   {
00098     return static_cast<const T*>( this->m_PointerDevice );
00099   }
00100 
00102   T* Ptr()
00103   {
00104     return static_cast<T*>( this->m_PointerDevice );
00105   }
00106 
00108   void CopyToDevice( const T *const srcPtrHost, const size_t count )
00109   {
00110     this->Superclass::CopyToDevice( srcPtrHost, count * sizeof( T ) );
00111   }
00112   
00114   void CopyToHost( void *const dstPtrHost, const size_t count ) const
00115   {
00116     this->Superclass::CopyToHost( dstPtrHost, count * sizeof( T ) );
00117   }
00118   
00120   void CopyOnDevice( const Self& srcPtrDevice, const size_t count )
00121   {
00122     this->Superclass::CopyOnDevice( srcPtrDevice, count * sizeof( T ) );
00123   }
00124 
00126   void SetToZero()
00127   {
00128     this->Superclass::Memset( 0, this->m_NumberOfItems * sizeof( T ) );
00129   }
00130 
00132   size_t GetNumberOfItems() const
00133   {
00134     return this->m_NumberOfItems;
00135   }
00136 
00137 private:
00139   size_t m_NumberOfItems;
00140 };
00141 
00143 
00144 } // namespace cmtk
00145 
00146 #endif // #ifndef __cmtkDeviceMemory_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines