Skip to content
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

Make constants constexpr (where possible) #2478

Merged
merged 6 commits into from
Oct 21, 2021
Merged

Make constants constexpr (where possible) #2478

merged 6 commits into from
Oct 21, 2021

Conversation

andrjohns
Copy link
Collaborator

Summary

This PR changes the declaration of constants to use static constexpr where supported.

static constexpr was used over plain constexpr as it compiles to more efficient assembly. Examples provided on godbolt below:

Additionally the Euler's Gamma constant was added after a request on the forum

Tests

Added test for egamma, and existing tests for constants all pass

Side Effects

N/A

Release notes

Changed constants to static constexpr for efficiency, added Euler's Gamma constant

Checklist

  • Math issue #(issue number)

  • Copyright holder: Andrew Johnson

    The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
    - Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
    - Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

  • the basic tests are passing

    • unit tests pass (to run, use: ./runTests.py test/unit)
    • header checks pass, (make test-headers)
    • dependencies checks pass, (make test-math-dependencies)
    • docs build, (make doxygen)
    • code passes the built in C++ standards checks (make cpplint)
  • the code is written in idiomatic C++ and changes are documented in the doxygen

  • the new changes are tested

@SteveBronder
Copy link
Collaborator

Interesting, it looks like this is failing the sir model in the cmdstan upstream performance tests

https://jenkins.mc-stan.org/job/CmdStan%20Performance%20Tests/job/downstream_tests/2577/consoleFull

FAIL: golds/stat_comp_benchmarks_benchmarks_sir_sir.gold param gamma not within (0.200873675 - 0.199968898) / 0.00232163707201 < 0.3
'stat_comp_benchmarks/benchmarks/sir/sir.stan' had fails '[('gamma', 0.199968898, 0.002321637072015, 0.20087367499999986)]' and errors '[]'

@andrjohns
Copy link
Collaborator Author

@SteveBronder Oddly enough I can't reproduce the performance test failures locally, either on Linux or Windows. Would you mind trying it locally at some point to see if you can reproduce?

@SteveBronder
Copy link
Collaborator

Ack sorry I've been so slow on this been making a really steady amount of progress on the new matrix type in the compiler. I can run this on the mac we use for testing tomorrow. My guess is that whatever old clang version is on that mac doesn't follow something in the standard on infinities. But let me run this tmrw before we change anything

@SteveBronder
Copy link
Collaborator

Actually I can't login to the mac because I had to wipe my hard drive recently because of a computer crash and think I lost the ssh I needed to get in. I tried replicating this locally to no avail so I think this must be something with the mac

Let me email Ben G and see if I can get access to that mac. The MCSE difference is wide enough that I think it's worth looking into.

@andrjohns
Copy link
Collaborator Author

No wokkas at all, thanks for looking into it! This is much lower priority than the varmat stuff, so no pressure from my end

@SteveBronder
Copy link
Collaborator

@serban-nicusor-toptal it looks like after the Jenkins update the little drop down arrows do not work anymore, is it possible to fix that? (Ftr can still see the full log if I go to the Jenkins site but it's nice to have the dropdown)

Screenshot_20211019-121619_Samsung Internet

@serban-nicusor-toptal
Copy link
Contributor

serban-nicusor-toptal commented Oct 19, 2021

@SteveBronder fixed! I didn't see that one, thanks!

PS: If the mac you want to get into is the one we're using on Jenkins I can help you regain your ssh access, just send me an email.

@SteveBronder
Copy link
Collaborator

@serban-nicusor-toptal much appreciated!

@stan-buildbot
Copy link
Contributor


Name Old Result New Result Ratio Performance change( 1 - new / old )
gp_pois_regr/gp_pois_regr.stan 3.52 3.52 1.0 0.2% faster
low_dim_corr_gauss/low_dim_corr_gauss.stan 0.02 0.02 1.0 -0.46% slower
eight_schools/eight_schools.stan 0.09 0.09 1.03 2.86% faster
gp_regr/gp_regr.stan 0.14 0.14 0.97 -3.47% slower
irt_2pl/irt_2pl.stan 5.1 5.83 0.88 -14.22% slower
performance.compilation 91.82 90.51 1.01 1.42% faster
low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan 8.31 8.15 1.02 2.04% faster
pkpd/one_comp_mm_elim_abs.stan 30.49 31.01 0.98 -1.71% slower
sir/sir.stan 120.98 116.99 1.03 3.3% faster
gp_regr/gen_gp_data.stan 0.03 0.03 0.94 -6.21% slower
low_dim_gauss_mix/low_dim_gauss_mix.stan 2.99 3.0 1.0 -0.36% slower
pkpd/sim_one_comp_mm_elim_abs.stan 0.37 0.4 0.94 -6.9% slower
arK/arK.stan 2.79 2.79 1.0 -0.21% slower
arma/arma.stan 0.27 0.25 1.09 8.36% faster
garch/garch.stan 0.65 0.72 0.9 -10.8% slower
Mean result: 0.985763226119

Jenkins Console Log
Blue Ocean
Commit hash: f31f43d


Machine information ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G22010

CPU:
Intel(R) Xeon(R) CPU E5-1680 v2 @ 3.00GHz

G++:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

Clang:
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.6.0
Thread model: posix

@andrjohns
Copy link
Collaborator Author

@SteveBronder looks like the macOS issues were just temporary, all seems to have completed without issue (phew!)

Copy link
Member

@syclik syclik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Thanks!

@syclik syclik merged commit 14e20f1 into stan-dev:develop Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants