C++Open MPI:自定义结构类型无法通过Ssend发送
C++ Open-MPI: custom struct type Not able to send over Ssend
因此,我正在使用MPI进行一个项目,并尝试创建自己的结构。我遇到的问题是,它似乎工作不正常,因为我从来没有在工人体内收到任何东西,所以我超时了。我尝试了一个标准的MPI_INT,效果很好。然而,当我尝试实现我自己的类型时,它不喜欢它…以下是我的代码:
#include <iostream>
#include <mpi.h>
#include "main.hpp"
int main(int argc, char *argv[]){
//initialize MPI
MPI_Init(&argc, &argv);
message_to_worker message;
//declare my new type
MPI_Datatype MessageType;
//declare the types the the structure will have
MPI_Datatype type[1] = { MPI_INT };
//the number of results for each type(int[50] will be 50 etc..)
int blocklen[1] = {1};
//this will store the displacement for each var in the structure
MPI_Aint disp[1] = {0};
MPI_Aint var1, var2;
//number of vars
int count = 1;
//define the type
MPI_Type_struct(count, blocklen, disp, type, &MessageType);
MPI_Type_commit(&MessageType);
std::cout << "The displacement is " << disp[0] << std::endl;
int rank;
int world_size;
//give me my current rank(node 1 reiceves 1 , node 2 etc... )
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int counter = 1;
message.N = 5;
//IF MASTER
if (rank == 0) {
int result;
//send messages to the workers
while(counter<world_size){
//send 1 message of type MessageType using my own structure(first parameter)
result = MPI_Ssend(&message, 1, MessageType, counter, 123, MPI_COMM_WORLD);
//make sure the mssage was sent correctly
if (result == MPI_SUCCESS){
std::cout << "I am master and the world size is : " << world_size << std::endl;
std::cout << " I send a message to mky worker thread " << std::endl;
}else{
std::cout << "There was a problem with the sending for worker " << counter << std::endl;
}
counter++;
}
}
//IF WORKER
else if (rank > 0) {
//the worker will wait until it receives a message
int result = MPI_Recv(&message, 1, MessageType, 0, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
//make sure the message was RECEIVED correctly
if (result == MPI_SUCCESS){
std::cout << "Worker - Rank: " << rank <<" OK! and the value received through the message is " << message.N << std::endl;
}
}
//shutdown MPI
MPI_Finalize();
return 0;
}
我的标记param(上一个为3个param)对于发送方和接收方不同。。主进程正在使用标记123,而辅助进程正在查找标记0。它们都需要同步,因为它们需要建立一个通信通道。这里是修复(注意第五个参数,标签,是如何改变的)。
之前:
MPI_Ssend(&message, 1, MessageType, counter, 123, MPI_COMM_WORLD);
MPI_Recv(&message, 1, MessageType, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
之后
MPI_Ssend(&message, 1, MessageType, counter, 123, MPI_COMM_WORLD);
MPI_Recv(&message, 1, MessageType, 0, 123, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
相关文章:
- 从命令行c++发送文本文件名
- 发送一个带有libcurl C++问题的帖子请求:s
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 如何在C++中将函数发送到另一个进程
- boost xml parsingl将xml的路径作为变量发送
- 如何将数组从Objective-C发送到C++函数
- 如何使用ESP8266向谷歌主页发送命令
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 为什么默认复制函数在按值发送参数时不调用?
- 程序不断发送垃圾邮件号码?
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何使用 Google Test 向测试添加元数据 / 如何将数据从 Google Test 发送到 TestEven
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- Qt:如何使不兼容的发送方/接收方参数兼容?
- (Winsock) UDP 接收工作正常,但同一套接字的发送失败
- MSG_WAITALL的套接字发送得到了 22 EINVAL
- Win32 发送输入鼠标移动滞后并冻结
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- C++Open MPI:自定义结构类型无法通过Ssend发送