/******************************************************************* * * A similar example that uses MPI_Scatterv and MPI_Gatherv * * The program should be run with exactly 8 processes. * Process 0 initializes a matrix (x) of 8 by 8 integers and distributes * the lower triangular part of the matrix to the processes using * MPI_Scatterv. The processes receive different number of elements: * process 0 gets one element, process 1 gets 2 elements and process i * gets i+1 elements. The elements of all processes are then gathered * (using MPI_Gatherv) again in another 8 by 8 matrix (res) of process 0 * (which is initialized with zeros) in triangular shape. * * ********************************************************************/ #include #include #include "mpi.h" #define MAXPROC 8 /* Max number of procsses */ #define LENGTH 8 /* Size of matrix is LENGTH * LENGTH */ main(int argc, char* argv[]) { int i, j, np, me; const int root = 0; /* Root process in scatter */ int x[LENGTH][LENGTH]; /* Send buffer */ int y[LENGTH]; /* Receive buffer */ int res[LENGTH][LENGTH]; /* Final receive buffer */ int *sendcount, *recvcount; /* Arrays for sendcounts and recvcounts */ int *displs1, *displs2; /* Arrays for displacements */ MPI_Init(&argc, &argv); /* Initialize MPI */ MPI_Comm_size(MPI_COMM_WORLD, &np); /* Get nr of processes */ MPI_Comm_rank(MPI_COMM_WORLD, &me); /* Get own identifier */ /* Check that we have one process for each row in the matrix */ if (np != LENGTH) { if (me == 0) { printf("You have to use %d processes\n", LENGTH); } MPI_Finalize(); exit(0); } /* Allocate memory for the sendcount and displacements arrays */ sendcount = (int *)malloc(LENGTH*sizeof(int)); displs1 = (int *)malloc(LENGTH*sizeof(int)); /* Initialize sendcount and displacements arrays */ for (i=0; i