@@ -9,12 +9,10 @@ namespace math {
9
9
10
10
/* *
11
11
* Provides a thread_local singleton if needed. Read warnings below!
12
- * With STAN_THREADS defined, the singleton is a thread_local static pointer
13
- * for performance reasons. When STAN_THREADS is not set, we have the old
14
- * static AD stack in the instance_ field because we saw odd performance
15
- * issues on the Mac Pro[4]. The rest of this commentary is specifically
16
- * talking about the design choices in the STAN_THREADS=true case.
17
- * When a TLS is used then initialization with
12
+ * For performance reasons the singleton is a global static pointer
13
+ * which is stored as thread local (TLS) if STAN_THREADS is
14
+ * defined. The use of a pointer is motivated by performance reasons
15
+ * for the threading case. When a TLS is used then initialization with
18
16
* a constant expression is required for fast access to the TLS. As
19
17
* the AD storage struct is non-POD it must be initialized as a
20
18
* dynamic expression such that compilers will wrap any access to the
@@ -61,7 +59,6 @@ namespace math {
61
59
* [2] https://github.com/stan-dev/math/pull/826
62
60
* [3]
63
61
* http://discourse.mc-stan.org/t/potentially-dropping-support-for-older-versions-of-apples-version-of-clang/3780/
64
- * [4] https://github.com/stan-dev/math/pull/1135
65
62
*/
66
63
template <typename ChainableT, typename ChainableAllocT>
67
64
struct AutodiffStackSingleton {
@@ -70,12 +67,10 @@ struct AutodiffStackSingleton {
70
67
71
68
AutodiffStackSingleton () : own_instance_(init()) {}
72
69
~AutodiffStackSingleton () {
73
- #ifdef STAN_THREADS
74
70
if (own_instance_) {
75
71
delete instance_;
76
72
instance_ = nullptr ;
77
73
}
78
- #endif
79
74
}
80
75
81
76
struct AutodiffStackStorage {
@@ -95,39 +90,25 @@ struct AutodiffStackSingleton {
95
90
explicit AutodiffStackSingleton (AutodiffStackSingleton_t const &) = delete;
96
91
AutodiffStackSingleton &operator =(const AutodiffStackSingleton_t &) = delete ;
97
92
98
- static constexpr inline AutodiffStackStorage &instance () {
99
- return
93
+ static
100
94
#ifdef STAN_THREADS
101
- *
95
+ #ifdef __GNUC__
96
+ __thread
97
+ #else
98
+ thread_local
102
99
#endif
103
- instance_;
104
- }
100
+ # endif
101
+ AutodiffStackStorage *instance_;
105
102
106
103
private:
107
104
static bool init () {
108
- #ifdef STAN_THREADS
109
105
if (!instance_) {
110
106
instance_ = new AutodiffStackStorage ();
111
107
return true ;
112
108
}
113
- #endif
114
109
return false ;
115
110
}
116
111
117
- static
118
- #ifdef STAN_THREADS
119
- #ifdef __GNUC__
120
- __thread
121
- #else
122
- thread_local
123
- #endif
124
- #endif
125
- AutodiffStackStorage
126
- #ifdef STAN_THREADS
127
- *
128
- #endif
129
- instance_;
130
-
131
112
bool own_instance_;
132
113
};
133
114
@@ -141,13 +122,8 @@ thread_local
141
122
#endif
142
123
typename AutodiffStackSingleton<ChainableT,
143
124
ChainableAllocT>::AutodiffStackStorage
144
-
145
- #ifdef STAN_THREADS
146
125
*AutodiffStackSingleton<ChainableT, ChainableAllocT>::instance_
147
126
= nullptr ;
148
- #else
149
- AutodiffStackSingleton<ChainableT, ChainableAllocT>::instance_;
150
- #endif
151
127
152
128
} // namespace math
153
129
} // namespace stan
0 commit comments