Package cl.niclabs.skandium.gcm.examples.mergesort

Source Code of cl.niclabs.skandium.gcm.examples.mergesort.MergeSort

package cl.niclabs.skandium.gcm.examples.mergesort;

import java.util.ArrayList;
import java.util.Random;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;

import cl.niclabs.skandium.gcm.examples.mergesort.MSCondition;
import cl.niclabs.skandium.gcm.examples.mergesort.MSExecute;
import cl.niclabs.skandium.gcm.examples.mergesort.MSMerge;
import cl.niclabs.skandium.gcm.examples.mergesort.MSSplit;
import cl.niclabs.skandium.gcm.SCResultReceiver;
import cl.niclabs.skandium.gcm.SkandiumComponent;
import cl.niclabs.skandium.skeletons.DaC;
import cl.niclabs.skandium.skeletons.Skeleton;

public class MergeSort implements MSRunnable, SCResultReceiver,
    BindingController {

  SkandiumComponent sc;
  long time;
 
  static Random random = new Random();
 
  @Override
  public void run(int exp, int base) {
   
    ArrayList<Integer> in = generate((int) Math.pow(2, exp));

    Skeleton<ArrayList<Integer>, ArrayList<Integer>> msort =
        new DaC<ArrayList<Integer>, ArrayList<Integer>>(
            new MSCondition(base), new MSSplit(),
            new MSExecute(), new MSMerge());
   
    System.out.println("Execute Mergesort with 2^" + exp + "elements");
    time = System.currentTimeMillis();
    sc.execute(msort, in);
  }
 
  public static ArrayList<Integer> generate(int size){
                     
    ArrayList<Integer> array = new ArrayList<Integer>(size);
   
    for(int i=0;i<size;i++)
      array.add(i, new Integer(random.nextInt()));

    return array;
  }

  @Override
  @SuppressWarnings("unchecked")
  public void receive(Object result) {
   
    ArrayList<Integer> out = (ArrayList<Integer>) result;
    time = System.currentTimeMillis() - time;
    System.out.println("TOTAL TIME = " + time + " [ms]");

   
    System.out.println("Checking result...");
    System.out.flush();
   
    int aux = Integer.MIN_VALUE, errors = 0;
    for(Integer value : out){
      if(aux > value.intValue()){
        errors++;
        System.out.println("Error: " + aux + " antes que " + value.intValue());
      }
      aux = value.intValue();
    }
 
    System.out.println("Finish with " + errors + "error(s)");
  }

  @Override
  public void bindFc(String itfName, Object itf)
      throws NoSuchInterfaceException {
    if(itfName.compareTo("sc") == 0)
      sc = (SkandiumComponent) itf;
    else throw new NoSuchInterfaceException(itfName);
  }

  @Override
  public String[] listFc() {
    return new String[] {"sc"};
  }

  @Override
  public Object lookupFc(String itfName) throws NoSuchInterfaceException {
    if(itfName.compareTo("sc") == 0)
      return sc;
    else throw new NoSuchInterfaceException(itfName);
  }

  @Override
  public void unbindFc(String itfName) throws NoSuchInterfaceException {
    if(itfName.compareTo("sc") == 0)
      sc = null;
    else throw new NoSuchInterfaceException(itfName);
  }

}
TOP

Related Classes of cl.niclabs.skandium.gcm.examples.mergesort.MergeSort

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.