
// MultWorkerRow.java
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* 
   Each worker does one row-column multiplication
   and then terminates.

   The n rows of the first matrix are added to the space as
   tuples of the form:
     ("ROW", row-id, int[])
  
   The n columns of the second matrix are added to the space as
   tuples of the form:
     ("COLUMN", col-id, int[])
  
   A worker reads in copies of the relevant row and column arrays
   based on the row and column ids passed to it in its constructor.
  
   The result (a single integer value) is put into the space in
   a tuple of the form:
       ("RESULT", row-id, col-id, result)
  
   Usage:
    > compile.bat MultWorkerRow.java

    Part of the application made up of:
       * MatrixMultRow.java
       * MultWorkerRow.java
*/

import org.jspace.*;


public class MultWorkerRow implements Runnable
{
  private Space space;
  private int row, col;


  public MultWorkerRow(int row, int col, Space space)
  {
    this.space = space;
    this.row = row;
    this.col = col;
  }


  public void run()
  {
    try {
      Object[] rowObj =   // read in row of first matrix
          space.query( new ActualField("ROW"),
                     new ActualField(row), 
                     new FormalField(Object.class));
      int[] rowArr = (int[])rowObj[2];
 
      Object[] colObj =   // read in column of second matrix
          space.query( new ActualField("COLUMN"),
                     new ActualField(col), 
                     new FormalField(Object.class));
      int[] colArr = (int[])colObj[2];

      int result = 0;   // multiply together
      for (int i=0; i< rowArr.length; i++)
        result += rowArr[i]*colArr[i]; 

      space.put("RESULT", row, col, result);
      // System.out.print("W" + row + col + "=" + result + " ");
    }
    catch (InterruptedException e) 
    {  System.out.println("W" + row + col + " error: " + e);  }
  } /* run */

}  // end of MultWorkerRow class
