* \brief Stores the cuda context of the current thread. * cuMat uses one cuda stream per thread, and also potentially * different devices. * */
| 130 | * |
| 131 | */ |
| 132 | class Context |
| 133 | { |
| 134 | private: |
| 135 | cudaStream_t stream_; |
| 136 | int device_ = 0; |
| 137 | |
| 138 | #if CUMAT_CONTEXT_DEBUG_MEMORY==1 |
| 139 | int allocationsHost_ = 0; |
| 140 | int allocationsDevice_ = 0; |
| 141 | #endif |
| 142 | |
| 143 | CUMAT_DISALLOW_COPY_AND_ASSIGN(Context); |
| 144 | |
| 145 | public: |
| 146 | Context(int device = 0) |
| 147 | : device_(device) |
| 148 | , stream_(nullptr) |
| 149 | { |
| 150 | #if 0 |
| 151 | //stream creation must be synchronized |
| 152 | //TODO: seems to work without |
| 153 | static std::mutex mutex; |
| 154 | std::lock_guard<std::mutex> lock(mutex); |
| 155 | #endif |
| 156 | |
| 157 | #if CUMAT_SINGLE_THREAD_CONTEXT==0 |
| 158 | //each context has its own stream |
| 159 | CUMAT_SAFE_CALL(cudaStreamCreateWithFlags(&stream_, cudaStreamNonBlocking)); |
| 160 | #endif |
| 161 | //for a global context, use the global stream |
| 162 | |
| 163 | //TODO: init BLAS context and so on |
| 164 | |
| 165 | CUMAT_LOG_DEBUG("Context initialized for thread 0x" << std::hex << std::this_thread::get_id() << ", stream: 0x" << stream_); |
| 166 | } |
| 167 | |
| 168 | ~Context() |
| 169 | { |
| 170 | if (stream_ != nullptr) |
| 171 | { |
| 172 | cudaStreamDestroy(stream_); |
| 173 | stream_ = nullptr; |
| 174 | } |
| 175 | CUMAT_LOG_DEBUG("Context deleted for thread 0x" << std::hex << std::this_thread::get_id()); |
| 176 | #if CUMAT_CONTEXT_DEBUG_MEMORY==1 |
| 177 | CUMAT_ASSERT(allocationsHost_ == 0 && "some host memory was not released"); |
| 178 | CUMAT_ASSERT(allocationsDevice_ == 0 && "some device memory was not released"); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * \brief Explicitly frees the context's resources. |
| 184 | * Warning: use with care! All future calls on this context will fail |
| 185 | * and the context can't be created again. |
| 186 | * Use only if you shut down the CUDA driver before the normal program |
| 187 | * termination. |
| 188 | */ |
| 189 | void destroy() |
nothing calls this directly
no outgoing calls
no test coverage detected