Skip to content

Commit

Permalink
Merge pull request #6 from kennethassogba/main
Browse files Browse the repository at this point in the history
Fix OpenMP flag in CMakefile and add an example. Clean the github action.
  • Loading branch information
Goul-tard authored Nov 10, 2024
2 parents 896a9ef + 4a3b0b4 commit a83950b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
32 changes: 14 additions & 18 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# Set up a matrix to run the following 2 configurations:
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# 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
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
# c_compiler: clang
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
Expand All @@ -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.
Expand All @@ -61,6 +51,9 @@ jobs:
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
- name: Install libomp-dev
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
Expand All @@ -78,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).
Expand Down
17 changes: 15 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -13,17 +14,29 @@ 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()

# Eigen
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})
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})
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions examples/test_omp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <vector>
#include <omp.h>

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;
}

0 comments on commit a83950b

Please sign in to comment.