package edu.rice.hj.example.comp322; import edu.rice.hj.api.HjMetrics; import edu.rice.hj.api.HjSuspendable; import edu.rice.hj.api.HjSuspendingProcedureInt2D; import edu.rice.hj.api.SuspendableException; import edu.rice.hj.runtime.config.HjSystemProperty; import edu.rice.hj.runtime.metrics.AbstractMetricsManager; import static edu.rice.hj.Module1.*; /** * MatrixMultiplicationMetrics --- Multiplies two square matrices *

* The purpose of this example is to illustrate abstract metrics and Amdahl's Law using the forallNb construct on matrix * multiplication. * * @author Shams Imam (shams@rice.edu) */ public class MatrixMultiplicationMetrics { private static final int DEFAULT_MATRIX_SIZE = 20; /** *

main.

* * @param args an array of {@link String} objects. */ public static void main(final String[] args) { final int matrixSize = args.length > 0 ? Integer.parseInt(args[0]) : DEFAULT_MATRIX_SIZE; System.out.println("Matrix size: " + matrixSize + " \n"); HjSystemProperty.abstractMetrics.setProperty(true); HjSystemProperty.executionGraph.setProperty(true); HjSystemProperty.speedUpGraph.setProperty(true); launchHabaneroApp(new HjSuspendable() { @Override public void run() throws SuspendableException { finish(new HjSuspendable() { @Override public void run() throws SuspendableException { testMatrixMultiply(matrixSize); } }); final HjMetrics actualMetrics = abstractMetrics(); AbstractMetricsManager.dumpStatistics(actualMetrics); } }); } private static void testMatrixMultiply(final int N) throws SuspendableException { final int[][] A = new int[N][N]; final int[][] B = new int[N][N]; final int[][] C = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { A[i][j] = i; B[i][j] = j; } } forall(0, N - 1, 0, N - 1, new HjSuspendingProcedureInt2D() { @Override public void apply(final int i, final int j) throws SuspendableException { C[i][j] = 0; for (int k = 0; k < N; k++) { doWork(1); C[i][j] += A[i][k] * B[k][j]; } } }); verifyComputation(C, N); } private static void verifyComputation(final int[][] dataArray, final int N) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { final int actual = dataArray[i][j]; final int expected = i * j * N; if (actual != expected) { throw new IllegalStateException("At position [" + i + ", " + j + "] expected " + expected + ", found " + actual); } } } } }