EX. NO.
: 1 WRITE A SIMPLE PROGRAM TO DEMONSTRATE AN OPENMP FORK-
JOIN PARALLELISM.
DATE:
AIM:
To write a program to demonstrate Fork–Join Parallelism using OpenMP.
ALGORITHM:
1. Start the program
2. Include required header files (stdio.h, omp.h)
3. Set the number of threads using omp_set_num_threads()
4. Print a message before entering the parallel region
5. Create a parallel region using #pragma omp parallel (Fork)
6. Inside the parallel region, each thread prints its ID
7. Exit the parallel region (Join happens automatically)
8. Print a message after the parallel region
9. End the program
PROGRAM:
#include <stdio.h>
#include <omp.h>
int main()
{
int tid, nthreads;
// Set number of threads
omp_set_num_threads(4);
printf("Before Parallel Region\n");
// Fork: Parallel region starts
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
nthreads = omp_get_num_threads();
printf("Hello from Thread %d out of %d threads\n", tid, nthreads);
}
// Join: Parallel region ends
printf("After Parallel Region\n");
return 0;
}
OUTPUT:
Before Parallel Region
Hello from Thread 1 out of 4 threads
Hello from Thread 2 out of 4 threads
Hello from Thread 0 out of 4 threads
Hello from Thread 3 out of 4 threads
After Parallel Region
RESULT:
Thus, the program to demonstrate Fork–Join Parallelism using OpenMP was executed
successfully and the output was verified.
EX. NO.: 2 CREATE A PROGRAM THAT COMPUTES A SIMPLE MATRIX-
VECTOR MULTIPLICATION B=AX, EITHER IN C/C++. USE OPENMP
DATE:
DIRECTIVES TO MAKE IT RUN IN PARALLEL
AIM:
To compute the sum of all elements and find the largest element in an array using OpenMP
parallelization.
ALGORITHM:
1. Start the program
2. Declare array and variables
3. Read number of elements n
4. Input array elements
5. Initialize sum = 0 and max = first element
6. Use OpenMP parallel for with reduction to compute sum
7. Use another OpenMP parallel loop to find maximum
8. Print sum and maximum
9. End the program
PROGRAM:
#include <stdio.h>
#include <omp.h>
#define N 3 // Size of matrix and vector
int main()
{
int i, j;
int A[N][N] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int x[N] = {1, 1, 1}; // Input vector
int b[N] = {0}; // Result vector
omp_set_num_threads(4);
// Parallel loop
#pragma omp parallel for private(j)
for(i = 0; i < N; i++) {
b[i] = 0;
for(j = 0; j < N; j++) {
b[i] += A[i][j] * x[j];
}
}
// Print result
printf("Result vector b:\n");
for(i = 0; i < N; i++) {
printf("%d ", b[i]);
}
return 0;
}
OUTPUT:
Result vector b:
6 15 24
RESULT:
Thus, the program to perform matrix–vector multiplication using OpenMP was executed
successfully and the output was verified.
EX. NO.: 3 CREATE A PROGRAM THAT COMPUTES THE SUM OF ALL THE
ELEMENTS IN AN ARRAY A (C/C++) OR A PROGRAM THAT FINDS
DATE:
THE LARGEST NUMBER IN AN ARRAY A. USE OPENMP DIRECTIVES
TO MAKE IT RUN IN PARALLEL
AIM:
To write a C program to compute the sum of elements in an array using OpenMP.
ALGORITHM:
1. Start the program
2. Include required header files (stdio.h, omp.h)
3. Declare an array and initialize its elements
4. Initialize a variable sum = 0
5. Set number of threads using omp_set_num_threads()
6. Use #pragma omp parallel for reduction(+:sum) to parallelize the loop
7. Add each element of the array to sum
8. Print the final sum
9. End the program
PROGRAM:
#include <stdio.h>
#include <omp.h>
int main() {
int n, i;
int a[100];
int sum = 0, max;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
// Parallel Sum
#pragma omp parallel for reduction(+:sum)
for(i = 0; i < n; i++) {
sum += a[i];
// Find Maximum
max = a[0];
#pragma omp parallel for
for(i = 1; i < n; i++) {
#pragma omp critical
if(a[i] > max) {
max = a[i];
printf("Sum of array elements = %d\n", sum);
printf("Largest element = %d\n", max);
return 0;
}
OUTPUT:
Enter number of elements: 5
Enter elements:
15
10
20
25
Sum of array elements = 75
Largest element = 25
RESULT:
Thus, the program to compute the sum of array elements using OpenMP was executed
successfully and the output was verified.
EX. NO.: 4 WRITE A SIMPLE PROGRAM DEMONSTRATING MESSAGE-PASSING
LOGIC USING OPENMP
DATE:
AIM:
To write a program to demonstrate message passing between threads using OpenMP.
ALGORITHM:
1. Start the program
2. Include required header files (stdio.h, omp.h)
3. Declare a shared variable message
4. Set number of threads using omp_set_num_threads()
5. Create a parallel region using #pragma omp parallel
6. Use #pragma omp single to allow one thread to write the message
7. Use #pragma omp barrier to synchronize threads
8. All threads read and print the message
9. End the program
PROGRAM:
#include <stdio.h>
#include <omp.h>
int main()
char message[50];
omp_set_num_threads(4);
#pragma omp parallel shared(message)
// Only one thread writes the message
#pragma omp single
sprintf(message, "Hello from OpenMP thread communication!");
printf("Message written by one thread\n");
// Synchronize all threads
#pragma omp barrier
// All threads read the message
printf("Thread %d reads message: %s\n",
omp_get_thread_num(), message);
return 0;
}
OUTPUT:
Message written by one thread
Thread 0 reads message: Hello from OpenMP thread communication!
Thread 1 reads message: Hello from OpenMP thread communication!
Thread 3 reads message: Hello from OpenMP thread communication!
Thread 2 reads message: Hello from OpenMP thread communication!
RESULT:
Thus, the program to demonstrate message passing using OpenMP was executed
successfully and the output was verified.
EX. NO.: 5 IMPLEMENT THE ALL-PAIRS SHORTEST-PATH PROBLEM (FLOYD'S
ALGORITHM) USING OPENMP
DATE:
AIM:
To write a program to implement All-Pairs Shortest Path using Floyd’s Algorithm with
OpenMP.
ALGORITHM:
1. Start the program
2. Define number of vertices n
3. Initialize the distance matrix dist[][]
4. Set number of threads using omp_set_num_threads()
5. Use three nested loops:
a. Outer loop for intermediate vertex k
b. Inner loops for vertices i and j
6. Update shortest path using:
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
7. Parallelize inner loops using #pragma omp parallel for
8. Print the final distance matrix
9. End the program
PROGRAM:
#include <stdio.h>
#include <omp.h>
#define N 4
int main()
int i, j, k;
int dist[N][N] = {
{0, 3, 999, 7},
{8, 0, 2, 999},
{5, 999, 0, 1},
{2, 999, 999, 0}
};
omp_set_num_threads(4);
for(k = 0; k < N; k++) {
#pragma omp parallel for private(i, j)
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
if(dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
printf("Shortest distance matrix:\n");
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
printf("%d ", dist[i][j]);
printf("\n");
return 0;
}
OUTPUT:
Shortest distance matrix:
0356
5023
3601
2570
RESULT:
Thus, the program to implement Floyd’s Algorithm using OpenMP was executed
successfully and the output was verified.
EX. NO.: 6 IMPLEMENT A PROGRAM PARALLEL RANDOM NUMBER
GENERATORS USING MONTE CARLO METHODS IN OPENMP
DATE:
AIM:
To write a program to estimate the value of π (pi) using the Monte Carlo method with
OpenMP.
ALGORITHM:
1. Start the program
2. Initialize number of iterations (random points)
3. Initialize a counter count = 0
4. Set number of threads using omp_set_num_threads()
5. Generate random points (x, y) between 0 and 1
6. Check if the point lies inside a unit circle:
x^2+y^2≤1
7. If yes, increment count
8. Use #pragma omp parallel for reduction(+:count) for parallel execution
9. Estimate π using:
π=4×"count" /"total points"
10. Print the result
11. End the program
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main()
{
int i, count = 0;
int n = 1000000; // Number of random points
double x, y, pi;
omp_set_num_threads(4);
#pragma omp parallel for private(x, y) reduction(+:count)
for(i = 0; i < n; i++) {
x = (double)rand() / RAND_MAX;
y = (double)rand() / RAND_MAX;
if(x*x + y*y <= 1.0) {
count++;
}
}
pi = 4.0 * count / n;
printf("Estimated value of Pi = %f\n", pi);
return 0;
}
OUTPUT:
Estimated value of Pi = 3.139904
RESULT:
Thus, the program to estimate the value of π using Monte Carlo method with OpenMP was
executed successfully and the output was verified.
EX. NO.: 7 WRITE A PROGRAM TO DEMONSTRATE MPI-BROADCAST-AND-
COLLECTIVE-COMMUNICATION IN C
DATE:
AIM:
To write a C program to demonstrate MPI Broadcast and Collective Communication.
ALGORITHM:
1. Start the program
2. Initialize MPI environment using MPI_Init()
3. Get:
a. Process ID using MPI_Comm_rank()
b. Total number of processes using MPI_Comm_size()
4. Root process (rank 0) initializes a value
5. Broadcast the value to all processes using MPI_Bcast()
6. Each process prints the received value
7. Use a collective operation like MPI_Reduce() to compute sum
8. Root process prints the final result
9. Finalize MPI using MPI_Finalize()
10. End the program
PROGRAM:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
int rank, size;
int data;
int sum;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Root process initializes data
if(rank == 0) {
data = 10;
printf("Root process broadcasting data = %d\n", data);
// Broadcast data to all processes
MPI_Bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("Process %d received data = %d\n", rank, data);
// Collective communication (sum)
MPI_Reduce(&data, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
if(rank == 0) {
printf("Sum of all values = %d\n", sum);
MPI_Finalize();
return 0;
}
OUTPUT:
Root process broadcasting data = 10
Process 0 received data = 10
Process 1 received data = 10
Process 2 received data = 10
Process 3 received data = 10
Sum of all values = 40
RESULT:
Thus, the program to demonstrate MPI Broadcast and Collective Communication was
executed successfully and the output was verified.
EX. NO.: 8 WRITE A PROGRAM TO DEMONSTRATE MPI-SCATTER-GATHER-
AND-ALL GATHER IN C
DATE:
AIM:
To write a C program to demonstrate MPI Scatter, Gather, and Allgather operations.
ALGORITHM:
1. Start the program
2. Initialize MPI using MPI_Init()
3. Get process ID (rank) and total processes (size)
4. Root process initializes an array
5. Use MPI_Scatter() to distribute equal parts of array to all processes
6. Each process performs a simple operation (e.g., multiply by 2)
7. Use MPI_Gather() to collect results back to root
8. Use MPI_Allgather() to share results with all processes
9. Print outputs
10. Finalize MPI using MPI_Finalize()
11. End the program
PROGRAM:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
int rank, size;
int sendbuf[8], recvbuf[2];
int gatherbuf[8], allgatherbuf[8];
int i;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Root initializes data
if(rank == 0) {
for(i = 0; i < 8; i++) {
sendbuf[i] = i + 1; // 1 to 8
printf("Initial array:\n");
for(i = 0; i < 8; i++) {
printf("%d ", sendbuf[i]);
}
printf("\n");
// Scatter (2 elements per process if size = 4)
MPI_Scatter(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, MPI_COMM_WORLD);
// Each process modifies its data
for(i = 0; i < 2; i++) {
recvbuf[i] = recvbuf[i] * 2;
printf("Process %d received and modified data: %d %d\n",
rank, recvbuf[0], recvbuf[1]);
// Gather results at root
MPI_Gather(recvbuf, 2, MPI_INT, gatherbuf, 2, MPI_INT, 0, MPI_COMM_WORLD);
if(rank == 0) {
printf("Gathered data at root:\n");
for(i = 0; i < 8; i++) {
printf("%d ", gatherbuf[i]);
printf("\n");
}
// Allgather (everyone gets full result)
MPI_Allgather(recvbuf, 2, MPI_INT, allgatherbuf, 2, MPI_INT, MPI_COMM_WORLD);
printf("Process %d after Allgather:\n", rank);
for(i = 0; i < 8; i++) {
printf("%d ", allgatherbuf[i]);
printf("\n");
MPI_Finalize();
return 0;
}
OUTPUT:
Initial array: 1 2 3 4 5 6 7 8
Process 0 received and modified data: 2 4
Process 1 received and modified data: 6 8
Process 2 received and modified data: 10 12
Process 3 received and modified data: 14 16
Gathered data at root:
2 4 6 8 10 12 14 16
Process 0 after Allgather:
2 4 6 8 10 12 14 16
Process 1 after Allgather:
2 4 6 8 10 12 14 16
...
RESULT:
Thus, the program to demonstrate MPI Scatter, Gather, and Allgather operations was
executed successfully and the output was verified.
EX. NO.: 9 WRITE A PROGRAM TO DEMONSTRATE MPI-SEND-AND-RECEIVE
IN C
DATE:
AIM:
To write a C program to demonstrate point-to-point communication using MPI Send and
Receive.
ALGORITHM:
1. Start the program
2. Initialize MPI using MPI_Init()
3. Get process ID (rank) and total processes (size)
4. If process is sender (rank 0):
a. Initialize data
b. Send data using MPI_Send()
5. If process is receiver (rank 1):
a. Receive data using MPI_Recv()
6. Print the sent and received values
7. Finalize MPI using MPI_Finalize()
8. End the program
PROGRAM:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
int rank, size;
int data;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if(rank == 0) {
data = 100;
printf("Process 0 sending data = %d\n", data);
MPI_Send(&data, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
else if(rank == 1) {
MPI_Recv(&data, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process 1 received data = %d\n", data);
MPI_Finalize();
return 0;
}
OUTPUT:
Process 0 sending data = 100
Process 1 received data = 100
RESULT:
Thus, the program to demonstrate MPI Send and Receive was executed successfully and the
output was verified.
EX. NO.: 10 WRITE A PROGRAM TO DEMONSTRATE BY PERFORMING-
PARALLEL-RANK-WITH-MPI IN C
DATE:
AIM:
To write a C program to demonstrate parallel execution and rank identification using MPI.
ALGORITHM:
1. Start the program
2. Initialize MPI using MPI_Init()
3. Get:
a. Process ID using MPI_Comm_rank()
b. Total number of processes using MPI_Comm_size()
4. Each process prints its rank and total processes
5. Finalize MPI using MPI_Finalize()
6. End the program
PROGRAM:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char *argv[])
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
printf("Hello from process %d out of %d processes\n", rank, size);
MPI_Finalize();
return 0;
}
OUTPUT:
Hello from process 0 out of 4 processes
Hello from process 1 out of 4 processes
Hello from process 2 out of 4 processes
Hello from process 3 out of 4 processes
RESULT:
Thus, the program to demonstrate parallel rank identification using MPI was executed successfully
and the output was verified.