/*
 * RelaxDemo.java -- run and measure execution time of Relax. 
 */

class RelaxDemo {
    public static void main(String argv[]) 
	throws java.lang.InterruptedException
    {
	if( argv.length != 3 )
        {
	    System.err.println("Usage:% java RelaxDemo matrix-size nthreads niters");
	    System.err.println("Hints: matrix-size: 40..60, nthreads: 1..nCPU, niters: 1..100");
	    System.exit( 1 );
	}
	int matrix_size = Integer.parseInt( argv[0] );
	int nthreads    = Integer.parseInt( argv[1] );
	int niters      = Integer.parseInt( argv[2] );
	run_relax( matrix_size, nthreads, niters );
	System.exit( 0 );
    }
    static void run_relax( int matrix_size, int nthreads, 
			   int niters )
	throws java.lang.InterruptedException
    {
	// pthread_setconcurrency( nthreads );
	double t[][]        = Matrix.alloc( matrix_size, matrix_size );
	double boundary[][] = Matrix.alloc( matrix_size, matrix_size );
	matrix_t_fill( t );
	matrix_boundary_fill( boundary );
	long start = System.currentTimeMillis();
	    for( int i=0; i<niters; i++ )
	    {
		RelaxMaster m = new RelaxMaster( boundary, t, nthreads );
		m.relax();
	    }
	long end = System.currentTimeMillis();
	double diff = (double)(end - start)/1000.0 ;
	int concurrency = 0; // pthread_getconcurrency();
	System.out.printf("matrix_size==%d, nthreads==%d, niters==%d, concurrency==%d\n",
	       matrix_size, nthreads, niters, concurrency );
	System.out.printf("%6.3f [sec] / %d [iteration] == %6.3f [sec/iteration]\n",
	       diff, niters, diff/niters );
	System.out.printf("%d [iteration] / %6.3f [sec] == %6.3f [iteration/sec]\n",
	       	niters, diff, niters/diff );
    }
    static void matrix_t_fill( double t[][] )
    {
	int n_rows = Matrix.nrows( t );
	int n_cols = Matrix.nrows( t );
	int i,j;
	for( i=0 ; i<n_rows; i++ )
	{
	    for( j=0; j<n_cols; j++ )
	    {
		t[i][j] = 0.0 ;
	    }
	}
	j = n_cols/2 ;
	for( i=0 ; i<n_rows; i++ )
	{
	    t[i][j] = 1.0 ;
	}
	i = n_cols/2 ;
	for( j=0; j<n_cols; j++ )
	{
	    t[i][j] = 1.0 ;
	}
    }
    static void matrix_boundary_fill( double m[][] )
    {
	int n_rows = Matrix.nrows( m );
	int n_cols = Matrix.nrows( m );
	int i,j ;
	for( i=0 ; i<n_rows; i++ )
	{
	    for( j=0; j<n_cols; j++ )
	    {
		m[i][j] = 0 ;
	    }
	}
	for( i=0 ; i<n_rows; i++ )
	{
	    m[i][0] = 1 ;
	    m[i][n_cols-1] = 1 ;
	}
	for( j=0; j<n_cols; j++ )
	{
	    m[0][j] = 1 ;
	    m[n_rows-1][j] = 1 ;
	}
    }
}
