c - 二维数组的 MPI_Gather 段错误

标签 c parallel-processing mpi

我在 C 语言的 MPI 代码中遇到问题。

我想我创建了一个很好的算法来处理二维数组的双循环。但是,当我尝试使用 MPI_Gather 从进程中收集数据时,出现段错误。这是代码:

#define NN 4096
#define NM 4096

double global[NN][NM];

void range(int n1, int n2, int nprocs, int irank, int *ista, int *iend){
    int iwork1;
    int iwork2;
    iwork1 = ( n2 - n1 + 1 ) / nprocs;
    iwork2 = ( ( n2 - n1 + 1 ) % nprocs );
    *ista = irank * iwork1 + n1 + fmin(irank, iwork2);
    *iend = *ista + iwork1 - 1;
    if ( iwork2 > irank ) 
        iend = iend + 1;
}

void runCalculation(int n, int m, int argc, char** argv)
{
    const int iter_max = 1000;

    const double tol = 1.0e-6;
    double error     = 1.0;

    int rank, size;
    int start, end;

    MPI_Init( &argc, &argv );

    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    MPI_Comm_size( MPI_COMM_WORLD, &size );

    if (size != 16) MPI_Abort( MPI_COMM_WORLD, 1 );

    memset(global, 0, n * m * sizeof(double));

    if(rank == 0){
        for (int j = 0; j < n; j++)
        {
            global[j][0] = 1.0;
        }
    }

    int iter = 0;

    while ( error > tol && iter < iter_max )
    {
        error = 0.0;

        MPI_Bcast(global, NN*NM, MPI_DOUBLE, 0, MPI_COMM_WORLD); 

        if(iter == 0)
            range(1, n, size, rank, &start, &end);

        int size = end - start;

        double local[size][NM];
        memset(local, 0, size * NM * sizeof(double));

        for( int j = 1; j < size - 1; j++)
        {   
            for( int i = 1; i < m - 1; i++ )
            {   
                local[j][i] = 0.25 * ( global[j][i+1] + global[j][i-1]
                                + global[j-1][i] + global[j+1][i]);
                error = fmax( error, fabs(local[j][i] - global[j][i]));
            }
        }

        MPI_Gather(&local[0][0], size*NM, MPI_DOUBLE, &global[0][0], NN*NM, MPI_DOUBLE, 0, MPI_COMM_WORLD);

        printf("%d\n", iter);

        if(iter % 100 == 0) 
            printf("%5d, %0.6f\n", iter, error);

        iter++;
    }

    MPI_Finalize();

}

我用 4096x4096 数组运行它。对于进程等级 0,它会在 MPI_Gather 行创建段错误。我检查了本地数组的大小是否合适,我认为它工作得很好。

编辑:添加了本地初始化行。新的段错误:

*** Process received signal ***
Signal: Segmentation fault (11)
Signal code: Address not mapped (1)
Failing at address: 0x10602000
--------------------------------------------------------------------------
mpirun noticed that process rank 0 with PID 19216 on machine_name exited on signal 11 (Segmentation fault).

最佳答案

MPI_Gatherrecvcount 参数表示它从每个进程接收的项目数,而不是它接收的项目总数。

MPI_Gather(&local[0][0], size*NM, MPI_DOUBLE, &global[0][0], NN*NM, MPI_DOUBLE, 0, MPI_COMM_WORLD);

应该是:

MPI_Gather(&local[0][0], size*NM, MPI_DOUBLE, &global[0][0], size*NM, MPI_DOUBLE, 0, MPI_COMM_WORLD);

关于c - 二维数组的 MPI_Gather 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993214/

相关文章:

parallel-processing - Rust 如何处理分布式内存并行性?

c - 为什么openmp 32线程比1线程慢得多?

在 C 中检查链表是否为空

c++ - C 中的全局变量不连续

algorithm - 排序和负载均衡

python - 如何在类中并行化 python 中的 for?

c - 如何使用 OpenMP 在一段时间内并行化代码

c - 打印 mpiexec 参数而不是正在执行的程序的参数

c - 指针错误

c - 终止进程树(Windows 的 C)