线程创建失败:资源暂时不可用

时间:2017-03-09 07:58:19

标签: c++ openmp

我正在使用openMP运行这个C ++代码。

#include <float.h>
#include <omp.h>
#include <iostream>

double fRand(double fMin, double fMax);

int main(int argc, char* argv[])
{

int columns = 20;
int rows = 10000;

//Create 2D array table
double **table;
table = (double**) malloc( rows * sizeof( double *) );
for(int row=0; row<rows; row++) {
    table[row] = (double*) malloc( columns * sizeof( double ) );
}

//Generate random numbers
for(int i=0; i<rows; i++) {
    for(int j=0; j<columns; j++) {
        table[i][j]=fRand(0,1000);
    }
}

//Create 2D array P
float **P;
P = (float**) malloc(rows * sizeof(float*));
for(int row=0; row<rows; row++) {
    P[row] = (float*) malloc(rows * sizeof(float));
}

//Compute P elements
#pragma omp parallel for schedule(dynamic)
for(int i=0; i<rows; i++) {
    for(int j=i+1; j<rows; j++) {
        double d = 0;
        for(int k=0; k<columns; k++) {
            double df = table[i][k] - table[j][k];
            d += df * df;
        }
        P[i][j] = (float) d;
    }
}

std::cout << "Finish";

}

double fRand(double fMin, double fMax)
{
     double f = (double)rand() / RAND_MAX;
     return fMin + f * (fMax - fMin);
}

当行数很小(~10000)时,它运行正常并且正在使用所有核心。但是,当运行具有更多行的算法(例如rows = 120000)时,它会崩溃并且我收到以下错误:

libgomp: Thread creation failed: Resource temporarily unavailable

我尝试增加OMP_STACKSIZE,但错误仍然存​​在。 我正在使用Windows 10 64位。我有128Gb的RAM和CPU有32个核心。 我正在使用以下命令编译代码:

 g++ -std=c++11 -fopenmp myfile.cpp

0 个答案:

没有答案