I wrote a little program to test the performance gain of OpenMP. I compile using Microsoft Visual Studio.
void findAllPrimesUntilX() {
for (int i = 2; i <= upToXthPrimes; i++) {
if (i % 500 == 0) std::cout << "First " << i * 500 << "primes have been checked\n";
if (checkPrime(i)) primes.push_back(i);
}
std::cout << "All primes have been calculated!\n";
}
this is the function calling "checkPrime(i)" which looks like this:
bool checkPrime(int n) {
if (n == 2) return true;
if (n < 2 || n % 2 == 0) return false;
#pragma omp parallel for
for (int i = 3; i <= static_cast<int>(sqrt(n)); i += 2) {
if (n % i == 0) return false;
}
return true;
}
I am now getting a "C1001 Error : An internal error has occured in the compiler."
Removing the #pragma omp parallel for solves this problem. So what's the deal?
Thanks in advance
Folling
returnstatements orgototo labels outside the parallel region are not allowed. The compiler should issue an error, but Microsoft's OpenMP implementation is very old and unsupported and apparently very broken.std::vectorthough. Either reserve a chunk of memory ahead of time that will contain all the primes or fill privatestd::vectorfor each thread and then join them afterwards.