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:
parent
1032e099a1
commit
3bb757dc0d
25
src/Main.cpp
25
src/Main.cpp
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user