diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000000..87e0bdc5dc --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,19 @@ +# Enabled checks: +# +# - all the checks of the bugprone-* group except: +# easily-swappable-parameters, implicit-widening-of-multiplication-result, +# misplaced-widening-cast, narrowing-conversions +# +# - cppcoreguidelines-avoid-goto +# +Checks: ' + -*, + bugprone-*, + -bugprone-easily-swappable-parameters, + -bugprone-implicit-widening-of-multiplication-result, + -bugprone-misplaced-widening-cast, + -bugprone-narrowing-conversions, + cppcoreguidelines-avoid-goto + ' + +HeaderFilterRegex: 'src[a-z_A-Z0-9\/]+\.H$' diff --git a/.github/workflows/clangtidy.yml b/.github/workflows/clangtidy.yml new file mode 100644 index 0000000000..bcee08174c --- /dev/null +++ b/.github/workflows/clangtidy.yml @@ -0,0 +1,25 @@ + +name: 🧹 clang-tidy + +on: + push: + branches: + - development + pull_request: + branches: + - development + +jobs: + + linux_clang_tidy: + name: clang-tidy + runs-on: ubuntu-latest + env: {CXXFLAGS: "-Wno-pass-failed"} + steps: + - uses: actions/checkout@v4 + - name: Dependencies + run: .github/workflows/setup/ubuntu_clang.sh + - name: Build & Install + run: | + ./tools/runClangTidy.sh + diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 7fa33065e2..236f2d615e 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -78,22 +78,3 @@ jobs: cmake --build build -j 2 - name: Run Tests run: ctest --test-dir build --output-on-failure - -# enable again when we open-source (free core-hours for GH Actions) -# -# linux_clang7: -# name: Clang@7 C++17 OMPI -# runs-on: ubuntu-latest -# steps: -# - uses: actions/checkout@v4 -# - name: Dependencies -# run: .github/workflows/setup/ubuntu_clang.sh -# - name: Build & Install -# run: | -# mkdir build -# cd build -# cmake .. \ -# -DCMAKE_C_COMPILER=$(which clang-7) \ -# -DCMAKE_CXX_COMPILER=$(which clang++-7) -# make -j 2 VERBOSE=ON -# ctest --output-on-failure diff --git a/.github/workflows/setup/ubuntu_clang.sh b/.github/workflows/setup/ubuntu_clang.sh index 59ca005186..77f65e539d 100755 --- a/.github/workflows/setup/ubuntu_clang.sh +++ b/.github/workflows/setup/ubuntu_clang.sh @@ -19,6 +19,10 @@ sudo apt-get update sudo apt-get install -y --no-install-recommends \ build-essential \ ccache \ - clang-7 \ + clang \ + clang-tidy \ + libomp-dev \ libopenmpi-dev \ - openmpi-bin + openmpi-bin \ + libfftw3-dev \ + libfftw3-single3 diff --git a/src/mg_solver/HpMultiGrid.H b/src/mg_solver/HpMultiGrid.H index 5cf1c67f84..f8c0ca9d39 100644 --- a/src/mg_solver/HpMultiGrid.H +++ b/src/mg_solver/HpMultiGrid.H @@ -185,8 +185,10 @@ public: return 2; case 3: return 1; + default: + amrex::Abort(std::to_string(system_type) + " is not a valid system_type!"); } - return 0; + return 0; //unreachable } /** \brief Return the number of acf components used for a given system type @@ -201,8 +203,10 @@ public: return 2; case 3: return 0; + default: + amrex::Abort(std::to_string(system_type) + " is not a valid system_type!"); } - return 0; + return 0; //unreachable } /** When applying Dirichlet boundary conditions, shift boundary value by offset number of cells */ @@ -230,8 +234,11 @@ private: int m_num_mg_levels; /** Number of single-block-kernel levels */ int m_num_single_block_levels; + +#if defined(AMREX_USE_GPU) /** If the single block kernel should be used */ bool m_use_single_block_kernel = true; +#endif /** Alias to the solution argument passed in solve() */ amrex::FArrayBox m_sol; diff --git a/src/mg_solver/HpMultiGrid.cpp b/src/mg_solver/HpMultiGrid.cpp index 2ffb2ef215..ba9a52d9de 100644 --- a/src/mg_solver/HpMultiGrid.cpp +++ b/src/mg_solver/HpMultiGrid.cpp @@ -15,7 +15,9 @@ namespace hpmg { namespace { -constexpr int n_cell_single = 32; // switch to single block when box is smaller than this +#if defined(AMREX_USE_GPU) + constexpr int n_cell_single = 32; // switch to single block when box is smaller than this +#endif Box valid_domain_box (Box const& domain) { diff --git a/src/utils/MultiBuffer.cpp b/src/utils/MultiBuffer.cpp index a5e1474d02..eaae5addcf 100644 --- a/src/utils/MultiBuffer.cpp +++ b/src/utils/MultiBuffer.cpp @@ -807,7 +807,7 @@ void MultiBuffer::pack_data (int slice, MultiBeam& beams, MultiLaser& laser, int if (beam.communicateIdCpuComponent()) { // only pack idcpu component if it should be communicated - memcpy_to_buffer(slice, bo.m_beam_idcpu[b].value(), + memcpy_to_buffer(slice, bo.m_beam_idcpu[b].value(), // NOLINT(bugprone-unchecked-optional-access) soa.GetIdCPUData().dataPtr(), num_particles * sizeof(std::uint64_t)); } @@ -861,7 +861,7 @@ void MultiBuffer::unpack_data (int slice, MultiBeam& beams, MultiLaser& laser, i if (beam.communicateIdCpuComponent()) { // only undpack idcpu component if it should be communicated - memcpy_from_buffer(slice, bo.m_beam_idcpu[b].value(), + memcpy_from_buffer(slice, bo.m_beam_idcpu[b].value(), // NOLINT(bugprone-unchecked-optional-access) soa.GetIdCPUData().dataPtr(), num_particles * sizeof(std::uint64_t)); } else { diff --git a/tools/runClangTidy.sh b/tools/runClangTidy.sh new file mode 100755 index 0000000000..4e0c9bcf1e --- /dev/null +++ b/tools/runClangTidy.sh @@ -0,0 +1,121 @@ +#! /usr/bin/env bash + +# Copyright 2025 +# +# This file is part of HiPACE++. +# +# Authors: Luca Fedeli +# License: BSD-3-Clause-LBNL + +# This script is a developer's tool to perform +# checks with clang-tidy. +# +# Note: this script is only tested on Linux + +set -eu -o pipefail + +echo "=============================================" +echo +echo "This script is a developer's tool to perform" +echo "checks with clang-tidy." +echo "_____________________________________________" + +# Check source dir +REPO_DIR=$(cd $(dirname ${BASH_SOURCE})/../ && pwd) +echo +echo "Your current source directory is: ${REPO_DIR}" +echo "_____________________________________________" + +# Set number of jobs to use for compilation +PARALLEL="${HIPACE_TOOLS_LINTER_PARALLEL:-4}" +echo +echo "${PARALLEL} jobs will be used for compilation." +echo "This can be overridden by setting the environment" +echo "variable HIPACE_TOOLS_LINTER_PARALLEL, e.g.: " +echo +echo "$ export HIPACE_TOOLS_LINTER_PARALLEL=8" +echo "$ ./tools/runClangTidy.sh" +echo "_____________________________________________" + +# Check clang version +export CC="${CLANG:-"clang"}" +export CXX="${CLANGXX:-"clang++"}" +export CTIDY="${CLANGTIDY:-"clang-tidy"}" +echo +echo "The following versions of the clang compiler and" +echo "of the clang-tidy linter will be used:" +echo +echo "clang version:" +which ${CC} +${CC} --version +echo +echo "clang++ version:" +which ${CXX} +${CXX} --version +echo +echo "clang-tidy version:" +which ${CTIDY} +${CTIDY} --version +echo +echo "This can be overridden by setting the environment" +echo "variables CLANG, CLANGXX, and CLANGTIDY e.g.: " +echo "$ export CLANG=clang-20" +echo "$ export CLANGXX=clang++-20" +echo "$ export CTIDCLANGTIDYY=clang-tidy-20" +echo "$ ./tools/runClangTidy.sh" +echo "_____________________________________________" + +# Prepare clang-tidy wrapper +echo +echo "Prepare clang-tidy wrapper" +echo "The following wrapper ensures that only source files" +echo "in hipace/src/* are actually processed by clang-tidy" +echo +cat > ${REPO_DIR}/clang_tidy_wrapper << EOF +#!/bin/bash +REGEX="[a-z_A-Z0-9\/]*hipace\/src[a-z_A-Z0-9\/]+.cpp" +if [[ \$4 =~ \$REGEX ]];then + ${CTIDY} \$@ +fi +EOF +chmod +x ${REPO_DIR}/clang_tidy_wrapper +echo "clang_tidy_wrapper: " +cat ${REPO_DIR}/clang_tidy_wrapper +echo "_____________________________________________" + +# Compile HiPACE++ using clang-tidy +echo +echo "*******************************************" +echo "* Compile HiPACE++ using clang-tidy *" +echo "* Please ensure that all the dependencies *" +echo "* required to compile HiPACE++ are met *" +echo "*******************************************" +echo + +rm -rf ${REPO_DIR}/build_clang_tidy + +cmake -S ${REPO_DIR} -B ${REPO_DIR}/build_clang_tidy \ + -DCMAKE_CXX_CLANG_TIDY="${REPO_DIR}/clang_tidy_wrapper;--system-headers=0;--config-file=${REPO_DIR}/.clang-tidy" \ + -DCMAKE_VERBOSE_MAKEFILE=ON \ + -DHiPACE_MPI=ON \ + -DHiPACE_COMPUTE=OMP \ + -DHiPACE_OPENPMD=ON \ + -DHiPACE_PRECISION=SINGLE + +cmake --build ${REPO_DIR}/build_clang_tidy -j ${PARALLEL} 2> ${REPO_DIR}/build_clang_tidy/clang-tidy.log + +if [ -s ${REPO_DIR}/build_clang_tidy/clang-tidy.log ]; then + echo + echo "clang-tidy found the following issues:" + echo + cat ${REPO_DIR}/build_clang_tidy/clang-tidy.log + echo + echo "=============================================" + exit 1 +else + echo + echo "clang-tidy has not found any issue." + echo + echo "=============================================" + exit 0 +fi