运行 时的 C++ MPI 代码段错误 11
C++ MPI code Seg Fault 11 when run
我正在尝试使用 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)
您的问题位于这些代码行:
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();
那么第一个参数就是你插入元素的位置。新元素插入 before 迭代器指向的位置。因此,无论您最初是选择 begin() 还是 end() 作为迭代器(它们对于空列表都是相同的),通过递增它,您将它递增到列表的末尾之外,这是未定义的行为。只需始终在末尾插入:
rand_list.insert (rand_list.end(), r);
但是你为什么不简单地使用 push_back
(或者 push_front
,如果你愿意的话)?
我正在尝试使用 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)
您的问题位于这些代码行:
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();
那么第一个参数就是你插入元素的位置。新元素插入 before 迭代器指向的位置。因此,无论您最初是选择 begin() 还是 end() 作为迭代器(它们对于空列表都是相同的),通过递增它,您将它递增到列表的末尾之外,这是未定义的行为。只需始终在末尾插入:
rand_list.insert (rand_list.end(), r);
但是你为什么不简单地使用 push_back
(或者 push_front
,如果你愿意的话)?