Can I use Thread Sanitizer for OpenMP programs?

前端 未结 2 338
不思量自难忘°
不思量自难忘° 2020-12-16 18:58

Consider the following example:

#include  

int main () {
    int i = 0;
    #pragma omp parallel
    {
        #pragma omp critical
                 


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 19:47

    Yes, at least with Clang this is relatively easy. You'll need to build libomp (which Clang uses instead of libgomp) with ThreadSanitizer support. This doesn't take that long:

    git clone https://github.com/llvm/llvm-project
    cd llvm-project
    mkdir build
    cd build
    cmake -DLIBOMP_TSAN_SUPPORT=1 ../openmp
    sudo cmake --build . --target install
    

    (sudo and --target install is optional if you adjust the path to libomp.so below)

    Now running your example works without any errors if you use this libomp.so instead of the system one:

    clang++ -fsanitize=thread -fopenmp main.cpp
    env LD_PRELOAD=/usr/local/lib/libomp.so ./a.out
    

提交回复
热议问题