0

以下最小示例计算从 1 到 1000 的所有数字的总和,并与 OpenMP 并行化:

#include <iostream>

double sum;

void do_it() {
  const size_t n = 1000;
#pragma omp parallel
  {
#pragma omp for
    for (size_t i = 1; i <= n; ++i) {
#pragma omp atomic
      sum += static_cast<double>(i);
    }
  }
}

int main() {
  sum = 0.;
  do_it();
  std::cout << sum << std::endl;
  return 0;
}

我尝试使用clang++-6.0.0andg++-5.4.0和 ThreadSanitizer 来编译它。libomp.so两个编译器都会在/中产生一些关于竞争条件的警告libgomp.so,我假设这是误报,以及关于我的代码的以下警告:

==================
WARNING: ThreadSanitizer: data race (pid=22081)
  Read of size 8 at 0x000001555f48 by main thread:
    #0 main /home/arekfu/src/foo/openmp.cc:20 (openmp+0x4be0ce)

  Previous atomic write of size 8 at 0x000001555f48 by thread T11:
    #0 __tsan_atomic64_compare_exchange_val ??:? (openmp+0x476470)
    #1 .omp_outlined._debug__ /home/arekfu/src/foo/openmp.cc:12 (openmp+0x4be011)
    #2 .omp_outlined. /home/arekfu/src/foo/openmp.cc:8 (openmp+0x4be011)
    #3 __kmp_invoke_microtask ??:? (libomp.so.5+0x994b2)

  Location is global '<null>' at 0x000000000000 (openmp+0x000001555f48)

  Thread T11 (tid=22093, running) created by main thread at:
    #0 pthread_create ??:? (openmp+0x4284db)
    #1 __kmpc_threadprivate_register_vec ??:? (libomp.so.5+0x5bc1f)
    #2 __libc_start_main /build/glibc-LK5gWL/glibc-2.23/csu/../csu/libc-start.c:291 (libc.so.6+0x2082f)

SUMMARY: ThreadSanitizer: data race /home/arekfu/src/foo/openmp.cc:20 in main
==================

不过,我在我的代码中看不到任何数据竞争!

我也尝试用atomic一个部分替换更新critical,如下所示:

#pragma omp critical
      {
        sum += static_cast<double>(i);
      }

这改变了警告,但新的警告没有多大意义:

==================
WARNING: ThreadSanitizer: data race (pid=27477)
  Write of size 8 at 0x000001555f48 by thread T4:
    #0 .omp_outlined._debug__ /home/arekfu/src/foo/openmp.cc:13 (openmp+0x4be0a2)
    #1 .omp_outlined. /home/arekfu/src/foo/openmp.cc:8 (openmp+0x4be0a2)
    #2 __kmp_invoke_microtask ??:? (libomp.so.5+0x994b2)

  Previous write of size 8 at 0x000001555f48 by thread T3:
    #0 .omp_outlined._debug__ /home/arekfu/src/foo/openmp.cc:13 (openmp+0x4be0a2)
    #1 .omp_outlined. /home/arekfu/src/foo/openmp.cc:8 (openmp+0x4be0a2)
    #2 __kmp_invoke_microtask ??:? (libomp.so.5+0x994b2)

  Location is global '<null>' at 0x000000000000 (openmp+0x000001555f48)

  Thread T4 (tid=27482, running) created by main thread at:
    #0 pthread_create ??:? (openmp+0x42857b)
    #1 __kmpc_threadprivate_register_vec ??:? (libomp.so.5+0x5bc1f)
    #2 __libc_start_main /build/glibc-LK5gWL/glibc-2.23/csu/../csu/libc-start.c:291 (libc.so.6+0x2082f)

  Thread T3 (tid=27481, running) created by main thread at:
    #0 pthread_create ??:? (openmp+0x42857b)
    #1 __kmpc_threadprivate_register_vec ??:? (libomp.so.5+0x5bc1f)
    #2 __libc_start_main /build/glibc-LK5gWL/glibc-2.23/csu/../csu/libc-start.c:291 (libc.so.6+0x2082f)

SUMMARY: ThreadSanitizer: data race /home/arekfu/src/foo/openmp.cc:13 in .omp_outlined._debug__
==================

这些警告是真实数据竞争的迹象,还是误报?

4

1 回答 1

0

“问题”是第 20 行 sum 的读取操作:

std::cout << sum << std::endl; // here you are reading the value of sum

TSAN 无法推断此读取与循环中的(原子)更新之间的线程间发生前关系。但是当然存在这样的关系,因为所有线程都在 omp 块的末尾同步。所以是的,这是一个误报。

这篇文章提供了更多关于如何避免 OpenMP 误报的信息:我可以将 Thread Sanitizer 用于 OpenMP 程序吗?

于 2020-01-27T10:42:39.123 回答