Sei sulla pagina 1di 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);

Potrebbero piacerti anche