here in this code, I'm trying to do Sieve of Eratosthenes algorithm in parallel using OpenMP when the program is serial it's working fine in
for (int i = p * p; i <= n; i += p) prime[i] = false;
but when I'm using
#pragma omp for
it give this error
error: invalid controlling predicate 17 | for (int p = 2; p * p <= n; p++)
this is the code
#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
using namespace std;
void SieveOfEratosthenes(int n)
{
#pragma omp parallel
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
#pragma omp for
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
#pragma omp critical
for (int p = 2; p <= n; p++)
if (prime[p])
printf("%d ", p);
}
}
int main()
{
printf("Enter the siza: ");
int n ;
scanf("%d", &n);
printf("Following are the prime numbers from 1 to %f \n", sqrt(n));
SieveOfEratosthenes(sqrt(n));
printf("\n");
return 0;
}
n
should be small as arrayprime
created on the stack, so performance is not an issue here. – Laci