Advertisement
Guest User

Read Integers using mmap

a guest
Jun 12th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <sys/mman.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9.  
  10. using namespace std;
  11.  
  12. string filepath;
  13. unsigned int READBLOCKSIZE = 1024*1024;
  14. unsigned long long nFileLength = 0;
  15.  
  16. unsigned long long accumulator = 0; // assuming 32 bit OS running on X86-64
  17.  
  18. time_t seconds1;
  19. time_t seconds2;
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.     if (argc < 2)
  24.     {
  25.         cout << "Please enter a file path\n";
  26.         return -1;
  27.     }
  28.    
  29.     seconds1 = time (NULL);
  30.     cout << "Start Time in seconds since January 1, 1970 -> " << seconds1 << "\n";
  31.  
  32.     int fd = fd = open(argv[1], O_RDONLY);
  33.     if(fd == -1) cout << "Could not find/open input file\n";
  34.  
  35.     struct stat sb;
  36.     if (fstat(fd, &sb) == -1) cout << "Could not get stat on input file\n";          /* To obtain file size */
  37.        
  38.     off_t pa_offset = 0 & ~(sysconf(_SC_PAGE_SIZE) - 1); // page align from start of file
  39.  
  40.     char* addr = (char*)mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, pa_offset);
  41.         if (addr == MAP_FAILED) cout << "mmap(2) failed\n";
  42.  
  43.  
  44.  
  45.         cout << "Using One Thread\n"; //**
  46.         //READBLOCKSIZE *= 64;
  47.         char* readBuf = new char[READBLOCKSIZE];
  48.         if(0 == readBuf) return -1;
  49.  
  50.         unsigned int startOffset = 0;  
  51.         if(nFileLength >  READBLOCKSIZE)
  52.         {
  53.             while(startOffset + READBLOCKSIZE < nFileLength)
  54.             {
  55.                 int* num = reinterpret_cast<int*>(addr + startOffset);
  56.                 for(unsigned int i = 0 ; i < (READBLOCKSIZE/4) ; i++)
  57.                 {
  58.                     accumulator += *(num + i); 
  59.                 }
  60.                 startOffset += READBLOCKSIZE;
  61.             }
  62.    
  63.         }
  64.  
  65.         if(nFileLength - startOffset > 0)
  66.         {
  67.             int* num = reinterpret_cast<int*>(addr + startOffset);
  68.             for(unsigned int i = 0 ; i < ((nFileLength - startOffset)/4) ; ++i)
  69.             {
  70.                 accumulator += *(num + i); 
  71.             }
  72.         }
  73.         delete[] readBuf; readBuf = 0;
  74.  
  75.  
  76.     seconds2 = time (NULL);
  77.     cout << "End Time in seconds since January 1, 1970 -> " << seconds2 << "\n";
  78.     cout << "Total time to add " << nFileLength/4 << " integers -> " << seconds2 - seconds1 << " seconds\n";
  79.  
  80.     cout << accumulator << "\n";
  81.     //fclose(pFile);     
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement