// single process matrix multiplication // written by ian a. mason // may 21 @ u.n.e // reads from and writes to files. comparable to // mm.0.4 and Lecture_23/Examples/torus.c #include #include #include "sblock.h" #include "mmlib.h" #include #include #include #include #define VERBOSE 0 int parse_args(int, char*[], int*, int[]); int main(int argc, char* argv[]){ //file descriptors /* A = F[0] B = F[1] C = F[2] */ int F[3]; //actual data /* A = M[0] B = M[1] C = M[2] */ int *M[3] = { NULL, NULL, NULL }; int i, j, dim, row, col; int start_time = time(NULL); if(parse_args(argc, argv, &dim, F) < 0) goto exit; for(i = 0; i < 3; i++) M[i] = (int*)malloc(sizeof(int)*dim*dim); if (!(M[0] && M[1] && M[2])) { fprintf(stderr, "%s: out of memory!\n", argv[0]); free(M[0]); free(M[1]); free(M[2]); }; for(j = 0; j < 2; j++) for(i = 0; i < dim; i++) get_block_row(F[j], dim, 0, 0, i, dim, &(M[j][i*dim])); // InitBlock(M[0], M[1], M[2], dim); BlockMult(M[2],M[0],M[1],dim); for(i = 0; i < dim; i++) set_block_row(F[2], dim, 0, 0, i, dim, &M[2][i*dim]); if(VERBOSE){ printf("C\n"); for(row = 0; row < dim; row++){ for(col = 0; col < dim; col++) printf("%5d ", M[2][(row*dim) + col]); printf("\n"); } } fprintf(stderr, "Done smoothly after %ld seconds\n", time(NULL) - start_time); exit: free(M[0]); free(M[1]); free(M[2]); return 0; } int parse_args(int argc, char *argv[], int *dim, int F[]){ if ((argc != 5) || ((F[0] = open(argv[1], O_RDONLY)) == -1) || ((F[1] = open(argv[2], O_RDONLY)) == -1) || ((F[2] = open(argv[3], O_WRONLY)) == -1) || ((*dim = atoi(argv[4])) <= 0)){ fprintf(stderr, "Usage: %s matrixA matrixB matrixC dim\n", argv[0]); return(-1); }; return(0); }