From 36bb97c617e5f143a6c567381a537ed096e79064 Mon Sep 17 00:00:00 2001 From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz> Date: Fri, 29 Apr 2016 09:47:30 +0200 Subject: [PATCH] Adding tnlDeviceObject. --- src/core/tnlCuda.cu | 9 ++ src/core/tnlCuda.h | 4 +- src/core/tnlDeviceObject.h | 137 +++++++++++++++++++++++++++ src/core/tnlDeviceObjectBase.h | 27 ++++++ src/core/tnlDeviceObjectsContainer.h | 57 +++++++++++ 5 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 src/core/tnlDeviceObject.h create mode 100644 src/core/tnlDeviceObjectBase.h create mode 100644 src/core/tnlDeviceObjectsContainer.h diff --git a/src/core/tnlCuda.cu b/src/core/tnlCuda.cu index 113cba3522..158e29f4fa 100644 --- a/src/core/tnlCuda.cu +++ b/src/core/tnlCuda.cu @@ -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(); diff --git a/src/core/tnlCuda.h b/src/core/tnlCuda.h index 3d5e6eb6eb..4de1a2d8ac 100644 --- a/src/core/tnlCuda.h +++ b/src/core/tnlCuda.h @@ -42,7 +42,7 @@ class tnlCuda static tnlString getDeviceType(); - __cuda_callable__ static inline tnlDeviceEnum getDevice(); + __cuda_callable__ static inline tnlDeviceEnum getDevice(); __cuda_callable__ static inline int getMaxGridSize(); @@ -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 diff --git a/src/core/tnlDeviceObject.h b/src/core/tnlDeviceObject.h new file mode 100644 index 0000000000..68b050f43c --- /dev/null +++ b/src/core/tnlDeviceObject.h @@ -0,0 +1,137 @@ +/*************************************************************************** + * * + * 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; +}; + diff --git a/src/core/tnlDeviceObjectBase.h b/src/core/tnlDeviceObjectBase.h new file mode 100644 index 0000000000..468bfaac83 --- /dev/null +++ b/src/core/tnlDeviceObjectBase.h @@ -0,0 +1,27 @@ +/*************************************************************************** + * * + * 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; +}; + + diff --git a/src/core/tnlDeviceObjectsContainer.h b/src/core/tnlDeviceObjectsContainer.h new file mode 100644 index 0000000000..4d4defb51b --- /dev/null +++ b/src/core/tnlDeviceObjectsContainer.h @@ -0,0 +1,57 @@ +/*************************************************************************** + * * + * 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; +}; + -- GitLab