1 #ifndef CAFFE_COMMON_HPP_
2 #define CAFFE_COMMON_HPP_
4 #include <boost/shared_ptr.hpp>
8 #include <glog/logging.h>
11 #include "driver_types.h"
13 #define CUDA_CHECK(condition) CHECK_EQ((condition), cudaSuccess)
14 #define CUBLAS_CHECK(condition) CHECK_EQ((condition), CUBLAS_STATUS_SUCCESS)
15 #define CURAND_CHECK(condition) CHECK_EQ((condition), CURAND_STATUS_SUCCESS)
16 #define VSL_CHECK(condition) CHECK_EQ((condition), VSL_STATUS_OK)
18 #define CUDA_POST_KERNEL_CHECK \
19 if (cudaSuccess != cudaPeekAtLastError()) {\
20 LOG(FATAL) << "Cuda kernel failed. Error: " << cudaGetLastError(); \
23 #define INSTANTIATE_CLASS(classname) \
24 template class classname<float>; \
25 template class classname<double>
27 #define NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented Yet"
32 // We will use the boost shared_ptr instead of the new C++11 one mainly
33 // because cuda does not work (at least now) well with C++11 features.
34 using boost::shared_ptr;
36 // For backward compatibility we will just use 512 threads per block
37 const int CAFFE_CUDA_NUM_THREADS = 512;
39 inline int CAFFE_GET_BLOCKS(const int N) {
40 return (N + CAFFE_CUDA_NUM_THREADS - 1) / CAFFE_CUDA_NUM_THREADS;
43 // A singleton class to hold common caffe stuff, such as the handler that
44 // caffe is going to use for cublas.
49 enum Brew { CPU, GPU };
50 enum Phase { TRAIN, TEST };
52 // The getters for the variables.
53 // Returns the cublas handle.
54 static cublasHandle_t cublas_handle();
55 // Returns the curand generator.
56 static curandGenerator_t curand_generator();
57 // Returns the MKL random stream.
58 static VSLStreamStatePtr vsl_stream();
59 // Returns the mode: running on CPU or GPU.
61 // Returns the phase: TRAIN or TEST.
63 // The setters for the variables
65 static void set_mode(Brew mode);
67 static void set_phase(Phase phase);
68 // Sets the random seed of both MKL and curand
69 static void set_random_seed(const unsigned int seed);
72 // The private constructor to avoid duplicate instantiation.
76 static shared_ptr<Caffe> singleton_;
77 cublasHandle_t cublas_handle_;
78 curandGenerator_t curand_generator_;
79 VSLStreamStatePtr vsl_stream_;
87 #endif // CAFFE_COMMON_HPP_