MPI(C ++)中的向量用法 [英] Vector Usage in MPI(C++)
问题描述
我是MPI编程,入门学习的新手,直到通过定义结构创建派生数据类型为止,我一直是成功的.现在,我想将Vector包含在我的结构中,并希望在整个流程中发送数据.例如:
I am new to MPI programming,stiil learning , i was successful till creating the Derived data-types by defining the structures . Now i want to include Vector in my structure and want to send the data across the Process. for ex:
struct Structure{
//Constructor
Structure(): X(nodes),mass(nodes),ac(nodes) {
//code to calculate the mass and accelerations
}
//Destructor
Structure() {}
//Variables
double radius;
double volume;
vector<double> mass;
vector<double> area;
//and some other variables
//Methods to calculate some physical properties
现在使用MPI,我想跨进程在结构中发送数据.我可以创建包含的MPI_type_struct矢量并发送数据吗?
Now using MPI i want to sent the data in the structure across the processes. Is it possible for me to create the MPI_type_struct vectors included and send the data?
我尝试阅读论坛,但无法从那里得到的答复中得到清晰的画面.希望我能够对发送数据有一个清晰的想法或方法
I tried reading through forums, but i am not able to get the clear picture from the responses given there. Hope i would be able to get a clear idea or approach to send the data
PS:我可以单独发送数据,但是如果我们认为域很大(例如10000 * 10000),则使用MPI_Send/Receve发送数据的开销可能会很大
PS: i can send the data individually , but its an overhead of sending the data using may MPI_Send/Recieve if we consider the domain very large(say 10000*10000)
推荐答案
在MPI中定义结构很麻烦.我认为一种更简单的方法是使用这样的事实:保证STL向量具有连续分配的内存.也就是说,您可以通过获取指向第一个元素的指针,将它们像C数组一样对待.
Defining structures in MPI is a pain. I think an easier approach would be to use the fact that STL vectors are guaranteed to have contiguously allocated memory. That is, you can treat them like C arrays by getting a pointer to the first element.
std::vector<float> data;
// ... add elements to your vector ...
MPI_Send(&data.front(), data.size(), MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
这篇关于MPI(C ++)中的向量用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!