MorphoGraphX  2.0-1-227
Cuda.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2016 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef CUDA_HPP
12 #define CUDA_HPP
13 
14 #include <Config.hpp>
15 
16 #include <thrust/version.h>
17 #include <thrust/host_vector.h>
18 #include <thrust/device_vector.h>
19 #include <thrust/device_ptr.h>
20 
21 #define COMPILE_CUDA // RSS Cant' we make this depend on THRUST_CUDA_BACKEND?
22 #ifdef THRUST_BACKEND_CUDA
23  #include <cuda_runtime.h>
24 #endif
25 
26 #include <CudaGlobal.hpp>
27 #include <Geometry.hpp>
28 #include <ThrustTypes.hpp>
29 
30 #ifdef WIN32
31 # include <windows.h>
32 #else
33 # include <unistd.h>
34 #endif
35 
36 namespace mgx
37 {
38  // Progress bar
39  // #if ((defined(WIN32) || defined(WIN64)) && defined(MINGW))
40  #if (defined(WIN32) || defined(WIN64))
41  // Define fake functions for Windows
42  inline bool progressAdvance() { return true; }
43  inline bool progressAdvance(int step) { return true; }
44  inline void progressStart(const std::string &msg, int steps) {}
45  inline void progressSetMsg(const std::string & msg) {}
46  inline bool progressCancelled() { return false; }
47  inline void progStop() {}
48  #else
49  extern bool progressAdvance();
50  extern bool progressAdvance(int step);
51  extern void progressStart(const std::string &msg, int steps);
52  extern void progressSetMsg(const std::string &msg);
53  extern bool progressCancelled();
54  extern void progressStop();
55  #endif
56 
57  typedef thrust::device_ptr<int> DevicePi;
58  typedef thrust::device_ptr<uint> DevicePui;
59  typedef thrust::device_ptr<double> DevicePd;
60  typedef thrust::device_ptr<Point3d> DeviceP3d;
61  typedef thrust::device_ptr<Matrix3d> DevicePM3d;
62 
63  // This requires changing after thrust is updated everywhere
64  #if THRUST_VERSION >= 100600
65  typedef thrust::counting_iterator<int, thrust::device_system_tag> DCountIter;
66  #else
67  typedef thrust::counting_iterator<int, thrust::device_space_tag> DCountIter;
68  #endif
69 
70  // Dimensions
71  enum DimEnum { X, Y, Z, XY, YZ, XZ, XYZ };
72 
73  // Check for Cuda errors
74  extern "C" cuda_EXPORT int checkCudaError(const std::string& msg);
75  cuda_EXPORT uint getThreadCount(uint threadPos, uint totThreads);
76  cuda_EXPORT int errMsg(const std::string& s);
77  cuda_EXPORT size_t userMem();
78  #define MByte size_t(1024 * 1024)
79 
80  // Get a pointer to first element from a device vector
81  template <typename T> T* devP(thrust::device_vector<T> &DVec) { return (&DVec[0]).get(); }
82  template <typename T> T* devP(thrust::device_vector<T> *DVec) { return (&(*DVec)[0]).get(); }
83 
84  // Atomic add for doubles
85  #ifdef THRUST_BACKEND_CUDA
86  __device__
87  inline double atomicAddMGX(double* address, double val)
88  {
89  unsigned long long int* address_as_ull = (unsigned long long int*)address;
90  unsigned long long int old = *address_as_ull, assumed;
91  do {
92  assumed = old;
93  old = atomicCAS(address_as_ull, assumed, __double_as_longlong(val + __longlong_as_double(assumed)));
94  } while (assumed != old);
95  return __longlong_as_double(old);
96  }
97  #else
98  inline double atomicAddMGX(double* address, double val)
99  {
100  #pragma omp atomic
101  *address += val;
102  return *address;
103  }
104  #endif
105 
106  // Class to lock memory
107  extern void freeMem();
108  extern void holdMem();
109  extern size_t userMem();
110  extern size_t memLeft(size_t memrq, size_t roomrq, size_t mem);
111  extern int errMsg(const std::string &s);
112 
113  struct HoldMem
114  {
115  HoldMem() { freeMem(); }
116  ~HoldMem() { holdMem(); }
117  };
118 }
119 #endif
mgx::holdMem
void holdMem()
mgx::Y
@ Y
Definition: Cuda.hpp:71
mgx::YZ
@ YZ
Definition: Cuda.hpp:71
mgx::HoldMem
Definition: Cuda.hpp:113
mgx::DevicePi
thrust::device_ptr< int > DevicePi
Definition: Cuda.hpp:57
mgx::HoldMem::~HoldMem
~HoldMem()
Definition: Cuda.hpp:116
mgx::getThreadCount
cuda_EXPORT uint getThreadCount(uint threadPos, uint totThreads)
mgx::freeMem
void freeMem()
mgx::progressSetMsg
void progressSetMsg(const std::string &msg)
uint
unsigned int uint
Definition: MorphoGraphX.hpp:14
mgx::errMsg
cuda_EXPORT int errMsg(const std::string &s)
mgx::XY
@ XY
Definition: Cuda.hpp:71
ThrustTypes.hpp
Geometry.hpp
mgx::DevicePui
thrust::device_ptr< uint > DevicePui
Definition: Cuda.hpp:58
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::atomicAddMGX
double atomicAddMGX(double *address, double val)
Definition: Cuda.hpp:98
mgx::progressStart
void progressStart(const std::string &msg, int steps)
mgx::devP
T * devP(thrust::device_vector< T > &DVec)
Definition: Cuda.hpp:81
mgx::Z
@ Z
Definition: Cuda.hpp:71
mgx::memLeft
size_t memLeft(size_t memrq, size_t roomrq, size_t mem)
mgx::XZ
@ XZ
Definition: Cuda.hpp:71
CudaGlobal.hpp
mgx::XYZ
@ XYZ
Definition: Cuda.hpp:71
mgx::X
@ X
Definition: Cuda.hpp:71
mgx::progressAdvance
bool progressAdvance()
mgx::progressCancelled
bool progressCancelled()
mgx::progressStop
void progressStop()
mgx::userMem
cuda_EXPORT size_t userMem()
mgx::DevicePd
thrust::device_ptr< double > DevicePd
Definition: Cuda.hpp:59
cuda_EXPORT
#define cuda_EXPORT
Definition: CudaGlobal.hpp:40
mgx::DevicePM3d
thrust::device_ptr< Matrix3d > DevicePM3d
Definition: Cuda.hpp:61
mgx::DCountIter
thrust::counting_iterator< int, thrust::device_space_tag > DCountIter
Definition: Cuda.hpp:67
mgx::HoldMem::HoldMem
HoldMem()
Definition: Cuda.hpp:115
mgx::DimEnum
DimEnum
Definition: Cuda.hpp:71
mgx::DeviceP3d
thrust::device_ptr< Point3d > DeviceP3d
Definition: Cuda.hpp:60
mgx::checkCudaError
cuda_EXPORT int checkCudaError(const std::string &msg)