繁体   English   中英

C++中的OpenMP分段错误

[英]OpenMP Segmentation Fault in C++

我有一个非常简单的 function,它计算N × N二维矩阵(由指针arr表示)的内部条目数低于某个阈值,并更新通过引用传递的计数器below_threshold

void count(float *arr, const int N, const float threshold, int &below_threshold) {
    below_threshold = 0;  // make sure it is reset
    bool comparison;
    float temp;
    
    #pragma omp parallel for shared(arr, N, threshold) private(temp, comparison) reduction(+:below_threshold)
    for (int i = 1; i < N-1; i++)  // count only the inner N-2 rows
    {
        for (int j = 1; j < N-1; j++)  // count only the inner N-2 columns
        {
            temp = *(arr + i*N + j);
            comparison = (temp < threshold);
            below_threshold += comparison;
        }
    }
}

当我不使用 OpenMP 时,它运行良好(因此,分配和初始化已经正确完成)。

当我使用N小于 40000 左右的 OpenMP 时,它运行良好。

但是,一旦我开始在 OpenMP 中使用更大的N ,它就会不断给我一个分段错误(我目前正在测试N = 50000 ,并希望最终达到 ~100000)。

这在软件层面有什么问题吗?


PS 分配是动态完成的( float *arr = new float [N*N] ),这里是用于随机初始化整个矩阵的代码,对于大N的 OpenMP 没有任何问题:

void initialize(float *arr, const int N)
{
    #pragma omp parallel for
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            *(arr + i*N + j) = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
        }
    }

}

更新:

我已经尝试将ijN更改为long long int ,但它仍然没有修复我的分段错误。 如果这是问题所在,为什么它在没有 OpenMP 的情况下也能正常工作? 只有一次我添加#pragma omp...它失败了。

我想,这是因为,你的值(50000*50000 = 2500000000)在c++达到了INT_MAX (2147483647)以上。结果,数组访问行为将是未定义的。

因此,您应该使用UINT_MAX或适合您的用例的其他一些类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM