/*
 * MatrixMultiplySlave.java -- Slave threads for Matrix multiplication.
 */

class MatrixMultiplySlave extends java.lang.Thread {
    double[][] a, b, c;
    int i,n ;
    double trace_ans = 0.0 ;
    public MatrixMultiplySlave( double a[][], double b[][], double c[][],
				int i, int n )
    {
	this.a = a ; this.b = b ; this.c = c ;
	this.i = i ; this.n = n ;
	double trace = 0.0;
    }
    public void run()
    {
	int c_nrows = Matrix.nrows(c), c_ncols = Matrix.ncols(c), 
	    a_ncols = Matrix.ncols(a);
	int quot = c_nrows /n; 	int rem = c_nrows % n;
	int do_rows = quot + (i < rem ? 1 : 0);
	int first_row = quot * i + (i < rem ? i : rem);
	double trace = 0.0;
	
	int row, col, j;
	for( row = first_row; row < first_row + do_rows; row++ )
	{
	    for( col=0; col < c_ncols; col++ )
	    {
		double sum = 0.0;
		for( j = 0; j < a_ncols; j++ )
		{
		   sum += a[row][j] * b[j][col];
		}
		c[row][col] = sum;
		if( row == col ) 
		{
		    trace += sum;
		}
	    }
	}
	trace_ans = trace;
    }
    public double get_trace()
    {
	return( trace_ans );
    }
}
