简体   繁体   English

如何将boost :: multi_array保存到文件

[英]How to save a boost::multi_array to a file

I'm looking for a simple way to save to and load from a file a 3D boost::multi_array. 我正在寻找一种简单的方法来保存文件并从文件中加载3D boost :: multi_array。 As far as I know there is no methods in the Boost library which I find odd. 据我所知,Boost库中没有我觉得很奇怪的方法。

I don't need the file to be human readable, so a binary file would be better for performance. 我不需要该文件是人类可读的,因此二进制文件会更好地提高性能。 Can anyone point me to a ready-made solution or give me ideas on how to implement such read/write methods ? 谁能给我指出现成的解决方案,或者给我有关如何实现这种读/写方法的想法?

I use 3D multi_arrays of types bool and ints so I'll need to use a template. 我使用bool和ints类型的3D multi_arrays,因此需要使用模板。

It is unnecessary to use some special serialization libraries, because your data is already serialized in the memory and you can get it as follows (suppose A is the array containing int data: 不必使用一些特殊的序列化库,因为您的数据已经在内存中进行了序列化,并且可以按以下方式获取它(假设A是包含int数据的数组:

int *data = A.data();
size_t size = A.num_elements();

You can just write it one by one to a file. 您可以将其一个一个地写入文件。 A better way may be using mmap , like follows: 更好的方法可能是使用mmap ,如下所示:

int fd = open("myfile", O_RDWR);
size_t bytes = size * sizeof(int);
ftruncate(fd, bytes);
void *buf = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
memcpy(buf, data, bytes);
close(fd);
munmap(buf, bytes);

It is very easy to reload the data from file to A . 将数据从文件重新加载到A非常容易。 Just ommit the invocation of ftruncate and call memcpy(data, buf, bytes); 只是省略ftruncate的调用并调用memcpy(data, buf, bytes); .

An even better way is that, if your data is huge, you simply store the data in a file, use mmap to map it to a memory address, then pass the address to multi_array_ref . 更好的方法是,如果您的数据很大,则只需将数据存储在文件中,使用mmap将其映射到内存地址,然后将该地址传递给multi_array_ref In this case you don't need to specifically write it to file. 在这种情况下,您无需专门将其写入文件。 It is aromatically done by the OS. 它是由操作系统芳香地完成的。

The above code is considered for Linux, but I believe other platforms should have similar functionality. 以上代码适用于Linux,但我相信其他平台也应具有类似的功能。 Error checking is omitted for clarity. 为了清楚起见,省略了错误检查。

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

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