Skip to content

Add OpenMP to default cmake build

Jonathan Toomim requested to merge jtoomim/bitcoin-cash-node:openmp-build into master

Overview

This adds the option (default enabled) to the cmake build to compile and link BCHN with OpenMP support. OpenMP support can be disabled with cmake -GNinja .. -DENABLE_OPENMP=OFF.

OpenMP is a backwards-compatible, easy-to-use system for writing parallel threaded code. When used correctly, OpenMP allows for programs to be compiled with and without OpenMP enabled from the same source code and get the same computation results, differing only in execution threading and timing: compiling without OpenMP merely results in single-threaded execution.

For example, in many cases you can parallelize a loop simply by adding

#pragma omp parallel for

before the loop entry; the compiler and runtime will then handle thread pool creation, memory sharing, work scheduling, etc. for you.

This MR does not add OpenMP support to the autotools build configuration. As autotools is deprecated, and as OpenMP code is backwards-compatible, I consider adding autotools OpenMP support to be unnecessary at this time.

On my machine, the bitcoind executable is 198035768 bytes with OpenMP, and 198036096 bytes without. Including OpenMP support shrinks* the binary by 328 bytes, likely due to minor differences in optimization strategies. I found no differences in test/benchmarks/p2p_stresstest.py performance.

Test plan

git remote add jtoomim git@gitlab.com:jtoomim/bitcoin-cash-node.git
git fetch jtoomim
git checkout jtoomim/openmp-build

# Compilation and testing should succeed with OpenMP enabled
# Should see a line like "-- OpenMP found: -fopenmp"
cmake -GNinja .. | grep OpenMP
ninja check 

# Should not see anything like "-- OpenMP found: -fopenmp"
cmake -GNinja .. -DENABLE_OPENMP=OFF | grep OpenMP
# ninja may re-run cmake, and will probably find OpenMP if it does,
# but OpenMP won't actually be enabled for compilation, so ignore that
# message for now. We'll double-check it later.
ninja check 

# This MR contains 3 commits. The 2nd commit adds trivial example 
# usage of OpenMP in src/validation.cpp and in src/test/merkle_tests.cpp
# to facilitate testing; the final commit reverts the 2nd. Checking out
# HEAD^ adds those trivial uses back in.
git checkout HEAD^

# This should produce warnings during compilation in validation.cpp and
# test/merkle_tests.cpp, but no errors during compilation or execution.
# The warnings should be something like this:
#  ../src/validation.cpp:3607: warning: ignoring ‘#pragma omp parallel’ [-Wunknown-pragmas]
cmake -GNinja .. -DENABLE_OPENMP=OFF
ninja check

# This should produce no warnings or errors, indicating OpenMP support.
# The previous -DENABLE_OPENMP=OFF will be cached, so we need to enable it
# explicitly this time
cmake -GNinja ..  -DENABLE_OPENMP=ON
ninja check
Edited by Jonathan Toomim

Merge request reports