OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryEngine.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include "CudaMgr/CudaMgr.h"
8 #include "Shared/LruCache.h"
9 
10 inline bool g_query_engine_cuda_streams{false};
11 inline size_t g_code_cache_max_num_items{1000};
12 inline size_t g_gpu_code_cache_max_size_in_bytes{size_t(1) << 27}; // 128MB
13 
14 class QueryEngine {
15  public:
16  QueryEngine(CudaMgr_Namespace::CudaMgr* cuda_mgr, bool cpu_only)
17  : cuda_mgr_(cuda_mgr)
21  "s_stubs_cache"))
25  "s_code_cache"))
29  "cpu_code_cache"))
33  "gpu_code_cache"))
37  "tf_code_cache")) {
38  if (cpu_only) {
40  }
41 #ifdef HAVE_CUDA
43  // See:
44  // https://docs.nvidia.com/cuda/cuda-runtime-api/stream-sync-behavior.html
45  LOG(INFO) << "Query Engine CUDA streams enabled";
46  int original_device_context = cuda_mgr_->getContext();
47  CUstream s;
48  for (int device_num = 0; device_num < cuda_mgr_->getDeviceCount(); ++device_num) {
49  cuda_mgr_->setContext(device_num);
50  checkCudaErrors(cuStreamCreate(&s, /*CU_STREAM_DEFAULT*/ CU_STREAM_NON_BLOCKING));
51  cuda_streams_.push_back(s);
52  }
53  cuda_mgr_->setContext(original_device_context);
54  } else {
55  LOG(INFO) << "Query Engine CUDA streams disabled";
56  }
57 #endif // HAVE_CUDA
58  }
59 
61 #ifdef HAVE_CUDA
63  for (auto& c : cuda_streams_) {
64  checkCudaErrors(cuStreamDestroy(c));
65  }
66  }
67 #endif // HAVE_CUDA
68  }
69 
70  CUstream getCudaStream() { // NOTE: CUstream is cudaStream_t
72  int device_num = cuda_mgr_->getContext();
73  return getCudaStreamForDevice(device_num);
74  } else {
75  return 0;
76  }
77  }
78 
79  CUstream getCudaStreamForDevice(int device_num) { // NOTE: CUstream is cudaStream_t
81  CHECK_GE(device_num, 0);
82  CHECK_LT((size_t)device_num, cuda_streams_.size());
83  return cuda_streams_[device_num];
84  } else {
85  return 0;
86  }
87  }
88 
89  static std::shared_ptr<QueryEngine> getInstance() {
90  if (auto s = instance_.lock()) {
91  return s;
92  } else {
93  throw std::runtime_error("QueryEngine instance hasn't been created");
94  }
95  }
96 
97  static std::shared_ptr<QueryEngine> createInstance(CudaMgr_Namespace::CudaMgr* cuda_mgr,
98  bool cpu_only) {
100  if (auto s = instance_.lock()) {
101  return s;
102  } else {
103  s = std::make_shared<QueryEngine>(cuda_mgr, cpu_only);
104  instance_ = s;
105  return s;
106  }
107  }
108 
109  private:
111  std::vector<CUstream> cuda_streams_;
112 
113  inline static std::mutex mutex_; // TODO(sy): use atomics instead?
114  inline static std::weak_ptr<QueryEngine> instance_;
115 
116  public:
117  std::unique_ptr<CodeCacheAccessor<CpuCompilationContext>> s_stubs_accessor;
118  std::unique_ptr<CodeCacheAccessor<CpuCompilationContext>> s_code_accessor;
119  std::unique_ptr<CodeCacheAccessor<CpuCompilationContext>> cpu_code_accessor;
120  std::unique_ptr<CodeCacheAccessor<GpuCompilationContext>> gpu_code_accessor;
121  std::unique_ptr<CodeCacheAccessor<CompilationContext>> tf_code_accessor;
122 }; // class QueryEngine
123 
124 CUstream getQueryEngineCudaStream(); // NOTE: CUstream is cudaStream_t
126  int device_num); // NOTE: CUstream is cudaStream_t
std::unique_ptr< CodeCacheAccessor< CpuCompilationContext > > cpu_code_accessor
Definition: QueryEngine.h:119
CUstream getCudaStream()
Definition: QueryEngine.h:70
size_t g_gpu_code_cache_max_size_in_bytes
Definition: QueryEngine.h:12
void * CUstream
Definition: nocuda.h:23
#define LOG(tag)
Definition: Logger.h:285
void checkCudaErrors(CUresult err)
Definition: sample.cpp:38
CUstream getCudaStreamForDevice(int device_num)
Definition: QueryEngine.h:79
CudaMgr_Namespace::CudaMgr * cuda_mgr_
Definition: QueryEngine.h:110
std::unique_ptr< CodeCacheAccessor< CompilationContext > > tf_code_accessor
Definition: QueryEngine.h:121
void setContext(const int device_num) const
Definition: CudaMgr.cpp:511
#define CHECK_GE(x, y)
Definition: Logger.h:306
static std::weak_ptr< QueryEngine > instance_
Definition: QueryEngine.h:114
int getDeviceCount() const
Definition: CudaMgr.h:90
CUstream getQueryEngineCudaStream()
Definition: QueryEngine.cpp:3
static std::shared_ptr< QueryEngine > createInstance(CudaMgr_Namespace::CudaMgr *cuda_mgr, bool cpu_only)
Definition: QueryEngine.h:97
std::unique_lock< T > unique_lock
size_t g_code_cache_max_num_items
Definition: QueryEngine.h:11
std::vector< CUstream > cuda_streams_
Definition: QueryEngine.h:111
std::unique_ptr< CodeCacheAccessor< CpuCompilationContext > > s_stubs_accessor
Definition: QueryEngine.h:117
#define CHECK_LT(x, y)
Definition: Logger.h:303
QueryEngine(CudaMgr_Namespace::CudaMgr *cuda_mgr, bool cpu_only)
Definition: QueryEngine.h:16
CUstream getQueryEngineCudaStreamForDevice(int device_num)
Definition: QueryEngine.cpp:7
static std::shared_ptr< QueryEngine > getInstance()
Definition: QueryEngine.h:89
EvictionMetricType
Definition: LruCache.h:22
static std::mutex mutex_
Definition: QueryEngine.h:113
bool g_query_engine_cuda_streams
Definition: QueryEngine.h:10
std::unique_ptr< CodeCacheAccessor< GpuCompilationContext > > gpu_code_accessor
Definition: QueryEngine.h:120
std::unique_ptr< CodeCacheAccessor< CpuCompilationContext > > s_code_accessor
Definition: QueryEngine.h:118