I am trying to make the following program parallel using OpenMP :
#include <time.h> // Program computes the total number of primes larger than 100000001 and smaller than 16000001. main() { int number = 100000001; int primes[20]; int i, j, is_prime, index = 0, nprimes = 0; time_t start_time, end_time; start_time = time(NULL); for (i = 0; i < 3000000; i++) { // get the next number to check if it is a prime number += 2; is_prime = 1; for (j = 2; j < 10001; j++) { if ((number % j) == 0) { is_prime = 0; break; } } // f0und a prime number. Count it and save the first 20 primes if (is_prime) nprimes++; if (is_prime && (index < 20)) { primes[index] = number; index++; } } for (i = 0; i < 20; i++) printf("%d is prime\n", primes[i]); end_time = time(NULL); printf("number of primes = %d, elapsed time is %d seconds\n", nprimes, end_time - start_time); } What I have done is this:
#include <stdio.h> #include <time.h> #include <omp.h> #define CHUNKSIZE 750000 //#define CHUNKSIZE2 2500 // Program computes the total number of primes larger than 100000001 and smaller than 16000001. int main() { int number = 100000001; int primes[20]; int i, j, is_prime, index = 0, nprimes = 0; time_t start_time, end_time; start_time = time(NULL); int chunk = CHUNKSIZE; //int chunk2 = CHUNKSIZE2; #pragma omp parallel shared(number, index, nprimes, chunk) private(i, j, is_prime) { #pragma omp parallel for schedule (dynamic, chunk) for (i = 0; i < 3000000; i++) { // get the next number to check if it is a prime number += 2; is_prime = 1; //#pragma omp parallel for schedule (dynamic, chunk2) for (j = 2; j < 10001; j++) { if ((number % j) == 0) { is_prime = 0; break; } } // f0und a prime number. Count it and save the first 20 primes if (is_prime) nprimes++; if (is_prime && (index < 20)) { primes[index] = number; index++; } } for (i = 0; i < 20; i++) printf("%d is prime\n", primes[i]); end_time = time(NULL); printf("number of primes = %d, elapsed time is %d seconds\n", nprimes, end_time - start_time); //return 0; } I have tried many things but most of them gave me longer or same time !!!