Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdio.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- using namespace std;
- string filepath;
- unsigned int READBLOCKSIZE = 1024*1024;
- unsigned long long nFileLength = 0;
- unsigned long long accumulator = 0; // assuming 32 bit OS running on X86-64
- time_t seconds1;
- time_t seconds2;
- int main(int argc, char *argv[])
- {
- if (argc < 2)
- {
- cout << "Please enter a file path\n";
- return -1;
- }
- seconds1 = time (NULL);
- cout << "Start Time in seconds since January 1, 1970 -> " << seconds1 << "\n";
- int fd = fd = open(argv[1], O_RDONLY);
- if(fd == -1) cout << "Could not find/open input file\n";
- struct stat sb;
- if (fstat(fd, &sb) == -1) cout << "Could not get stat on input file\n"; /* To obtain file size */
- off_t pa_offset = 0 & ~(sysconf(_SC_PAGE_SIZE) - 1); // page align from start of file
- char* addr = (char*)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, pa_offset);
- if (addr == MAP_FAILED) cout << "mmap(2) failed\n";
- cout << "Using One Thread\n"; //**
- //READBLOCKSIZE *= 64;
- char* readBuf = new char[READBLOCKSIZE];
- if(0 == readBuf) return -1;
- unsigned int startOffset = 0;
- if(nFileLength > READBLOCKSIZE)
- {
- while(startOffset + READBLOCKSIZE < nFileLength)
- {
- int* num = reinterpret_cast<int*>(addr + startOffset);
- for(unsigned int i = 0 ; i < (READBLOCKSIZE/4) ; i++)
- {
- accumulator += *(num + i);
- }
- startOffset += READBLOCKSIZE;
- }
- }
- if(nFileLength - startOffset > 0)
- {
- int* num = reinterpret_cast<int*>(addr + startOffset);
- for(unsigned int i = 0 ; i < ((nFileLength - startOffset)/4) ; ++i)
- {
- accumulator += *(num + i);
- }
- }
- delete[] readBuf; readBuf = 0;
- seconds2 = time (NULL);
- cout << "End Time in seconds since January 1, 1970 -> " << seconds2 << "\n";
- cout << "Total time to add " << nFileLength/4 << " integers -> " << seconds2 - seconds1 << " seconds\n";
- cout << accumulator << "\n";
- //fclose(pFile);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement