MappedByteBuffer对2GIG的Java限制使得用于映射大文件变得棘手.通常推荐的方法是使用MappedByteBuffer数组并通过以下方式对其进行索引:
long PAGE_SIZE = Integer.MAX_VALUE;
MappedByteBuffer[] buffers;
private int getPage(long offset) {
return (int) (offset / PAGE_SIZE)
}
private int getIndex(long offset) {
return (int) (offset % PAGE_SIZE);
}
public byte get(long offset) {
return buffers[getPage(offset)].get(getIndex(offset));
}
Run Code Online (Sandbox Code Playgroud)
这可能适用于单个字节,但如果要处理更大且需要跨越边界的读/写(getLong()或get(byte [])),则需要重写大量代码.
问题是:对于这些场景,你最好的做法是什么,你知道任何可以在不重新发明轮子的情况下重复使用的工作解决方案/代码吗?
在Delphi 7中,我使用CreateFileMapping打开一个文件,然后使用MapViewOfFile获取指针.
如何扩展内存并在内存中添加一些字符并将其保存到该文件中?
我已经用适当的模式(fmOpenReadWrite,PAGE_READWRITE)打开了文件,如果我覆盖了字符,它会被保存到文件中,但我需要在文件中间添加额外的值.
我们在MemoryMappedFile中加载了一个222MB的文件,用于原始数据访问.使用write方法更新此数据.经过一些计算后,数据应重置为文件的原始值.我们目前通过部署类并创建新实例来实现这一点.这种情况很好,但有时CreateViewAccessor崩溃时出现以下异常:
System.Exception:没有足够的存储空间来处理此命令.---> System.IO.IOException:没有足够的存储空间来处理此命令.
at System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)在System.IO.MemoryMappedFiles.MemoryMappedFile.CreateViewAccessor的System.IO.MemoryMappedFiles.MemoryMappedView.CreateView(SafeMemoryMappedFileHandle> memMappedFileHandle,MemoryMappedFileAccess访问,Int64偏移,Int64大小)处. Int64偏移量,Int64>大小,MemoryMappedFileAccess访问权限)
以下类用于访问memorymapped文件:
public unsafe class MemoryMapAccessor : IDisposable
{
private MemoryMappedViewAccessor _bmaccessor;
private MemoryMappedFile _mmf;
private byte* _ptr;
private long _size;
public MemoryMapAccessor(string path, string mapName)
{
FileInfo info = new FileInfo(path);
_size = info.Length;
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite))
_mmf = MemoryMappedFile.CreateFromFile(stream, mapName, _size, MemoryMappedFileAccess.Read, null, HandleInheritability.None, false);
_bmaccessor = _mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite);
_bmaccessor.SafeMemoryMappedViewHandle.AcquirePointer(ref _ptr);
}
public void Dispose()
{
if (_bmaccessor != …Run Code Online (Sandbox Code Playgroud) 给出以下测试代码(x64环境):
static void Test()
{
string fileName = @"d:\map";
long length = new FileInfo(fileName).Length;
using (var file = MemoryMappedFile.CreateFromFile(fileName, FileMode.Open, "mapFile", length, MemoryMappedFileAccess.ReadWrite))
{
byte* bytePtr = (byte*)0;
var view = file.CreateViewAccessor(0, length, MemoryMappedFileAccess.ReadWrite);
view.SafeMemoryMappedViewHandle.AcquirePointer(ref bytePtr);
long count = (long)(length / sizeof(int));
long sum = 0;
long step = count / 2000;
int* ptr = (int*)&bytePtr[0];
long currentCount = 0 ;
Parallel.For(0, count, new ParallelOptions { MaxDegreeOfParallelism = 8 }, (i) =>
{
Interlocked.Add(ref sum, ptr[i]);
Interlocked.Increment(ref currentCount) ;
if …Run Code Online (Sandbox Code Playgroud) 我正在 OCaml 中进行实验,看看如何从内存映射文件读取/写入数值数组。
我想我需要使用 Bigarray 但不确定如何将 Bigarray 数组写入内存映射文件,然后读回?
我似乎在任何地方都找不到例子。我检查了Jane St. core的源代码但没有结果。
有没有办法直接获取IntPtrMemoryMappedFile中的数据?我有大数据块,频率变化很大,我不想复制它
我试图了解Boost内存映射文件的工作原理.下面的代码可以工作,它可以完成它应该做的事情,但问题是它生成的文件存储在磁盘上(在可执行文件的同一目录中)而不是内存.也许有一个标志设置在某处,但我找不到它...
提前感谢任何信息!
#include <iostream>
#include <string>
#include <cstring>
#include <boost/iostreams/device/mapped_file.hpp>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
const int blockSize = 64;
bool writer = false;
if(argc > 1) {
if(!strcmp(argv[1], "w"))
writer = true;
}
boost::iostreams::mapped_file_params params;
params.path = "map.dat";
// params.length = 1024; // default: all the file
params.new_file_size = blockSize;
if(writer) {
cout << "Writer" << endl;
params.mode = std::ios_base::out;
}
else {
cout << "Reader" << endl;
params.mode = std::ios_base::in;
}
boost::iostreams::mapped_file mf; …Run Code Online (Sandbox Code Playgroud) 我已经根据经验确定,在我的系统上,默认情况下,创建为特定大小的内存映射文件始终完全归零.例如,使用呼叫
HANDLE hMM =
CreateFileMapping (h,
NULL,
PAGE_READWRITE,
0,
0x01400000,//20MB
NULL);
Run Code Online (Sandbox Code Playgroud)
..并且写入该文件的映射视图总是会导致20MB的文件完全归零,除非我写了非零数据.
我想知道文件的未初始化部分是否可以假定为零.这种行为一般在Windows上得到保证吗?
如何读取文件字节MemoryMappedFile并将其放入byte[]数组?
有没有方法将numpy memmap数组保存到.npy文件中?显然,有一种方法可以从.npy文件中加载这样的数组,如下所示
data = numpy.load("input.npy", mmap_mode='r')
Run Code Online (Sandbox Code Playgroud)
但刷新文件并不等同于以.npy格式存储它.
如果冲洗是唯一的方法,那么有没有办法推断存储阵列的形状?我更喜欢在另一个脚本中自动存储和检索动态形状(可能再次作为memmap).
我在各个地方搜索过这个,但没有找到任何结果.我.npy现在的存储方式是
numpy.save(output.filename, output.copy())
Run Code Online (Sandbox Code Playgroud)
这违背了使用memmap但保留形状的想法.
注意:我知道hdf5和h5py,但我想知道是否有一个纯粹的numpy解决方案.