Repository navigation
Benchmark compiler optimization flags (clang + g++/gcc); add opt-in OSQP_OPT_FAST_MATH - #4
Merged
Merged
Conversation
Benchmarks g++/gcc/clang optimization levels for the solver core (OSQP ADMM loop + built-in algebra + qdldl/AMD factorizer) and documents the findings in BENCHMARK.md, with a reproducible harness under bench/. Findings (Apple M2 arm64): - -O3 is already the sweet spot; -O2/-O1 are within noise, -O0 ~3x slower. - -march=native and -flto give no measurable gain on this workload. - -ffast-math speeds up the C OSQP core ~25-30% (and ~4% end-to-end through the Python wrapper, whose dense->CSC conversion dominates). Change: add an opt-in OSQP_OPT_FAST_MATH CMake option (OFF by default, so the shipped wheel stays IEEE-conformant -O3). It appends -ffast-math (/fp:fast on MSVC) to the base C/CXX flags before add_subdirectory(osqp), so it reaches the vendored OSQP/qdldl as well as the wrapper. All 46 tests pass on both the default and the fast-math build, with identical solutions. Co-authored-by: openhands <openhands@all-hands.dev>
…GCC) Cross-compiler result: clang 21 is ~30% faster with -Ofast/-ffast-math, but GCC 16 is ~15-18% slower. This is the strongest argument for keeping OSQP_OPT_FAST_MATH opt-in and OFF by default. Co-authored-by: openhands <openhands@all-hands.dev>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Benchmarks the compiler optimization flags applied to the solver core (OSQP's ADMM loop + built-in CSC algebra + the qdldl/AMD LDL^t factorizer — the code that dominates solve time) on both clang and g++/gcc, and adds a small, opt-in CMake option for the one flag that can pay off.
This was generated by an AI agent (OpenHands) on behalf of the user.
What was measured
A new
bench/harness compiles exactly the C sources the package links against (OSQP + qdldl, same sources/defines/include layout as the CMake build) with a chosen flag set, then times two deterministic dense-SPD QP workloads (n=800 and n=2500) with cold-started solves. Compiling the C core directly guarantees that only the optimization flags vary between runs. The matrix was run under both Apple clang 21 and Homebrew GCC 16 (gcc-16/g++-16).Variants:
-O0,-O1,-O2,-O3,-Ofast,-O3 -march=native,-O3 -march=native -flto,-Ofast -march=native -flto.Full results are in
BENCHMARK.md. Headline (Apple M2, arm64):-O3-O3-O2/-O1-O0-march=native(±-flto)-Ofast/-ffast-mathAll variants produce the same objective values and status; no solution changes.
Key findings
-O3is already the sweet spot. The vendoredosqp/andqdldl/submodules force-O3in their own CMake files, and the wrapper C++ follows the top-level build type (Release→-O3). Both compilers agree there's nothing to gain by changing the level.-march=nativeand-fltoadd nothing measurable on this workload, and-march=nativewould break manylinux portability of the wheel — so neither is a candidate for a default.The only effective lever is fast-math — and it is compiler-specific. This is the headline cross-compiler result:
-Ofast/-ffast-math.Two subtleties verified experimentally:
-O3after any injected flag, and the last-Owins, so a bare-Ofastgets overridden and does nothing.-ffast-mathis independent of the-Olevel and does survive.Because fast-math helps clang but hurts GCC, it's a gamble, not a universal win.
It's numerically safe here. OSQP runs to a tolerance;
-ffast-mathdoesn't change the reported solutions. All 46 tests pass on both the default and the fast-math build, and the example prints identical output.Change
CMakeLists.txt: new optionOSQP_OPT_FAST_MATH(OFF by default). When ON it appends-ffast-math(or/fp:faston MSVC) to the baseC/CXXflags beforeadd_subdirectory(osqp), so it reaches OSQP, qdldl, and the wrapper. The default build is byte-for-byte the IEEE-conformant-O3— the shipped wheel and CI are unaffected.BENCHMARK.md: full methodology, per-compiler results, and reproduction steps.bench/: the reproducible harness (osqp_bench.c,build.sh,run.sh,aggregate.py,scan_flags.py)..gitignore: ignore the harness's output dirs (bench/{bin,gen,results}/).Enable it per-consumer (on a clang toolchain) if the relaxed FP contract is acceptable:
Validation
BENCHMARK.mdnumbers reproduced twice from a clean checkout (same ratios within noise), under both clang and GCC.python -m pytestfromtests/: 46 passed on the default wheel and on theOSQP_OPT_FAST_MATH=ONwheel.example_osqpprints identical results (x = [0.2, 1.0],obj = -0.68) for both builds.ubuntu-latest+ubuntu-24.04-arm, Python 3.12/3.13) builds the wheel successfully with the default (fast-math OFF) configuration.