From b1180f29c93c23132f3cd1dd5c2861e979642665 Mon Sep 17 00:00:00 2001 From: Cyprien Noel Date: Tue, 3 Mar 2015 14:44:05 -0800 Subject: [PATCH 1/2] thread specific singleton --- include/caffe/common.hpp | 3 ++- src/caffe/common.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/caffe/common.hpp b/include/caffe/common.hpp index 6cf80a37bc1..cd4c6c059ad 100644 --- a/include/caffe/common.hpp +++ b/include/caffe/common.hpp @@ -2,6 +2,7 @@ #define CAFFE_COMMON_HPP_ #include +#include #include #include @@ -158,7 +159,7 @@ class Caffe { shared_ptr random_generator_; Brew mode_; - static shared_ptr singleton_; + static boost::thread_specific_ptr singleton_; private: // The private constructor to avoid duplicate instantiation. diff --git a/src/caffe/common.cpp b/src/caffe/common.cpp index af96cac40aa..be7cb808b04 100644 --- a/src/caffe/common.cpp +++ b/src/caffe/common.cpp @@ -7,7 +7,7 @@ namespace caffe { -shared_ptr Caffe::singleton_; +boost::thread_specific_ptr Caffe::singleton_; // random seeding int64_t cluster_seedgen(void) { From 8d4cbd937ec8a4f128cc47127e43e09a69dfd6d4 Mon Sep 17 00:00:00 2001 From: Jonathan L Long Date: Sat, 7 Mar 2015 18:19:27 -0800 Subject: [PATCH 2/2] forward declare instead of including boost/thread.hpp (#1009) This means that Caffe::Get has to be moved to common.cpp, and loses its "inline" (but there are no real performance implications). --- include/caffe/common.hpp | 11 ++++------- src/caffe/common.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/caffe/common.hpp b/include/caffe/common.hpp index cd4c6c059ad..85db0c8ea31 100644 --- a/include/caffe/common.hpp +++ b/include/caffe/common.hpp @@ -2,7 +2,6 @@ #define CAFFE_COMMON_HPP_ #include -#include #include #include @@ -69,6 +68,9 @@ private:\ // See PR #1236 namespace cv { class Mat; } +// Avoid issues with including boost/thread in NVCC source (#1009). +namespace boost { template class thread_specific_ptr; } + namespace caffe { // We will use the boost shared_ptr instead of the new C++11 one mainly @@ -99,12 +101,7 @@ void GlobalInit(int* pargc, char*** pargv); class Caffe { public: ~Caffe(); - inline static Caffe& Get() { - if (!singleton_.get()) { - singleton_.reset(new Caffe()); - } - return *singleton_; - } + static Caffe& Get(); enum Brew { CPU, GPU }; // This random number generator facade hides boost and CUDA rng diff --git a/src/caffe/common.cpp b/src/caffe/common.cpp index be7cb808b04..181675655ce 100644 --- a/src/caffe/common.cpp +++ b/src/caffe/common.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -39,6 +40,13 @@ void GlobalInit(int* pargc, char*** pargv) { ::google::InstallFailureSignalHandler(); } +Caffe& Caffe::Get() { + if (!singleton_.get()) { + singleton_.reset(new Caffe()); + } + return *singleton_; +} + #ifdef CPU_ONLY // CPU-only Caffe. Caffe::Caffe()