Skip to content

Navigation Menu

Sign in
Sign up

How to disable c++ optimizations using cpp_options #716

Unanswered
justindomke asked this question in Q&A
Discussion options

Hello! For the sake of speeding up compilation during testing, I'd like to disable all C++ optimization. I think that the cpp_options flag for CmdStanModel is the way to do this, but I'm not sure about the correct syntax. Should I use cpp_options={'O':0} or cpp_options={'O',"0"}, or something else? Using nonsense options doesn't seem to trigger an error so it's hard to be confident if the choice is having any effect...

You must be logged in to vote

Replies: 3 comments 8 replies

Comment options

I think both should work because all of them are piped to subprocess and finally to commandline as a text.

opts.append(f'{key}={val}')
You must be logged in to vote
0 replies
Comment options

@justindomke: This can be a win if you're evaluating really simple models, but keep in mind that the speed difference once compiled between -O3 and -O0 is about a factor of 10 last time I checked.

Allowing inputs like cpp_options={'0', "0"}, not to mention the mixing of single and double quotes for strings, really annoys my consistency and type sensitive side (I realize I'm stuck with programmer's choice on string escapes). The reason we do that is that it's impossible to reflect all of the C++ compiler options inside of CmdStanPy!

But I'm very confused because O=0 is a Stan makefile option, not a C++ compiler option. So I'm not sure how to add actual C++ compiler options, especially ones that aren't of the form key=value. Is this just the doc that's wrong and it should be makefile options? Do they then specify actual C++ options through make/local somewhere?

class CompilerOptions:
 """
 User-specified flags for stanc and C++ compiler.

 Attributes:
 stanc_options - stanc compiler flags, options
 cpp_options - makefile options (NAME=value)
 user_header - path to a user .hpp file to include during compilation
 """
def __init__(
 self,
 *,
 stanc_options: Optional[Dict[str, Any]] = None,
 cpp_options: Optional[Dict[str, Any]] = None,
 user_header: OptionalPath = None,
 ) -> None:
 ...
 self._cpp_options = cpp_options if cpp_options is not None else {}
You must be logged in to vote
6 replies
Comment options

So then, is this what I want?

cpp_options = {"CXXFLAGS": "-O0"}
Comment options

I somehow missed that they were documented as makefile options because of the Stroop effect of the variable being named "cpp_options".

Comment options

@justindomke the Stan makefiles pass -O by themselves, which is why "O":0 is needed (otherwise both get passed, and the one that appears last will win!)

Comment options

@justindomke. I'm also curious about that. Also, do the flags get added to existing flags or do they overwrite?

Comment options

@bob-carpenter @WardBrian

Revisiting this... I think I can confirm that the existing flags get overwritten.

Or to step back a bit, because of a conspiracy of Apple, PyCharm and pytest, I seem to need to directly include the directory with standard header files like stdio.h and so on.

If I don't fool with cpp_options at all, this is the ultimate command that cmdstanpy seems to issue:

clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -Wno-ignored-attributes -I stan/lib/stan_math/lib/tbb_2020.3/include -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DBOOST_DISABLE_ASSERTS -c -o src/cmdstan/main.o src/cmdstan/main.cpp

But if I try adding

cpp_options = {
 "CXXFLAGS": "-I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
}

Then I ultimately get this compilation command:

clang++ -I /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -DBOOST_DISABLE_ASSERTS -c -o src/cmdstan/main.o src/cmdstan/main.cpp

It's adding the directory I want to add. But it's also dropping all the other directories, which means still nothing can compile. Any hints?

Edit: In case it isn't clear, all I want to do is add my new directory without deleting all the standard ones.

Comment options

@justindomke I think the cmdstan makefiles have something wrong with them re:CXXFLAGS right now where setting it prevents all the defaults from appearing, as you've discovered. I have an issue open to track this: #732

The easiest fix seems to be using the variable CXXFLAGS_PROGRAM instead. Alternatively you could try baking it in with CXX by setting CXX=clang++ -whatever-flag-you-need

You must be logged in to vote
2 replies
Comment options

Thank you! CXXFLAGS_PROGRAM doesn't seem to actually do anything—maybe I have an old version or something—but cpp_options={'CXX':'clang++ -I /path/to/stuff'} seems to do it. (And has an appealing brute-force quality...)

Comment options

You inspired me to find the actual reason this is all going awry - stan-dev/math#3028

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /