分段故障OpenMPI [英] Segmentation fault OpenMPI

查看:542
本文介绍了分段故障OpenMPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括一个带有功能linspace的静态头文件utils.h.我的main.cpp文件如下:

I include a static header file utils.h with a function linspace. My main.cpp file is as follows:

#include <iostream>
#include <utils.h>
#include <mpi.h>

using namespace std;

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

    float start = 0., end = 1.;
    unsigned long int num = 100;

    double *linspaced;

    float delta = (end - start) / num;
    int size, rank;


    MPI_Init(NULL, NULL);

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

    // These have to be converted into unsigned long ints
    int casesPerNode = num / size;
    int remainderCases = num % size;


    if(rank==0){
        linspaced =  new double[num];

        if(remainderCases!=0){
            linspace(&linspaced[(size-1)*casesPerNode], end - delta*remainderCases, end, remainderCases);

        } else {
            linspace(&linspaced[(size-1)*casesPerNode], end - delta*casesPerNode, end, casesPerNode);

        }

    }

    MPI_Bcast(&linspaced, num, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    if(rank != 0) {




        // Sending job to master node.
        // The node is already overloaded with coordinating.
        // Additional task now is also to take on remainder cases.


        // cout << "Rank " << rank << endl;
        float start_in = start + casesPerNode*delta*(rank-1);
        float end_in = start + casesPerNode*delta*(rank) - delta;

        linspace(&linspaced[(rank-1)*casesPerNode], start_in, end_in, casesPerNode);


    }


    MPI_Finalize();


    for(int i=0; i< num; i++){
        cout << *(linspaced + i) << endl;
    }


    return 0;

}

和我的utils.h文件是:

void linspace(double *ret, double start_in, double end_in, unsigned long int num_in)
{
    /* This function generates equally spaced elements and returns
     an array with the results */


    assert(num_in!=0);


    cout <<  "\tReceived start :" << start_in << "\tEnd :" << end_in << "\tNum_in :" << num_in << endl;

    double delta_in = (end_in - start_in) / (num_in - 1);

    if(num_in == 1){
        *(ret) = start_in;
    }

    *(ret) = start_in;
    for(int i=1; i < num_in-1; i++) {
        *(ret + i) = *(ret + i - 1) + delta_in;
    }
    *(ret + (num_in - 1)) = end_in;

    /*
    cout << "Finished executing linspace " << endl;
     for(int i = 0; i<num_in; i++){
     cout << "Address : " << &ret << "\tElement " << i << " : " << *(ret + i) << endl;
     }
     */
}

我无法诊断为什么会产生以下错误.

I am unable to diagnose why the following error is generated.

 *** Process received signal ***
 *** Process received signal ***
 Signal: Segmentation fault: 11 (11)
 Signal code: Address not mapped (1)
 Failing at address: 0x7fb442529b98
 [ 0] 0   libsystem_platform.dylib            0x00007fffd6902b3a _sigtramp + 26
 [ 1] 0   ???                                 0x0000000000000000 0x0 + 0
 [ 2] 0   test                                0x0000000101227fda main + 602
 [ 3] 0   libdyld.dylib                       0x00007fffd66f3235 start + 1
 *** End of error message ***
    Received start :0.5 End :0.74   Num_in :25
 *** Process received signal ***
 Signal: Segmentation fault: 11 (11)
 Signal code: Address not mapped (1)
 Failing at address: 0x7fb442529ad0
 [ 0] 0   libsystem_platform.dylib            0x00007fffd6902b3a _sigtramp + 26
 [ 1] 0   ???                                 0x0000000000000000 0x0 + 0
 [ 2] 0   test                                0x000000010c87bfda main + 602
 [ 3] 0   libdyld.dylib                       0x00007fffd66f3235 start + 1
 *** End of error message ***
 Signal: Segmentation fault: 11 (11)
 Signal code: Address not mapped (1)
 Failing at address: 0x7fb442529c60
 [ 0] 0   libsystem_platform.dylib            0x00007fffd6902b3a _sigtramp + 26
 [ 1] 0   ???                                 0x0000000000000000 0x0 + 0
 [ 2] 0   test                                0x0000000104764fda main + 602
 [ 3] 0   libdyld.dylib                       0x00007fffd66f3235 start + 1
 *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 3 with PID 0 on node wlan-145-94-163-183 exited on signal 11 (Segmentation fault: 11).
--------------------------------------------------------------------------

我该如何解决呢?

谢谢!

PS:感谢您的耐心等待,我对OpenMPI还是陌生的.

PS: I'm very new to OpenMPI, thanks for your patience.

推荐答案

两个错误:

  1. linspaced仅分配给等级0,然后在所有级别的MPI_Bcast调用中使用.

  1. linspaced is only allocated in rank 0, but is then used in an MPI_Bcast call by all ranks.

linspaced是一个指针.将&linspaced传递给MPI_Bcast会导致指向要传递的指针的指针,这不是您想要的.

linspaced is a pointer. Passing &linspaced to MPI_Bcast results in a pointer to the pointer being passed, which is not what you want.

代码应类似于:

linspaced = new double[num]; // <--- outside the conditional

if(rank==0){

    if(remainderCases!=0){
        linspace(&linspaced[(size-1)*casesPerNode], end - delta*remainderCases, end, remainderCases);

    } else {
        linspace(&linspaced[(size-1)*casesPerNode], end - delta*casesPerNode, end, casesPerNode);

    }

}

MPI_Bcast(linspaced, num, MPI_DOUBLE, 0, MPI_COMM_WORLD);
//        ^-- no & here

这篇关于分段故障OpenMPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆