// file: /data/courses/ece_1111/lectures/current/lecture_04/example.cc // // local include files // #include "example.h" // function: main // // This is a simple program that prints to the terminal. // int main(int argc, const char** argv) { // decode the command line arguments // long nrows = atoi(argv[1]); long ncols = atoi(argv[2]); long niter = atoi(argv[3]); // echo the command line arguments // fprintf(stdout, "program name: %s\n", argv[0]); fprintf(stdout, "number of rows = %d\n", nrows); fprintf(stdout, "number of cols = %d\n", ncols); fprintf(stdout, "niter = %d\n", niter); // declare local variables // float* a = new float[nrows * ncols]; float* b = new float[ncols * nrows]; float* c = new float[nrows * nrows]; // initialize a matrix // fprintf(stdout, "... initializing...\n"); if (minit(a, nrows, ncols) == false) { fprintf(stdout, "%s: error initializing matrices\n", argv[0]); } if (minit(b, ncols, nrows) == false) { fprintf(stdout, "%s: error initializing matrices\n", argv[0]); } // print the matrices // fprintf(stdout, "... printing...\n"); if (mprint(a, nrows, ncols) == false) { fprintf(stdout, "%s: error printing matrices\n", argv[0]); } if (mprint(b, ncols, nrows) == false) { fprintf(stdout, "%s: error printing matrices\n", argv[0]); } // multiply both matrices // for (long i = 0; i < niter; i++) { if (i % NITER == 0) { fprintf(stdout, "iteration no. %d\n", i); } if (mmult(c, a, b, nrows, ncols) == false) { fprintf(stdout, "%s: error multiplying matrices\n", argv[0]); } } // exit gracefully // return(0); } // function: mmult // // arguments: // float* c: output matrix // float* a: operands // float* b: operands // long nrows: number of rows // long ncols: number of cols // // return: returns a boolean value indicating status // // This method multiplies two matrices (c = a * b). // bool mmult(float* c, float* a, float* b, long nrows, long ncols) { // print an informational message // fprintf(stdout, "mmult: %d %d\n", nrows, ncols); // exit gracefully // return true; } // function: minit // // arguments: // float* a: operands // long nrows: number of rows // long ncols: number of cols // // return: returns a boolean value indicating status // // This method initializes a matrix. // bool minit(float* a, long nrows, long ncols) { // print an informational message // fprintf(stdout, "mmult: initializing (%d %d)\n", nrows, ncols); // loop over the rows // for (long i = 0; i < nrows; i++) { // loop over the cols // for (long j = 0; j < ncols; j++) { // set the value // if (i == j) { *a++ = (float)1.0; } else { *a++ = (float)0.0; } } } // exit gracefully // return true; } // function: mprint // // arguments: // float* a: operands // long nrows: number of rows // long ncols: number of cols // // return: returns a boolean value indicating status // // This method prints a matrix. // bool mprint(float* a, long nrows, long ncols) { // loop over the rows // for (long i = 0; i < nrows; i++) { // print the row // fprintf(stdout, "%5d:", i); // loop over the cols // for (long j = 0; j < ncols; j++) { // print the value // fprintf(stdout, "%10.4f ", *a++); } // print a new line // fprintf(stdout, "\n"); } fprintf(stdout, "\n"); // exit gracefully // return true; }