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