[go: up one dir, main page]

0% found this document useful (0 votes)
42 views3 pages

Program 4

The document provides a step-by-step guide to create an OpenMP program in Dev C++ to find prime numbers from 1 to n using parallel processing. It includes code for both serial and parallel execution, with timing measurements for each method. The program counts prime numbers and displays the execution time for both approaches, highlighting the efficiency of parallel execution as n increases.

Uploaded by

shivakumar31855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views3 pages

Program 4

The document provides a step-by-step guide to create an OpenMP program in Dev C++ to find prime numbers from 1 to n using parallel processing. It includes code for both serial and parallel execution, with timing measurements for each method. The program counts prime numbers and displays the execution time for both approaches, highlighting the efficiency of parallel execution as n increases.

Uploaded by

shivakumar31855
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a OpenMP program to find the prime numbers from 1 to n employing parallel for

directive. Record both serial and parallel execution times

STEP1

Open Dev C++

STEP2

Click on File----Project----Select Console----OpenMP

Select Project as C++ and press OK

STEP3

After Project is created under project create prime.cpp file and type code as in below

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>

int is_prime(int num) {


if (num < 2) return 0;
if (num == 2) return 1;
if (num % 2 == 0) return 0;
int limit = sqrt(num);
for (int i = 3; i <= limit; i += 2) {
if (num % i == 0) return 0;
}
return 1;
}

int main() {
int n;
printf("Enter value of n: ");
scanf("%d", &n);

double start_time, end_time;

// Serial Execution
start_time = omp_get_wtime();
int serial_count = 0;
for (int i = 2; i <= n; i++) {
if (is_prime(i)) serial_count++;
}
end_time = omp_get_wtime();
printf("Serial Execution Time: %f seconds\n", end_time - start_time);

// Parallel Execution
omp_set_num_threads(4); // Set based on CPU
int* prime_flags = (int*)calloc(n + 1, sizeof(int));

start_time = omp_get_wtime();
#pragma omp parallel for schedule(dynamic, 100)
for (int i = 2; i <= n; i++) {
if (is_prime(i)) {
prime_flags[i] = 1;
}
}
end_time = omp_get_wtime();

int parallel_count = 0;
for (int i = 2; i <= n; i++) {
if (prime_flags[i]) parallel_count++;
}

printf("Parallel Execution Time: %f seconds\n", end_time - start_time);


printf("Primes found (Serial): %d, (Parallel): %d\n", serial_count, parallel_count);

free(prime_flags);
return 0;
}
Final Step Goto Execute Menu and click on Compile and Run You can see the parallel execution is
reduced more u increase the value of n.

You might also like