From 3bb757dc0d817c5ed46dd485fbaab7b78232b0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Wed, 30 Nov 2016 21:08:44 +0100 Subject: [PATCH] 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. --- src/Main.cpp | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index f0686e4..66d67e9 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,6 +1,8 @@ #include +#include #include #include +#include static bool run; @@ -16,17 +18,32 @@ int main(void) { primes.push_back(2); unsigned long long num = 2; - while(run) { - bool isPrime = true; - for(auto i : primes) { - if(i > num / 2) break; - if(num % i == 0) isPrime = false; - } - if(isPrime) { - primes.push_back(num); - std::cout << num << std::endl; - } - ++num; + // 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 > 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;