Basic multi-core capabilities.

It's buggy, at some point it gives a segmentation fault at around the
`for(auto i : primes)` part. I think I should add a pragma critical
there.
This commit is contained in:
Nicolás A. Ortega 2016-11-30 21:08:44 +01:00
parent 1032e099a1
commit 3bb757dc0d
No known key found for this signature in database
GPG Key ID: 614272579C2070D1

View File

@ -1,6 +1,8 @@
#include <iostream> #include <iostream>
#include <omp.h>
#include <vector> #include <vector>
#include <csignal> #include <csignal>
#include <cassert>
static bool run; static bool run;
@ -16,17 +18,32 @@ int main(void) {
primes.push_back(2); primes.push_back(2);
unsigned long long num = 2; unsigned long long num = 2;
while(run) { // Use for to accomodate for OpenMP
bool isPrime = true; #pragma omp parallel
for(auto i : primes) { {
if(i > num / 2) break; do {
if(num % i == 0) isPrime = false; unsigned long long myNum;
} #pragma omp critical
if(isPrime) { {
primes.push_back(num); myNum = num;
std::cout << num << std::endl; ++num;
} }
++num; bool isPrime = true;
for(auto i : primes) {
if(i > myNum / 2) break;
if(myNum % i == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
#pragma omp critical
{
primes.push_back(num);
std::cout << num << std::endl;
}
}
} while(run);
} }
return 0; return 0;