<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * Parallel Matrix Multiplication. 
 * Using specified number of threads, matrix B is transposed.
 * @author Heinz Kredel.
 */

public class ParMultProcTrans implements MMInf {

    int anzahl = 1;

    public ParMultProcTrans(int threads) {
         anzahl = threads;	
    }


/**
 * Performs the multiplication of two matrices, B transposed.
 * C = A * transpose(B).
 * @param C result matrix.
 * @param A matrix.
 * @param B matrix.
 */
    public void multiply(double[][] C, double[][] A, double[][] B) {
      Thread[] t = new Thread[anzahl];
      System.out.print("Starting " + anzahl + " threads ...");
      for (int i=0; i &lt; anzahl; i++) {
          t[i] = new RowMultProcTrans(C,A,B,i,anzahl);
          t[i].start();
      }
      System.out.print(" started ...");
      for (int i=0; i &lt; anzahl; i++) {
          try { t[i].join(); }
          catch (InterruptedException e) { }
      }
      System.out.println(" done ParMultProcTrans");
    }

}


/**
 * This class is derived from the class Thread.
 * It performs a row multiplication.
 */
class RowMultProcTrans extends Thread {

    double[][] A;
    double[][] B;
    double[][] C;
    int i;  
    int anzahl;

/**
 * Constructor.
 * @param Cp Two dimensional matrices.
 * @param Ap Two dimensional matrices.
 * @param Bp Two dimensional matrices.
 */
  RowMultProcTrans(double[][] Cp, double[][] Ap, double[][] Bp, int ip, int a) {
    A = Ap; B = Bp; C = Cp; i = ip; anzahl = a;
  }

/**
 * Runs the multiplication.
 */
  public void run() {
    int schritte = A.length / anzahl; 
    if ( (A.length % anzahl) != 0 ) {
        schritte++; // ceiling
    }
    for (int ii = i*schritte; ii &lt; Math.min((i+1)*schritte,A.length); ii++ ) {
        double[] Aii = A[ii];
        for (int j=0; j &lt; B.length; j++) {
            double[] Bj = B[j];
            double c = 0.0;
            for (int k=0; k &lt; Bj.length; k++) {
                 c += Aii[k] * Bj[k];
            }
            C[ii][j] = c;
        }
    }
  }

}
</pre></body></html>