DAPPLE Example: matmul

Source code:

// @TITLE "matmul.cc -- matrix multiply" // // matmul -- a parallel matrix-matrix multiply program // written using the Data-Parallel Programming Library for Education (DAPPLE) // // This version is fairly straightforward, but it has limited parallelism. // For each entry in the result matrix, we do a parallel dot product // of the appropriate row and the appropriate column. // Thus, it takes N^2 iterations each with N-way parallelism, // for an NxN matrix. // // David Kotz 1994 // $Id: matmul.cc,v 1.3 94/09/29 12:18:06 dfk Exp Locker: dfk $ #include #include #include #include "dapple.h" // we'll multiply a PxQ matrix by a QxR matrix to get a PxR matrix const int P = 3; const int Q = 3; const int R = 3; int main(int argc, char **argv) { // we'll compute C = A * B intMatrix A(P,Q); intMatrix B(Q,R); intMatrix C(P,R); intMatrix D(P,R); // D is what C should be // load matrices; row-major order, whitespace-separated integers cout << "To multiply C = A * B." << endl; cout << "Enter " << P << " by " << Q << " matrix A: " << endl; cin >> A; cout << "Enter " << Q << " by " << R << " matrix B: " << endl; cin >> B; // loop through the result locations for (int r = 0; r < P; r++) for (int c = 0; c < R; c++) C[r][c] = inner(A[r][_], B[_][c]); cout << "Result C = " << endl; cout << C; cout << "Enter correct " << P << " by " << R << " matrix D: " << endl; cin >> D; if (any(C != D)) cout << "The answers are different." << endl; else cout << "The answers are the same!" << endl; return(0); }

Demonstration:

cat matmul.data 5 -3 1 2 1 4 3 -1 2 2 -1 1 0 1 2 1 0 1 11 -8 0 8 -1 8 8 -4 3 matmul < matmul.data To multiply C = A * B. Enter 3 by 3 matrix A: Enter 3 by 3 matrix B: Result C = 11 -8 0 8 -1 8 8 -4 3 Enter correct 3 by 3 matrix D: The answers are the same!