Guest User

Untitled

a guest
Aug 4th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3. #include <amp.h>
  4. #include <stdio.h>
  5. #include <tchar.h>
  6. using namespace concurrency;
  7. using namespace std;
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11.     LARGE_INTEGER timeStart;
  12.     QueryPerformanceCounter(&timeStart);
  13.  
  14.     std::array<int, 10500> primes;
  15.     array_view<int, 1> primesAV(10500, primes);
  16.     int numPrimes = 0;
  17.     array_view<int> numPrimesAV(1, &numPrimes);
  18.     concurrency::extent<1> numKernels(150000);
  19.  
  20.     parallel_for_each(
  21.         numKernels,
  22.         [=](index<1> idx) mutable restrict(amp)
  23.     {
  24.         int curIdx = idx[0] + 2;
  25.  
  26.         bool prime=true;
  27.         for (int j=2; j*j <= curIdx; j++)
  28.         {
  29.             if (curIdx % j == 0)
  30.             {
  31.                 prime=false;
  32.                 break;    
  33.             }
  34.         }  
  35.  
  36.         if(prime)
  37.         {
  38.             int curNumPrimes = atomic_fetch_inc(&numPrimesAV(0));
  39.             if(curNumPrimes <= 10500)
  40.                 primesAV[curNumPrimes] = curIdx;
  41.         }
  42.     }
  43.     );
  44.  
  45.     primesAV.synchronize();
  46.     numPrimesAV.synchronize();
  47.  
  48.     std::sort(begin(primes), end(primes));
  49.  
  50.     LARGE_INTEGER timeEnd;
  51.     QueryPerformanceCounter(&timeEnd);
  52.     float timeDiff = timeEnd.LowPart - timeStart.LowPart;
  53.  
  54.     LARGE_INTEGER freqPerSecond;
  55.     QueryPerformanceFrequency(&freqPerSecond);
  56.     float time = timeDiff/(float)freqPerSecond.LowPart;
  57.  
  58.     for(int i = 0; i < 10000; i++)
  59.     {
  60.         cout << primes[i] << " ";
  61.     }
  62.  
  63.     cout<<"Time elapsed: "<<time;
  64.  
  65.     int a;
  66.     cin>>a;
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment