-
-
Notifications
You must be signed in to change notification settings - Fork 201
Faster TLS AD tape #1103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faster TLS AD tape #1103
Changes from all commits
953a35d
9179928
b4c59c0
27e9ee9
9cc04b0
d462e9b
fef78b5
7a3fffe
710d387
a81d754
a3ee346
63cc01d
2757f80
8b27125
4a521b9
223df95
61e937c
198ce4d
fed0097
9e15996
52fffe9
00c99a0
1178f5f
f8dd3b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
|
|
||
| #include <stan/math/rev/core/autodiffstackstorage.hpp> | ||
|
|
||
| #include <iostream> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
|
|
||
|
|
@@ -11,6 +13,45 @@ class chainable_alloc; | |
|
|
||
| typedef AutodiffStackSingleton<vari, chainable_alloc> ChainableStack; | ||
|
|
||
| /** | ||
| * Instantiates an instance of the ChainableStack if not already | ||
| * initialized. This function must be called before any autodiff | ||
| * variable get's instantiated within any thread which performs | ||
| * reverse mode autodiff operations. | ||
| */ | ||
| /* | ||
| static inline ChainableStack::AutodiffStackStorage* init() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we give this a slightly longer name indicating when you need to call it? Maybe something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm... my thoughts:
If you want it more verbose, then how about |
||
| #ifdef STAN_THREADS | ||
| return ChainableStack::init(); | ||
| if (ChainableStack::instance_ == nullptr) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this |
||
| ChainableStack::instance_ = new ChainableStack::AutodiffStackStorage(); | ||
| #endif | ||
| return ChainableStack::instance_; | ||
| } | ||
| */ | ||
|
|
||
| namespace { | ||
|
|
||
| struct ad_tape_initializer { | ||
| ad_tape_initializer() { | ||
| // std::cout << "Initializer created" << std::endl; | ||
| tape_ = stan::math::ChainableStack::init(); | ||
| } | ||
| ~ad_tape_initializer() { | ||
| // std::lock_guard<std::mutex> cout_lock(cout_mutex_init); | ||
| // std::cout << "Initializer destructed" << std::endl; | ||
| } | ||
|
|
||
| typedef ChainableStack::AutodiffStackStorage* tape_ptr_t; | ||
| thread_local static tape_ptr_t tape_; | ||
| }; | ||
|
|
||
| thread_local ad_tape_initializer::tape_ptr_t ad_tape_initializer::tape_ | ||
| = ChainableStack::init(); | ||
| ad_tape_initializer global_initializer; | ||
|
|
||
| } // namespace | ||
|
|
||
| } // namespace math | ||
| } // namespace stan | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #ifndef STAN_MATH_REV_CORE_CHAINABLESTACK_INST_CPP | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of this file if we have a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we still need the file in place so that the respective code gets executed to initialize the AD stack of the main thread. The alternative is to require that any client code must call
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, fair enough. |
||
| #define STAN_MATH_REV_CORE_CHAINABLESTACK_INST_CPP | ||
|
|
||
| #include <stan/math/rev/core/chainablestack.hpp> | ||
|
|
||
| namespace stan { | ||
| namespace math { | ||
| namespace internal { | ||
|
|
||
| /* | ||
| const ChainableStack::AutodiffStackStorage* __chainable_stack | ||
| = ChainableStack::init(); | ||
| */ | ||
|
|
||
| } // namespace internal | ||
| } // namespace math | ||
| } // namespace stan | ||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shoudl this pointer still be private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmpf... in principle it can be private right now, yes. In my follow-up on this I do need to access it. We seem to be relatively liberal with access to class members which is why I took out the private. I think it also makes testing easier.
I would opt to leave it as is for developer convenience. In case you think it is important to make it private we can probably do that. How about a comment in the code saying that one should not mess with this?