- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我用 MPI 编写了一个简单的程序,它在处理器之间发送和接收消息,但它运行时出现段错误。
这是我的全部代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <strings.h>
#include <sstream>
#include<mpi.h>
using namespace std;
class Case {
public:
int value;
std::stringstream sta;
};
int main(int argc, char **argv) {
int rank,size;
MPI::Init(argc,argv);
rank=MPI::COMM_WORLD.Get_rank();
size=MPI::COMM_WORLD.Get_size();
if(rank==0){
Case *s=new Case();
s->value=1;
s->sta<<"test";
cout<<"\nInside send before copy value :"<<s->value;
fflush(stdout);
cout<<"\nInside send before copy data :"<<s->sta.str();
fflush(stdout);
Case scpy;
scpy.value=s->value;
scpy.sta<<(s->sta).rdbuf();
cout<<"\nInside send after copy value :"<<scpy.value;
cout<<"\nInside send after copy value :"<<scpy.sta.str();
MPI::COMM_WORLD.Send(&scpy,sizeof(Case),MPI::BYTE,1,23);
}
MPI::COMM_WORLD.Barrier();
if(rank==1){
Case r;
MPI::COMM_WORLD.Recv(&r,sizeof(Case),MPI::BYTE,0,23);
cout<<"\nRecieve value"<<r.value;
fflush(stdout);
cout<<"\nRecieve data"<<r.sta;
fflush(stdout);
}
MPI::Finalize();
return 0;
}
我收到以下错误消息,但我无法找出该程序中的错误所在。
谁能解释一下?
Inside send before copy value :1
Inside send before copy data :test
Inside send after copy value :1
Recieve value1
Recieve data0xbfa5d6b4[localhost:03706] *** Process received signal ***
[localhost:03706] Signal: Segmentation fault (11)
[localhost:03706] Signal code: Address not mapped (1)
[localhost:03706] Failing at address: 0x8e1a210
[localhost:03706] [ 0] [0xe6940c]
[localhost:03706] [ 1] /usr/lib/libstdc++.so.6(_ZNSt18basic_stringstreamIcSt11char_traitsIcESaIcEED1Ev+0xc6) [0x6a425f6]
[localhost:03706] [ 2] ./a.out(_ZN4CaseD1Ev+0x14) [0x8052d8e]
[localhost:03706] [ 3] ./a.out(main+0x2f9) [0x804f90d]
[localhost:03706] [ 4] /lib/libc.so.6(__libc_start_main+0xe6) [0x897e36]
[localhost:03706] [ 5] ./a.out() [0x804f581]
[localhost:03706] *** End of error message ***
--------------------------------------------------------------------------
mpirun noticed that process rank 1 with PID 3706 on node localhost.localdomain exited on signal 11 (Segmentation fault).
--------------------------------------------------------------------------
最佳答案
我认为问题在于该行:
MPI::COMM_WORLD.Send(&scpy,sizeof(Case),MPI::BYTE,1,23);
向接收方发送 Case 结构的拷贝,但它发送的是字节的原始拷贝,这不是很有用。 std::stringstream 类将包含一个指向用于存储字符串的实际内存的指针,因此这段代码将:
接收方在尝试取消引用无效指针时将出现段错误。
解决此问题的一种方法是自己发送字符数据。
在这种方法中,您将发送一条指向 std::stringstream::str()::c_str() 且长度为 std::stringstream::str()::size()*sizeof(char) 的消息。
另一种似乎更适合您尝试使用 MPI 和字符串的方法是使用 Boost 库。 Boost 包含 MPI 函数,可以自动为您序列化数据。
关于 Boost 和 MPI 的有用教程可用 on the boost website .以下是该教程中执行类似任务的示例代码:
#include <boost/mpi.hpp>
#include <iostream>
#include <string>
#include <boost/serialization/string.hpp>
namespace mpi = boost::mpi;
int main(int argc, char* argv[])
{
mpi::environment env(argc, argv);
mpi::communicator world;
if (world.rank() == 0) {
world.send(1, 0, std::string("Hello"));
std::string msg;
world.recv(1, 1, msg);
std::cout << msg << "!" << std::endl;
} else {
std::string msg;
world.recv(0, 0, msg);
std::cout << msg << ", ";
std::cout.flush();
world.send(0, 1, std::string("world"));
}
return 0;
}
关于c++ - C++ 中的 MPI_Send MPI_Recv 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20862996/
在下面的代码中,如果我没有将它声明为参数(我通常不能这样做),则 xysize 的值会发生变化。它只发生在 gfortran 4.7.2 和 OpenMPI 1.6 中的优化 -O2 和更多。这怎么可
在下面的代码中,如果我没有将它声明为参数(我通常不能这样做),则 xysize 的值会发生变化。它只发生在 gfortran 4.7.2 和 OpenMPI 1.6 中的优化 -O2 和更多。这怎么可
所以我试图自己编写一个使用 MPI(OpenMPI 实现)的矩阵乘法。问题是,尽管通过 MPI_Send/MPI_Recv 将矩阵的一部分从主设备发送到从设备工作正常,但主进程中应该从从设备接收答案的
我有一个问题。假设我有 np 个进程。对于每个进程,我根据输入文件计算需要发送到每个其他进程的消息数量(从 0 到...),并且我想向它们发送这个数字。问题是我只能从通过直接连接的节点创建的拓扑发送。
为了简单起见,向上发送就是发送到rank+1,向下发送就是发送到rank-1 该代码将数组从一个节点发送到另一个节点,并在它们之间来回发送。这是代码: MPI_Request req1, req2;
是否可以将源列表传递给 MPI_Recv(或等效)调用?目前,我的代码看起来像这样: do i=nod1,nod2 call mpi_recv(tmp,n,MPI_REAL,MPI_ANY_S
我在多线程 程序中遇到MPI_Isend/MPI_Recv 问题。 在程序中: 第一台机器有一个线程做一些计算并调用MPI_Isend将缓冲区发送到第二台机器,另一个线程总是试图MPI_Recv 来自
我有邻接矩阵和以下代码: if (is_broadcast_message) { MPI_Send(&broadcast_message,1,MPI_INT,j,3,MPI_COMM_WORL
假设我有一个包含 2 个进程的 MPI 程序,等级 0 和等级 1。 int i[20], j[20], temp, size; 在排名为 0 的进程中,我有 for(temp=0; temp<20;
#include #include #include "mpi.h" int main(int argc, char **argv) { int N; scanf("%d", &N
我在一个与 MPI 开发相关的项目中遇到问题。我正在使用 MPI 实现 RNA 解析算法,在该算法中,我使用主节点根据一些解析规则和解析表(包含不同的状态和相关操作)开始解析输入字符串。在解析表中,每
更新版本 我发现了导致问题的部分,之前已在下面解释过。我也想跟大家分享一下我的情况。我意识到我犯的只是一个荒谬的错误。但是,我想知道即使我犯了如下大错误,这个问题是如何发生的; 我有一个结构定义如下;
当我链接我的 MPI C 程序时,出现以下错误:对 MPI_recv 的 undefined reference 。 我应该怎么做才能解决这个错误? 最佳答案 是MPI_Recv,不是MPI_recv
我在 C 中运行以下程序时遇到上述错误。它使用 MPI 库。 #include "mpi.h" #include #include int main (int argc, char *argv[]
我知道我可以使用 status.MPI_TAG 获取 MPI_Recv 消息的标记,如下所示: MPI_Recv(&buffer, 1, MPI_INT, MPI_ANY_SOURCE, MPI_AN
我正在尝试实现一个 MPI 程序,以迭代方式将数组中的每个元素设置为其自身及其邻居(在前一个时间步长上)的平均值,同时保持第一个和最后一个元素不变。对于一个过程,这很好用;然而,对于多进程,我没有得到
我正在开发一个 C++ 应用程序,我在其中使用 MPI C 绑定(bind)通过网络发送和接收数据。我明白发送 const int VECTOR_SIZE = 1e6; std::vector vec
我有一个 malloc 的整数数组,我用 MPI_Recv 填充它 MPI_Recv(d.current, n, MPI_INT, 0, TAG_CURRENT_ARRAY, MPI_COMM_WOR
对于MPI中的异步通信,以下哪个更好(在性能、可靠性、可读性等方面): MPI_Isend with buffer 然后 MPI_Iprobe & MPI_Recv 一旦接收器准备好,或者 带缓冲区的
我正在尝试将 NxN 矩阵的分区发送到不同的进程。我使用 MPI_Scatterv 成功完成此操作,如下面的代码所示,但如果我尝试使用 MPI_Send 和 MPI_Recv ,结果会出现段错误。为什
我是一名优秀的程序员,十分优秀!