Advertisement
Guest User

Untitled

a guest
May 15th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. Compiled with: g++ main.cpp -fopenmp -O2 -o main
  2.  
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sys/time.h>
  8. #include <iomanip>
  9. #include <omp.h>
  10. #include <set>
  11. #include <unistd.h>
  12. #include <ios>
  13. #include <fstream>
  14. #include <string>
  15.  
  16. using namespace std;
  17.  
  18. class Timer
  19. {
  20. private:
  21.  
  22.   timeval startTime;
  23.  
  24. public:
  25.  
  26.   void start()
  27.   {
  28.     gettimeofday(&startTime, NULL);
  29.   }
  30.  
  31.   double stop()
  32.   {
  33.     timeval endTime;
  34.     long seconds, useconds;
  35.     double duration;
  36.    
  37.     gettimeofday(&endTime, NULL);
  38.    
  39.     seconds  = endTime.tv_sec  - startTime.tv_sec;
  40.     useconds = endTime.tv_usec - startTime.tv_usec;
  41.    
  42.     duration = seconds + useconds/1000000.0;
  43.    
  44.     return duration;
  45.   }
  46.  
  47.   static void printTime(double duration)
  48.   {
  49.     cout << setprecision(10) << fixed << duration << " seconds" << endl;
  50.   }
  51. };
  52.  
  53. static inline long hash(const char* str)
  54. {
  55.   return (*(long*)str)>> 2;
  56. }  
  57. int coll;
  58. set<long> mymap;
  59.  
  60. //////////////////////////////////////////////////////////////////////////////
  61. //
  62. // process_mem_usage(double &, double &) - takes two doubles by reference,
  63. // attempts to read the system-dependent data for a process' virtual memory
  64. // size and resident set size, and return the results in KB.
  65. //
  66. // On failure, returns 0.0, 0.0
  67.  
  68. void process_mem_usage(double& vm_usage, double& resident_set)
  69. {
  70.   using std::ios_base;
  71.   using std::ifstream;
  72.   using std::string;
  73.  
  74.   vm_usage     = 0.0;
  75.   resident_set = 0.0;
  76.  
  77.   // 'file' stat seems to give the most reliable results
  78.   //
  79.   ifstream stat_stream("/proc/self/stat",ios_base::in);
  80.  
  81.   // dummy vars for leading entries in stat that we don't care about
  82.   //
  83.   string pid, comm, state, ppid, pgrp, session, tty_nr;
  84.   string tpgid, flags, minflt, cminflt, majflt, cmajflt;
  85.   string utime, stime, cutime, cstime, priority, nice;
  86.   string O, itrealvalue, starttime;
  87.  
  88.   // the two fields we want
  89.   //
  90.   unsigned long vsize;
  91.   long rss;
  92.  
  93.   stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
  94.               >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
  95.               >> utime >> stime >> cutime >> cstime >> priority >> nice
  96.               >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
  97.  
  98.   stat_stream.close();
  99.  
  100.   long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
  101.   vm_usage     = vsize / 1024.0;
  102.   resident_set = rss * page_size_kb;
  103. }
  104. Timer timer;
  105. void signal_handlerkill(int sig)
  106. {
  107.   cout << "Number of collisions: " << coll << endl;
  108.   cout << "Map size: " << mymap.size() << endl;
  109.   double vm, rss;
  110.   process_mem_usage(vm, rss);
  111.   vm /= 1024.0;
  112.   rss /= 1024.0;
  113.   cout << "VM: " << vm << "MB" << endl;
  114.   timer.printTime(timer.stop());
  115.   exit(1);
  116. }
  117. int main()
  118. {
  119.   signal(SIGINT, signal_handlerkill);
  120.   timer = Timer();
  121.   timer.start();
  122.   coll = 0;
  123.  
  124. #pragma omp parallel for
  125.   for (int i = 0; i < 256; i++)
  126.     for (int j = 0; j < 256; j++)
  127.       for (int k = 0; k < 256; k++)
  128.         //for (int l = 0; l < 256; l++)
  129.       {
  130. //                string temp;
  131. //                temp = i;
  132. //                temp += j;
  133. //                temp += k;
  134. //                //temp += l;
  135. //                temp += temp;
  136.         //const char temp[7] = {i, j, k, l, i, j, k};
  137.         const char temp[7] = {i, j, k, i, j, k,'\0'};
  138.         long myhash = hash(temp);
  139.        
  140.         if(mymap.count(myhash))
  141.         {
  142. #pragma omp critical
  143.           {
  144.             coll++;
  145.             cout << "Collision at " << i << "." << j << "." << k << endl;
  146.           }
  147.         }
  148.         else
  149.         {
  150. #pragma omp critical
  151.           mymap.insert(myhash);
  152.         }
  153.       }
  154.  
  155.   cout << "Number of collisions: " << coll << endl;
  156.   cout << "Map size: " << mymap.size() << endl;  
  157.   double vm, rss;
  158.   process_mem_usage(vm, rss);
  159.   vm /= 1024.0;
  160.   rss /= 1024.0;
  161.   cout << "VM: " << vm << "MB" << endl;
  162.   timer.printTime(timer.stop());
  163.  
  164.   return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement