Xmipp  v3.23.11-Nereus
gpu.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: David Strelak (davidstrelak@gmail.com)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #ifndef LIBRARIES_RECONSTRUCTION_CUDA_GPU_H_
27 #define LIBRARIES_RECONSTRUCTION_CUDA_GPU_H_
28 
29 #include <assert.h>
30 #include "data/hw.h"
31 #include "core/xmipp_error.h"
32 
36 class GPU : public HW {
37 public:
38  explicit GPU(int device = 0, int stream = 0):
39  HW(1),
40  m_device(device),
41  m_streamId(stream), m_stream(nullptr),
42  m_isSet(false) {};
43 
44  ~GPU();
45 
46  inline int device() const {
47  return m_device;
48  }
49 
50  inline void* stream() const {
51  check();
52  return m_stream;
53  }
54 
55  inline int streamId() const {
56  return m_streamId;
57  }
58 
59  inline size_t lastFreeBytes() const {
60  check();
61  return HW::lastFreeBytes();
62  }
63 
64  inline size_t totalBytes() const {
65  check();
66  return HW::totalBytes();
67  }
68 
69  inline size_t lastUsedBytes() const {
70  check();
71  return HW::lastUsedBytes();
72  }
73 
74  void updateMemoryInfo();
75 
76  void peekLastError() const;
77 
78  static void pinMemory(const void *h_mem, size_t bytes, unsigned int flags=0); // must not be nullptr
79 
80  static void unpinMemory(const void *h_mem); // must not be nullptr
81 
82  static bool isMemoryPinned(const void *h_mem);
83 
84  void set();
85 
86  void synchAll() const;
87 
88  void synch() const;
89 
90  inline std::string getUUID() const {
91  check();
92  return HW::getUUID();
93  }
94 
95  inline bool isSet() const {
96  return m_isSet;
97  }
98 
99  // FIXME DS do not use, it's for backward compatibility only
100  static void setDevice(int device);
101 
102  static int getDeviceCount();
103 
104  void lockMemory(const void *h_mem, size_t bytes) override {
105  GPU::pinMemory(h_mem, bytes, 0);
106  }
107 
108  void unlockMemory(const void *h_mem) override {
109  GPU::unpinMemory(h_mem);
110  }
111 
112  bool isMemoryLocked(const void *h_mem) override {
113  return GPU::isMemoryPinned(h_mem);
114  }
115 
116  bool isGpuPointer(const void *);
117 
118  int getCudaVersion();
119 
120 private:
121  int m_device;
122  int m_streamId;
123  void* m_stream;
124  bool m_isSet;
125 
126  inline void check() const {
127  if ( ! m_isSet) {
128  REPORT_ERROR(ERR_LOGIC_ERROR, "You have to set() this GPU before using it");
129  }
130  }
131 
132  void obtainUUID();
133 };
135 #endif /* LIBRARIES_RECONSTRUCTION_CUDA_GPU_H_ */
void * stream() const
Definition: gpu.h:50
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void unlockMemory(const void *h_mem) override
Definition: gpu.h:108
static void setDevice(int device)
Definition: gpu.cpp:135
virtual size_t lastUsedBytes() const
Definition: hw.h:65
~GPU()
Definition: gpu.cpp:32
GPU(int device=0, int stream=0)
Definition: gpu.h:38
Definition: hw.h:35
static bool isMemoryPinned(const void *h_mem)
Definition: gpu.cpp:140
bool isSet() const
Definition: gpu.h:95
size_t totalBytes() const
Definition: gpu.h:64
virtual std::string getUUID() const
Definition: hw.h:69
static void pinMemory(const void *h_mem, size_t bytes, unsigned int flags=0)
Definition: gpu.cpp:92
int streamId() const
Definition: gpu.h:55
std::string getUUID() const
Definition: gpu.h:90
int device() const
Definition: gpu.h:46
virtual size_t lastFreeBytes() const
Definition: hw.h:57
void lockMemory(const void *h_mem, size_t bytes) override
Definition: gpu.h:104
void updateMemoryInfo()
Definition: gpu.cpp:82
void synch() const
Definition: gpu.cpp:129
virtual size_t totalBytes() const
Definition: hw.h:61
int getCudaVersion()
Definition: gpu.cpp:44
size_t lastUsedBytes() const
Definition: gpu.h:69
void peekLastError() const
Definition: gpu.cpp:87
static int getDeviceCount()
Definition: gpu.cpp:118
Definition: gpu.h:36
void synchAll() const
Definition: gpu.cpp:124
static void unpinMemory(const void *h_mem)
Definition: gpu.cpp:108
size_t lastFreeBytes() const
Definition: gpu.h:59
bool isMemoryLocked(const void *h_mem) override
Definition: gpu.h:112
bool isGpuPointer(const void *)
Definition: gpu.cpp:153
Some logical error in the pipeline.
Definition: xmipp_error.h:147