C++ 发送vector的最快方式是什么<;向量<;双倍>&燃气轮机;从x86平台中创建的一个进程到x64中构建的另一个进程

C++ 发送vector的最快方式是什么<;向量<;双倍>&燃气轮机;从x86平台中创建的一个进程到x64中构建的另一个进程,c++,visual-studio,boost,mfc,C++,Visual Studio,Boost,Mfc,我有两个进程p1和p2。 p1是在x86中构建的,p2是在x64平台中使用(Visual Studio 2013/MFC/C++)构建的 p1计算结果并保存在 std::矢量数据。 我希望这个数据以尽可能快的方式从p1传输到p2的std::vector data 我使用了boost::archive::text\u iarchive将其写入文本文件。由于它是跨平台兼容的,所以我可以使用p2中的boost::archive::text\u oarchive来阅读它。但是它需要花费太多的时间,因为它

我有两个进程p1p2p1是在x86中构建的,p2是在x64平台中使用(Visual Studio 2013/MFC/C++)构建的

p1计算结果并保存在
std::矢量数据
。 我希望这个
数据以尽可能快的方式从p1传输到p2
std::vector data

我使用了
boost::archive::text\u iarchive
将其写入文本文件。由于它是跨平台兼容的,所以我可以使用p2中的
boost::archive::text\u oarchive
来阅读它。但是它需要花费太多的时间,因为它涉及磁盘读写。所以我需要更好的方法来完成这项工作


请建议我一些跨平台数据传输的更快方法。如果有人能给我提供一些代码,我将非常感激。

正如其他人已经提到的,最快、最直接的方法就是使用共享内存。下面是Posix系统的示例

服务器创建一个共享内存对象,并通过将每个子向量作为长度加项目写入内存来将向量序列化

接收器打开共享内存对象,并再次将内存反序列化为向量对象的向量

发件人:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include <vector>
#include <algorithm>

static const std::vector<std::vector<double>> vec = {
  { 1.0, 1.1, 1.2 },
  { 2.0, 2.1 },
  { 3.0, 3.1, 3.2, 3.3 },
};

int main(int argc, char *const argv[])
{
  uint64_t vecsSize = 0;
  for (const auto& v : vec) {
    vecsSize += sizeof(double) * v.size() + sizeof(uint64_t);
  }

  // create a file of size 'vecsSize'
  int fd = open("VEC", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  lseek(fd, vecsSize - 1, SEEK_SET);
  write(fd, "", 1);
  lseek(fd, 0, SEEK_SET);

  // memory map the file into the process and copy the data into it
  uint8_t* ptrFile = reinterpret_cast<uint8_t*>(mmap(0, vecsSize, PROT_WRITE, MAP_SHARED, fd, 0));
  uint8_t* ptr = ptrFile;
  for (const auto& v : vec)
  {
    reinterpret_cast<uint64_t*>(ptr)[0] = v.size();
    ptr += sizeof(uint64_t);

    std::copy(v.begin(), v.end(), reinterpret_cast<double*>(ptr));
    ptr += sizeof(double) * v.size();
  }

  munmap(ptrFile, vecsSize);
  close(fd);
  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
静态常量std::vector vec={
{ 1.0, 1.1, 1.2 },
{ 2.0, 2.1 },
{ 3.0, 3.1, 3.2, 3.3 },
};
int main(int argc,char*const argv[]
{
uint64_t veccsize=0;
用于(常数自动和v:vec){
VeccSize+=sizeof(双精度)*v.size()+sizeof(双精度);
}
//创建大小为“vecsize”的文件
int fd=开放(“VEC”,O|RDWR | O|CREAT,S|IRUSR | S|IWUSR);
lseek(fd,veccsize-1,SEEK_集);
写(fd,“,1);
lseek(fd,0,SEEK_集);
//内存将文件映射到进程中,并将数据复制到进程中
uint8_t*ptrFile=重新解释强制转换(mmap(0,向量化,保护写入,映射共享,fd,0));
uint8_t*ptr=ptrFile;
用于(常数自动和v:vec)
{
重新解释铸件(ptr)[0]=v.尺寸();
ptr+=sizeof(uint64_t);
复制(v.begin(),v.end(),reinterpret_cast(ptr));
ptr+=sizeof(双)*v.size();
}
munmap(ptrFile,veccsize);
关闭(fd);
返回0;
}
接收人:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include <iostream>
#include <vector>
#include <algorithm>

template<class T>
inline std::ostream& operator<< (std::ostream& o, std::vector<T> const& v) {
  for (auto const& i : v)
    o << i << " ";
  o << std::endl;
  return o;
}

static std::vector<std::vector<double>> vec;

int main(int argc, char *const argv[])
{
  int fd = open("VEC", O_RDONLY, S_IRUSR | S_IWUSR);

  struct stat fileStat = { 0 };
  fstat(fd, &fileStat);

  uint64_t vecsSize = static_cast<uint64_t>(fileStat.st_size);
  uint8_t* ptrFile = reinterpret_cast<uint8_t*>(mmap(0, vecsSize, PROT_READ, MAP_SHARED, fd, 0));

  uint8_t* ptr = ptrFile;
  while (ptr < ptrFile + vecsSize)
  {
    uint64_t vecSize = reinterpret_cast<uint64_t*>(ptr)[0];
    ptr += sizeof(uint64_t);

    std::vector<double> v(
      reinterpret_cast<double*>(ptr),
      reinterpret_cast<double*>(ptr) + vecSize);
    ptr += sizeof(double) * vecSize;

    vec.emplace_back(v);
  }

  munmap(ptrFile, vecsSize);
  close(fd);

  std::cout << vec;
  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
模板

内联std::ostream&operator我认为是在一台机器内?很可能是共享内存。或者,如果需要文件,至少用二进制而不是文本来编写。或者,因为它位于向量内部,所以可能只是通过一个你可能想要查看的管道。如果二维数组是矩形的,则将其转换为一维数组,并在文件映射对象中为其分配存储(请参阅)。如果二维数组是锯齿状的,则只能在文件映射对象中存储单个行。确保在文件映射对象中保留描述数据(维度、大小)的元数据。文件映射对象可以映射到任何比特的进程,而不需要实际复制数据?使用IPC,可能通过TCP/IP套接字,并以二进制形式发送数据。请看保存问题的答案。v这当然不是最快的方法,正如问题所要求的那样。最快的方法是在共享内存中构建向量的内存支持。您可以将自定义分配器传递给任何标准容器以实现此目的。感谢大家分享您的宝贵意见。