#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
MPI_Init(&argc, &argv); // Initialize the MPI environment
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Get the rank of the process
MPI_Comm_size(MPI_COMM_WORLD, &size); // Get the total number of processes
if (size != 5) {
if (rank == 0) {
printf("This program requires exactly 5 processes.\n"); }
MPI_Finalize();
return 1;
}
// Generate a unique seed for each process
unsigned int seed
= time(NULL
) + rank
; // Unique seed using current time and rank
// Generate a pseudo-random number
int random_number
= rand(); printf("Process %d generated random number: %d\n", rank
, random_number
);
// Find the maximum using MPI_Reduce
int max_random_number;
MPI_Reduce(&random_number, &max_random_number, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
// Root process prints the maximum random number
if (rank == 0) {
printf("The maximum random number is: %d\n", max_random_number
); }
MPI_Finalize(); // Finalize the MPI environment
return 0;
}
I2luY2x1ZGUgPG1waS5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8dGltZS5oPgoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqKiBhcmd2KSB7CiAgICBNUElfSW5pdCgmYXJnYywgJmFyZ3YpOyAvLyBJbml0aWFsaXplIHRoZSBNUEkgZW52aXJvbm1lbnQKCiAgICBpbnQgcmFuaywgc2l6ZTsKICAgIE1QSV9Db21tX3JhbmsoTVBJX0NPTU1fV09STEQsICZyYW5rKTsgLy8gR2V0IHRoZSByYW5rIG9mIHRoZSBwcm9jZXNzCiAgICBNUElfQ29tbV9zaXplKE1QSV9DT01NX1dPUkxELCAmc2l6ZSk7IC8vIEdldCB0aGUgdG90YWwgbnVtYmVyIG9mIHByb2Nlc3NlcwoKICAgIGlmIChzaXplICE9IDUpIHsKICAgICAgICBpZiAocmFuayA9PSAwKSB7CiAgICAgICAgICAgIHByaW50ZigiVGhpcyBwcm9ncmFtIHJlcXVpcmVzIGV4YWN0bHk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