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

[FR] No bounds checks when NDEBUG flag is set #2420

Closed
SteveBronder opened this issue Mar 10, 2021 · 7 comments
Closed

[FR] No bounds checks when NDEBUG flag is set #2420

SteveBronder opened this issue Mar 10, 2021 · 7 comments

Comments

@SteveBronder
Copy link
Collaborator

Description

From #853 in stanc3 we recently reverted a check where the compiler actually avoided bounds checks on single indexing functions. This can have a pretty bad performance hit so I think now would be a good time to do something like #2255 where when NDEBUG is set bounds checks are a no-op. This is a standard no debug flag in C++ and is used by boost, eigen, and the standard library to turn off bounds checking. So I think its reasonable that if a user has it set they expect to not have these checks

Example

// in macro.hpp
#ifdefine NDEBUG
#ifndef STAN_NO_RANGE_AND_SIZE_CHECK
#define STAN_NO_RANGE_AND_SIZE_CHECK return
#endif
#else
#ifndef STAN_NO_RANGE_AND_SIZE_CHECK
#define STAN_NO_RANGE_AND_SIZE_CHECK
#endif
#endif

// in check_ranges.hpp
inline void check_range(const char* function, const char* name, int max,
                        int index, int nested_level, const char* error_msg) {
  STAN_NO_RANGE_AND_SIZE_CHECK;
  //... rest of body
}

Expected Output

Bounds checks are not turned on

Current Version:

v4.0.1

@bbbales2
Copy link
Member

Closed by #2423

@rok-cesnovar
Copy link
Member

@SteveBronder @bbbales2 so I need to set

CXXFLAGS += -DSTAN_NO_RANGE_CHECKS

and that escapes the range and size checks only?

@bbbales2
Copy link
Member

I don't think there's a guarantee that we are turning off every range and size check, but we're hopefully turning off enough that it counts performance-wise.

@bbbales2
Copy link
Member

And yeah, it's only range and size checks. All value checks should still be there.

@rok-cesnovar
Copy link
Member

Cool cool.

I would add a makefile helper so that STAN_NO_RANGE_CHECKS=true adds CXXFLAGS += -DSTAN_NO_RANGE_CHECKS. I hate the CXXFLAGS += syntax :) Alo makes it much easier to add it as a cpp_options.

@bbbales2
Copy link
Member

Alo makes it much easier to add it as a cpp_options.

Oh whoops, I thought this was somehow done automagically. I'll add to my list for today and if I don't get to it I'll open an issue for later.

@rok-cesnovar
Copy link
Member

Sure thing. For STAN_THREADS for example we do this:

ifdef STAN_THREADS

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

No branches or pull requests

3 participants