From 7d187c5bbf988f88c6bd70554cef7d0ccb86beca Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Sun, 25 May 2025 21:10:08 +0200 Subject: [PATCH 1/4] [CI] Add thread sanitizer run --- .github/workflows/ubuntu.yml | 116 +++++++++++++++++++++++++++++++++++ cmake/configure.cmake | 11 ++-- cmake/sanitizers.cmake | 4 ++ 3 files changed, 126 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 1cbc6a5b..7b08f079 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -301,6 +301,122 @@ jobs: PPC_ASAN_RUN: 1 ASAN_OPTIONS: abort_on_error=1 UBSAN_OPTIONS: halt_on_error=1 + ubuntu-clang-thread-sanitizer-build: + needs: + - ubuntu-clang-build + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: ["ubuntu-24.04"] + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Setup environment + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends -y ninja-build python3-pip \ + openmpi-bin openmpi-common libopenmpi-dev + wget https://apt.llvm.org/llvm.sh + chmod u+x llvm.sh + sudo ./llvm.sh 20 all + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ runner.os }}-clang + create-symlink: true + max-size: 1G + - name: CMake configure + run: > + cmake -S . -B build -G Ninja + -D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache + -D CMAKE_BUILD_TYPE=RELEASE -D ENABLE_THREAD_SANITIZER=ON + -D CMAKE_INSTALL_PREFIX=install + env: + CC: clang-20 + CXX: clang++-20 + - name: Build project + run: | + cmake --build build --parallel + env: + CC: clang-20 + CXX: clang++-20 + - name: Install project + run: | + cmake --build build --target install + - name: Archive installed package + run: | + tar -czvf ubuntu-clang-thread-sanitizer-install-${{ matrix.os }}.tar.gz -C install . + - name: Upload installed package + uses: actions/upload-artifact@v4 + with: + name: ubuntu-clang-thread-sanitizer-install-${{ matrix.os }} + path: ubuntu-clang-thread-sanitizer-install-${{ matrix.os }}.tar.gz + ubuntu-clang-thread-sanitizer-test: + needs: + - ubuntu-clang-thread-sanitizer-build + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: ["ubuntu-24.04"] + steps: + - uses: actions/checkout@v4 + - name: Setup environment + run: | + sudo apt-get update + sudo apt-get install --no-install-recommends -y ninja-build python3-pip valgrind \ + openmpi-bin openmpi-common libopenmpi-dev + wget https://apt.llvm.org/llvm.sh + chmod u+x llvm.sh + sudo ./llvm.sh 20 all + - name: Download installed package + uses: actions/download-artifact@v4 + with: + name: ubuntu-clang-thread-sanitizer-install-${{ matrix.os }} + - name: Extract installed package + run: | + mkdir -p install + tar -xzvf ubuntu-clang-thread-sanitizer-install-${{ matrix.os }}.tar.gz -C install + - name: Run tests (MPI) + run: python3 scripts/run_tests.py --running-type="processes" --additional-mpi-args="--oversubscribe" + env: + PPC_NUM_PROC: 2 + PPC_NUM_THREADS: 2 + PPC_ASAN_RUN: 1 + ASAN_OPTIONS: abort_on_error=1 + UBSAN_OPTIONS: halt_on_error=1 + - name: Run tests (threads, num_threads=1) + run: python3 scripts/run_tests.py --running-type="threads" + env: + PPC_NUM_PROC: 1 + PPC_NUM_THREADS: 1 + PPC_ASAN_RUN: 1 + ASAN_OPTIONS: abort_on_error=1 + UBSAN_OPTIONS: halt_on_error=1 + - name: Run tests (threads, num_threads=2) + run: python3 scripts/run_tests.py --running-type="threads" + env: + PPC_NUM_PROC: 1 + PPC_NUM_THREADS: 2 + PPC_ASAN_RUN: 1 + ASAN_OPTIONS: abort_on_error=1 + UBSAN_OPTIONS: halt_on_error=1 + - name: Run tests (threads, num_threads=3) + run: python3 scripts/run_tests.py --running-type="threads" + env: + PPC_NUM_PROC: 1 + PPC_NUM_THREADS: 3 + PPC_ASAN_RUN: 1 + ASAN_OPTIONS: abort_on_error=1 + UBSAN_OPTIONS: halt_on_error=1 + - name: Run tests (threads, num_threads=4) + run: python3 scripts/run_tests.py --running-type="threads" + env: + PPC_NUM_PROC: 1 + PPC_NUM_THREADS: 4 + PPC_ASAN_RUN: 1 + ASAN_OPTIONS: abort_on_error=1 + UBSAN_OPTIONS: halt_on_error=1 ubuntu-clang-sanitizer-test-extended: needs: - ubuntu-clang-sanitizer-test diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 802010d6..4fe0b31d 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -64,11 +64,12 @@ if(UNIX) -Wold-style-definition \ -Wmissing-prototypes") - if("${ENABLE_ADDRESS_SANITIZER}" - OR "${ENABLE_UB_SANITIZER}" - OR "${ENABLE_LEAK_SANITIZER}") - set(COMMON_COMPILER_FLAGS "${COMMON_COMPILER_FLAGS} -Wno-cast-align") - endif() + if ("${ENABLE_ADDRESS_SANITIZER}" + OR "${ENABLE_UB_SANITIZER}" + OR "${ENABLE_LEAK_SANITIZER}" + OR "${ENABLE_THREAD_SANITIZER}") + set (COMMON_COMPILER_FLAGS "${COMMON_COMPILER_FLAGS} -Wno-cast-align") + endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_COMPILER_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_COMPILER_FLAGS}") diff --git a/cmake/sanitizers.cmake b/cmake/sanitizers.cmake index d589a080..503e027d 100644 --- a/cmake/sanitizers.cmake +++ b/cmake/sanitizers.cmake @@ -15,6 +15,10 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") add_compile_options(-fsanitize=leak) add_link_options(-fsanitize=leak) message(STATUS "Enabled leak sanitizer") + if (ENABLE_THREAD_SANITIZER) + add_compile_options(-fsanitize=thread) + add_link_options(-fsanitize=thread) + message(STATUS "Enabled thread sanitizer") endif() else() message(WARNING "Sanitizers are supported on gcc and clang compilers only!") From e6f1d4516a1304a94c5fe77e7aff3e396ab1975c Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Mon, 23 Jun 2025 13:38:59 +0200 Subject: [PATCH 2/4] opt --- .github/workflows/ubuntu.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 7b08f079..6d9db67c 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -385,6 +385,7 @@ jobs: PPC_ASAN_RUN: 1 ASAN_OPTIONS: abort_on_error=1 UBSAN_OPTIONS: halt_on_error=1 + TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=1) run: python3 scripts/run_tests.py --running-type="threads" env: @@ -393,6 +394,7 @@ jobs: PPC_ASAN_RUN: 1 ASAN_OPTIONS: abort_on_error=1 UBSAN_OPTIONS: halt_on_error=1 + TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=2) run: python3 scripts/run_tests.py --running-type="threads" env: @@ -401,6 +403,7 @@ jobs: PPC_ASAN_RUN: 1 ASAN_OPTIONS: abort_on_error=1 UBSAN_OPTIONS: halt_on_error=1 + TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=3) run: python3 scripts/run_tests.py --running-type="threads" env: @@ -409,6 +412,7 @@ jobs: PPC_ASAN_RUN: 1 ASAN_OPTIONS: abort_on_error=1 UBSAN_OPTIONS: halt_on_error=1 + TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=4) run: python3 scripts/run_tests.py --running-type="threads" env: @@ -417,6 +421,7 @@ jobs: PPC_ASAN_RUN: 1 ASAN_OPTIONS: abort_on_error=1 UBSAN_OPTIONS: halt_on_error=1 + TSAN_OPTIONS: ignore_noninstrumented_modules=1 ubuntu-clang-sanitizer-test-extended: needs: - ubuntu-clang-sanitizer-test From 076c9a112913b5f06ddca21f398210e0bf01befb Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Mon, 23 Jun 2025 14:04:33 +0200 Subject: [PATCH 3/4] remove extra flags for tsan --- .github/workflows/ubuntu.yml | 10 ---------- cmake/sanitizers.cmake | 1 + 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 6d9db67c..8512d978 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -383,8 +383,6 @@ jobs: PPC_NUM_PROC: 2 PPC_NUM_THREADS: 2 PPC_ASAN_RUN: 1 - ASAN_OPTIONS: abort_on_error=1 - UBSAN_OPTIONS: halt_on_error=1 TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=1) run: python3 scripts/run_tests.py --running-type="threads" @@ -392,8 +390,6 @@ jobs: PPC_NUM_PROC: 1 PPC_NUM_THREADS: 1 PPC_ASAN_RUN: 1 - ASAN_OPTIONS: abort_on_error=1 - UBSAN_OPTIONS: halt_on_error=1 TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=2) run: python3 scripts/run_tests.py --running-type="threads" @@ -401,8 +397,6 @@ jobs: PPC_NUM_PROC: 1 PPC_NUM_THREADS: 2 PPC_ASAN_RUN: 1 - ASAN_OPTIONS: abort_on_error=1 - UBSAN_OPTIONS: halt_on_error=1 TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=3) run: python3 scripts/run_tests.py --running-type="threads" @@ -410,8 +404,6 @@ jobs: PPC_NUM_PROC: 1 PPC_NUM_THREADS: 3 PPC_ASAN_RUN: 1 - ASAN_OPTIONS: abort_on_error=1 - UBSAN_OPTIONS: halt_on_error=1 TSAN_OPTIONS: ignore_noninstrumented_modules=1 - name: Run tests (threads, num_threads=4) run: python3 scripts/run_tests.py --running-type="threads" @@ -419,8 +411,6 @@ jobs: PPC_NUM_PROC: 1 PPC_NUM_THREADS: 4 PPC_ASAN_RUN: 1 - ASAN_OPTIONS: abort_on_error=1 - UBSAN_OPTIONS: halt_on_error=1 TSAN_OPTIONS: ignore_noninstrumented_modules=1 ubuntu-clang-sanitizer-test-extended: needs: diff --git a/cmake/sanitizers.cmake b/cmake/sanitizers.cmake index 503e027d..17c8772e 100644 --- a/cmake/sanitizers.cmake +++ b/cmake/sanitizers.cmake @@ -15,6 +15,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") add_compile_options(-fsanitize=leak) add_link_options(-fsanitize=leak) message(STATUS "Enabled leak sanitizer") + endif() if (ENABLE_THREAD_SANITIZER) add_compile_options(-fsanitize=thread) add_link_options(-fsanitize=thread) From 6478e117ed6cba538cc4b117a5551b3bcee99964 Mon Sep 17 00:00:00 2001 From: Arseniy Obolenskiy Date: Mon, 23 Jun 2025 16:34:06 +0200 Subject: [PATCH 4/4] fix reduction --- cmake/configure.cmake | 12 ++++++------ tasks/example_threads/omp/src/ops_omp.cpp | 10 ++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 4fe0b31d..277c53b3 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -64,12 +64,12 @@ if(UNIX) -Wold-style-definition \ -Wmissing-prototypes") - if ("${ENABLE_ADDRESS_SANITIZER}" - OR "${ENABLE_UB_SANITIZER}" - OR "${ENABLE_LEAK_SANITIZER}" - OR "${ENABLE_THREAD_SANITIZER}") - set (COMMON_COMPILER_FLAGS "${COMMON_COMPILER_FLAGS} -Wno-cast-align") - endif() + if("${ENABLE_ADDRESS_SANITIZER}" + OR "${ENABLE_UB_SANITIZER}" + OR "${ENABLE_LEAK_SANITIZER}" + OR "${ENABLE_THREAD_SANITIZER}") + set(COMMON_COMPILER_FLAGS "${COMMON_COMPILER_FLAGS} -Wno-cast-align") + endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_COMPILER_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_COMPILER_FLAGS}") diff --git a/tasks/example_threads/omp/src/ops_omp.cpp b/tasks/example_threads/omp/src/ops_omp.cpp index ee3f42df..159f109d 100644 --- a/tasks/example_threads/omp/src/ops_omp.cpp +++ b/tasks/example_threads/omp/src/ops_omp.cpp @@ -36,11 +36,13 @@ bool NesterovATestTaskOMP::RunImpl() { const int num_threads = ppc::util::GetNumThreads(); GetOutput() *= num_threads; - std::atomic counter(0); -#pragma omp parallel default(none) shared(counter) num_threads(ppc::util::GetNumThreads()) - counter++; + std::atomic counter{0}; +#pragma omp parallel default(none) shared(counter) num_threads(num_threads) + { + counter.fetch_add(1, std::memory_order_relaxed); + } - GetOutput() /= counter; + GetOutput() /= counter.load(std::memory_order_relaxed); return GetOutput() > 0; }