A singleton class to hold common caffe stuff, such as the handler that caffe is going to use for cublas, curand, etc.
| 100 | // A singleton class to hold common caffe stuff, such as the handler that |
| 101 | // caffe is going to use for cublas, curand, etc. |
| 102 | class Caffe { |
| 103 | public: |
| 104 | ~Caffe(); |
| 105 | |
| 106 | // Thread local context for Caffe. Moved to common.cpp instead of |
| 107 | // including boost/thread.hpp to avoid a boost/NVCC issues (#1009, #1010) |
| 108 | // on OSX. Also fails on Linux with CUDA 7.0.18. |
| 109 | static Caffe& Get(); |
| 110 | |
| 111 | enum Brew { CPU, GPU }; |
| 112 | |
| 113 | // This random number generator facade hides boost and CUDA rng |
| 114 | // implementation from one another (for cross-platform compatibility). |
| 115 | class RNG { |
| 116 | public: |
| 117 | RNG(); |
| 118 | explicit RNG(unsigned int seed); |
| 119 | explicit RNG(const RNG&); |
| 120 | RNG& operator=(const RNG&); |
| 121 | void* generator(); |
| 122 | private: |
| 123 | class Generator; |
| 124 | shared_ptr<Generator> generator_; |
| 125 | }; |
| 126 | |
| 127 | // Getters for boost rng, curand, and cublas handles |
| 128 | inline static RNG& rng_stream() { |
| 129 | if (!Get().random_generator_) { |
| 130 | Get().random_generator_.reset(new RNG()); |
| 131 | } |
| 132 | return *(Get().random_generator_); |
| 133 | } |
| 134 | #ifndef CPU_ONLY |
| 135 | inline static cublasHandle_t cublas_handle() { return Get().cublas_handle_; } |
| 136 | inline static curandGenerator_t curand_generator() { |
| 137 | return Get().curand_generator_; |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | // Returns the mode: running on CPU or GPU. |
| 142 | inline static Brew mode() { return Get().mode_; } |
| 143 | // The setters for the variables |
| 144 | // Sets the mode. It is recommended that you don't change the mode halfway |
| 145 | // into the program since that may cause allocation of pinned memory being |
| 146 | // freed in a non-pinned way, which may cause problems - I haven't verified |
| 147 | // it personally but better to note it here in the header file. |
| 148 | inline static void set_mode(Brew mode) { Get().mode_ = mode; } |
| 149 | // Sets the random seed of both boost and curand |
| 150 | static void set_random_seed(const unsigned int seed); |
| 151 | // Sets the device. Since we have cublas and curand stuff, set device also |
| 152 | // requires us to reset those values. |
| 153 | static void SetDevice(const int device_id); |
| 154 | // Prints the current GPU status. |
| 155 | static void DeviceQuery(); |
| 156 | // Check if specified device is available |
| 157 | static bool CheckDevice(const int device_id); |
| 158 | // Search from start_id to the highest possible device ordinal, |
| 159 | // return the ordinal of the first available device. |