0% found this document useful (0 votes)
6 views17 pages

OpenMP and MPI Programming Examples

The document contains multiple OpenMP and MPI programs demonstrating various parallel programming techniques. Programs include implementations of mergesort, Fibonacci number calculation, prime number identification, and communication methods using MPI such as send/receive, broadcast, scatter/gather, and reduction operations. Each program includes code snippets and execution time measurements for both sequential and parallel approaches.

Uploaded by

abhimanyuhm711
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)
6 views17 pages

OpenMP and MPI Programming Examples

The document contains multiple OpenMP and MPI programs demonstrating various parallel programming techniques. Programs include implementations of mergesort, Fibonacci number calculation, prime number identification, and communication methods using MPI such as send/receive, broadcast, scatter/gather, and reduction operations. Each program includes code snippets and execution time measurements for both sequential and parallel approaches.

Uploaded by

abhimanyuhm711
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

PROGRAM-1

Write a OpenMP program to sort an array on n elements using both sequential and parallel
mergesort(using Section). Record the difference in execution time.

#include <stdio.h>

#include <stdlib.h>

#include <omp.h>

#define N 10000

void merge(int *a, int l, int m, int r) {

int temp[N], i = l, j = m + 1, k = 0;

while (i <= m && j <= r) {

if (a[i] < a[j]) {

temp[k++] = a[i++];

} else {

temp[k++] = a[j++];

while (i <= m) temp[k++] = a[i++];

while (j <= r) temp[k++] = a[j++];

for (i = l, k = 0; i <= r; i++, k++) a[i] = temp[k];

void mergesort_seq(int *a, int l, int r) {

if (l < r) {

int m = (l + r) / 2;
mergesort_seq(a, l, m);

mergesort_seq(a, m + 1, r);

merge(a, l, m, r);

void mergesort_par(int *a, int l, int r) {

if (l < r) {

int m = (l + r) / 2;

#pragma omp parallel sections

#pragma omp section

mergesort_seq(a, l, m);

#pragma omp section

mergesort_seq(a, m + 1, r);

merge(a, l, m, r);

int main() {

int a[N], b[N];

for (int i = 0; i < N; i++) {

a[i] = rand() % 10000;

b[i] = a[i];

}
double t1 = omp_get_wtime();

mergesort_seq(a, 0, N - 1);

double t2 = omp_get_wtime();

printf("Sequential: %f sec\n", t2 - t1);

t1 = omp_get_wtime();

mergesort_par(b, 0, N - 1);

t2 = omp_get_wtime();

printf("Parallel: %f sec\n", t2 - t1);

return 0;

OUTPUT

Program-2

Write an OpenMP program that divides the Iterations into chunks containing 2 iterations,
respectively (OMP_SCHEDULE=static,2). Its input should be the number of iterations, and its output
should be which iterations of a parallelized for loop are executed by which thread. For example, if
there are two threads and four iterations, the output might be the following:

a. Thread 0 : Iterations 0 - 1

b. Thread 1 : Iterations 2 – 3
#include <stdio.h>

#include <omp.h>

int main() {

int n;

printf("Enter number of iterations: ");

scanf("%d", &n);

omp_set_num_threads(4); // You can change the number of threads

#pragma omp parallel

int tid = omp_get_thread_num();

#pragma omp for schedule(static, 2)

for (int i = 0; i < n; i++) {

printf("Iteration %d is handled by thread %d\n", i, tid);

return 0;

OUTPUT
Program-3

Write a OpenMP program to calculate n Fibonacci numbers using tasks.

#include <stdio.h>

#include <stdlib.h>

#include <omp.h>

// Recursive Fibonacci using OpenMP tasks

int fib(int n) {

int x, y;

if (n < 2)

return n;

#pragma omp task shared(x)

x = fib(n - 1);

#pragma omp task shared(y)

y = fib(n - 2);

#pragma omp taskwait

return x + y;
}

int main() {

int n;

printf("Enter number of Fibonacci terms: ");

scanf("%d", &n);

printf("Fibonacci numbers:\n");

double start = omp_get_wtime();

#pragma omp parallel

#pragma omp single

for (int i = 0; i < n; i++) {

int result = fib(i);

printf("fib(%d) = %d\n", i, result);

double end = omp_get_wtime();

printf("Execution Time: %f seconds\n", end - start);

return 0;

}
Output

Program-4

Write a OpenMP program to find the prime numbers from 1 to n employing parallel for directive.
Record both serial and parallel execution times.

#include <stdio.h>

#include <omp.h>

int is_prime(int n) {

if (n < 2) return 0;

for (int i = 2; i * i <= n; i++)

if (n % i == 0) return 0;

return 1;

int main() {

int n;

printf("Enter n: ");

scanf("%d", &n);
double t1 = omp_get_wtime();

for (int i = 2; i <= n; i++)

if (is_prime(i)) printf("%d ", i);

printf("\nSerial Time: %f\n", omp_get_wtime() - t1);

double t2 = omp_get_wtime();

#pragma omp parallel for

for (int i = 2; i <= n; i++)

if (is_prime(i))

#pragma omp critical

printf("%d ", i);

printf("\nParallel Time: %f\n", omp_get_wtime() - t2);

return 0;

OUTPUT
Program-5

Write a MPI Program to demonstration of MPI_Send and MPI_Recv.

#include <mpi.h>

#include <stdio.h>

#include <string.h>

int main(int argc, char *argv[]) {

int rank, size;

char message[100];

MPI_Init(&argc, &argv); // Initialize MPI

MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get current process rank

MPI_Comm_size(MPI_COMM_WORLD, &size); // Get total number of processes

if (size < 2) {

if (rank == 0)

printf("This program requires at least 2 processes.\n");

MPI_Finalize();

return 0;

if (rank == 0) {

// Process 0 sends message

strcpy(message, "Hello from Process 0!");


MPI_Send(message, strlen(message) + 1, MPI_CHAR, 1, 0, MPI_COMM_WORLD);

printf("Process 0 sent message: %s\n", message);

} else if (rank == 1) {

// Process 1 receives message

MPI_Recv(message, 100, MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

printf("Process 1 received message: %s\n", message);

MPI_Finalize(); // Finalize MPI

return 0;

OUTPUT

Program-6

Write a MPI program to demonstration of deadlock using point to point communication and
avoidance of deadlock by altering the call sequence

#include <mpi.h>

#include <stdio.h>

#include <stdlib.h>
int main(int argc, char *argv[]) {

int rank, mode = 1, data = 100, recv;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (argc > 1) mode = atoi(argv[1]);

if (rank == 0 && mode) printf("Running in DEADLOCK mode...\n");

if (rank == 0 && !mode) printf("Running in SAFE mode...\n");

if (mode) {

// Deadlock: both send then receive

if (rank == 0) {

MPI_Send(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

MPI_Recv(&recv, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

} else {

MPI_Send(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);

MPI_Recv(&recv, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

} else {

// Safe: one recv then send

if (rank == 0) {

MPI_Send(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);

MPI_Recv(&recv, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

} else {

MPI_Recv(&recv, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);


MPI_Send(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);

printf("Process %d received %d\n", rank, recv);

MPI_Finalize();

return 0;

OUTPUT
PROGRAM-7

Write a MPI Program to demonstration of Broadcast operation

// mpi_broadcast_demo.c

#include <mpi.h>

#include <stdio.h>

int main(int argc, char *argv[]) {

int rank, size;

int data;

MPI_Init(&argc, &argv); // Initialize MPI

MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get process rank

MPI_Comm_size(MPI_COMM_WORLD, &size); // Get number of processes

if (rank == 0) {

data = 42; // The root process sets the value

printf("Process %d broadcasting data = %d\n", rank, data);

// Broadcast the value of 'data' from process 0 to all

MPI_Bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);


// Every process prints the received value

printf("Process %d received data = %d\n", rank, data);

MPI_Finalize(); // Finalize MPI

return 0;

OUTPUT

PROGRAM-8

Write a MPI Program demonstration of MPI_Scatter and MPI_Gather

#include <mpi.h>

#include <stdio.h>

int main(int argc, char *argv[]) {

int rank, size, send[4], recv;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

// Only works correctly with 4 processes

if (size != 4) {

if (rank == 0) printf("Please run with 4 processes.\n");

MPI_Finalize();

return 0;

if (rank == 0) {

for (int i = 0; i < 4; i++) send[i] = i + 1; // send = [1, 2, 3, 4]

MPI_Scatter(send, 1, MPI_INT, &recv, 1, MPI_INT, 0, MPI_COMM_WORLD);

recv *= 10; // Each process modifies its data

MPI_Gather(&recv, 1, MPI_INT, send, 1, MPI_INT, 0, MPI_COMM_WORLD);

if (rank == 0) {

printf("Gathered data: ");

for (int i = 0; i < 4; i++) printf("%d ", send[i]);

printf("\n");

MPI_Finalize();

return 0;

OUTPUT
PROGRAM-9

Write a MPI Program to demonstration of MPI_Reduce and MPI_Allreduce (MPI_MAX, MPI_MIN,


MPI_SUM, MPI_PROD)

#include <mpi.h>

#include <stdio.h>

int main(int argc, char *argv[]) {

int rank, value;

int sum, prod, max, min;

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &rank);

value = rank + 1; // Each process contributes rank+1

MPI_Reduce(&value, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

MPI_Allreduce(&value, &prod, 1, MPI_INT, MPI_PROD, MPI_COMM_WORLD);


MPI_Allreduce(&value, &max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);

MPI_Allreduce(&value, &min, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);

if (rank == 0)

printf("Reduce SUM (at root): %d\n", sum);

printf("Process %d: PROD=%d MAX=%d MIN=%d\n", rank, prod, max, min);

MPI_Finalize();

return 0;

OUTPUT

You might also like