简体   繁体   English

向量的 MPI 发送/接收

[英]MPI send/recv of a vector

Hello everyone i'm working on a project for university that involves using the MPI library, unfortunatly i cannot share the entire code but i hope somebody will be able to give me some pointers regardless.大家好,我正在从事一个涉及使用 MPI 库的大学项目,不幸的是我无法分享整个代码,但我希望有人能够给我一些指示。 I need to send an array from proc X to proc 0, however as i've read on google the only way to send a dinamycally created array is to find out the size, send the size to Proc 0 and only then send the array (i'm not able to predetermine the size of it),so this is what i did on Proc X:我需要将一个数组从 proc X 发送到 proc 0,但是正如我在 google 上读到的,发送一个 dinamycally 创建的数组的唯一方法是找出大小,将大小发送到 Proc 0,然后才发送数组(我无法预先确定它的大小),所以这就是我在 Proc X 上所做的:

vector<int> tosend = aglib.ExtractEvaluationFunctions();
int tosend_size = tosend.size();
MPI_Send(&tosend_size, 1, MPI_INT, 0, 666, MPI_COMM_WORLD);
MPI_Send(&tosend, tosend_size, MPI_INT, 0, 777, MPI_COMM_WORLD);

this is what happens on Proc 0(i cannot share the same buffer as the vector tosend is locally created in Proc1 each time):这就是 Proc 0 上发生的事情(我无法共享相同的缓冲区,因为每次在 Proc1 中本地创建要发送的向量):

 vector<int> results_and_rank;
 int results_rank_size;
 MPI_Recv(&results_rank_size, 1, MPI_INT, MPI_ANY_SOURCE, 666, MPI_COMM_WORLD, &status);
 MPI_Recv(&results_and_rank, results_rank_size, MPI_INT, MPI_ANY_SOURCE, 777, MPI_COMM_WORLD, &status); 
 cout << "size of results_and_rank is :"<< results_and_rank.size()endl;
 cout<< "last element of array is :"<< results_and_rank.back()<<endl;

i think communication works fine as i'm able to read the size of the received vector which is identical to the one i sent, however my code crashes whenever i try to access an element of the array results_and_rank thus crashing on the last print.我认为通信工作正常,因为我能够读取与我发送的向量相同的接收向量的大小,但是每当我尝试访问数组 results_and_rank 的一个元素时,我的代码就会崩溃,从而在最后一次打印时崩溃。 By the way i need to use blocking communication for the purpose of my project.顺便说一下,我需要为我的项目使用阻塞通信。
Am i missing something?我错过了什么吗? thank you for your time and help.感谢您的时间和帮助。

You want to send/receive the data to the vector, thus you need the data inside the vector, not the vector itself.您想向矢量发送/接收数据,因此您需要矢量内的数据,而不是矢量本身。

Here you pass the address of std::vector itself to the MPI_Recv() , which will corrupt the std::vector .在这里,您将std::vector本身的地址传递给MPI_Recv() ,这将破坏std::vector

MPI_Recv(&results_and_rank, results_rank_size, MPI_INT, MPI_ANY_SOURCE, 777, MPI_COMM_WORLD, &status);

Correct way:正确做法:

Sender:发件人:

MPI_Send(&tosend[0], tosend_size, MPI_INT, 0, 777, MPI_COMM_WORLD);

Receiver:接收者:

results_and_rank.resize(results_rank_size);
MPI_Recv(&results_and_rank[0], results_rank_size, MPI_INT, MPI_ANY_SOURCE, 777, MPI_COMM_WORLD, &status); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM