OmniSciDB  cde582ebc3
 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 
9 inline bool g_query_engine_cuda_streams{true};
10 
11 class QueryEngine {
12  public:
13  QueryEngine(CudaMgr_Namespace::CudaMgr* cuda_mgr, bool cpu_only)
14  : cuda_mgr_(cuda_mgr)
17  "s_stubs_cache"))
20  "s_code_cache"))
23  "cpu_code_cache"))
26  "gpu_code_cache"))
29  "tf_code_cache")) {
30  if (cpu_only) {
32  }
33 #ifdef HAVE_CUDA
35  // See:
36  // https://docs.nvidia.com/cuda/cuda-runtime-api/stream-sync-behavior.html
37  LOG(INFO) << "Query Engine CUDA streams enabled";
38  int original_device_context = cuda_mgr_->getContext();
39  CUstream s;
40  for (int device_num = 0; device_num < cuda_mgr_->getDeviceCount(); ++device_num) {
41  cuda_mgr_->setContext(device_num);
42  checkCudaErrors(cuStreamCreate(&s, /*CU_STREAM_DEFAULT*/ CU_STREAM_NON_BLOCKING));
43  cuda_streams_.push_back(s);
44  }
45  cuda_mgr_->setContext(original_device_context);
46  } else {
47  LOG(INFO) << "Query Engine CUDA streams disabled";
48  }
49 #endif // HAVE_CUDA
50  }
51 
53 #ifdef HAVE_CUDA
55  for (auto& c : cuda_streams_) {
56  checkCudaErrors(cuStreamDestroy(c));
57  }
58  }
59 #endif // HAVE_CUDA
60  }
61 
62  CUstream getCudaStream() { // NOTE: CUstream is cudaStream_t
64  int device_num = cuda_mgr_->getContext();
65  return getCudaStreamForDevice(device_num);
66  } else {
67  return 0;
68  }
69  }
70 
71  CUstream getCudaStreamForDevice(int device_num) { // NOTE: CUstream is cudaStream_t
73  CHECK_GE(device_num, 0);
74  CHECK_LT((size_t)device_num, cuda_streams_.size());
75  return cuda_streams_[device_num];
76  } else {
77  return 0;
78  }
79  }
80 
81  static std::shared_ptr<QueryEngine> getInstance() {
82  if (auto s = instance_.lock()) {
83  return s;
84  } else {
85  throw std::runtime_error("QueryEngine instance hasn't been created");
86  }
87  }
88 
89  static std::shared_ptr<QueryEngine> createInstance(CudaMgr_Namespace::CudaMgr* cuda_mgr,
90  bool cpu_only) {
92  if (auto s = instance_.lock()) {
93  return s;
94  } else {
95  s = std::make_shared<QueryEngine>(cuda_mgr, cpu_only);
96  instance_ = s;
97  return s;
98  }
99  }
100 
101  private:
103  std::vector<CUstream> cuda_streams_;
104 
105  inline static std::mutex mutex_; // TODO(sy): use atomics instead?
106  inline static std::weak_ptr<QueryEngine> instance_;
107 
108  static constexpr size_t code_cache_size{1000};
109 
110  public:
111  std::unique_ptr<CodeCacheAccessor<CpuCompilationContext>> s_stubs_accessor;
112  std::unique_ptr<CodeCacheAccessor<CpuCompilationContext>> s_code_accessor;
113  std::unique_ptr<CodeCacheAccessor<CpuCompilationContext>> cpu_code_accessor;
114  std::unique_ptr<CodeCacheAccessor<GpuCompilationContext>> gpu_code_accessor;
115  std::unique_ptr<CodeCacheAccessor<CompilationContext>> tf_code_accessor;
116 }; // class QueryEngine
117 
118 CUstream getQueryEngineCudaStream(); // NOTE: CUstream is cudaStream_t
120  int device_num); // NOTE: CUstream is cudaStream_t
std::unique_ptr< CodeCacheAccessor< CpuCompilationContext > > cpu_code_accessor
Definition: QueryEngine.h:113
CUstream getCudaStream()
Definition: QueryEngine.h:62
void * CUstream
Definition: nocuda.h:23
#define LOG(tag)
Definition: Logger.h:216
void checkCudaErrors(CUresult err)
Definition: sample.cpp:38
CUstream getCudaStreamForDevice(int device_num)
Definition: QueryEngine.h:71
CudaMgr_Namespace::CudaMgr * cuda_mgr_
Definition: QueryEngine.h:102
std::unique_ptr< CodeCacheAccessor< CompilationContext > > tf_code_accessor
Definition: QueryEngine.h:115
void setContext(const int device_num) const
Definition: CudaMgr.cpp:405
#define CHECK_GE(x, y)
Definition: Logger.h:235
static std::weak_ptr< QueryEngine > instance_
Definition: QueryEngine.h:106
int getDeviceCount() const
Definition: CudaMgr.h:87
CUstream getQueryEngineCudaStream()
Definition: QueryEngine.cpp:3
static std::shared_ptr< QueryEngine > createInstance(CudaMgr_Namespace::CudaMgr *cuda_mgr, bool cpu_only)
Definition: QueryEngine.h:89
static constexpr size_t code_cache_size
Definition: QueryEngine.h:108
std::unique_lock< T > unique_lock
std::vector< CUstream > cuda_streams_
Definition: QueryEngine.h:103
std::unique_ptr< CodeCacheAccessor< CpuCompilationContext > > s_stubs_accessor
Definition: QueryEngine.h:111
#define CHECK_LT(x, y)
Definition: Logger.h:232
QueryEngine(CudaMgr_Namespace::CudaMgr *cuda_mgr, bool cpu_only)
Definition: QueryEngine.h:13
CUstream getQueryEngineCudaStreamForDevice(int device_num)
Definition: QueryEngine.cpp:7
static std::shared_ptr< QueryEngine > getInstance()
Definition: QueryEngine.h:81
static std::mutex mutex_
Definition: QueryEngine.h:105
bool g_query_engine_cuda_streams
Definition: QueryEngine.h:9
std::unique_ptr< CodeCacheAccessor< GpuCompilationContext > > gpu_code_accessor
Definition: QueryEngine.h:114
std::unique_ptr< CodeCacheAccessor< CpuCompilationContext > > s_code_accessor
Definition: QueryEngine.h:112