Guest User

Untitled

a guest
Aug 12th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. // Prim3r.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <windows.h>
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include <vector>
  10. #include <thread>
  11. #include <atomic>
  12. #include <algorithm>
  13. #include <mutex>
  14.  
  15. //-----------------------------------------------------------------------------
  16.  
  17. typedef std::vector<UINT> uint_vect;
  18.  
  19. std::atomic_uint_fast32_t pool_at, pool_advance, pool_primes, pool_primes_need;
  20. std::mutex pool_mutex, prime_mutex;
  21. //-----------------------------------------------------------------------------
  22.  
  23. inline bool IsPrime(UINT num, uint_vect *vect)
  24. {
  25.     UINT primes_count;
  26.  
  27.     if(num <= 1)
  28.     return false;
  29.  
  30.     {
  31.         //std::lock_guard<std::mutex> l(prime_mutex);
  32.  
  33.         primes_count = vect->size();
  34.     }
  35.  
  36.     for(UINT i = 0; i < primes_count; i++)
  37.     {
  38.         UINT p = vect->at(i);
  39.  
  40.         if(p * p > num)
  41.         break;
  42.  
  43.         if((num % p) == 0)
  44.         return false;
  45.     }
  46.  
  47.     return true;
  48. }
  49.  
  50. //-----------------------------------------------------------------------------
  51.  
  52. UINT FindPrimes(UINT from, UINT to, uint_vect *vect)
  53. {
  54.     UINT found = 0;
  55.  
  56.     for(UINT n = from; n < to; n++)
  57.     {
  58.         if(IsPrime(n, vect))
  59.         {
  60.             std::lock_guard<std::mutex> l(prime_mutex);
  61.  
  62.             vect->push_back(n);
  63.            
  64.             found += 1;
  65.         }
  66.     }
  67.  
  68.     return found;
  69. }
  70.  
  71. //-----------------------------------------------------------------------------
  72.  
  73. void PrimeWorker(uint_vect *vect)
  74. {
  75.     while(pool_primes < pool_primes_need)
  76.     {
  77.         UINT start, end;
  78.  
  79.         {
  80.             std::lock_guard<std::mutex> l(pool_mutex);
  81.  
  82.             start = pool_at;
  83.             end   = start + pool_advance;
  84.  
  85.             pool_at = end;
  86.         }
  87.  
  88.         UINT n = FindPrimes(start, end, vect);
  89.  
  90.         pool_primes += n;
  91.     }
  92. }
  93.  
  94. //-----------------------------------------------------------------------------
  95.  
  96. int _tmain(int argc, _TCHAR* argv[])
  97. {
  98.     UINT find_them = 10000000;
  99.     UINT amount    = 1000;
  100.  
  101.     uint_vect primes;
  102.  
  103.     primes.reserve(find_them / 4);
  104.  
  105.     LARGE_INTEGER perf_start, perf_end, perf_freq;
  106.  
  107.     printf("Looking for %u Primes...\n", find_them);
  108.  
  109.     pool_at = 0;
  110.     pool_advance = amount;
  111.     pool_primes  = 0;
  112.     pool_primes_need = find_them;
  113.  
  114.     QueryPerformanceCounter(&perf_start);
  115.  
  116.     std::thread th1(PrimeWorker, &primes);
  117.     std::thread th2(PrimeWorker, &primes);
  118.     std::thread th3(PrimeWorker, &primes);
  119.     std::thread th4(PrimeWorker, &primes);
  120.  
  121.     th1.join();
  122.     th2.join();
  123.     th3.join();
  124.     th4.join();
  125.  
  126.     std::sort(primes.begin(), primes.end());
  127.  
  128.     QueryPerformanceCounter(&perf_end);
  129.  
  130.     QueryPerformanceFrequency(&perf_freq);
  131.  
  132.     float perf_time = (perf_end.QuadPart - perf_start.QuadPart) * 1000.0f / perf_freq.QuadPart;
  133.  
  134.     printf("Found %u primes in %.3fms (%u k/sec)\n", primes.size(), perf_time, (UINT)(primes.size() / perf_time));
  135.  
  136.     primes.resize(find_them);
  137.  
  138.     printf("First 10: ");
  139.  
  140.         for(int i = 0; i < 10; i++)
  141.         {
  142.             printf("%u ", primes[i]);
  143.         }
  144.  
  145.     printf("\n");
  146.  
  147.     printf("Last 10: ");
  148.  
  149.         for(int i = primes.size() - 10; i < primes.size(); i++)
  150.         {
  151.             printf("%u ", primes[i]);
  152.         }
  153.  
  154.     printf("\n");
  155.  
  156.  
  157.     system("pause");
  158.  
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment