Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <array>
- #include <amp.h>
- #include <stdio.h>
- #include <tchar.h>
- using namespace concurrency;
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- LARGE_INTEGER timeStart;
- QueryPerformanceCounter(&timeStart);
- std::array<int, 10500> primes;
- array_view<int, 1> primesAV(10500, primes);
- int numPrimes = 0;
- array_view<int> numPrimesAV(1, &numPrimes);
- concurrency::extent<1> numKernels(150000);
- parallel_for_each(
- numKernels,
- [=](index<1> idx) mutable restrict(amp)
- {
- int curIdx = idx[0] + 2;
- bool prime=true;
- for (int j=2; j*j <= curIdx; j++)
- {
- if (curIdx % j == 0)
- {
- prime=false;
- break;
- }
- }
- if(prime)
- {
- int curNumPrimes = atomic_fetch_inc(&numPrimesAV(0));
- if(curNumPrimes <= 10500)
- primesAV[curNumPrimes] = curIdx;
- }
- }
- );
- primesAV.synchronize();
- numPrimesAV.synchronize();
- std::sort(begin(primes), end(primes));
- LARGE_INTEGER timeEnd;
- QueryPerformanceCounter(&timeEnd);
- float timeDiff = timeEnd.LowPart - timeStart.LowPart;
- LARGE_INTEGER freqPerSecond;
- QueryPerformanceFrequency(&freqPerSecond);
- float time = timeDiff/(float)freqPerSecond.LowPart;
- for(int i = 0; i < 10000; i++)
- {
- cout << primes[i] << " ";
- }
- cout<<"Time elapsed: "<<time;
- int a;
- cin>>a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment