简体   繁体   English

SSD使用C ++读取大文件

[英]SSD read for a large file using c++

I am working on a windows 10 64-bit machine, 6850K CPU, and 64 GB DDR4 RAM, with a Samsung SSD connected via M.2. 我正在Windows 10 64位计算机,6850K CPU和64 GB DDR4 RAM上工作,并通过M.2连接了Samsung SSD。 I want to read a file that is about 15 GB in size, to memory. 我想读取一个大约15 GB的文件到内存中。 I am currently using fstream to read the entire file in to an array of unsigned chars using a single call to its read function. 我目前正在使用fstream,通过对其读取函数的一次调用将整个文件读取到无符号字符数组中。 However, the speeds I achieve are not hitting the maximum read speeds of the SSD (1500 MB/s when the SSD read is around 3500 MB/s). 但是,我达到的速度并未达到SSD的最大读取速度(当SSD读取约为3500 MB / s时为1500 MB / s)。

So I was wondering if there was a faster way? 所以我想知道是否有更快的方法? Would it be faster if I made multiple read calls for smaller chunks? 如果我对较小的块进行多次读取调用会更快吗? If so, what is the optimal chunk size? 如果是这样,最佳块大小是多少? I have seen some people mention 4K reads in some previously asked questions. 我已经看到有人在一些先前提出的问题中提到4K读取。 Does that apply in this case? 在这种情况下适用吗?

Any help is appreciated. 任何帮助表示赞赏。

My code excerpt is as follows 我的代码摘录如下

my read code is as follows 我阅读的代码如下

fstream myFile;
myFile.open("file", ios::binary | ios::in);
myFile.read(reinterpret_cast<char*>(buf), 14929920000LL); 

where buf is the same size as the read. 其中buf与读取的大小相同。

To get the fastest read speed, you need to bypass the windows disk cache. 为了获得最快的读取速度,您需要绕过Windows磁盘缓存。 Use Windows API calls CreateFile , ReadFile , etc. and use unbuffered reads (pass FILE_FLAG_NO_BUFFERING to CreateFile ). 使用Windows API调用CreateFileReadFile等,并使用无缓冲读取(将FILE_FLAG_NO_BUFFERING传递给CreateFile )。 This will transfer data directly from the disk to the memory block you need without having to copy the data from one memory address to another. 这样会将数据直接从磁盘传输到所需的存储块,而不必将数据从一个存储地址复制到另一个存储地址。

You'll need to pay close attention to the required memory alignment necessities that may be imposed by hardware. 您需要密切注意硬件可能施加的必需的内存对齐需求。 This typically requires memory addresses to be 512 byte aligned, but some newer hardware may want 4096 bytes, and others may not be as strict. 这通常需要将内存地址对齐为512字节,但是某些较新的硬件可能需要4096字节,而其他硬件可能不那么严格。 A link in the CreateFile documentation gives the full details for FILE_FLAG_NO_BUFFERING . CreateFile文档中的链接提供了FILE_FLAG_NO_BUFFERING的完整详细信息。

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

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