0% found this document useful (0 votes)
6 views

CP4292 Multicore Architecture lab manual

Uploaded by

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

CP4292 Multicore Architecture lab manual

Uploaded by

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

lOMoARcPSD|317 841 22

EX:NO:1 OPEN MP FORK - JOIN PARALLELISM.


DATE:

AIM:

To write a simple program for demonstration of an OpenMP Fork-JoinParallelism.

ALGORITHM:
Step1 : Start
Step 2: Create a program that computes a simple matrix vectormultiplication.
Step 3: Input the values for the matrix.
Step 4: Calculate the multiplicative value.
Step 5: Output the value.

Step 6: Stop

THEORY:
 In this program, the #pragma omp parallel directive is used to define
a parallel region.

 All the code inside this region will be executed by multiple threads
in parallel. The private(thread_id) clause ensures that each thread has
its own copy of the thread_id variable.
 Inside the parallel region, we use the omp_get_num_threads()
function to get the total number of threads and
omp_get_thread_num() function to get the ID of the current thread.

 Each thread then prints a hello message along with its thread ID and
the total number of threads.
 When you compile and run this program, you will see output similar
to the following: Hello from thread 0 out of 4 threads.
 Hello from thread 2 out of 4 threads. Hello from thread 1 out of 4
threads. Hello from thread 3 out of 4 threads.
 The exact output may vary depending on your system and the
number of threads available for parallel execution.
 Note that you need to compile this program with OpenMP support.

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

 For example, using the GCC compiler, you can compile it with the
following command: gcc -o gfg -fopenmp filename.c
output

 For the execution using following command : ./gfg

PROGRAM:
#include<stdio.h>
#include <omp.h>
int main(void)
{
printf("Before: total thread number is %d\n", omp_get_num_threads());
#pragmaomp parallel
{
printf("Thread id is %d\n",omp_get_thread_num());
}
printf("After: total thread number is %d\n", omp_get_num_threads());
return 0;
}

OUTPUT:

Input: mat1[3][2] = { {1, 1}, {2, 2}, {3, 3} }

mat2[2][3] = { {1, 1, 1}, {2, 2, 2} }

Output: result[3][3] = { {3, 3, 3}, {6, 6, 6}, {9, 9, 9} }

RESULT:

Thus the program has been executed successfully.


lOMoARcPSD|317 841 22

EX:NO: 2 SIMPLE MATRIX-VECTOR MULTIPLICATION


DATE: USING OPENMP DIRECTIVES .

AIM:

To create a program that computes a simple matrix-vector multiplication b=Ax,

either in C/C++. Use OpenMP directives to make it run in parallel.


ALGORITHM:
Step 1: Start

Step 2: Creation of program to compute b=Ax.

Step 3: Get the input of two matrices


Step 4: Multiply the given matrices
Step 5: Output the resultant matrix
Step 6: Stop

THEORY:
 In this program, we have a matrix A and a vector x, and we want to compute the
matrixvector multiplication b = Ax.
 Inside the parallel region defined by the #pragma omp parallel directive, we
perform the matrix-vector multiplication in parallel.
 Each thread calculates a chunk of rows in the result vector b. The number of
threads and the range of rows assigned to each thread are determined using
OpenMP functions.
 The omp_get_num_threads() function returns the total number of threads, and
omp_get_thread_num() function returns the ID of the current thread.
 Each thread computes its assigned chunk of rows in parallel using nested loops.
 The outer loop iterates over the assigned rows, and the inner loop iterates over
the columns of matrix A.
 Each thread updates its portion of the result vector b by multiplying the
corresponding row of matrix A with the vector x.
 After the parallel region, we print the resulting vector b. To compile and
run this program with OpenMP support, you can use the following
command:

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

 For example, using the GCC compiler, you can compile it with the
following command: gcc -o gfg -fopenmp filename.c output

 For the execution using following command : ./gfg


 When you run the program, you will see the result vector b printed:
 Result vector b: 14 32 50 Each element in the result vector is computed in
parallel by the different threads.

PROGRAM:

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

int main()
{
float A[2][2] = {{1,2},{3,4}};
float b[] = {8,10};float c[2];
int i,j;

// computes A*b
#pragmaomp parallel for
for (i=0; i<2; i++)
{
c[i]=0;
for(j=;j<2;j++)
{
c[i]=c[i]+A[i][j]*b[j];
}
}
lOMoARcPSD|317 841 22

// prints result
for(i=0; i<2; i++)
{
printf("c[%i]=%f \n",i,c[i]);
}

return 0;
}

OUTPUT:

Input:

mat1[3][2] = { {1, 1}, {2, 2}, {3, 3} }

mat2[2][3] = { {1, 1, 1}, {2, 2, 2} }

Output:

result[3][3] = { {3, 3, 3}, {6, 6, 6}, {9, 9, 9} }

RESULT:

Thus the program has been executed successfully.

Downloaded by series dramas (mansa12345@gmail.com)

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

EX:NO: 3 FINDS THE LARGEST NUMBER IN AN ARRAY


DATE: USING OPENMP DIRECTIVES .

AIM:

To create a program that computes the sum of all the elements in an array.

ALGORITHM:
Step 1: Start
Step 2: Creation of a program for computing the sum of all the elements an
array.

Step 3: Input the array elements.

Step4: Process of addition.

Step 5: Print the resultant sum.

Step6: Stop.

THEORY:

 In this program, we have an array A, and we want to compute the sum of all
the elements in parallel using OpenMP.

 Inside the parallel region defined by the #pragma omp parallel directive, we
calculate a partial sum for each thread.

 The number of threads and the range of array indices assigned to each thread
are determined using OpenMP functions.

 The omp_get_num_threads() function returns the total number of threads, and


omp_get_thread_num() function returns the ID of the current thread.

 Each thread computes its assigned chunk of array elements in parallel using a
loop.

 The loop iterates over the assigned indices, and each thread accumulates its
partial sum.

 To avoid race conditions while updating the shared sum variable, we use the
#pragma omp atomic directive to perform a reduction operation.

 This ensures that the updates to sum are done atomically.

 After the parallel region, we print the final sum


of all the elements.

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

 To compile and run this program with OpenMP support,


 For example, using the GCC compiler, you can compile it with the following
command: gcc -o gfg -fopenmp filename.c output

 For the execution using following command : ./gfg

 When you run the program, you will see the sum of all the elements printed:
Sum: 55 Program

PROGRAM:
#include<omp.h>
#include<bits/stdc++.h>
usingnamespace std;

int main()
{
vector<int>
arr{3,1,2,5,4,0};
queue<int> data;
int arr_sum=accumulate(arr.begin(),arr.end(),0);
int arr_size=arr.size();
int new_data_size, x, y;
for(i=0;i<arr_size;i++)
{
data.push(arr[i]);
}
omp_set_num_threads(ceil(arr_size/2));

#pragmaomp parallel
{
#pragmaomp critical
{
lOMoARcPSD|317 841 22

new_data_size=data.size();
for(int j=1; j<new_data_size; j=j*2)
{
x=data.front();data.pop();
y =data.front();data.pop();
data.push(x+y);
}
}
}

cout<<"Array prefix sum:"<<data.front()<<endl;


if(arr_sum==data.front())
{
cout<<"Correct sum"<<endl;
}
else
{
cout<<"Incorrect Answer"<<endl;
}
return0;
}

OUTPUT:
Array of elements: 1 5 7 9 11
Sum: 33

RESULT:

Thus the program has been executed successfully.

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

EX:NO: 4 MESSAGE-PASSING LOGIC USING OPENMP.


DATE :

AIM:

To write a simple program demonstrating Message-Passing logic using OpenMP.

ALGORITHM:

Step 1: Start
Step 2: Creation of simple program demonstrating message-
passing logic.
Step 3: The message creation for transformation across web.
Step 4: Input the message.
Step 5: Process and print the result
Step 6: Stop

THEORY:
 OpenMP is primarily designed for shared memory parallel programming,
where threads share memory and work cooperatively on a shared task.
 Message passing, on the other hand, is typically associated with
distributed memory systems, where processes communicate by sending
and receiving messages. While OpenMP does not natively support
message passing,
 it is possible to use OpenMP in conjunction with a message passing
interface (MPI) library to achieve a hybrid parallel programming model.
 In this approach, OpenMP can be used to parallelize code within each MPI
process, while MPI handles communication between processes.
 In this program, we have a hybrid parallel programming model where we
use OpenMP within each MPI process.
 Each process will have its own set of threads that can execute code in
parallel. Inside the parallel region defined by the #pragma omp parallel
directive, we use OpenMP to parallelize code within each MPI process.
 Each thread prints a hello message, including the process rank and the
thread ID. To compile and run this program, you'll need to have both
OpenMP and MPI installed and properly configured on your system.
 The compilation command may vary depending on the MPI
implementation you are using.

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

 For example, using the GCC compiler, you can compile it with the
following command: gcc -o gfg -fopenmp filename.c
output

 For the execution using following command : ./gfg


 When you run the program using an appropriate MPI launcher (e.g.,
mpiexec or mpirun), each MPI process will execute in parallel, and within
each process, multiple threads will execute the OpenMP code in parallel.
 For example, if you run the program with two MPI processes and two
OpenMP threads per process, you may see output similar to the following:
 Hello from process 0 thread 0 out of 2 threads.
 Hello from process 0 thread 1 out of 2 threads.
 Hello from process 1 thread 0 out of 2 threads.
 Hello from process 1 thread 1 out of 2 threads.

PROGRAM:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

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


{

// Beginning of parallel region


#pragmaomp parallel
{
printf("Hello World... from
thread = %d\n",
omp_get_thread_num());
}
// Ending of parallel region
}

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

OUTPUT:

Hello World

RESULT:

Thus the program has been executed successfully.

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

EX:NO:5 ALL-PAIRS SHORTEST-PATH PROBLEM (FLOYD'S ALGORITHM)


DATE: USINGOPENMP.

AIM:

To write a program implementing All-Pairs Shortest-Path Problem


(Flyod’s Algorithm) using OpenMP.

ALGORITHM:
Step 1: Start
Step 2: Get the input of all pairs of co-ordinates
Step 3: Process the path and sort out the shortest path.

Step 4: Print the resultant path


Step 5: Stop

THEORY:
 In this program, we have a graph represented by an adjacency matrix
graph. The number of vertices is num_vertices, and the value INF
represents infinity or an unreachable path.
 Inside the parallel region defined by the #pragma omp parallel
directive, we use OpenMP to parallelize the outer loop of Floyd's
algorithm, where k represents the intermediate vertex.
 The #pragma omp for directive distributes the iterations of the loop
among the available threads, with a dynamic scheduling policy.
 Each thread updates a subset of the graph's elements based on the k
intermediate vertex. The innermost loop checks if the path from
vertex i to vertex j through vertex k is shorter than the current
distance.
 If so, the distance is updated. After the parallel region, we print the
shortest path distances.
 To compile and run this program with OpenMP support, you can use
the following command
 For example, using the GCC compiler, you can compile it with the
following command: gcc -o gfg -fopenmp filename.c
output

 For the execution using following command : ./gfg

 When you run the program, you will see the shortest path distances
printed: Shortest Path Distances: 0 5 8 9 INF 0 3 4 INF INF 0 1 INF
INF INF 0

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

PROGRAM:

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

//Define the number of nodes in the graph

#define N1200

//Define minimum function that will be used later on to calcualte minimum values
betweentwo numbers
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif

//Define matrix of size N * N to store distances between nodes


//Initialize all distances to zero int distance_matrix[N][N] = {0};

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


{
int nthreads;
int src, dst, middle;

//Initialize the graph with random distances


for (src =0; src< N; src++)
{
for (dst = 0; dst< N; dst++)
{
// Distance from node to same node is 0. So, skipping these elements
if(src != dst)
{
//Distances are generated to be between 0
and 19distance_matrix[src][dst] = rand()
% 20;

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

}
}
}

//Define time variable to record start time for execution of programdouble


start_time = omp_get_wtime();

for (middle = 0; middle < N; middle++)


{
int * dm=distance_matrix[middle];for (src
= 0; src< N; src++)
{
int * ds=distance_matrix[src];for
(dst = 0; dst< N; dst++)
{
ds[dst]=min(ds[dst],ds[middle]+dm[dst]);
}
}
}

double time = omp_get_wtime() - start_time; printf("Total time for


sequential (in sec):%.2f\n", time);

for(nthreads=1; nthreads<= 10; nthreads++) {


//Define different number of threads
omp_set_num_threads(nthreads);

// Define iterator to iterate over distance matrix


//Define time variable to record start time for execution of programdouble
start_time = omp_get_wtime();

/* Taking a node as mediator


check if indirect distance between source and distance via mediatoris less than
direct distance between them */
#pragmaomp parallel shared(distance_matrix)for
(middle = 0; middle < N; middle++)
{

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

int * dm=distance_matrix[middle];
#pragma omp parallel for private(src, dst) schedule(dynamic)for (src = 0;
src< N; src++)
{
int * ds=distance_matrix[src];for
(dst = 0; dst< N; dst++)
{
ds[dst]=min(ds[dst],ds[middle]+dm[dst]);
}
}
}
double time = omp_get_wtime() - start_time;
printf("Total time for thread %d (in sec):%.2f\n", nthreads, time);
}
return 0;

Input:
The cost matrix of the graph.
036∞∞∞∞
3021∞∞∞
620142∞
∞1102∞4
∞∞42021
∞∞2∞201
∞∞∞4110

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

Output:
Matrix of all pair shortest
path.0 3 4 5 6 7 7
3021344
4201323
5110233
6332021
7423201
7433110

RESULT:

Thus the program has been executed successfully.


lOMoARcPSD|317 841 22

EX:NO:6 PARALLEL RANDOM NUMBER GENERATORS


DATE: USING MONTECARLO METHODS IN OPENMP.

AIM:

To implement a program Parallel Random Number Generators using Monte

Carlo Methods in OpenMP.

ALGORITHM:
Step 1: Start
Step 2: Get the input of random number
Step 3: Process it using Monte Carlo Methods in OpenMP

Step 4: Get the output of estimated value.


Step 5: Stop

THEORY:

 In this program, we use Monte Carlo simulation to estimate the value of Pi.

 We generate random (x, y) coordinates within the unit square and check if
the point is inside the unit circle.
 By counting the number of points inside the circle, we can estimate the
value of Pi.
 Inside the parallel region defined by the #pragma omp parallel directive, we
perform the Monte Carlo simulation in parallel using OpenMP.
 The #pragma omp for directive distributes the iterations of the loop among
the available threads, and the reduction(+:count) clause ensures that the
count variable is correctly updated in a thread-safe manner.
 Each thread generates its own set of random (x, y) coordinates using a
random number generator. The dis(gen) function generates a random number
between 0.0 and 1.0.
 Then, the thread checks if the point is inside the unit circle and updates the
count variable accordingly.
 After the parallel region, we estimate the value of Pi by dividing the total
count of points inside the circle by the total number of points and
multiplying by 4.
 To compile and run this program with OpenMP support, you can use the
following command:
 For example, using the GCC compiler, you can compile it with the following
command: gcc -o gfg -fopenmp filename.c output

 For the execution using following command : ./gfg


 When you run the program, you will see the estimated value of Pi printed:
 Estimated Pi: 3.1422

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

PROGRAM:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to find estimated


// value of PI using Monte
// Carlo algorithm
void monteCarlo(int N, int K)
{
// Stores X and Y coordinates
// of a random point
double x, y;
// Stores distance of a random
// point from
origindouble d;
// Stores number of points
// lying inside
Circle int pCircle = 0;
// Stores number of points

// lying inside squareint pSquare = 0;


int i = 0;
// Parallel calculation of random
// points lying inside a circle
#pragma omp parallel firstprivate(x, y, d, i) reduction(+ : pCircle, pSquare)
num_threads(K)
{
// Initializes random points
// with a seed srand48((int)time(NULL));
for (i = 0; i< N; i++)
{
// Finds random X co-ordinatex =(double)drand48();

// Finds random X co-ordinatey =(double)drand48();

// Finds the square of distance


// of point (x, y) from origind =((x * x) + (y * y));

// If d is less than or
// equal to 1
if(d <= 1)
{
// Increment pCircle by 1
pCircle++;
}
// Increment pSquare by 1
pSquare++;
}

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

}
// Stores the estimated value of PI
double pi = 4.0 * ((double)pCircle / (double)(pSquare));

// Prints the value in pi


printf("Final Estimation of Pi = %f\n", pi);

// Driver Codeint
main()
{
// Input
int N = 100000;
int K = 8;
// Function call
monteCarlo(N, K);

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

OUTPUT:

Final Estimation of Pi =3.1320757

RESULT:

Thus the program has been executed successfully


lOMoARcPSD|317 841 22

EX:NO:7 MPI-BROADCAST-AND-
DATE: COLLECTIVE-COMMUNICATION

AIM:

To write a program to demonstrate MPI-broadcast-and-collective communication in C.

ALGORITHM:

Step 1: Start

Step 2: Get the values for broadcasting.

Step 3: Process using MPI-broadcast-and-collective communicationStep 4:

Print the output

Step 4: Stop

THEORY:

 In this program, we have multiple processes executing in parallel using MPI.

 Each process has its own rank and can communicate with other processes.

 First, we initialize MPI and obtain the rank and size of the

MPI_COMM_WORLD communicator.

 The rank 0 process initializes the data variable with a value of 123. Then, we

use the MPI_Bcast function to broadcast the data from rank 0 to all other

processes in MPI_COMM_WORLD.

 The MPI_Bcast function takes the address of the data, the count, the data type,

the root process (in this case, 0), and the communicator.

 All processes, including rank 0, print the received data using printf.

 Next, we perform a collective communication using the MPI_Allgather

function.

 This function gathers data from all processes and distributes it to all

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

processes.

 In this example, we gather the data variable from each process into the

recv_buf array.

 Each process provides one element of data to be gathered.

 The MPI_Allgather function takes the address of the data, the send count,

the data type, the receive buffer, the receive count, the data type, and the

communicator.

 After the collective communication, each process calculates the sum of all the

received data using a simple loop.

 Finally, we finalize MPI and clean up resources using MPI_Finalize().

 To compile and run this program with MPI support, you can use the following

command: gcc -o gfg -fopenmp filename.c output

 For the execution using following command : ./gfg

 When you run the program, each process will print the received data, and the

root process (rank 0) will also print the sum of all the received data.

For example, if you run the program with four processes, you may see output like

the following:

Process 0 received data: 123


Process 1 received data: 123
Process 2 received
data: 123
Process 3 received data: 123
Process 0: Sum of all data received: 492
Process 1: Sum of all data received: 492
Process 2: Sum of all data received: 492
Process 3: Sum of all data received: 492
lOMoARcPSD|317 841 22

PROGRAM:

#include<mpi.h>

#include<stdio.h>

int main(int argc, char** argv)


{
int rank;
intbuf;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) { buf = 777;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
else
{
MPI_Recv(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
printf("rank %d receiving received %d\n", rank, buf);
}
MPI_Finalize();return0;
}
lOMoARcPSD|317 841 22

OUTPUT:

Process 0 broadcasting data 100

Process 2 received data 100 from root

RESULT:

Thus the program has been executed successfully

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

EX:NO:8 MPI-SCATTER-GATHER-AND-ALL GATHER IN C

DATE:

AIM:

To write a program to demonstrate MPI-scatter-gather-and-all gather.

ALGORITHM:

Step 1: Start

Step 2: Get an array of random numbers as input.

Step 3: Compute the average of array of numbers.

Step 4: Process and print the result.

Step 3: Stop

THEORY:

 In this program, we have multiple processes executing in parallel using MPI.

 Each process has its own rank and can communicate with other processes.

 First, we initialize MPI and obtain the rank and size of the

MPI_COMM_WORLD communicator.

 In the root process (rank 0), we initialize the sendbuf array with values from

1 to 12.

 We then use the MPI_Scatter function to scatter the data from the root

process to all processes.

 The root process provides the sendbuf array as the send buffer, and each

process receives a portion of the data into the recvbuf array.

 The MPI_Scatter function takes the send buffer, send count, send type,

receive buffer, receive count, receive type,root process (in this case, 0), and

the communicator.

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

 Each process prints the received data using printf.

 Next, we use the MPI_Gather function to gather the data from all processes

back to the root process.

 Each process provides its portion of the data from the recvbuf array, and

the root process gathers the data into the sendbuf array.

 The MPI_Gather function takes the send buffer, send count, send type,

receive buffer, receive count, receive type, root process, and the

communicator.

 The root process prints the received data using printf.

 Finally, we use the MPI_Allgather function to perform an all gather

operation, where all processes gather the data from all processes into their

own sendbuf arrays.

 The MPI_Allgather function takes the send buffer, send count, send type,

receive buffer,receive count, receive type, and the communicator.

 Each process prints the received all data using printf.

 To compile and run this program with MPI support, you can use the

following

 command: gcc -o gfg -fopenmp filename.c output

 For the execution using following command : ./gfg

 When you run the program, each process will print the received data, the

root process will print the gathered data, and each process will print the

received all data.


lOMoARcPSD|317 841 22

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#include <assert.h>

// Creates an array of random numbers. Each number has a value from 0 - 1float
*create_rand_nums(int num_elements) {
float *rand_nums = (float *)malloc(sizeof(float) * num_elements);

assert(rand_nums != NULL);
int i;
for (i = 0; i<num_elements; i++)

rand_nums[i] =(rand() / (float)RAND_MAX);


}
return rand_nums;
}

// Computes the average of an array of numbers float


compute_avg(float *array, int num_elements)
{
float sum = 0.f;

int i;
for (i = 0; i<num_elements; i++) {sum
+= array[i];
}
return sum / num_elements;
}

int main(int argc, char** argv)


{
if(argc != 2)
{

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

fprintf(stderr, "Usage: avgnum_elements_per_proc\n");

exit(1);
}

int num_elements_per_proc = atoi(argv[1]);


// Seed the random number generator to get different results each time

srand(time(NULL));

MPI_Init(NULL, NULL);

int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD,
&world_rank);int world_size;
MPI_Comm_size(MPI_COMM_WORLD,
&world_size);

// Create a random array of elements on the root process. Its total


// size will be the number of elements per process times the number
// of processes
float *rand_nums = NULL;

if(world_rank == 0)

{
rand_nums = create_rand_nums(num_elements_per_proc * world_size);
}
// For each process, create a buffer that will hold a subset of the entire
// array
float *sub_rand_nums = (float *)malloc(sizeof(float) *num_elements_per_proc);
assert(sub_rand_nums != NULL);

// Scatter the random numbers from the root process to all processes in
// the MPI world
MPI_Scatter(rand_nums, num_elements_per_proc, MPI_FLOAT, sub_rand_nums,
num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD);

// Compute the average of your subset


float sub_avg = compute_avg(sub_rand_nums, num_elements_per_proc);

// Gather all partial averages down to all the processes

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

float *sub_avgs= (float *)malloc(sizeof(float) * world_size);

assert(sub_avgs != NULL);
MPI_Allgather(&sub_avg, 1, MPI_FLOAT, sub_avgs, 1, MPI_FLOAT,
MPI_COMM_WORLD);

// Now that we have all of the partial averages, compute the


// total average of all numbers. Since we are assuming each processcomputed
// an average across an equal amount of elements, this computation will
// produce the correct answer.
float avg = compute_avg(sub_avgs, world_size);
printf("Avg of all elements from proc %d is %f\n", world_rank, avg);

// Clean up
if (world_rank == 0)

free(rand_nums);
}
lOMoARcPSD|317 841 22

free(sub_avgs);
free(sub_rand_nums);

MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}

OUTPUT:

100Avg ofall elements is 0.478699


Avg computed across original data is 0.478699

RESULT:

Thus the program has been executed successfully

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

EX:NO:9 MPI-SEND-AND-RECEIVE
DATE:

AIM:

To write a program to demonstrate MPI-send-and-receive in C.

ALGORITHM:

Step 1: Start
Step 2: Create a program to demonstrate MPI-send-and-
receive.

Step 3: Input the message to send and receive.


Step 4: Process the message and print the output message.
Step 5: Stop

THEORY :

 In this program, we have two processes executing in parallel using MPI.


Each process has its own rank and can communicate with other processes.
 First, we initialize MPI and obtain the rank and size of the
MPI_COMM_WORLD
communicator.
 In the root process (rank 0), we initialize the send_data variable with a value
of 123.
 We then use the MPI_Send function to send the data to process 1. The
MPI_Send function takes the address of the send buffer, the send count, the
send type, the destination process (in this case, 1), a tag (0 in this case), and
the communicator.
 Process 1 receives the data from process 0 using the MPI_Recv function.
The MPI_Recv function takes the address of the receive buffer, the receive
count, the receive type, the source process (in this case, 0), a tag (0 in this
case), the communicator, and the status (ignored in this example using
MPI_STATUS_IGNORE).
 Both processes print the sent and received data using printf.
 Finally, we finalize MPI and clean up resources using MPI_Finalize().
 To compile and run this program with MPI support, you can use the
following command:
 gcc -o gfg -fopenmp filename.c output
 For the execution using following command : ./gfg

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

PROGRAM:

int main(intargc, char ** argv)


{
int *array;
int tag=1; int size;
int rank;
MPI_Status status;
MPI_Init
(&argc,&argv);
MPI_Comm_size (MPI_COMM_WORLD,&size);
MPI_Comm_rank (MPI_COMM_WORLD,&rank);

if (rank == 0)
{
array = malloc (10 * sizeof(int)); // Array of 10 elementsif(!array)
// error checking
{
MPI_Abort (MPI_COMM_WORLD,1);
}
MPI_Send(&array,10,MPI_INT,1,tag,MPI_COMM_WORLD);
}

if (rank == 1)
{
MPI_Recv (&array,10,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
// more code here
}

MPI_Finalize();

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

OUTPUT:

Process 1 received number -1 from process 0

Process 0 sent data: 123

Process 1 received data: 123

RESULT:

Thus the program has been executed successfully.


lOMoARcPSD|317 841 22

EX:NO:10 PARALLEL-RANK-WITH-MPI
DATE :

AIM:

To write a program for demonstrating performing-parallel-rank-with-MPI in C.

ALGORITHM:
Step 1: Start
Step 2: We have multiple processes executing in parallel using MPI.
Step 4: First, we initialize MPI and obtain the rank and size of the
MPI_COMM_WORLD communicator.
Step 5 : Each process then prints its rank and size using printf. The %d format
specifier is used to print integer values.
Step 6: Finally, we finalize MPI and clean up resources using MPI_Finalize().

THEORY:
 In this program, we have multiple processes executing in parallel using MPI.
 Each process has its own rank and can communicate with other processes.
 First, we initialize MPI and obtain the rank and size of the
MPI_COMM_WORLD communicator.
 Each process then prints its rank and size using printf. The %d format
specifier is used to print integer values.
 Finally, we finalize MPI and clean up resources using MPI_Finalize().
 To compile and run this program with MPI support, you can use the
following command:
 gcc -o gfg -fopenmp filename.c output
 For the execution using following command : ./gfg

PROGRAM:
#include <stdio.h>
#include<stdlib.h>
#include <mpi.h>
#include "tmpi_rank.h"
#include <time.h>

Downloaded by series dramas (mansa12345@gmail.com)


lOMoARcPSD|317 841 22

int main(int argc, char** argv)

MPI_Init(NULL, NULL);

int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

int world_size;

MPI_Comm_size(MPI_COMM_WORLD, &world_size);

// Seed the random number generator to get different results each time
srand(time(NULL) * world_rank);

float rand_num = rand() / (float)RAND_MAX; int rank;


TMPI_Rank(&rand_num, &rank, MPI_FLOAT, MPI_COMM_WORLD);

printf("Rank for %fon process %d - %d\n", rand_num, world_rank, rank);

MPI_Barrier(MPI_COMM_WORLD);

MPI_Finalize();
}
lOMoARcPSD|317 841 22

OUTPUT:

Rank for 0.242578 on process 0 – 0


Rank for 0.894732 on process 1 – 3
Rank for 0.789463 on process 2 – 2
Rank for 0.684195 on process 3 – 1

RESULT:

Thus the program has been executed successfully.

Downloaded by series dramas (mansa12345@gmail.com)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy