Multicore Architecture and Programming Lab Manual
Multicore Architecture and Programming Lab Manual
Aim:
Algorithm:
3. Initialize any shared variables or data structures that will be accessed by multiple
threads.
4. Use the OpenMP #pragma omp parallel directive to create a team of threads.
5. Inside the parallel region, use the #pragma omp for directive to parallelize a loop or
distribute tasks among the threads.
6. If necessary, specify any reduction operations using the reduction clause to combine
results from different threads.
7. Each thread executes its assigned portion of the loop or tasks independently and
updates any shared variables or data structures.
8. After the loop or tasks are completed, the threads synchronize using the implicit
barrier at the end of the parallel region, ensuring that all computations are finished.
10. If required, retrieve and combine results from shared variables or data structures to
obtain the final result.
1
Program:
#include <stdio.h>
#include <omp.h>
int main()
int i, n = 10;
int sum = 0;
sum += i;
return 0;
Run:
./program
Output:
Result:
Thus, simple Program for OpenMP Fork-Join Parallelism was executed successfuly.
2
Ex No: 2
Aim:
Algorithm:
Input:
4. Check if the number of columns in matrix A (N) matches the size of vector x (N). If
they are not equal, return an error, as matrix-vector multiplication is not possible.
6. For each row i in the matrix A (0 <= i < M): a. Initialize the element b[i] to zero. b.
For each column j in the matrix A (0 <= j < N): i. Add the product of A[i][j] and x[j]
to b[i].
7. The resulting vector b contains the result of the matrix-vector multiplication (b = Ax).
Program:
#include <iostream>
#include <omp.h>
#define N 1000
int main() {
int A[N][N];
3
int x[N];
int b[N];
return 0;
}
Output:
[2 4 6]
[1 3 5]
b[0] = (2 * 1) + (4 * 2) + (6 * 3) = 2 + 8 + 18 = 28
b[1] = (1 * 1) + (3 * 2) + (5 * 3) = 1 + 6 + 15 = 22
Result:
Thus, creation of simple matrix-vector multiplication b=Ax, either in C/C++ was created and
output is verified.
4
Ex No: 3
Aim:
To create a program that computes the sum of all the elements in an array A (C/C++)
or a program that finds the largest number in an array A. Use OpenMP directives to make it
run in parallel.
Algorithm:
Input:
Output:
2. Use OpenMP parallel for loop to distribute the iterations among multiple threads. a.
For each element A[i] in the array (0 <= i < N): i. Add A[i] to the sum using a
reduction clause.
3. The variable sum now contains the sum of all elements in the array.
Program:
#include <iostream>
#include <omp.h>
return sum;
}
int main() {
5
int A[ARRAY_SIZE];
6
// Initialize array A with values
// (omitted for brevity)
return 0;
}
Output:
Array A: [1, 2, 3, 4, 5]
Sum: 15
Result:
Thus, creation of the sum of all the elements in an array was created and executed
successfully.
7
Ex No: 4
Date : Message-Passing
Aim:
Algorithm:
2. Determine the rank of the current process and the total number of processes.
Receive the message from the desired source process using MPI_Recv.
Program:
#include <iostream>
#include <omp.h>
#define NUM_TASKS 5
int main() {
#pragma omp parallel
{
#pragma omp single
{
// Create tasks
for (int taskID = 0; taskID < NUM_TASKS; ++taskID) {
8
#pragma omp task
processTask(taskID);
}
}
}
return 0;
}
Output:
Result:
10
Ex No: 5
Date : Floyd's Algorithm
Aim:
Algorithm:
2. Create a distance matrix dist of size N x N to store the shortest path distances.
3. Copy the values from the graph matrix to the distance matrix.
4. Parallelize the following loop using OpenMP directives: a. For each intermediate
vertex k from 0 to N-1:
Use #pragma omp parallel for directive to parallelize the outer loop.
5. Print the resulting shortest path distances stored in the dist matrix.
Program:
#include <iostream>
#include <limits>
#include <omp.h>
11
}
12
}
printShortestPaths(dist);
}
13
int main() {
int graph[N][N];
initializeGraph(graph);
computeShortestPaths(graph);
return 0;
}
Output:
Shortest Paths:
0 1 1 2
3 0 2 3
2 3 0 1
1 2 1 0
Result:
Thus implementation of All-Pairs Shortest-Path Problem (Floyd's Algorithm) Using
OpenMP were executed successfully.
14
Ex No : 6
Aim:
To implement a program Parallel Random Number Generators using Monte Carlo Methods
in OpenMP.
Algorithm:
1. Set the number of points (numPoints) to generate for the Monte Carlo simulation.
2. Set the number of threads (numThreads) for parallel execution.
3. Set the maximum number of threads for OpenMP using
omp_set_num_threads(numThreads).
4. Define a function estimatePi that takes the number of points as input and returns an
estimate of Pi:
Declare a variable numPointsInsideCircle and initialize it to 0.
Initialize a random number generator for each thread with a unique seed.
Use the #pragma omp parallel for directive to parallelize the following
steps:
For each point:
Generate random coordinates (x and y) using the random
number generator specific to each thread.
Check if the point is inside the unit circle (i.e., x^2 + y^2 <=
1).
If the point is inside the circle, increment
numPointsInsideCircle by 1.
Calculate the estimate of Pi by multiplying the ratio of points inside the circle
to the total points by 4.
Return the estimated value of Pi.
5. Call the estimatePi function with the number of points to obtain the estimate of Pi.
6. Print the estimated value of Pi.
Program:
#include <iostream>
#include <random>
#include <omp.h>
15
// Initialize random number generator with a unique seed for each thread
16
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_real_distribution<double> distribution(-1.0, 1.0);
// Estimate Pi using the ratio of points inside the circle to total points
double pi = 4.0 * static_cast<double>(numPointsInsideCircle) /
static_cast<double>(numPoints);
return pi;
}
int main() {
// Number of points to generate for the Monte Carlo simulation
int numPoints = 10000000;
// Estimate Pi using Monte Carlo simulation with parallel random number generation
double pi = estimatePi(numPoints);
return 0;
}
17
Output:
Estimated Pi: 3.1416596
Result:
Thus, implementation of Parallel Random Number Generators using Monte Carlo
Methods in OpenMP were executed successfully.
18
Ex No : 7
Date : MPI-broadcast-and-collective-communication
Aim:
Algorithm:
5. If the rank is 0, set value to the desired initial value (e.g., 42).
6. Use MPI_Bcast to broadcast the value from process 0 to all other processes.
8. Allocate memory for an array values of size size to store the gathered values.
9. Use MPI_Allgather to gather the values from all processes into the values array.
10. Print the gathered values for each process using printf.
11. Free the dynamically allocated memory for the values array.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Initialize MPI
MPI_Init(&argc, &argv);
20
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Finalize MPI
MPI_Finalize();
return 0;
}
Output:
Process 0 received value: 42
Process 1 received value: 42
Process 2 received value: 42
Process 3 received value: 42
Result:
21
Thus the Program to demonstrate MPI-broadcast-and-collective-communication in C.
22
Ex No : 8
Aim:
Algorithm:
5. If the rank is 0, initialize the send buffer with the desired values.
8. Use MPI_Gather to gather the received values from all processes into the send buffer
of process 0.
10. Use MPI_Allgather to gather values from all processes into the send buffer of each
process.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Initialize MPI
MPI_Init(&argc, &argv);
23
// Get the rank and size of the MPI process
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Finalize MPI
24
MPI_Finalize();
return 0;
}
Output:
Result:
Thus the Program to demonstrate MPI-scatter-gather-and-all gather in C were executed
successfully.
25
Ex No : 9
Date : MPI-send-and-receive
Aim:
Algorithm:
6. If the rank is 0, send the value to the target process using MPI_Send.
7. If the rank is the target process, receive the value from the source process using
MPI_Recv.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Initialize MPI
MPI_Init(&argc, &argv);
// Finalize MPI
MPI_Finalize();
return 0;
}
Output:
Process 0 sends value: 42
Process 1 receives value: 42
Result:
Thus, a Program to demonstrate MPI-send-and-receive in C was executed successfully.
27
Ex No: 10
Date : Parallel-rank
Aim:
To write a Program to demonstrate by performing-parallel-rank-with-MPI in C
Algorithm:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
// Initialize MPI
MPI_Init(&argc, &argv);
// Finalize MPI
MPI_Finalize();
return 0;
}
28
Output:
Process 0 out of 4 processes
Process 1 out of 4 processes
Process 2 out of 4 processes
Process 3 out of 4 processes
Result:
Thus, a Program to demonstrate by performing-parallel-rank-with-MPI in C was
executed successfully.
29