与竞赛条件相平行的-fsanitize =线程

发布于 2025-01-31 07:20:19 字数 7997 浏览 3 评论 0原文

我正在使用OpenMP进行一些基本的多处理计算,并且使用消毒器编译时,非常简单的代码正在生成数据竞赛,但是一旦删除了消毒剂,似乎可以与任何数量的线程正确使用。我将其简化为MWE:

#include <omp.h>
#include <iostream>
#include <cmath>
#include <cstdlib>

void fill(double * array, int size);
double suma(const double * array, int size);

int main(int argc, char *argv[])
{
  const int N = std::atoi(argv[1]);
  double * data = new double [N];

  fill(data, N);
  std::cout << data[0] << "\n";

  double total = suma(data, N);
  std::cout << total/N << "\n";

  delete [] data;

  return 0;
}

void fill(double * array, int size)
{
#pragma omp parallel for
  for(int ii = 0; ii < size; ii++) {
      array[ii] = 2*ii*std::sin(std::sqrt(ii/56.7)) +
        std::cos(std::pow(1.2*ii, 0.3));
  }
}

double suma(const double * array, int size)
{
  double val = 0.0;
#pragma omp parallel for reduction(+:val)
  for(int ii = 0; ii < size; ii++) {
    val += array[ii];
  }
  return val;
}

我像

g++ -g -fopenmp -fsanitize=thread mwe.cpp

G ++版本11.2.0一样对其进行了编译,然后执行它,

OMP_NUM_THREADS=2 ./a.out 10000000 1

然后得到消毒者错误,例如:

==================                                                                                                                                                                                                                      
WARNING: ThreadSanitizer: data race (pid=26332)                                                                                                                                                                                         
  Write of size 4 at 0x7ffd2ecd3e90 by main thread:                                                                                                                                                                                     
    #0 suma(double const*, int) mwe.cpp:37 (a.out+0x4014ac)                                                                                                                    
    #1 main mwe.cpp:17 (a.out+0x401380)                                                                                                                                        
                                                                                                                                                                                                                                        
  Previous read of size 8 at 0x7ffd2ecd3e90 by thread T1:                                                                                                                                                                               
    [failed to restore the stack]                                                                                                                                                                                                       
                                                                                                                                                                                                                                        
  Location is stack of main thread.                                                                                                                                                                                                     
                                                                                                                                                                                                                                        
  Location is global '<null>' at 0x000000000000 ([stack]+0x00000001ee90)                                                                                                                                                                
                                                                                                                                                                                                                                        
  Thread T1 (tid=26334, running) created by main thread at:                                                                                                                                                                             
    #0 pthread_create <null> (libtsan.so.0+0x5fdc5)                                                                                                                                                                                     
    #1 <null> <null> (libgomp.so.1+0x1d99b)                                                                                                                                                                                             
    #2 main mwe.cpp:14 (a.out+0x401340)                                                                                                                                        
                                                                                                                                                                                                                                        
SUMMARY: ThreadSanitizer: data race mwe.cpp:37 in suma(double const*, int)                                                                                                     
==================                                                                                                                                                                                                                      
==================                                                                                                                                                                                                                      
WARNING: ThreadSanitizer: data race (pid=26332)                                                                                                                                                                                         
  Read of size 8 at 0x7ffd2ecd3e80 by thread T1:                                                                                                                                                                                        
    #0 suma(double const*, int) [clone ._omp_fn.0] mwe.cpp:37 (a.out+0x401766)                                                                                                 
    #1 <null> <null> (libgomp.so.1+0x1d385)                                                                                                                                                                                             
                                                                                                                                                                                                                                        
  Previous write of size 8 at 0x7ffd2ecd3e80 by main thread:                                                                                                                                                                            
    #0 suma(double const*, int) mwe.cpp:37 (a.out+0x4014be) 
    #1 main mwe.cpp:17 (a.out+0x401380)

  Location is stack of main thread.

  Location is global '<null>' at 0x000000000000 ([stack]+0x00000001ee80)

  Thread T1 (tid=26334, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x5fdc5)
    #1 <null> <null> (libgomp.so.1+0x1d99b)
    #2 main mwe.cpp:14 (a.out+0x401340)

SUMMARY: ThreadSanitizer: data race mwe.cpp:37 in suma(double const*, int) [clone ._omp_fn.0]
==================

我看不到此数据竞赛的来源来解决它。实际上,我首先使用std :: vector,结果相同。代码有什么问题?我很确定可能有其他方法可以实施我想做的事情,但是我想学习如何修复当前的方法。还是这是假阳性? 提前致谢。

编辑:具有正确消毒剂选项的标题

I am using OpenMP for some basic multiprocessing computations and a very simple code is producing data races when compiled with sanitizers but seems to work correctly with any number of threads once the sanitizer is removed. I reduce it to a MWE:

#include <omp.h>
#include <iostream>
#include <cmath>
#include <cstdlib>

void fill(double * array, int size);
double suma(const double * array, int size);

int main(int argc, char *argv[])
{
  const int N = std::atoi(argv[1]);
  double * data = new double [N];

  fill(data, N);
  std::cout << data[0] << "\n";

  double total = suma(data, N);
  std::cout << total/N << "\n";

  delete [] data;

  return 0;
}

void fill(double * array, int size)
{
#pragma omp parallel for
  for(int ii = 0; ii < size; ii++) {
      array[ii] = 2*ii*std::sin(std::sqrt(ii/56.7)) +
        std::cos(std::pow(1.2*ii, 0.3));
  }
}

double suma(const double * array, int size)
{
  double val = 0.0;
#pragma omp parallel for reduction(+:val)
  for(int ii = 0; ii < size; ii++) {
    val += array[ii];
  }
  return val;
}

I compiled it as

g++ -g -fopenmp -fsanitize=thread mwe.cpp

with g++ version 11.2.0, and then execute it as

OMP_NUM_THREADS=2 ./a.out 10000000 1

and then I get sanitizers errors like:

==================                                                                                                                                                                                                                      
WARNING: ThreadSanitizer: data race (pid=26332)                                                                                                                                                                                         
  Write of size 4 at 0x7ffd2ecd3e90 by main thread:                                                                                                                                                                                     
    #0 suma(double const*, int) mwe.cpp:37 (a.out+0x4014ac)                                                                                                                    
    #1 main mwe.cpp:17 (a.out+0x401380)                                                                                                                                        
                                                                                                                                                                                                                                        
  Previous read of size 8 at 0x7ffd2ecd3e90 by thread T1:                                                                                                                                                                               
    [failed to restore the stack]                                                                                                                                                                                                       
                                                                                                                                                                                                                                        
  Location is stack of main thread.                                                                                                                                                                                                     
                                                                                                                                                                                                                                        
  Location is global '<null>' at 0x000000000000 ([stack]+0x00000001ee90)                                                                                                                                                                
                                                                                                                                                                                                                                        
  Thread T1 (tid=26334, running) created by main thread at:                                                                                                                                                                             
    #0 pthread_create <null> (libtsan.so.0+0x5fdc5)                                                                                                                                                                                     
    #1 <null> <null> (libgomp.so.1+0x1d99b)                                                                                                                                                                                             
    #2 main mwe.cpp:14 (a.out+0x401340)                                                                                                                                        
                                                                                                                                                                                                                                        
SUMMARY: ThreadSanitizer: data race mwe.cpp:37 in suma(double const*, int)                                                                                                     
==================                                                                                                                                                                                                                      
==================                                                                                                                                                                                                                      
WARNING: ThreadSanitizer: data race (pid=26332)                                                                                                                                                                                         
  Read of size 8 at 0x7ffd2ecd3e80 by thread T1:                                                                                                                                                                                        
    #0 suma(double const*, int) [clone ._omp_fn.0] mwe.cpp:37 (a.out+0x401766)                                                                                                 
    #1 <null> <null> (libgomp.so.1+0x1d385)                                                                                                                                                                                             
                                                                                                                                                                                                                                        
  Previous write of size 8 at 0x7ffd2ecd3e80 by main thread:                                                                                                                                                                            
    #0 suma(double const*, int) mwe.cpp:37 (a.out+0x4014be) 
    #1 main mwe.cpp:17 (a.out+0x401380)

  Location is stack of main thread.

  Location is global '<null>' at 0x000000000000 ([stack]+0x00000001ee80)

  Thread T1 (tid=26334, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x5fdc5)
    #1 <null> <null> (libgomp.so.1+0x1d99b)
    #2 main mwe.cpp:14 (a.out+0x401340)

SUMMARY: ThreadSanitizer: data race mwe.cpp:37 in suma(double const*, int) [clone ._omp_fn.0]
==================

I cannot see the source of this data race to fix it. Actually, I was using std::vector first, with the same result. What could be wrong with the code? I am pretty sure that there could be alternative ways to implement what I want to do, but I want to learn how to fix the current one. Or is this a false positive?
Thanks in advance.

Edit: title with correct sanitizer option

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文