From 05ede4ac142fc61895b0ec489c505121f290037e Mon Sep 17 00:00:00 2001 From: Kenneth Assogba Date: Fri, 8 Nov 2024 01:40:45 +0100 Subject: [PATCH 1/3] Solve OpenMP flag problem and update docs --- CMakeLists.txt | 17 +++++++++++++++-- README.md | 20 +++++++++++++------- examples/test_omp.cpp | 19 +++++++++++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 examples/test_omp.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b7d462c..61ae7c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,8 +2,9 @@ cmake_minimum_required(VERSION 3.4...3.31) # specifying a minimum CMake version project(demeter) # set the project name set(CMAKE_CXX_STANDARD 14) # specifying the C++ Standard -set(GCC_COVERAGE_COMPILE_FLAGS "-Wall -Wextra -pedantic") # probably not the best variable name +set(GCC_COVERAGE_COMPILE_FLAGS "-Wall -Wextra -pedantic") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") +# add_compile_options(-Wall -Wextra -pedantic) set(CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -funroll-loops -funroll-all-loops -DNDEBUG") set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address,undefined") @@ -13,6 +14,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "-g -fsanitize=address,undefined") # OpenMP find_package(OpenMP) if(OpenMP_CXX_FOUND) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") set(LINK_LIBS ${LINK_LIBS} ${OpenMP_CXX_LIBRARIES}) endif() @@ -20,10 +22,21 @@ endif() find_package(Eigen3 3.3 REQUIRED NO_MODULE) set(LINK_LIBS ${LINK_LIBS} Eigen3::Eigen) +# Verbose +include(CMakePrintHelpers) +cmake_print_variables(PROJECT_SOURCE_DIR CMAKE_BUILD_TYPE) +cmake_print_variables(CMAKE_CXX_COMPILER CMAKE_CXX_COMPILER_ID CMAKE_CXX_COMPILER_VERSION) +cmake_print_variables(CMAKE_CXX_FLAGS COMPILE_DEFINITIONS) +cmake_print_variables(CMAKE_CXX_COMPILER_RANLIB) +cmake_print_variables(OpenMP_CXX_FOUND OpenMP_CXX_FLAGS OpenMP_CXX_INCLUDE_DIRS) +cmake_print_variables(OpenMP::OpenMP_CXX OpenMP_CXX_VERSION) # Executables add_executable(main examples/main.cpp) # create an executable using the specified file target_link_libraries(main PUBLIC ${LINK_LIBS}) add_executable(simple examples/simple.cpp) # create an executable using the specified file -target_link_libraries(simple PUBLIC ${LINK_LIBS}) \ No newline at end of file +target_link_libraries(simple PUBLIC ${LINK_LIBS}) + +add_executable(test_omp examples/test_omp.cpp) # create an executable using the specified file +target_link_libraries(test_omp PUBLIC ${LINK_LIBS}) \ No newline at end of file diff --git a/README.md b/README.md index 71edb0f..aed0d5e 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,11 @@ Numerically, the physics part is coded in C++ with Python as a user interface fo ## Requirements -A C++ compiler compatible with at least version 17 of the standard, eg. GCC 8, Clang 5 and later. -- CMake `apt install cmake` -- Eigen `apt install libeigen3-dev` +- A C++ compiler compatible with at least version 17 of the standard, eg: + - GCC 8 and later `apt install build-essential`, + - Clang 5 and later `apt install clang libomp-dev clang-tidy clang-format`. +- CMake `apt install cmake`. +- Eigen `apt install libeigen3-dev`. ## Build and Run @@ -48,16 +50,20 @@ Then try ./main ``` -### Configure a debug build +### Configure the build type: Debug or Release ```bash cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Debug cmake --build build/ # build the binaries ``` -### Configure a release build +### Choose a compiler ```bash -cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Release -cmake --build build/ +cmake -S . -B build/ -D CMAKE_CXX_COMPILER=clang++-18 +``` + +### Append compiler flags +```bash +cmake -S . -B build/ -D CMAKE_CXX_FLAGS=-fsanitize=memory ``` ## Troubleshooting diff --git a/examples/test_omp.cpp b/examples/test_omp.cpp new file mode 100644 index 0000000..a5706de --- /dev/null +++ b/examples/test_omp.cpp @@ -0,0 +1,19 @@ +#include +#include +#include + +int main() +{ +#if defined(_OPENMP) + std::cout << "OpenMP version " << _OPENMP << '\n'; +#endif + +#pragma omp parallel for + for (int i = 0; i < 9; ++i) + { + std::cerr << "rank = " << omp_get_thread_num() + << " size = " << omp_get_num_threads() + << '\n'; + } + return 0; +} \ No newline at end of file From c2e58a8ae481fa76abc0828a84f24e7e39d0d91b Mon Sep 17 00:00:00 2001 From: Kenneth Assogba Date: Fri, 8 Nov 2024 01:50:56 +0100 Subject: [PATCH 2/3] Update cmake-multi-platform.yml --- .github/workflows/cmake-multi-platform.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index cf8c92d..78f436c 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -31,10 +31,10 @@ jobs: # c_compiler: cl # cpp_compiler: cl - os: ubuntu-latest - # c_compiler: gcc + c_compiler: gcc cpp_compiler: g++ - os: ubuntu-latest - # c_compiler: clang + c_compiler: clang cpp_compiler: clang++ # exclude: # - os: windows-latest @@ -61,6 +61,9 @@ jobs: run: | echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" + - name: Install xmllint + run: sudo apt install -y clang-tidy clang-format libomp-dev + - name: Configure CMake # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type From 4a3b0b4e33d0c47cc0ef8ffe53de29bb2edcc164 Mon Sep 17 00:00:00 2001 From: Kenneth Assogba Date: Fri, 8 Nov 2024 02:12:13 +0100 Subject: [PATCH 3/3] Clean the action --- .github/workflows/cmake-multi-platform.yml | 27 ++++++++-------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/.github/workflows/cmake-multi-platform.yml b/.github/workflows/cmake-multi-platform.yml index 78f436c..f194491 100644 --- a/.github/workflows/cmake-multi-platform.yml +++ b/.github/workflows/cmake-multi-platform.yml @@ -16,33 +16,23 @@ jobs: # Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable. fail-fast: false - # Set up a matrix to run the following 3 configurations: - # 1. + # Set up a matrix to run the following 2 configurations: # 2. # 3. # # To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list. matrix: - os: [ubuntu-latest] #, windows-latest] + os: [ubuntu-latest] build_type: [Release, Debug] - c_compiler: [gcc, clang] #, cl] + c_compiler: [gcc, clang] include: - # - os: windows-latest - # c_compiler: cl - # cpp_compiler: cl - os: ubuntu-latest c_compiler: gcc cpp_compiler: g++ - os: ubuntu-latest c_compiler: clang cpp_compiler: clang++ - # exclude: - # - os: windows-latest - # c_compiler: gcc - # - os: windows-latest - # c_compiler: clang - # - os: ubuntu-latest - # c_compiler: cl + steps: - uses: actions/checkout@v4 @@ -52,7 +42,7 @@ jobs: # with: # version: 3.4.0 env: - CMAKE_GENERATOR: ${{ matrix.gen }} + CMAKE_GENERATOR: Unix Makefiles - name: Set reusable strings # Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file. @@ -61,7 +51,7 @@ jobs: run: | echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT" - - name: Install xmllint + - name: Install libomp-dev run: sudo apt install -y clang-tidy clang-format libomp-dev - name: Configure CMake @@ -81,9 +71,12 @@ jobs: - name: Run example run: ${{github.workspace}}/build/main - - name: Run example + - name: Run simple run: ${{github.workspace}}/build/simple + - name: Run test_omp + run: ${{github.workspace}}/build/test_omp + # - name: Test # working-directory: ${{ steps.strings.outputs.build-output-dir }} # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).