Last active
December 14, 2015 20:49
-
-
Save kellabyte/5146568 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void TestReadingMMap() | |
{ | |
double bytesRead = 0; | |
double elapsed = 0; | |
mapped_file_source file; | |
__int64 checksum = 0; | |
{ | |
auto_cpu_timer timer; | |
file.open("C:\SomeBigFile.dat", 2147483647); | |
stream<mapped_file_source> input(file); | |
if(file.is_open()) | |
{ | |
const int segmentSize = 4096; | |
const int size = file.size(); | |
const int remainder = file.size() % segmentSize; | |
const int segmentLoops = file.size() / segmentSize; | |
char bytes[segmentSize]; | |
for (int x=0; x<segmentLoops; x++) | |
{ | |
input.read(bytes, segmentSize); | |
#pragma unroll 4096 | |
for (int i=0; i<segmentSize; i++) | |
{ | |
// This block reduces IO rate from 1.6GB/s | |
// to 1GB/s loosing 600MB/s. | |
checksum += bytes[i]; | |
} | |
bytesRead += segmentSize; | |
} | |
// Moving this out here rather than a condition in the loop above | |
// reduced branch mispredictions. | |
if (remainder > 0) | |
{ | |
input.read(bytes, remainder); | |
for (int i=0; i<remainder; i++) | |
{ | |
checksum += bytes[i]; | |
} | |
bytesRead += remainder; | |
} | |
input.close(); | |
file.close(); // Unmap the file. | |
if (checksum == 43089565243) | |
{ | |
cout << "Checksum passed" << endl; | |
} | |
} | |
else | |
{ | |
cout << "could not map the file" << std::endl; | |
} | |
elapsed = timer.elapsed().wall; | |
} | |
cout << bytesRead / 1048576 << "MB at " << bytesRead / 1048576 / (elapsed/ 1000000000) << " MB/s" << endl; | |
cout << "Checksum: " << checksum << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I achieved great speedup by using an array of 64bit int and doing 64bit addition instead of 8 times as many 8bit addition
.