简体   繁体   English

信号:MPI C ++中的分段错误(11)

[英]Signal: Segmentation fault (11) in MPI C++

I have a code, which counts the average value of integers in MPI: 我有一个代码,该代码计算MPI中整数的平均值:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#include <assert.h>

// Average method
int compute_avg(int *array, int num_elements) {
    int sum = 0;
    int i;
    for (i = 0; i < num_elements; i++) {
    sum += array[i];
    }
    return sum / num_elements;
}

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

    if (argc != 2) {
    fprintf(stderr, "Usage: avg num_elements_per_proc\n");
    exit(1);
    }
    int num_elements_per_proc = atoi(argv[1]);

    MPI_Init(NULL, NULL);
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Create array with integers
    int *nums = NULL;
    if (world_rank == 0) {

        for (int i =0; i<5; i++){
            nums[i] = i;
        }
    }


// Subtable from any processes
    int *sub_nums = (int *)malloc(sizeof(int) * num_elements_per_proc);
    assert(sub_nums != NULL);


// distribution numbers for all processes
    MPI_Scatter(nums, num_elements_per_proc, MPI_INT, sub_nums,
    num_elements_per_proc, MPI_INT, 0, MPI_COMM_WORLD);

// Count avg subtable
    int sub_avg = compute_avg(sub_nums, num_elements_per_proc);

// Collectiong averages
    int *sub_avgs = NULL;
    if (world_rank == 0) {
    sub_avgs = (int *)malloc(sizeof(int) * world_size);
    assert(sub_avgs != NULL);
    }
    MPI_Gather(&sub_avg, 1, MPI_INT, sub_avgs, 1, MPI_INT, 0, MPI_COMM_WORLD);


// Calculates the overall average
    if (world_rank == 0) {
    int avg = compute_avg(sub_avgs, world_size);
    printf("Avg of all elements is %d\n", avg);
// Obliczenie średniej na danych oryginalnych i wyświetlenie.
    int original_data_avg =
    compute_avg(nums, num_elements_per_proc * world_size);
    printf("Avg computed across original data is %d\n", original_data_avg);
}

// free memory
    if (world_rank == 0) {
    free(nums);
    free(sub_avgs);
    }
    free(sub_nums);
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Finalize();

return 0;
}

When i try to run this ( mpirun -c 4 avg 4 ), i`m getting the error list: 当我尝试运行此命令( mpirun -c 4 avg 4 )时,我得到了错误列表:

[mangeke-mpi-2431940:03372] * Process received signal * [mangeke-mpi-2431940:03372] *处理收到的信号*

[mangeke-mpi-2431940:03372] Signal: Segmentation fault (11) [mangeke-mpi-2431940:03372]信号:分段错误(11)

[mangeke-mpi-2431940:03372] Signal code: Address not mapped (1) [mangeke-mpi-2431940:03372]信号代码:地址未映射(1)

[mangeke-mpi-2431940:03372] Failing at address: (nil) [mangeke-mpi-2431940:03372]在以下地址失败:(无)

[mangeke-mpi-2431940:03372] * End of error message * [mangeke-mpi-2431940:03372] *错误消息结尾*

How i can fix this problem? 我该如何解决这个问题?

As Hristo comments, the nums is initialized to NULL. 正如Hristo所说,nums被初始化为NULL。 If you explore the core file generated with the debugger, it raises the following statement 如果您探索用调试器生成的核心文件,它将引发以下语句

Core was generated by `./m 4'. 核心是由“ ./m 4”生成的。 Program terminated with signal SIGSEGV, Segmentation fault. 程序以信号SIGSEGV终止,分段错误。 #0 0x0000000000408809 in main (argc=2, argv=0x7ffd4fc87e68) at m.cxx:36 36 nums[i] = i; #0 0x0000000000408809位于m.cxx:36 36 nums [i] = i;(argc = 2,argv = 0x7ffd4fc87e68)

if you change the following code as shown below you'll get to make it run without segfaulting. 如果您更改以下代码,如下所示,则可以使其运行而不会出现段错误。

    ....

    // Create array with integers
    int nums[num_elements_per_proc]; // <<-- change here
    if (world_rank == 0) {

        for (int i =0; i<5; i++){
            nums[i] = i;
        }
    }

    ....
    // free memory
    if (world_rank == 0) {
    // free(nums);                   // <<-- change here, free not needed
    free(sub_avgs);
    }

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

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