#include #include #include typedef struct Complex_s { double x,y; } Complex; void proba(int *x, int *y, int *len, MPI_Datatype * type) { int i; for(i = 0; i < *len; i++) { *y += (*x); x++; y++; } } void opp_complex(Complex *c1, Complex *c2, int *len, MPI_Datatype * type) { int i; for(i = 0; i < *len; i++) { // if((c1->x > c2->x) || ((c1->x == c2->x) && (c1->y > c2->y))) //*c2 = *c1; (c2->x) += (c1->x); (c2->y) += (c1->y); c1++; c2++; } } int main(int argc, char *argv[]) { MPI_Status status; int id, np, err; int a, i; MPI_Op opp, mpi_opp_complex; MPI_Datatype typeComplex; err = MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &np); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Type_contiguous(2, MPI_DOUBLE, &typeComplex); MPI_Type_commit(&typeComplex); MPI_Op_create((MPI_User_function *)proba, 1, &opp); a = id+10; MPI_Reduce(&a, &i, 1, MPI_INT, opp, 0, MPI_COMM_WORLD); printf("my id is %d, and a = %d\n", id, a); if(id == 0) printf("my id is %d, and i = %d\n", id, i); Complex aa,bb; aa.x = id; aa.y = id + 1; MPI_Op_create((MPI_User_function *)opp_complex, 1, &mpi_opp_complex); MPI_Reduce(&aa, &bb, 1, typeComplex, mpi_opp_complex, 0, MPI_COMM_WORLD); printf("aa.x = %lf, aa.y = %lf\n", aa.x, aa.y); if(id == 0) { printf("bb.x = %lf, bb.y = %lf\n", bb.x, bb.y); } Complex *cc, *rr; int n = 3; cc = (Complex*)malloc(n*sizeof(Complex)); rr = (Complex*)malloc(n*sizeof(Complex)); for(i = 0; i < n; i++) { cc[i].x = id*10+i; cc[i].y = id*100+i; } MPI_Reduce(cc, rr, n, typeComplex, mpi_opp_complex, 0, MPI_COMM_WORLD); if(id == 0) { for(i = 0; i < n; i++) printf("rr[%d].x = %lf, rr[%d].y = %lf\n", i, rr[i].x, i, rr[i].y); } MPI_Finalize(); }