
import net.jini.core.lease.Lease;
import net.jini.space.JavaSpace;
import net.jini.core.entry.Entry;
import java.io.*;

/**
 * JavaSpaces array i/o benchmark.
 *
 * @author Michael S. Noble (mnoble@cfa.harvard.edu) Copyright (c) 2000
 * This code may only be used under the terms of GNU General Public License.
 */

public class ArrayIO extends Master {

    private TypedArr typedArr = new TypedArr();

    public ArrayIO(String[] args) {
       try {

	    if (args.length < 3) {
	    	System.out.print("Usage: java ArrayIO numIters numObjects"+
								" objSize\n");
		System.exit(-1);
	    }

	    short i,iter;
            numIters = Integer.parseInt(args[0]);
	    numObjs = Integer.parseInt(args[1]);
	    short ObjSize = Short.parseShort(args[2]);
	    TypedArr tuple = new DoubleArr(ObjSize);
	    TypedArr stuple = new StridedArr((double[])tuple.arr,2);
	    boolean doStrided=false;
	    if (args.length == 4 && args[3].equals("-s"))
		doStrided=true;

	    init(typedArr);
	    System.out.println("\n# Array Elements: "+ObjSize+"  Type: double"+
						"  NumBytes: "+ObjSize*8);

	    // I/O Test 1: write/take the full arrays to/from the space
	    reporter = new Reporter("Full array write/take test, "+
				"using snapshot template",numObjs,tuple);


            for (iter=0; iter < numIters; iter++)
            {
	      timer.checkp();
	      for (i=0; i < numObjs; i++)
		  space.write(tuple, null, Lease.FOREVER);
	      reporter.writeReport(timer.checkp());

	      i = 0;
	      timer.checkp();
	      while (i++ < numObjs)
		typedArr = (TypedArr)space.take(snapshot,null,Long.MAX_VALUE);
	      reporter.takeReport(timer.checkp());
	    }
	    reporter.summarize();

	    // I/O Test 2: same test, this time using a null template
	    reporter = new Reporter("Same test, using null template "+
						"for retrieval",numObjs,null);
            for (iter=0; iter < numIters; iter++)
            {
	      timer.checkp();
	      for (i=0; i < numObjs; i++)
		  space.write(tuple, null, Lease.FOREVER);
	      reporter.writeReport(timer.checkp());

	      i = 0;
	      timer.checkp();
	      while (i++ < numObjs)
		typedArr = (TypedArr)space.take(null,null,Long.MAX_VALUE);
	      reporter.takeReport(timer.checkp());
	    }
	    reporter.summarize();

	    if (doStrided)
	    {
	       // I/O Test 3: write/take strided arrays to/from the space, to
	       // see if (creating strided array + writing it) costs less
	       // than writing the entire (unstrided) array.  If so, for
	       // example, then this would be the preferred approach to 
	       // communicating boundaries between neighbor processes during
	       // iterative mesh/grid computations.
	       reporter = new Reporter("Strided array write/take test",numObjs,
								stuple);
               for (iter=0; iter < numIters; iter++)
               {
	         timer.checkp();
	         for (i=0; i < numObjs; i++) {
		   stuple  = new StridedArr((double[])tuple.arr,2);
		   space.write(stuple, null, Lease.FOREVER);
	         }
	         reporter.writeReport(timer.checkp());

	         i = 0;
	         timer.checkp();
	         while (i++ < numObjs)
		  typedArr = (TypedArr)space.take(snapshot,null,Long.MAX_VALUE);

	         reporter.takeReport(timer.checkp());
	         reporter.summarize();
	       }
	    }

	 System.out.println("");
	 System.exit(0);

       } catch (Exception e) { e.printStackTrace(); }
    }

    public static void main(String[] args) { ArrayIO aio = new ArrayIO(args); }

}
