c++ - MPI 中 vector 的使用(C++)

标签 c++ vector parallel-processing mpi

我是 MPI 编程的新手,仍在学习,我成功地通过定义结构创建了派生数据类型。现在我想在我的结构中包含 Vector 并希望跨流程发送数据。例如:

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 vector 并发送数据吗?

我尝试通过论坛阅读,但我无法从那里给出的回复中获得清晰的画面。希望我能够得到一个清晰的想法或方法来发送数据

PS:我可以单独发送数据,但如果我们认为域非常大(比如 10000*10000),则使用 MPI_Send/Recieve 发送数据会产生开销

最佳答案

在 MPI 中定义结构是一件痛苦的事情。我认为一种更简单的方法是使用 STL vector 保证具有连续分配的内存这一事实。也就是说,您可以通过获取指向第一个元素的指针来将它们视为 C 数组。

std::vector<float> data;
// ... add elements to your vector ...
MPI_Send(&data.front(), data.size(), MPI_FLOAT, 0, 1, MPI_COMM_WORLD);

关于c++ - MPI 中 vector 的使用(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2546298/

相关文章:

python - 在 DBSCAN 中打开并行处理

c++ - "not, and, or, not_eq.."是 C++ 标准的一部分吗? (以及为什么可以在代码中使用或避免使用它们?)

c++ - std::is_trivially_xxx 其中一个暗示另一个

C++:如何将对象传递给函数作为模板化基类

c++ - 是否有用于 OpenNI2 的 CMakeLists?

python - spacy 与 joblib 库生成 _pickle.PicklingError : Could not pickle the task to send it to the workers

c++ - 如何使用 Vector Class Library 进行 AVX 矢量化以及 openmp #pragma omp parallel 进行缩减?

c++ - 结构与对象属性和 C++ 中的 std::vector 的交互

c++ - 如何将箭头::数组转换为标准:: vector ?

c++ - vector 的最后一项不显示/vector 索引问题