From 298e7a4129590599f4ce99b01a85a75636651210 Mon Sep 17 00:00:00 2001 From: Yangqing Jia Date: Fri, 13 Sep 2013 16:33:27 -0700 Subject: [PATCH] common.hpp update --- src/caffeine/common.hpp | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/caffeine/common.hpp b/src/caffeine/common.hpp index f309d89..38e6fd1 100644 --- a/src/caffeine/common.hpp +++ b/src/caffeine/common.hpp @@ -4,17 +4,48 @@ #include #include +#include #include #include "driver_types.h" namespace caffeine { - using boost::shared_ptr; -} -static std::ostream nullout(0); +// We will use the boost shared_ptr instead of the new C++11 one mainly +// because cuda does not work (at least now) well with C++11 features. +using boost::shared_ptr; + +#define CUDA_CHECK(condition) CHECK((condition) == cudaSuccess) +#define CUBLAS_CHECK(condition) CHECK((condition) == CUBLAS_STATUS_SUCCESS) + +// A singleton class to hold common caffeine stuff, such as the handler that +// caffeine is going to use for cublas. +class Caffeine { + private: + Caffeine() { + CUBLAS_CHECK(cublasCreate(&cublas_handle_)); + }; + static shared_ptr singleton_; + cublasHandle_t cublas_handle_; + public: + ~Caffeine() { + if (!cublas_handle_) { + CUBLAS_CHECK(cublasDestroy(cublas_handle_)); + } + } + static Caffeine& Get() { + if (!singleton_) { + singleton_.reset(new Caffeine()); + } + return *singleton_; + } + + static cublasHandle_t cublas_handle() { + return Get().cublas_handle_; + } +}; + +} -#define CUDA_CHECK(condition) \ - CHECK((condition) == cudaSuccess) #endif // CAFFEINE_COMMON_HPP_ -- 2.7.4