According to Can I use Thread Sanitizer for OpenMP programs?, I konw Tsan doesn't support well for Openmp and I have resolved this issue according to the official documentation https://github.com/llvm/llvm-project/tree/main/openmp/tools/archer by using llvm-archer. But there is a question: I only can use clang++ to solve false positive, but g++ can't. min_example_code:
parallel.h
#include "omp.h"
#include <vector>
namespace nio {
namespace parallel {
#define NUM_THREADS 3
template<typename ITER, typename FN>
void foreach(ITER beg, ITER end, FN fn) {
#pragma omp parallel for num_threads(NUM_THREADS)
for (auto i = beg; i != end; ++i) {
fn(*i);
}
}
template <typename FN>
void foreach(int n, FN fn) {
#pragma omp parallel for num_threads(NUM_THREADS)
for (auto i = 0; i < n; ++i) {
fn(i);
}
}
} // namespace parallel
} // namespace nio
main.cpp
#include "parallel.h"
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
const int N = 1e8;
int main(){
std::vector<int> pre_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> curr_numbers;
auto Square = [&](size_t i) {
auto &input = pre_numbers[i];
input *= input;
};
auto Zero = [&](size_t i) {
auto &input = curr_numbers[i];
input = 0;
};
auto Print = [&](size_t i) {
auto &input = curr_numbers[i];
std::cout << input << " ";
};
{
nio::parallel::foreach (pre_numbers.size(), Square);
}
{
for(const auto& input : pre_numbers) {
curr_numbers.emplace_back(input);
}
}
{
nio::parallel::foreach (curr_numbers.size(), Zero);
}
{
nio::parallel::foreach (curr_numbers.size(), Print);
}
return 0;
}
environment set:
sudo ln -s /usr/local/llvm/lib/libomp.so /usr/lib/libomp.so
export OMP_TOOL_LIBRARIES=/usr/local/llvm/lib/libarcher.so
export TSAN_OPTIONS='ignore_noninstrumented_modules=1'
clang++ command:
clang++ main.cpp -fopenmp -fsanitize=thread -g
./a.out
g++ command:
g++ main.cpp -fopenmp -fsanitize=thread -g
export LD_PRELOAD=$LD_PRELOAD:/lib/x86_64-linux-gnu/libtsan.so.0
./a.out
I am sure clang++ can support Tsan to understand Openmp, but g++ can't. And I must use g++ to compile my code, it's so sad:-( If you have some good solutions, please tell me. Have a good day, eveyone:D