#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define ARRAY_SIZE 1000
int main(int argc, char *argv[]) {
int rank, size;
int max_local, max_global;
int *data = NULL;
int i;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Allocate memory for data array
data
= (int*)malloc(sizeof(int) * ARRAY_SIZE
);
// Generate random numbers
srand(rank
); // seed based on rank for different numbers in each process for (i = 0; i < ARRAY_SIZE; i++) {
}
// Find local max
max_local = data[0];
for (i = 1; i < ARRAY_SIZE; i++) {
if (data[i] > max_local) {
max_local = data[i];
}
}
// Reduce all local max values to find global max
MPI_Reduce(&max_local, &max_global, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
// Print result
if (rank == 0) {
printf("The maximum number is: %d\n", max_global
); }
// Cleanup
MPI_Finalize();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1waS5oPgoKI2RlZmluZSBBUlJBWV9TSVpFIDEwMDAKCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pIHsKICAgIGludCByYW5rLCBzaXplOwogICAgaW50IG1heF9sb2NhbCwgbWF4X2dsb2JhbDsKICAgIGludCAqZGF0YSA9IE5VTEw7CiAgICBpbnQgaTsKCiAgICBNUElfSW5pdCgmYXJnYywgJmFyZ3YpOwogICAgTVBJX0NvbW1fcmFuayhNUElfQ09NTV9XT1JMRCwgJnJhbmspOwogICAgTVBJX0NvbW1fc2l6ZShNUElfQ09NTV9XT1JMRCwgJnNpemUpOwoKICAgIC8vIEFsbG9jYXRlIG1lbW9yeSBmb3IgZGF0YSBhcnJheQogICAgZGF0YSA9IChpbnQqKW1hbGxvYyhzaXplb2YoaW50KSAqIEFSUkFZX1NJWkUpOwoKICAgIC8vIEdlbmVyYXRlIHJhbmRvbSBudW1iZXJzCiAgICBzcmFuZChyYW5rKTsgIC8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