/*
 * RelaxMaster.java -- The Master thread for the relax method.
 */

import java.util.concurrent.CyclicBarrier;

class RelaxMaster {
    RelaxSlave slaves[];
    int nthreads;
    public RelaxMaster( double boundary[][], double t[][], 
			     final int nthreads)
    {
	this.nthreads = nthreads;
	CyclicBarrier barrier = 
	    new CyclicBarrier( nthreads,
			       new Runnable() {
				   public synchronized void run() {
				       all_done = n_done == nthreads;
				       n_done = 0;
				   }
			       }
			     );
	double t_temp[][] = Matrix.alloc( Matrix.nrows(t), Matrix.ncols(t) );
	slaves = new RelaxSlave[nthreads];
	int i;
	for( i=0; i<nthreads; i++ )
	{
	    slaves[i] = new RelaxSlave( i,nthreads, boundary, t, t_temp,
					barrier, this );
	}
    }
    public void relax()
	throws java.lang.InterruptedException
    {
	int i;
	for( i=0; i<nthreads; i++ )
	{
	    slaves[i].start();
	}
	for( i=0; i<nthreads; i++ )
	{
	    slaves[i].join();
	}
    }
    int n_done = 0;
    boolean all_done = false;
    public synchronized void i_am_done()
    {
	n_done ++;
    }
    public synchronized boolean all_done()
    {
	return( all_done );
    }
}
