Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Compiled with: g++ main.cpp -fopenmp -O2 -o main
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <sys/time.h>
- #include <iomanip>
- #include <omp.h>
- #include <set>
- #include <unistd.h>
- #include <ios>
- #include <fstream>
- #include <string>
- using namespace std;
- class Timer
- {
- private:
- timeval startTime;
- public:
- void start()
- {
- gettimeofday(&startTime, NULL);
- }
- double stop()
- {
- timeval endTime;
- long seconds, useconds;
- double duration;
- gettimeofday(&endTime, NULL);
- seconds = endTime.tv_sec - startTime.tv_sec;
- useconds = endTime.tv_usec - startTime.tv_usec;
- duration = seconds + useconds/1000000.0;
- return duration;
- }
- static void printTime(double duration)
- {
- cout << setprecision(10) << fixed << duration << " seconds" << endl;
- }
- };
- static inline long hash(const char* str)
- {
- return (*(long*)str)>> 2;
- }
- int coll;
- set<long> mymap;
- //////////////////////////////////////////////////////////////////////////////
- //
- // process_mem_usage(double &, double &) - takes two doubles by reference,
- // attempts to read the system-dependent data for a process' virtual memory
- // size and resident set size, and return the results in KB.
- //
- // On failure, returns 0.0, 0.0
- void process_mem_usage(double& vm_usage, double& resident_set)
- {
- using std::ios_base;
- using std::ifstream;
- using std::string;
- vm_usage = 0.0;
- resident_set = 0.0;
- // 'file' stat seems to give the most reliable results
- //
- ifstream stat_stream("/proc/self/stat",ios_base::in);
- // dummy vars for leading entries in stat that we don't care about
- //
- string pid, comm, state, ppid, pgrp, session, tty_nr;
- string tpgid, flags, minflt, cminflt, majflt, cmajflt;
- string utime, stime, cutime, cstime, priority, nice;
- string O, itrealvalue, starttime;
- // the two fields we want
- //
- unsigned long vsize;
- long rss;
- stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
- >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
- >> utime >> stime >> cutime >> cstime >> priority >> nice
- >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
- stat_stream.close();
- long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
- vm_usage = vsize / 1024.0;
- resident_set = rss * page_size_kb;
- }
- Timer timer;
- void signal_handlerkill(int sig)
- {
- cout << "Number of collisions: " << coll << endl;
- cout << "Map size: " << mymap.size() << endl;
- double vm, rss;
- process_mem_usage(vm, rss);
- vm /= 1024.0;
- rss /= 1024.0;
- cout << "VM: " << vm << "MB" << endl;
- timer.printTime(timer.stop());
- exit(1);
- }
- int main()
- {
- signal(SIGINT, signal_handlerkill);
- timer = Timer();
- timer.start();
- coll = 0;
- #pragma omp parallel for
- for (int i = 0; i < 256; i++)
- for (int j = 0; j < 256; j++)
- for (int k = 0; k < 256; k++)
- //for (int l = 0; l < 256; l++)
- {
- // string temp;
- // temp = i;
- // temp += j;
- // temp += k;
- // //temp += l;
- // temp += temp;
- //const char temp[7] = {i, j, k, l, i, j, k};
- const char temp[7] = {i, j, k, i, j, k,'\0'};
- long myhash = hash(temp);
- if(mymap.count(myhash))
- {
- #pragma omp critical
- {
- coll++;
- cout << "Collision at " << i << "." << j << "." << k << endl;
- }
- }
- else
- {
- #pragma omp critical
- mymap.insert(myhash);
- }
- }
- cout << "Number of collisions: " << coll << endl;
- cout << "Map size: " << mymap.size() << endl;
- double vm, rss;
- process_mem_usage(vm, rss);
- vm /= 1024.0;
- rss /= 1024.0;
- cout << "VM: " << vm << "MB" << endl;
- timer.printTime(timer.stop());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement