简体   繁体   English

发送一个 c++ std::vector<bool> 通过 MPI

[英]Send a c++ std::vector<bool> via mpi

I know that the storage of a std::vector<bool> is not necessarily an array of bools .我知道std::vector<bool>的存储不一定是 bools 数组

If I want to send receive int data stored in a std::vector<int> , I would use MPI_Send(vect.data(),num_of_ints,MPI_INT,dest_rk,tag,comm) .如果我想发送存储在std::vector<int>中的接收 int 数据,我会使用MPI_Send(vect.data(),num_of_ints,MPI_INT,dest_rk,tag,comm)

How should I use MPI_Send to send a std::vector<bool> ?我应该如何使用MPI_Send发送std::vector<bool> In particular :特别是 :

  • Can / should I use vect.data() as the pointer to buffer ?我可以/应该使用vect.data()作为指向缓冲区的指针吗?
  • What MPI type should I give ?我应该提供什么 MPI 类型? Somehow, I feel like MPI_CXX_BOOL does not apply (see this question )不知何故,我觉得MPI_CXX_BOOL不适用(见这个问题
  • What number of elements should I give ?我应该提供多少个元素? (related to the previous point) (与上一点相关)

std::vector<bool> specialization does not have the data() member function. std::vector<bool>专业化不具有data()成员函数。 The underlying storage scheme is not specified by the standard:标准没有规定底层存储方案:

There is no requirement that the data be stored as a contiguous allocation of bool values.不要求将数据存储为bool值的连续分配。 A space-optimized representation of bits is recommended instead.推荐使用位的空间优化表示。

The only reasonable option to send std::vector<bool> is to copy it into a vector of char (or some similar type like std::int8_t ), and then send that vector.发送std::vector<bool>的唯一合理选择是将其复制到char向量(或类似std::int8_t一些类似类型)中,然后发送该向量。 A better option might be to avoid std::vector<bool> in your code from the very beginning.更好的选择可能是从一开始就避免在代码中使用std::vector<bool>

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

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