rendered paste body#include "mpi.h"#include <math.h>#include <stdio.h>#define SIZE1 4#define SIZE2 4int main( int argc, char *argv[] ){ int n, myid, numprocs, i; long sum, localsum; double time; int matr[SIZE1][SIZE2]; int vect[SIZE2]; int locans[SIZE1]; int ans[SIZE1]; int j=0; int k=0; while(j<SIZE1){ while(k<SIZE2){ matr[j][k]=rand(); while(k<SIZE2){ matr[j][k]=rand(); // printf("%i ",matr[j][k]); vect[k++]=rand(); }// printf("\n"); locans[j++]=0; k=0; } // PART 1: Initialization MPI_Datatype size1type, size2type; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); // PART 2: broad-cast the number of intervals if (myid == 0) { n = numprocs; } n = numprocs; } //MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); //create a new broadcast type for the arrays MPI_Type_contiguous(SIZE2,MPI_INT,&size2type); MPI_Type_commit(&size2type); MPI_Type_contiguous(SIZE1,MPI_INT,&size1type); MPI_Type_commit(&size1type); int sendcount=0; while(sendcount<SIZE1){ MPI_Bcast(&matr[sendcount++][0],1,size2type,0,MPI_COMM_WORLD); } MPI_Bcast(&vect[0],1,size2type,0,MPI_COMM_WORLD); // PART 3: record the clock time time = MPI_Wtime(); // PART 4: compute j=myid; k=0; while(j<SIZE1){ while(k<SIZE2){// printf("%i ", matr[j][k]); locans[j]+=matr[j][k]*vect[k++]; } j+=numprocs; k=0;// printf("\n"); } // PART 5: reduce// MPI_Reduce(&locans, &ans, 1, size1type, MPI_SUM, 0, MPI_COMM_WORLD);// MPI_Gather(&locans,SIZE1,MPI_INT,&ans,SIZE1,MPI_INT,0,MPI_COMM_WORLD); j=0; while(j<SIZE1){// MPI_Gather(&locans[j],1,MPI_INT,&ans[j++],1,MPI_INT,0,MPI_COMM_WORLD); MPI_Reduce(&locans[j], &ans[j], 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); } if (myid == 0) { printf("The value is:\n[ "); int l=0; while(l<SIZE1){ printf("%i\n ",ans[l++]); } printf(" ]\n"); printf("Time cost = %g\n", MPI_Wtime()-time); } MPI_Finalize(); return 0;}