运行时C ++ MPI代码Seg Fault 11

时间:2016-06-08 20:26:29

标签: c++ segmentation-fault mpi

我正在尝试使用MPI创建一些服务器,我可以将其编译,但是当我运行它时会立即出现故障。我试图添加打印语句以查看它崩溃的位置,但这没有帮助。我是新手使用MPI而我无法找到问题所在。我正在使用Mac而我正试图找出llvm,因为我不能使用GDB来检查核心转储,所以在此期间的任何帮助都非常感谢。

#include "mpi.h" 
#include <iostream>
#include <list>
using namespace std;

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main( int argc, char **argv ) 
{ 
    int MAX_DATA = 255;
    MPI_Comm client; 
    MPI_Status status; 
    char port_name[MPI_MAX_PORT_NAME]; 
    int size, again; 
    double buf[MAX_DATA];
    cout << "Did we get here? [1]"; 
    MPI_Init(NULL, NULL);
    MPI_Comm_size(MPI_COMM_WORLD, &size); 
    cout << "Did we get here before it pukes";
    if (size != 1) error("ERROR");

    // Create the random list
    list<int> rand_list;
    list<int>::iterator it;
    for(int i = 0; i < 5; ++i)
    {
        int r;
        r = rand();
        rand_list.insert (it,r);
        it++;
     }

     cout << "mylist contains:";
     for (it=rand_list.begin(); it!=rand_list.end(); ++it)
    {
        cout << ' ' << *it;
        cout << '\n';
    }

    MPI_Open_port(MPI_INFO_NULL, port_name); 
    printf("server available at %s\n",port_name); 
    while (1) { 
        MPI_Comm_accept( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &client); 
    again = 1; 
    while (again) { 
        MPI_Recv( buf, MAX_DATA, MPI_DOUBLE,  
                  MPI_ANY_SOURCE, MPI_ANY_TAG, client, &status ); 
        switch (status.MPI_TAG) { 
            case 0: MPI_Comm_free( &client ); 
                    MPI_Close_port(port_name); 
                    MPI_Finalize(); 
                    return 0; 
            case 1: MPI_Comm_disconnect( &client ); 
                    again = 0; 
                    break; 
            case 2: /* do something */ 
                cout << "Why did we get here?"; 
            default: 
                    /* Unexpected message type */ 
                    MPI_Abort( MPI_COMM_WORLD, 1 ); 
            } 
        } 
        } 
 }

[asdfa:04443] *** Process received signal ***
[asdfa:04443] Signal: Segmentation fault: 11 (11)
[asdfa:04443] Signal code: Address not mapped (1)
[asdfa:04443] Failing at address: 0x0
[asdfa:04443] [ 0] 0   libsystem_platform.dylib            0x00007fff8ed1452a _sigtramp + 26
[asdfa:04443] [ 1] 0   ???                                 0x0000000000000001 0x0 + 1
[asdfa:04443] [ 2] 0   server.o                            0x000000010a40eba2 main + 834
[asdfa:04443] [ 3] 0   libdyld.dylib                       0x00007fff9cb245ad start + 1
[asdfa:04443] *** End of error message ***
Segmentation fault: 11 (core dumped)

1 个答案:

答案 0 :(得分:0)

您的问题位于以下代码行:

list<int>::iterator it;
for(int i = 0; i < 5; ++i)
{
    int r;
    r = rand();
    rand_list.insert (it,r);
    it++;
 }

有两个问题:

  • 您声明了迭代器,但您从未为其分配过有效值,因此它指向任何位置。但是,迭代器必须指向列表中的有效位置。 rand_list.begin()rand_list.end()都是合法的,因此您需要:

    list<int>::iterator it = rand_list.end();

  • 然后第一个参数是您插入元素的位置。在迭代器指向的位置之前插入新元素。所以无论你最初是选择begin()还是end()作为迭代器(它们对于空列表都是相同的),通过递增它,你将它增加到列表的末尾,这是未定义的行为。只需在末尾插入:

    rand_list.insert (rand_list.end(), r);

但为什么不简单地使用push_back(或push_front,如果您愿意的话)?