求数组最大值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
int max(int a, int b) {
if (a > b) return a;
else return b;
}
int main(int argc, char **argv) {
int rank, size, p, n;
int a[12] = {1, 3, 5, 4, 2, 6, 8, 9, 11, 21, 0, 33};
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
p = size - 1;
if (12 % p != 0) exit(-1);
n = 12 / p;
if (rank == 0) {
int ans = 0, temp = 0, i = 0;
for (i = 1; i < size; i++) {
MPI_Recv(&temp, 1, MPI_INT, i, 99, comm, &status);
ans = max(ans, temp);
}
printf("rank 0 : maxvalue = %d\n", ans);
for (i = 1; i < size; i++) {
MPI_Send(&ans, 1, MPI_INT, i, 99, comm);
}
}
else {
int ans =