PSkel
|
00001 //------------------------------------------------------------------------------- 00002 // Copyright (c) 2015, ICEI - PUC Minas 00003 // All rights reserved. 00004 // 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are met: 00007 // 00008 // 1. Redistributions of source code must retain the above copyright notice, this 00009 // list of conditions and the following disclaimer. 00010 // 00011 // 2. Redistributions in binary form must reproduce the above copyright notice, 00012 // this list of conditions and the following disclaimer in the documentation 00013 // and/or other materials provided with the distribution. 00014 // 00015 // 3. Neither the name of the copyright holder nor the names of its contributors 00016 // may be used to endorse or promote products derived from this software without 00017 // specific prior written permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00020 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00021 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00023 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00024 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00025 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00026 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00027 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 //------------------------------------------------------------------------------- 00030 00035 #ifndef PSKEL_ARRAY_H 00036 #define PSKEL_ARRAY_H 00037 00038 #include <cuda.h> 00039 #include "PSkelDefs.h" 00040 00041 namespace PSkel{ 00042 00049 template<typename T> 00050 class ArrayBase{ 00051 private: 00052 //variables that hold the real boundaries (total allocated data.) 00053 size_t realWidth, realHeight, realDepth; 00054 //variables that hold the "virtual" array boundaries (important for sliced arrays.) 00055 size_t width, height,depth; 00056 //offsets for the sliced array. 00057 size_t widthOffset, heightOffset, depthOffset; 00058 //host and device (GPU memory) pointers 00059 T *hostArray, *deviceArray; 00060 protected: 00069 __device__ __forceinline__ T & deviceGet(size_t h,size_t w,size_t d) const ; 00070 00079 __host__ __forceinline__ T & hostGet(size_t h,size_t w,size_t d) const ; 00080 00088 ArrayBase(size_t width, size_t height, size_t depth); 00089 public: 00093 void deviceAlloc(); 00094 00098 void deviceFree(); 00099 00100 void hostAlloc(size_t width, size_t height, size_t depth); 00101 00105 void hostAlloc(); 00106 00110 void hostFree(); 00111 00116 __device__ __host__ size_t getWidth() const; 00117 00122 __device__ __host__ size_t getHeight() const; 00123 00128 __device__ __host__ size_t getDepth() const; 00129 00134 __device__ __host__ size_t memSize() const; 00135 00140 __device__ __host__ size_t size() const; 00141 00146 __device__ __host__ size_t realSize() const; 00147 00159 template<typename Arrays> 00160 void hostSlice(Arrays array, size_t widthOffset, size_t heightOffset, size_t depthOffset, size_t width, size_t height, size_t depth); 00161 00167 template<typename Arrays> 00168 void hostClone(Arrays array); 00169 00174 template<typename Arrays> 00175 void hostMemCopy(Arrays array); 00176 00182 void copyToDevice(); 00183 00189 template<typename Arrays> 00190 void copyFromDevice(Arrays array); 00191 00197 void copyToHost(); 00198 00205 __device__ __host__ operator bool() const ; 00206 }; 00207 00208 //******************************************************************************************* 00209 // Array 3D 00210 //******************************************************************************************* 00211 00212 template <typename T> 00213 class Array3D: public ArrayBase<T>{ 00214 public: 00218 Array3D(); 00219 00220 /* 00221 //TODO O kernel cuda não aceita structs com destrutores. Corrigir nas próximas versões 00222 ~Array3D(){ 00223 free(hostArray); 00224 cudaFree(deviceArray); 00225 }*/ 00226 00234 Array3D(size_t width, size_t height, size_t depth); 00235 00244 __attribute__((always_inline)) __forceinline__ __device__ __host__ T & operator()(size_t h,size_t w,size_t d) const ; 00245 00246 }; 00247 00248 //******************************************************************************************* 00249 // Array 2D 00250 //******************************************************************************************* 00251 00252 template<typename T> 00253 class Array2D: public ArrayBase<T>{ 00254 public: 00258 Array2D(); 00259 00266 Array2D(size_t width, size_t height); 00267 00275 __attribute__((always_inline)) __forceinline__ __device__ __host__ T & operator()(size_t h, size_t w) const ; 00276 00277 }; 00278 00279 //******************************************************************************************* 00280 // Array 1D 00281 //******************************************************************************************* 00282 00283 template<typename T> 00284 class Array: public ArrayBase<T>{ 00285 public: 00289 Array(); 00290 00296 Array(size_t size); 00297 00304 __attribute__((always_inline)) __forceinline__ __device__ __host__ T & operator()(size_t w) const; 00305 }; 00306 00307 }//end namespace 00308 00309 #include "PSkelArray.hpp" 00310 00311 #endif