0% found this document useful (0 votes)
87 views5 pages

Lab Exercise - Mpi: NAME:-Shivam Kapoor REG. No: - 15BCE1339 FACULTY: - Prof. Sivagami M

The document describes an MPI lab exercise completed by Shivam Kapoor. It includes 5 questions involving MPI programs to: 1) Print a hello message from different processes 2) Print process ID and total number of processes 3) Send a value from process 0 to process 2 4) Send a user input value from process 1 to process 2 5) Send a user input value from process 1 to process 2 Each question includes the code written to answer the question.

Uploaded by

Shivam Kapoor
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)
87 views5 pages

Lab Exercise - Mpi: NAME:-Shivam Kapoor REG. No: - 15BCE1339 FACULTY: - Prof. Sivagami M

The document describes an MPI lab exercise completed by Shivam Kapoor. It includes 5 questions involving MPI programs to: 1) Print a hello message from different processes 2) Print process ID and total number of processes 3) Send a value from process 0 to process 2 4) Send a user input value from process 1 to process 2 5) Send a user input value from process 1 to process 2 Each question includes the code written to answer the question.

Uploaded by

Shivam Kapoor
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/ 5

LAB EXERCISE - MPI

NAME:- Shivam Kapoor

REG. No:- 15BCE1339

FACULTY:- Prof. Sivagami M.

---------------------------------------------------------------
1. Write MPI program to print hello message

CODE

#include <mpi.h>

#include <stdio.h>

#include <stdlib.h>

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

int rank, size;

MPI_Init (&argc, &argv);

printf( "Hello world from process %d of %d\n", rank, size );

MPI_Finalize();

return 0;

2. Write MPI program to print the hello message with process


id and size of the processes

CODE

#include <mpi.h>

#include <stdio.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 world from process %d of %d\n", rank, size );

MPI_Finalize();

return 0;

3. Write MPI program to send the value 1 by process 0 and


recieve by process 2.

CODE

#include <mpi.h>

#include <stdio.h>

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

MPI_Init (&argc, &argv);

int world_rank;

MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

int world_size;

MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int number;

if (world_rank == 0){

number = 1;

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

else if (world_rank == 1){


MPI_Recv(&number, 1, MPI_INT, 0, 0,

MPI_COMM_WORLD,MPI_STATUS_IGNORE); printf("Process
1 received number %d from process 0\n",number);

MPI_Finalize();

return 0;

4. Write MPI program to send the user input value by process


1 and receive by process 2.

CODE

#include <mpi.h>

#include <stdio.h>

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

MPI_Init (&argc, &argv);

int world_rank;

MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

int world_size;

MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int send_data=1;

int recv_data=0;

if(world_rank==0){

MPI_Send(&send_data,1,MPI_INT,2,30,MPI_COMM_WORLD);

printf("Sent data: %d from p_id: %d \n\n", send_data,


world_rank);

}
else if(world_rank==2){

MPI_Recv(&recv_data,1,MPI_INT,0,30,MPI_COMM_WORLD,M
PI_STATUS_IGNORE);

printf("Received data: %d at process: %d \n\n", recv_data,


world_rank);

Return 0;

QUESTION 5

CODE

#include <mpi.h>

#include <stdio.h>

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

MPI_Init (&argc, &argv);

int world_rank;

MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

int world_size;

MPI_Comm_size(MPI_COMM_WORLD, &world_size);

int send_data;

int recv_data=0;

if(world_rank==1){

printf("Enter a value: ");

scanf("%d",&send_data);
MPI_Send(&send_data,1,MPI_INT,2,30,MPI_COMM_WORLD);

printf("Sent data: %d from p_id: %d\n\n",send_data,world_rank);

else if(world_rank==2){

MPI_Recv(&recv_data,1,MPI_INT,1,30,MPI_COMM_WORLD,MPI_
STATUS_IGNORE);

printf("Received data: %d at process: %d \n\n", recv_data,


world_rank);

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