GCVL
Multi-platform (Windows, Unix and Mac OS) C++/OpenCL/CUDA library with several Computer Vision algorithms.
src/gcvl/cuda/cudautils.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * GPGPU Computer Vision Library (GCVL)
00004  *
00005  * Copyright (c) Luis Omar Alvarez Mures 2015 <omar.alvarez@udc.es>
00006  * Copyright (c) Emilio Padron Gonzalez 2015 <emilioj@gmail.com>
00007  *
00008  * All rights reserved.
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 3 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public
00021  * License along with this library.
00022  *
00023  */
00024 
00025 #pragma once
00026 
00027 #include <cuda_runtime.h>
00028 #include <string>
00029 #include <iostream>
00030 #include <vector>
00031 
00032 #define CUDA_Test_Success(err, fct_name)                            \
00033 if ((err) != cudaSuccess)                                           \
00034 {                                                                   \
00035     std::cout                                                        \
00036         << "ERROR calling " << fct_name << "() ("                   \
00037         << __FILE__ << " line " << __LINE__ << "): "                \
00038         << CUDA_Error_to_String(err) << "\n" << std::flush;         \
00039     abort();                                                        \
00040 }
00041 
00042 #ifdef __DRIVER_TYPES_H__
00043 static const std::string CUDA_Error_to_String(cudaError_t error);
00044 #endif
00045 #ifdef __cuda_cuda_h__
00046 static const std::string CUDA_Error_to_String(CUresult error);
00047 #endif
00048 
00049 // Defines for GPU Architecture types (using the SM version to determine the # of cores per SM
00050 typedef struct
00051 {
00052     int SM; // 0xMm (hexidecimal notation), M = SM Major version, and m = SM minor version
00053     int cores;
00054 } sSMtoCores;
00055 
00056 const static sSMtoCores nGpuArchCoresPerSM[] =
00057 {
00058     { 0x20, 32 }, // Fermi Generation (SM 2.0) GF100 class
00059     { 0x21, 48 }, // Fermi Generation (SM 2.1) GF10x class
00060     { 0x30, 192}, // Kepler Generation (SM 3.0) GK10x class
00061     { 0x32, 192}, // Kepler Generation (SM 3.2) GK10x class
00062     { 0x35, 192}, // Kepler Generation (SM 3.5) GK11x class
00063     { 0x37, 192}, // Kepler Generation (SM 3.7) GK21x class
00064     { 0x50, 128}, // Maxwell Generation (SM 5.0) GM10x class
00065     { 0x52, 128}, // Maxwell Generation (SM 5.2) GM20x class
00066     {   -1, -1 }
00067 };
00068 
00070 
00075 inline int SMVer2CU(int major, int minor) {
00076 
00077     int index = 0;
00078 
00079     while (nGpuArchCoresPerSM[index].SM != -1) {
00080 
00081         if (nGpuArchCoresPerSM[index].SM == ((major << 4) + minor)) {
00082             return nGpuArchCoresPerSM[index].cores;
00083         }
00084 
00085         index++;
00086 
00087     }
00088 
00089     // If we don't find the values, we default to using the previous one to run properly
00090     printf("MapSMtoCores for SM %d.%d is undefined.  Default to use %d Cores/SM\n", major, minor, nGpuArchCoresPerSM[index-1].cores);
00091     return nGpuArchCoresPerSM[index-1].cores;
00092 
00093 }
00094 
00095 class CUDA_device;
00096 class CUDA_devices_list;
00097 
00098 class CUDA_device
00099 {
00100 private:
00101     int id;
00102     cudaDeviceProp properties;
00103     bool compute;
00104     int sm_per_multiproc;
00105     int compute_units;
00106     unsigned long long compute_perf;
00107     cudaError_t err;
00108 
00109 public:
00110     CUDA_device(int id = -1);
00111     //~CUDA_device();
00112     bool operator<(const CUDA_device &b) const;
00113     void Set_Information(unsigned long long _compute_perf, int arch);
00114     void Print() const;
00116 
00119     inline std::string Get_Name() const { return properties.name; }
00121 
00124     inline int Get_ID() const { return id; }
00125 };
00126 
00127 class CUDA_devices_list
00128 {
00129 private:
00130     int count;
00131     std::vector<CUDA_device> device_list;
00132     bool is_initialized;
00133     int preferred_device;
00134     cudaError_t err;
00135 public:
00136     CUDA_devices_list();
00137     //~CUDA_devices_list();
00138     void Initialize();
00140 
00143     inline void Set_Preferred_CUDA(const int _preferred_device = -1) { preferred_device = _preferred_device; }
00145 
00148     inline int Preferred_CUDA() { return preferred_device; }
00149     void Print();
00150 };
00151 
00152 template <class T>
00153 class CUDA_Array
00154 {
00155 private:
00156     int N;                              // Number of elements in array
00157     size_t sizeof_element;              // Size of each array elements
00158     size_t new_array_size_bytes;      // Size (bytes) of new padded array
00159     T * host_array;                  // Pointer to start of host array
00160     T * device_array;               // Pointer to device array
00161     cudaError_t err;
00162 
00163 public:
00164     CUDA_Array();
00165     void Initialize(int _N, T *&host_array);
00166     void Release_Memory();
00167     void Host_to_Device();
00168     void Device_to_Host();
00169     //void Validate_Data();
00170 
00172 
00175     inline T * Get_Device_Array() { return device_array; }
00177 
00180     inline T * Get_Host_Pointer() { return  host_array; }
00181 
00182 };
 All Classes Namespaces Files Functions Variables Defines