C++ 分段故障OpenMPI

C++ 分段故障OpenMPI,c++,openmpi,C++,Openmpi,我包括一个静态头文件utils.h,其中包含一个函数linspace。我的main.cpp文件如下所示: #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

我包括一个静态头文件
utils.h
,其中包含一个函数
linspace
。我的main.cpp文件如下所示:

#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;

}
我无法诊断产生以下错误的原因

 *** 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的新手,谢谢您的耐心。

两个错误:

  • linspaced
    仅在列组0中分配,然后由所有列组在
    MPI\u Bcast
    调用中使用

  • linspaced
    是一个指针。将
    &linspaced
    传递到
    MPI\u Bcast
    会导致指向所传递指针的指针,而这不是您想要的

  • 代码应该如下所示:

    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
    
    linspaced=newdouble[num];// 两个错误:

  • linspaced
    仅在列组0中分配,然后由所有列组在
    MPI\u Bcast
    调用中使用

  • linspaced
    是一个指针。将
    &linspaced
    传递到
    MPI\u Bcast
    会导致指向所传递指针的指针,而这不是您想要的

  • 代码应该如下所示:

    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
    

    linspaced=newdouble[num];//如果你写
    if(xyz){*(ret)=start_in;}
    后跟
    *(ret)=start_in;,你的思维就有问题
    @MarkSetchell纠正了它。如果你写
    if(xyz){*(ret)=start_in;}
    后跟
    *(ret)=start_in@MarkSetchell更正了它。