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

MPI

The document contains several MPI programs that demonstrate different functionalities, including displaying messages, finding minimum and maximum numbers, and calculating sums of randomly generated numbers. Each program uses MPI functions to distribute tasks across processes in a cluster environment. The programs illustrate the use of MPI_Reduce for aggregating results from multiple processes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MPI

The document contains several MPI programs that demonstrate different functionalities, including displaying messages, finding minimum and maximum numbers, and calculating sums of randomly generated numbers. Each program uses MPI functions to distribute tasks across processes in a cluster environment. The programs illustrate the use of MPI_Reduce for aggregating results from multiple processes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Program 1: to display message

#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

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


int my_rank, num_processes;
int array[ARRAY_SIZE];
int local_min = INT_MAX, global_min;
int i;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);

printf("The contents of randomly generated array \n");


for (i = 0; i < ARRAY_SIZE / num_processes; i++)
{
array[i] = rand() % 1000;
printf("%d\n", array[i]);
if (array[i] < local_min)
{
local_min = array[i];
}

MPI_Reduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, 0,


MPI_COMM_WORLD);

if (my_rank == 0)
{

printf("The minimum number is %d\n", global_min);

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

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


int my_rank, num_processes;
int array[ARRAY_SIZE];
int local_max = INT_MIN, global_max;
int i;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);

printf("The contents of randomly generated array \n");


for (i = 0; i < ARRAY_SIZE / num_processes; i++)
{
array[i] = rand() % 1000;
printf("%d\n", array[i]);
if (array[i] > local_max)
{
local_max = array[i];
}

MPI_Reduce(&local_ max, &global_ max, 1, MPI_INT, MPI_MAX, 0,


MPI_COMM_WORLD);

if (my_rank == 0)
{

printf("The Maximum number is %d\n", global_max);

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

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


int my_rank, num_processes;
int array[ARRAY_SIZE];
int local_sum=0, global_sum;
int i;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);

printf("The contents of randomly generated array \n");


for (i = 0; i < ARRAY_SIZE / num_processes; i++)
{
array[i] = rand() % 10;
printf("%d\n", array[i]);
local_sum = local_sum+array[i];

MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0,


MPI_COMM_WORLD);

if (my_rank == 0)
{

printf("The sum of all numbers is %d\n", global_sum);

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

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


int my_rank, num_processes;
int array[ARRAY_SIZE];
int local_sum=0, global_sum;
int i;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);

printf("The contents of randomly generated array \n");


for (i = 0; i < ARRAY_SIZE / num_processes; i++)
{
array[i] = rand() % 10;
printf("%d\n", array[i]);
if (array[i]%2 == 0)
local_sum = local_sum+array[i];
}

MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0,


MPI_COMM_WORLD);

if (my_rank == 0)
{

printf("The sum of all even numbers is %d\n", global_sum);

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

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


int my_rank, num_processes;
int array[ARRAY_SIZE];
int local_sum=0, global_sum;
int i;

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);

printf("The contents of randomly generated array \n");


for (i = 0; i < ARRAY_SIZE / num_processes; i++)
{
array[i] = rand() % 10;
printf("%d\n", array[i]);
if (array[i]%2 == 1)
local_sum = local_sum+array[i];

}
MPI_Reduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, 0,
MPI_COMM_WORLD);

if (my_rank == 0)
{

printf("The sum of all odd numbers is %d\n", global_sum);

MPI_Finalize();
return 0;
}

You might also like