c - Trying to run MPI Matrix Multiplication Example -
i'm trying run code found online understand mpi better i'm thrown error when try compile following code.
i returned following:
$ mpicc -o mpimm mpimm.c mpimm.c: in function ‘main’: mpimm.c:10: error: storage size of ‘start’ isn’t known mpimm.c:10: error: storage size of ‘stop’ isn’t known
here example code below
#include "stdio.h" #include "mpi.h" #define n 500 /* number of rows , columns in matrix */ mpi_status status; double a[n][n],b[n][n],c[n][n]; //matrix used main(int argc, char **argv){ struct timeval start, stop; int numberoftasks, mtype, taskid, numberofworkers, source, destination, rows, averagerow, extra, offset,i,j,k; //first initialization mpi_init(&argc, &argv); mpi_comm_rank(mpi_comm_world, &taskid); mpi_comm_size(mpi_comm_world, &numberoftasks); numberofworkers = numberoftasks-1; //---------------------------- master ----------------------------// if (taskid == 0) { for (i=0; i<n; i++) { for (j=0; j<n; j++) { a[i][j]= 1.0; b[i][j]= 2.0; } } /* send matrix data worker tasks */ gettimeofday(&start, 0); averagerow = n/numberofworkers; //average rows per worker extra= n%numberofworkers; //extra rows offset = 0; for (destination=1; destination<=numberofworkers; destination++){ if(destination<=extra){ rows = averagerow+1; }else{ rows = averagerow; } mtype = 1; mpi_send(&offset, 1, mpi_int, destination, mtype, mpi_comm_world); mpi_send(&rows, 1, mpi_int, destination, mtype, mpi_comm_world); mpi_send(&a[offset][0], rows*n, mpi_double,destination,mtype, mpi_comm_world); mpi_send(&b, n*n, mpi_double, destination, mtype, mpi_comm_world); offset = offset + rows; } /* wait results worker tasks */ for (i=1; i<=numberofworkers; i++){ mtype = 2; source = i; mpi_recv(&offset, 1, mpi_int, source, mtype, mpi_comm_world, &status); mpi_recv(&rows, 1, mpi_int, source, mtype, mpi_comm_world, &status); mpi_recv(&c[offset][0], rows*n, mpi_double, source, mtype, mpi_comm_world, &status); } gettimeofday(&stop, 0); printf("upper left = %6.2f upper right = %6.2f \n",c[0][0],c[0][n-1]); printf("lower left = %6.2f lower right = %6.2f \n",c[n-1][0],c[n-1][n-1]); fprintf(stdout,"time = %.6f\n\n",(stop.tv_sec+stop.tv_usec*1e-6)-(start.tv_sec+start.tv_usec*1e-6)); } /*---------------------------- worker----------------------------*/ if (taskid > 0) { source = 0; mtype = 1; mpi_recv(&offset, 1, mpi_int, source, mtype, mpi_comm_world, &status); mpi_recv(&rows, 1, mpi_int, source, mtype, mpi_comm_world, &status); mpi_recv(&a, rows*n, mpi_double, source, mtype, mpi_comm_world, &status); mpi_recv(&b, n*n, mpi_double, source, mtype, mpi_comm_world, &status); /* matrix multiplication */ for (k=0; k<n; k++) for (i=0; i<rows; i++) { c[i][k] = 0.0; for (j=0; j<n; j++) c[i][k] = c[i][k] + a[i][j] * b[j][k]; } mtype = 2; mpi_send(&offset, 1, mpi_int, 0, mtype, mpi_comm_world); mpi_send(&rows, 1, mpi_int, 0, mtype, mpi_comm_world); mpi_send(&c, rows*n, mpi_double, 0, mtype, mpi_comm_world); } mpi_finalize(); }
struct timeval
defined in <time.h>
, have not yet included.
gettimeofday
fine, more "mpi" way use mpi_wtime()
. routine returns double, , can avoid struct timeval
issue entirely.
Comments
Post a Comment