Xmipp  v3.23.11-Nereus
Public Member Functions | Static Public Member Functions | List of all members
GPU Class Reference

#include <gpu.h>

Inheritance diagram for GPU:
Inheritance graph
[legend]
Collaboration diagram for GPU:
Collaboration graph
[legend]

Public Member Functions

 GPU (int device=0, int stream=0)
 
 ~GPU ()
 
int device () const
 
void * stream () const
 
int streamId () const
 
size_t lastFreeBytes () const
 
size_t totalBytes () const
 
size_t lastUsedBytes () const
 
void updateMemoryInfo ()
 
void peekLastError () const
 
void set ()
 
void synchAll () const
 
void synch () const
 
std::string getUUID () const
 
bool isSet () const
 
void lockMemory (const void *h_mem, size_t bytes) override
 
void unlockMemory (const void *h_mem) override
 
bool isMemoryLocked (const void *h_mem) override
 
bool isGpuPointer (const void *)
 
int getCudaVersion ()
 
- Public Member Functions inherited from HW
 HW (unsigned parallelUnits)
 
virtual ~HW ()
 
unsigned noOfParallUnits () const
 

Static Public Member Functions

static void pinMemory (const void *h_mem, size_t bytes, unsigned int flags=0)
 
static void unpinMemory (const void *h_mem)
 
static bool isMemoryPinned (const void *h_mem)
 
static void setDevice (int device)
 
static int getDeviceCount ()
 

Additional Inherited Members

- Protected Attributes inherited from HW
unsigned m_parallUnits
 
size_t m_totalBytes
 
size_t m_lastFreeBytes
 
std::string m_uuid
 

Detailed Description

Definition at line 36 of file gpu.h.

Constructor & Destructor Documentation

◆ GPU()

GPU::GPU ( int  device = 0,
int  stream = 0 
)
inlineexplicit

Definition at line 38 of file gpu.h.

38  :
39  HW(1),
40  m_device(device),
41  m_streamId(stream), m_stream(nullptr),
42  m_isSet(false) {};
void * stream() const
Definition: gpu.h:50
int device() const
Definition: gpu.h:46
HW(unsigned parallelUnits)
Definition: hw.h:37

◆ ~GPU()

GPU::~GPU ( )

Definition at line 32 of file gpu.cpp.

32  {
33  if (m_isSet) {
34  synch();
35  auto s = (cudaStream_t*)m_stream;
36  gpuErrchk(cudaStreamDestroy(*s));
37  delete (cudaStream_t*)m_stream;
38  m_stream = nullptr;
39  m_uuid = std::string();
40  }
41  m_isSet = false;
42 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
void synch() const
Definition: gpu.cpp:129
std::string m_uuid
Definition: hw.h:82

Member Function Documentation

◆ device()

int GPU::device ( ) const
inline

Definition at line 46 of file gpu.h.

46  {
47  return m_device;
48  }

◆ getCudaVersion()

int GPU::getCudaVersion ( )

Definition at line 44 of file gpu.cpp.

44  {
45  int version = 0;
46  gpuErrchk(cudaRuntimeGetVersion(&version));
47  return version;
48 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31

◆ getDeviceCount()

int GPU::getDeviceCount ( )
static

Definition at line 118 of file gpu.cpp.

118  {
119  int deviceCount = 0;
120  gpuErrchk(cudaGetDeviceCount(&deviceCount));
121  return deviceCount;
122 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31

◆ getUUID()

std::string GPU::getUUID ( ) const
inlinevirtual

Reimplemented from HW.

Definition at line 90 of file gpu.h.

90  {
91  check();
92  return HW::getUUID();
93  }
virtual std::string getUUID() const
Definition: hw.h:69
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ isGpuPointer()

bool GPU::isGpuPointer ( const void *  p)

Definition at line 153 of file gpu.cpp.

153  {
154  cudaPointerAttributes attr;
155  if (cudaPointerGetAttributes(&attr, p) == cudaErrorInvalidValue) {
156  cudaGetLastError(); // clear out the previous API error
157  return false;
158  }
159 #if defined(CUDART_VERSION) && CUDART_VERSION >= 10000
160  return (cudaMemoryTypeDevice == attr.type) || (cudaMemoryTypeManaged == attr.type);
161 #else
162  return cudaMemoryTypeDevice == attr.memoryType;
163 #endif
164 }

◆ isMemoryLocked()

bool GPU::isMemoryLocked ( const void *  h_mem)
inlineoverridevirtual

Implements HW.

Definition at line 112 of file gpu.h.

112  {
113  return GPU::isMemoryPinned(h_mem);
114  }
static bool isMemoryPinned(const void *h_mem)
Definition: gpu.cpp:140

◆ isMemoryPinned()

bool GPU::isMemoryPinned ( const void *  h_mem)
static

Definition at line 140 of file gpu.cpp.

140  {
141  cudaPointerAttributes attr;
142  if (cudaPointerGetAttributes(&attr, h_mem) != cudaSuccess) {
143  cudaGetLastError(); // clear out the previous API error
144  return false;
145  }
146  #if defined(CUDART_VERSION) && CUDART_VERSION >= 10000
147  return (cudaMemoryTypeHost == attr.type) || (cudaMemoryTypeManaged == attr.type);
148  #else
149  return cudaMemoryTypeHost == attr.memoryType;
150  #endif
151 }

◆ isSet()

bool GPU::isSet ( ) const
inline

Definition at line 95 of file gpu.h.

95  {
96  return m_isSet;
97  }

◆ lastFreeBytes()

size_t GPU::lastFreeBytes ( ) const
inlinevirtual

Reimplemented from HW.

Definition at line 59 of file gpu.h.

59  {
60  check();
61  return HW::lastFreeBytes();
62  }
virtual size_t lastFreeBytes() const
Definition: hw.h:57
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ lastUsedBytes()

size_t GPU::lastUsedBytes ( ) const
inlinevirtual

Reimplemented from HW.

Definition at line 69 of file gpu.h.

69  {
70  check();
71  return HW::lastUsedBytes();
72  }
virtual size_t lastUsedBytes() const
Definition: hw.h:65
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ lockMemory()

void GPU::lockMemory ( const void *  h_mem,
size_t  bytes 
)
inlineoverridevirtual

Implements HW.

Definition at line 104 of file gpu.h.

104  {
105  GPU::pinMemory(h_mem, bytes, 0);
106  }
static void pinMemory(const void *h_mem, size_t bytes, unsigned int flags=0)
Definition: gpu.cpp:92

◆ peekLastError()

void GPU::peekLastError ( ) const

Definition at line 87 of file gpu.cpp.

87  {
88  check();
89  gpuErrchk(cudaPeekAtLastError());
90 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ pinMemory()

void GPU::pinMemory ( const void *  h_mem,
size_t  bytes,
unsigned int  flags = 0 
)
static

Definition at line 92 of file gpu.cpp.

93  {
94  if (isMemoryPinned(h_mem)
95  && (isMemoryPinned((char*)h_mem + bytes - 1))) {
96  return;
97  }
98  assert(0 == cudaHostRegisterDefault); // default value should be 0
99  // check that it's aligned properly to the beginning of the page
100  if (0 != ((size_t)h_mem % 4096)) {
101  // otherwise the cuda-memcheck and cuda-gdb tends to randomly crash (confirmed on cuda 8 - cuda 10)
102  REPORT_ERROR(ERR_PARAM_INCORRECT, "Only pointer aligned to the page size can be registered");
103  }
104  // we remove const, but we don't change the data
105  gpuErrchk(cudaHostRegister(const_cast<void*>(h_mem), bytes, flags));
106 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
Parameter incorrect.
Definition: xmipp_error.h:181
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
static bool isMemoryPinned(const void *h_mem)
Definition: gpu.cpp:140

◆ set()

void GPU::set ( )
virtual

Reimplemented from HW.

Definition at line 50 of file gpu.cpp.

50  {
51  // set device (for current context / thread)
52  setDevice(m_device);
53  if ( ! m_isSet) {
54  // create stream
55  m_stream = new cudaStream_t;
56  gpuErrchk(cudaStreamCreate((cudaStream_t*)m_stream));
57  // remember the state
58  m_isSet = true;
59  // get additional info
60  HW::set();
61  }
62  peekLastError();
63 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
static void setDevice(int device)
Definition: gpu.cpp:135
virtual void set()
Definition: hw.h:50
void peekLastError() const
Definition: gpu.cpp:87

◆ setDevice()

void GPU::setDevice ( int  device)
static

Definition at line 135 of file gpu.cpp.

135  {
136  gpuErrchk(cudaSetDevice(device));
137  gpuErrchk(cudaPeekAtLastError());
138 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
int device() const
Definition: gpu.h:46

◆ stream()

void* GPU::stream ( ) const
inline

Definition at line 50 of file gpu.h.

50  {
51  check();
52  return m_stream;
53  }
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ streamId()

int GPU::streamId ( ) const
inline

Definition at line 55 of file gpu.h.

55  {
56  return m_streamId;
57  }

◆ synch()

void GPU::synch ( ) const
virtual

Implements HW.

Definition at line 129 of file gpu.cpp.

129  {
130  check();
131  auto stream = (cudaStream_t*)m_stream;
132  gpuErrchk(cudaStreamSynchronize(*stream));
133 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
void * stream() const
Definition: gpu.h:50
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ synchAll()

void GPU::synchAll ( ) const
virtual

Implements HW.

Definition at line 124 of file gpu.cpp.

124  {
125  check();
126  gpuErrchk(cudaDeviceSynchronize());
127 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ totalBytes()

size_t GPU::totalBytes ( ) const
inlinevirtual

Reimplemented from HW.

Definition at line 64 of file gpu.h.

64  {
65  check();
66  return HW::totalBytes();
67  }
virtual size_t totalBytes() const
Definition: hw.h:61
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

◆ unlockMemory()

void GPU::unlockMemory ( const void *  h_mem)
inlineoverridevirtual

Implements HW.

Definition at line 108 of file gpu.h.

108  {
109  GPU::unpinMemory(h_mem);
110  }
static void unpinMemory(const void *h_mem)
Definition: gpu.cpp:108

◆ unpinMemory()

void GPU::unpinMemory ( const void *  h_mem)
static

Definition at line 108 of file gpu.cpp.

108  {
109  // we remove const, but we don't change the data
110  auto err = cudaHostUnregister(const_cast<void*>(h_mem));
111  if (cudaErrorHostMemoryNotRegistered == err) {
112  cudaGetLastError(); // clear out the previous API error
113  } else {
114  gpuErrchk(err);
115  }
116 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31

◆ updateMemoryInfo()

void GPU::updateMemoryInfo ( )
virtual

Implements HW.

Definition at line 82 of file gpu.cpp.

82  {
83  check();
84  gpuErrchk(cudaMemGetInfo(&m_lastFreeBytes, &m_totalBytes));
85 }
#define gpuErrchk(code)
Definition: cuda_asserts.h:31
size_t m_totalBytes
Definition: hw.h:80
size_t m_lastFreeBytes
Definition: hw.h:81
check(nparam, nf, nfsr, &Linfty, nineq, nineqn, neq, neqn, ncsrl, ncsrn, mode, &modem, eps, bgbnd, param)

The documentation for this class was generated from the following files: