MPI_Reduce()程序得到分段错误

时间:2017-10-17 13:57:51

标签: c segmentation-fault mpi

我开始使用C和OpenMPI库开始并行编程。所以现在一切看起来都太复杂了。

我正在尝试执行单个程序多个数据: 主: - 初始化一个数组 - 细分吧 - 将相同大小的位发送到不同的进程(从属) 从站: - 进程更改数组值并计算所有新元素的总和 - 将更改的阵列发送回主站 主: - 进行集体沟通以收集和总和新值的总和 - 打印接收到的每个新数组的五个第一个元素以及新值的全局总和。 - 打印时间。

这就是我写的:

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#define ARRAYSIZE   16000000

int main (int argc, char *argv[]) {
    MPI_Init(&argc, &argv);
    int myrank;
    char name[100];
    int result;
    int size = 0;
    int number;
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
    MPI_Request request;
    int buffer;
    int count;
    double t1, t2;
    MPI_Comm_size (MPI_COMM_WORLD,&size);
    int cells = ARRAYSIZE/size;
    float array[cells];

    if (myrank == 0) {
        t1 = MPI_Wtime();
        t2 = MPI_Wtime();
        MPI_Get_processor_name(name, &result);

        // ********************** INICIALIZANDO ARRAY **********************
        int     i;          /* loop variable */
        float   data[ARRAYSIZE];    /* the intial array */

        printf("Starting serial array example...\n");
        printf("Using array of %d floats. Requires %ld bytes\n",ARRAYSIZE,sizeof(data));

        /* Initialize the array */
        printf("Initializing array...\n");
        for(i=0; i<ARRAYSIZE; i++)
          data[i] =  i * 1.0;

        /* Print a few sample results */
        printf("Sample results\n");
        printf("   data[1]=%e\n",  data[1]);
        printf("   data[100]=%e\n",  data[100]);
        printf("   data[1000]=%e\n",  data[1000]);
        printf("   data[10000]=%e\n",  data[10000]);
        printf("   data[100000]=%e\n",  data[100000]);
        printf("   data[1000000]=%e\n",  data[1000000]);
        printf("\nAll Done!\n");
        // ********************** ARRAY INICIALIZADO **********************

        MPI_Comm_size (MPI_COMM_WORLD,&size);
        printf("Total of tasks: %d", size);
        printf("Each task process %d cells", ARRAYSIZE/size);
        int cells = ARRAYSIZE/size;
        int id_task;
        for(id_task = 0; id_task < size; id_task++) {
            //float array[cells];
            int i=0;
            for(i=0; i<cells; i++)
                array[i] =  i * (id_task+1.0);
            MPI_Send(&array[id_task*cells], cells, MPI_FLOAT, id_task, 0, MPI_COMM_WORLD);
        }

        printf("master: %d at processor: %s\n",myrank, name);

    }
    MPI_Recv(array, cells, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    int i;
    float resultado;
    for(i=0; i<cells; i++)
        array[i] =  i * (myrank+1.0);

    if(myrank!=0){
        MPI_Send(array, cells, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
    }

    MPI_Reduce(&array, &resultado, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);

    if (myrank == 0) {
        int j;
        for(j=0; j<cells; j++){
            for(i=0; i<5; i++){
                printf("| %lf ",array[i*j]);
            }
        }
        //t1 = MPI_Wtime();
        t2 = MPI_Wtime();
        MPI_Get_processor_name(name, &result);
        printf("master: %d at processor: %s\ntime: %lf\n",myrank, name,t2-t1);
    }
    MPI_Finalize();

}

但我得到的只是一个&#34;分段错误错误&#34;当我运行它。我已阅读细分错误问题但无法诊断我的代码发生的原因。

提前致谢。 (我很抱歉我的写作不好,英语不是我的第一语言)

更新:我包含了一个malloc和一个免费的但是在运行时我仍然有&#34; mpirun noticed that process rank 0 with PID 0 on node Brunos-Air exited on signal 11 (Segmentation fault: 11).&#34;

如果我尝试&#34; $HOME/opt/usr/local/bin/mpicc -o -Wall -Wextra -g programa3-r1 ./programa3-r1.c&#34;它给出了:&#34; ld:无法链接主要的可执行文件&#39; programa3-r1&#39;对于体系结构x86_64 clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)&#34;

2 个答案:

答案 0 :(得分:2)

for(id_task = 0; id_task < size; id_task++) {
    //float array[cells];
    int i=0;
    for(i=0; i<cells; i++)
        array[i] =  i * (id_task+1.0);
    MPI_Send(&array[id_task*cells], cells, MPI_FLOAT, id_task, 0, MPI_COMM_WORLD);
}

您应该MPI_Send() array(又名&array[0])而不是&array[id_task*cells]。这可能是崩溃的根本原因。

请注意,从MPI的角度来看,您的程序不正确,因为任务0 MPI_Send()为自身,然后是MPI_Recv()。对于消息,这可能会很好用,并且消息会死锁。 small long 依赖于您的MPI库(使用的互连),因此您必须避免这种情况。

if(myrank!=0){
    MPI_Send(array, cells, MPI_FLOAT, 0, 0, MPI_COMM_WORLD);
}

在排名0上没有匹配的MPI_Recv(),所以这也可能是死锁。

MPI_Reduce(&array, &resultado, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);

从所有等级中总结array[0],并将结果存储在等级0的resultado中。我不确定这是否真的是你的想法(但我不知道你在尝试什么实现,所以可能没问题。)

答案 1 :(得分:1)

你很可能是stack overflow

#define ARRAYSIZE   16000000

稍后您宣布本地 automatic variable(在call stack上)

    float   data[ARRAYSIZE];    /* the intial array */

这是不合理。典型的调用堆栈帧最多应该有几千字节(因为整个调用堆栈通常限制在几兆字节或更少)。你想要一个64Mbyte。您应该使用C dynamic memory allocation,因此请声明float *data = NULL;并在适当的位置使用calloc or malloc(以及更晚的free;您应该避免memory leaks)。不要忘记check against failure of calloc

编译所有警告&amp;调试信息:gcc -Wall -Wextra -g然后使用调试器 gdb检查堆栈溢出是否发生。

阅读有关GCC command options的文档。您可能还想使用-Wstack-usage=2048

之类的内容