MPI
MPI
#include<mpi.h>
#include<stdio.h>
int main(int argc, char **argv)
{
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_size(MPI_COMM_WORLD, &world_rank);
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
printf("Hello world from processor %s rank %d out of %d processors\n", processor_name,
world_rank, world_size);
MPI_Finalize();
}
Program 2: Write an MPI program to find the min number from randomly generated 1000
numbers (stored in array) on a cluster (Hint: Use MPI_Reduce) (Slip No: 11, 16, 27, 30)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAY_SIZE 10
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
if (my_rank == 0)
{
MPI_Finalize();
return 0;
}
Program 3: Write an MPI program to find the max number from randomly generated
1000 numbers (stored in array) on a cluster (Hint: Use MPI_Reduce) (Slip No: 5, 20)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAY_SIZE 10
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
if (my_rank == 0)
{
MPI_Finalize();
return 0;
}
Program 4: Write an MPI program to calculate sum of randomly generated 1000 numbers
(stored in array) on a cluster (Slip No: 2, 12, 28)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAY_SIZE 10
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
if (my_rank == 0)
{
MPI_Finalize();
return 0;
}
Program 5: Write an MPI program to calculate sum of even randomly generated 1000
numbers (stored in array) on a cluster (Slip No: 21, 29)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAY_SIZE 10
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
if (my_rank == 0)
{
MPI_Finalize();
return 0;
}
Program 6: Write an MPI program to calculate sum of odd randomly generated 1000
numbers (stored in array) on a cluster (Slip No: 22, 24)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAY_SIZE 10
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
}
MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0,
MPI_COMM_WORLD);
if (my_rank == 0)
{
MPI_Finalize();
return 0;
}