{"action":"created","issue":{"url":"https://api.github.com/repos/stan-dev/math/issues/809","repository_url":"https://api.github.com/repos/stan-dev/math","labels_url":"https://api.github.com/repos/stan-dev/math/issues/809/labels{/name}","comments_url":"https://api.github.com/repos/stan-dev/math/issues/809/comments","events_url":"https://api.github.com/repos/stan-dev/math/issues/809/events","html_url":"https://github.com/stan-dev/math/pull/809","id":310433162,"number":809,"title":"Feature/issue 551 threaded ad","user":{"login":"wds15","id":12070126,"avatar_url":"https://avatars0.githubusercontent.com/u/12070126?v=4","gravatar_id":"","url":"https://api.github.com/users/wds15","html_url":"https://github.com/wds15","followers_url":"https://api.github.com/users/wds15/followers","following_url":"https://api.github.com/users/wds15/following{/other_user}","gists_url":"https://api.github.com/users/wds15/gists{/gist_id}","starred_url":"https://api.github.com/users/wds15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wds15/subscriptions","organizations_url":"https://api.github.com/users/wds15/orgs","repos_url":"https://api.github.com/users/wds15/repos","events_url":"https://api.github.com/users/wds15/events{/privacy}","received_events_url":"https://api.github.com/users/wds15/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-04-02T08:55:30Z","updated_at":"2018-04-02T10:57:27Z","closed_at":null,"author_association":"CONTRIBUTOR","pull_request":{"url":"https://api.github.com/repos/stan-dev/math/pulls/809","html_url":"https://github.com/stan-dev/math/pull/809","diff_url":"https://github.com/stan-dev/math/pull/809.diff","patch_url":"https://github.com/stan-dev/math/pull/809.patch"},"body":"#### Submission Checklist\r\n\r\n- [ ] Run unit tests: `./runTests.py test/unit`\r\n- [X] Run cpplint: `make cpplint`\r\n- [X] Declare copyright holder and open-source license: see below\r\n\r\n#### Summary:\r\nMakes the AD tape thread local such that AD can be performed within threads. This will enable threaded use of stan-math and pave the way for a threaded version of `map_rect`.\r\n\r\nThis change makes the AD tape `thread_local` which is a standard C++11 facility. However, in order to make this change work with g++ and clang++ I had to use a design which resembles what is done in the clang sources (see [context.h](https://github.com/llvm-mirror/clang-tools-extra/blob/master/clangd/Context.h#L74) and [context.cpp](https://github.com/llvm-mirror/clang-tools-extra/blob/master/clangd/Context.cpp#L24) from a clang project). The technique used there relies on a static thread_local variable declared inside a static function. All this function does is to return a reference to an instance of the stack. This function is called `context()` [(see line 15 of `autodiffstackstorage.hpp`)](https://github.com/stan-dev/math/blob/feature/issue-551-threaded-AD/stan/math/rev/core/autodiffstackstorage.hpp#L15).\r\n\r\nAs a consequence our codebase has to access the stack differently. So far we prefixed everything with `ChainableStack::` which now becomes `ChainableStack::context().`. For example, this\r\n\r\n```cpp\r\n// old\r\nChainableStack::var_stack_.push_back(this);\r\n```\r\n\r\nnow becomes\r\n\r\n```cpp\r\n// new\r\nChainableStack::context().var_stack_.push_back(this);\r\n```\r\n\r\n#### Intended Effect:\r\nMake AD thread safe. Enable threaded use of AD to allow client code to run AD in threads safely.\r\n\r\n#### How to Verify:\r\nI have added a threaded evaluation of the `gradient` facility to the test file `test/unit/math/rev/mat/functor/gradient_test.cpp` which verifies that this runs in parallel.\r\n\r\n#### Side Effects:\r\nNone.\r\n\r\nAt least I have not observed any performance impact or so. That is, there is no additional overhead at all as it seems. The compiler appears to just compile away the additional function call.\r\n\r\n#### Documentation:\r\n\r\n#### Copyright and Licensing\r\n\r\nPlease list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company): Sebastian Weber\r\n\r\nBy submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:\r\n- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)\r\n- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)"},"comment":{"url":"https://api.github.com/repos/stan-dev/math/issues/comments/377906872","html_url":"https://github.com/stan-dev/math/pull/809#issuecomment-377906872","issue_url":"https://api.github.com/repos/stan-dev/math/issues/809","id":377906872,"user":{"login":"wds15","id":12070126,"avatar_url":"https://avatars0.githubusercontent.com/u/12070126?v=4","gravatar_id":"","url":"https://api.github.com/users/wds15","html_url":"https://github.com/wds15","followers_url":"https://api.github.com/users/wds15/followers","following_url":"https://api.github.com/users/wds15/following{/other_user}","gists_url":"https://api.github.com/users/wds15/gists{/gist_id}","starred_url":"https://api.github.com/users/wds15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wds15/subscriptions","organizations_url":"https://api.github.com/users/wds15/orgs","repos_url":"https://api.github.com/users/wds15/repos","events_url":"https://api.github.com/users/wds15/events{/privacy}","received_events_url":"https://api.github.com/users/wds15/received_events","type":"User","site_admin":false},"created_at":"2018-04-02T10:57:27Z","updated_at":"2018-04-02T10:57:27Z","author_association":"CONTRIBUTOR","body":"ok, travis build on linux using clang seems to work now. The changes to the `make/tests` file will probably need to be revise; I did not know where to put this otherwise."}}
{"actor":{"display_login":"wds15"}}