// file: /data/courses/ece_1111/lectures/current/lecture_06/functs_00.cc // // local include files // #include "example.h" // function: mmult // // arguments: // float* c: output matrix // float* a: operands // float* b: operands // long nrows: number of rows // long ncols: number of cols // FILE* fp: output file pointer // // 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, FILE* fp) { // print an informational message // // fprintf(fp, "mmult: %d %d\n", nrows, ncols); // check dimensions // if (nrows != ncols) { fprintf(fp, "mmult: incompatible dimensions (%d %d)\n", nrows, ncols); return false; } // loop over all rows // for (long i = 0; i < nrows; i++) { float* c_ptr = &c[i*nrows]; for (long j = 0; j < ncols; j++) { float sum = 0.0; float* a_ptr = &a[i*ncols]; long k_ptr = 0; for (long k = 0; k < nrows; k++) { sum += *a_ptr++ * b[k_ptr + j]; k_ptr += ncols; } *c++ = sum; } } // exit gracefully // return true; } // function: minit // // arguments: // float* a: operands // long nrows: number of rows // long ncols: number of cols // long mode: type of initialization // FILE* fp: output file pointer // // return: returns a boolean value indicating status // // This method initializes a matrix. // bool minit(float* a, long nrows, long ncols, long mode, FILE* fp) { // initialize to an identify matrix // if (mode == MODE_IDENTITY) { // print an informational message // fprintf(fp, "minit: initializing (%d %d) to an idenity matrix\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; } } } } // else if: initialize to random values // else if (mode == MODE_RANDOM) { // print an informational message // fprintf(fp, "minit: initializing (%d %d) to random values\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 // *a++ = (float)((drand48() - MYOFFSET) * MYRANGE); } } } // else: unsupported mode // else { // print an informational message // fprintf(fp, "minit: unknown mode (%d\n", mode); // exit ungracefully // return false; } // exit gracefully // return true; } // function: mprint // // arguments: // float* a: operands // long nrows: number of rows // long ncols: number of cols // char* lbl: a label to tag each line // FILE* fp: output file pointer // // return: returns a boolean value indicating status // // This method prints a matrix. // bool mprint(float* a, long nrows, long ncols, char* lbl, FILE* fp) { // loop over the rows // for (long i = 0; i < nrows; i++) { // print the row // fprintf(stdout, "%s %5d: ", lbl, 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; }