C++;mac OS X El Capitan上的openmp,Valgrind错误(数据竞争) 我在C++下用OpNIMP库在Mac上测试一个简单的HelordRD,通过终端使用以下两个命令: /usr/local/bin/clang++-omp -fopenmp helloworld.cpp -o test /usr/local/bin/valgrind --tool=helgrind --log-file=a.log ./test
输出正确:C++;mac OS X El Capitan上的openmp,Valgrind错误(数据竞争) 我在C++下用OpNIMP库在Mac上测试一个简单的HelordRD,通过终端使用以下两个命令: /usr/local/bin/clang++-omp -fopenmp helloworld.cpp -o test /usr/local/bin/valgrind --tool=helgrind --log-file=a.log ./test,c++,macos,openmp,valgrind,C++,Macos,Openmp,Valgrind,输出正确: warning: no debug symbols in executable (-arch x86_64) Hello World from thread = 0 Hello World from thread = 1 Hello World from thread = 3 Hello World from thread = 2 Number of threads = 4 但是日志文件(a.log)包含:“来自231个上下文的174986个错误”(作为错误摘要) 以下是日志文件的
warning: no debug symbols in executable (-arch x86_64)
Hello World from thread = 0
Hello World from thread = 1
Hello World from thread = 3
Hello World from thread = 2
Number of threads = 4
但是日志文件(a.log)包含:“来自231个上下文的174986个错误”(作为错误摘要)
以下是日志文件的一部分:
==643== ---Thread-Announcement------------------------------------------
==643==
==643== Thread #1 is the program's root thread
==643==
==643== ----------------------------------------------------------------
==643==
==643== Possible data race during read of size 4 at 0x10057C118 by thread #3
==643== Locks held: none
==643== at 0x10055D1F4: spin_lock (in /usr/lib/system/libsystem_platform.dylib)
==643== by 0x10057092D: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==643== by 0x10056E384: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==643==
==643== This conflicts with a previous write of size 4 by thread #1
==643== Locks held: none
==643== at 0x10055D200: spin_unlock (in /usr/lib/system/libsystem_platform.dylib)
==643== by 0x1000434B0: __kmp_create_worker (in /usr/local/Cellar/libiomp/20150701/lib/libiomp5.dylib)
==643== by 0x100031E3D: __kmp_allocate_thread (in /usr/local/Cellar/libiomp/20150701/lib/libiomp5.dylib)
==643== by 0x10002E7A1: __kmp_allocate_team (in /usr/local/Cellar/libiomp/20150701/lib/libiomp5.dylib)
==643== by 0x10002FA2D: __kmp_fork_call (in /usr/local/Cellar/libiomp/20150701/lib/libiomp5.dylib)
==643== by 0x100027F0D: __kmpc_fork_call (in /usr/local/Cellar/libiomp/20150701/lib/libiomp5.dylib)
==643== by 0x100000CE8: main (in ./test)
==643== Address 0x10057c118 is in the Data segment of /usr/lib/system/libsystem_pthread.dylib
“helloworld”的代码是:
#包括
#包括
#包括
intmain(intargc,char*argv[]){
int-nthreads=4,tid;
#pragma omp并行num_线程(nthreads)私有(tid)
{
//获取线程数
tid=omp_get_thread_num();
printf(“Hello World from thread=%d\n”,tid);
//只有主线程才能执行此操作
如果(tid==0)
{
printf(“线程数=%d\n”,n个线程);
}
}
返回0;
}
有人知道这些错误(数据竞赛)吗?这些线程之间没有共享数据。这很可能是误报。有关libgomp的相同问题,请参见示例a。这也可能是libiomp/pthread实现中的实际问题,但这似乎不大可能 你似乎无能为力。通常,如果堆栈顶部位于库中,则可能是误报或库中的错误,或者您误用了它(例如,在来自多线程的缓冲区上运行
memcpy
)
如果二进制文件位于堆栈的顶部,则更明显的是代码存在问题
你的代码很好。数据竞赛。这绝对是误报
libiomp
实现Valgrind无法正确识别的自己的同步原语。毕竟,锁是使用不受保护但在硬件级别对共享变量进行控制的访问来实现的。
#include <stdio.h>
#include <libiomp/omp.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
int nthreads=4, tid;
#pragma omp parallel num_threads(nthreads) private(tid)
{
//Obtain thread number
tid = omp_get_thread_num();
printf("Hello World from thread = %d\n", tid);
// Only master thread does this
if (tid == 0)
{
printf("Number of threads = %d\n", nthreads);
}
}
return 0;
}