/** * Sequential program to test/time simple matrix-multiplication routine. * * Command-line arguments: size of matrices to multiply. */ #include #include #include #include "matrix-mult-seq-utility.h" #include "matrix-mult-seq-initfortest.h" #include "matrix-mult-seq-print.h" int main(int argc, char *argv[]) { double start_time, end_time; int N; /* input size */ int dimN, dimP, dimM; /* matrix dimensions */ double *A, *B, *C; /* matrices */ int myID, nprocs; /* MPI initialization */ MPI_Init(&argc, &argv); MPI_Comm_size (MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myID); /* command-line argument */ if (argc < 2) { fprintf(stderr, "usage: %s size\n", argv[0]); return EXIT_FAILURE; } N = atoi(argv[1]); if (N <= 0) { fprintf(stderr, "usage: %s size\n", argv[0]); return EXIT_FAILURE; } dimN = dimP = dimM = N; A = malloc(dimN*dimP*sizeof(double)); B = malloc(dimP*dimM*sizeof(double)); C = malloc(dimN*dimM*sizeof(double)); if ((A == NULL) || (B == NULL) || (C == NULL)) { fprintf(stderr, "unable to allocate space for matrices of size %d\n", dimN); return EXIT_FAILURE; } /* Initialize matrices */ initialize(A, B, dimN, dimP, dimM); /* Do the multiply */ start_time = MPI_Wtime(); matclear(C, dimN, dimM, dimM); matmul_add(A, B, C, dimN, dimP, dimM, dimP, dimM, dimM); end_time = MPI_Wtime(); /* Print results */ printMatrix(stdout, "A", A, dimN, dimP, dimP); printMatrix(stdout, "B", B, dimP, dimM, dimM); printMatrix(stdout, "A*B", C, dimN, dimM, dimM); fprintf(stderr, "Simple program, sequential\n"); fprintf(stderr, "Size = %d, time for multiplication = %g\n", N, end_time - start_time); /* MPI cleanup */ MPI_Finalize(); return EXIT_SUCCESS; }