Openholo  v5.0
Open Source Digital Holographic Library
cudaWrapper.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef CUDAWRAPPER_H
3 #define CUDAWRAPPER_H
4 #include "ophGen.h"
5 #include <cuda_runtime_api.h>
6 #include <atomic>
7 #include <mutex>
8 
9 #define MAX_GPU 16
10 #define MAX_INFO 14
11 #define MAX_LENGTH 256
12 
13 
15 {
16 public:
17  enum DEVICE_INFO : int {
18  DEVICE_NAME = 0,
19  GLOBAL_MEMORY = 1,
20  CONSTANT_MEMORY = 2,
21  MANAGED_MEMORY = 3,
22  MP_COUNT = 4,
23  TOTAL_MP_COUNT = 5,
24  MAX_THREADS_PER_MP = 6,
25  WARP_SIZE = 7,
26  BLOCK_PER_MP = 8,
27  SHARED_MEMORY_PER_MP = 9,
28  SHARED_MEMORY_PER_BLOCK = 10,
29  MAX_THREADS_PER_BLOCK = 11,
30  MAX_THREADS_DIMENSION = 12,
31  MAX_GRID_SIZE = 13
32  };
33 
34 private:
35  cudaWrapper();
36  ~cudaWrapper();
37  static cudaWrapper *instance;
38  static std::mutex mtx;
39 
40  cudaDeviceProp devProps[MAX_GPU];
41  char devInfos[MAX_GPU][MAX_INFO][MAX_LENGTH];
42  int num_gpu;
43  int m_nThread;
44  int cur_gpu;
45  int work_load[MAX_GPU];
46  int cuda_cores[MAX_GPU];
47  int active_gpus;
48 
49 public:
51  if (instance == nullptr) {
52  std::lock_guard<std::mutex> lock(mtx);
53  if (instance == nullptr) {
54  instance = new cudaWrapper();
55  atexit(releaseInstance);
56  }
57  }
58  return instance;
59  }
60 
61  static void releaseInstance() {
62  if (instance != nullptr) {
63  delete instance;
64  instance = nullptr;
65  }
66  }
67  cudaDeviceProp* getDeviceProps(int idx) { return &devProps[idx]; }
68  void setCurThreads(int thread) { m_nThread = thread; }
69  int getCurThreads() { return m_nThread; }
70  int getMaxThreads(int idx) { return devProps[idx].maxThreadsPerBlock; }
71  int getWarpSize(int idx) { return devProps[idx].warpSize; }
72  void printMemoryInfo(int idx);
73  int getCurrentGPU() { return cur_gpu; }
74  int getNumGPU() { return num_gpu; }
75 
76  void setWorkload(int size);
77  int getWorkload(int idx) { return work_load[idx]; }
78 
79  int getActiveGPUs() { return active_gpus; }
80  bool setActiveGPUs(int gpus) {
81  if (gpus <= MAX_GPU && gpus <= num_gpu) {
82  active_gpus = gpus;
83  return true;
84  }
85  else
86  return false;
87  }
88 
89 
90 private:
91  void initGPU();
92  bool printDevInfo();
93  int getSMPerCore(int major, int minor);
94 
95 public:
96 
97 private:
98 };
99 
100 #endif // cudaWrapperWRAPPER_H
int getActiveGPUs()
Definition: cudaWrapper.h:79
cudaDeviceProp * getDeviceProps(int idx)
Definition: cudaWrapper.h:67
static cudaWrapper * getInstance()
Definition: cudaWrapper.h:50
int getNumGPU()
Definition: cudaWrapper.h:74
int getCurrentGPU()
Definition: cudaWrapper.h:73
int getMaxThreads(int idx)
Definition: cudaWrapper.h:70
static void releaseInstance()
Definition: cudaWrapper.h:61
#define GEN_DLL
Definition: ophGen.h:61
bool setActiveGPUs(int gpus)
Definition: cudaWrapper.h:80
int getCurThreads()
Definition: cudaWrapper.h:69
#define MAX_INFO
Definition: cudaWrapper.h:10
void setCurThreads(int thread)
Definition: cudaWrapper.h:68
#define MAX_GPU
Definition: cudaWrapper.h:9
int getWorkload(int idx)
Definition: cudaWrapper.h:77
#define MAX_LENGTH
Definition: cudaWrapper.h:11
int getWarpSize(int idx)
Definition: cudaWrapper.h:71