org/objectweb/proactive/calcium/skeletons/DaC.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.calcium.skeletons;
00032 
00033 import java.util.Vector;
00034 
00035 import org.objectweb.proactive.calcium.Task;
00036 import org.objectweb.proactive.calcium.exceptions.MuscleException;
00037 import org.objectweb.proactive.calcium.exceptions.EnvironmentException;
00038 import org.objectweb.proactive.calcium.interfaces.Condition;
00039 import org.objectweb.proactive.calcium.interfaces.Conquer;
00040 import org.objectweb.proactive.calcium.interfaces.Divide;
00041 import org.objectweb.proactive.calcium.interfaces.Instruction;
00042 import org.objectweb.proactive.calcium.interfaces.Skeleton;
00043 import org.objectweb.proactive.calcium.statistics.Timer;
00044 
00059 public class DaC<T> implements Skeleton<T>, Instruction<T> {
00060 
00061         Divide<T> div;
00062         Conquer<T> conq;
00063         Condition<T> cond;
00064         Skeleton<T> child;
00065         
00073         public DaC(Divide<T> div, Condition<T> cond, Skeleton<T> child, Conquer<T> conq){
00074         
00075                 this.div=div;
00076                 this.cond=cond;
00077                 this.child=child;
00078                 this.conq=conq;
00079         }
00080         
00081         public Vector<Instruction<T>> getInstructionStack() {
00082 
00083                 Vector<Instruction<T>> v= new Vector<Instruction<T>>();
00084                 v.add(this);
00085                 
00086                 return v;
00087         }
00088 
00089         public Task<T> compute(Task<T> t) throws EnvironmentException{
00090                 
00091                 //Conquer if children are present
00092                 if(t.hasFinishedChild()){
00093                         return conquer(t);
00094                 }
00095                 
00096                 //Else no children are present
00097 
00098                 //Split the task if required
00099                 Timer timer = new Timer();
00100                 boolean evalCondition=cond.evalCondition(t.getObject());
00101                 timer.stop();
00102                 t.getStats().getWorkout().track(cond, timer);
00103                 
00104                 if(evalCondition){
00105                         return divide(t);
00106                 }
00107                 //else solve the tasks
00108                 else{
00109                         return execute(t);
00110                 }
00111         }
00112         
00113         protected Task<T> execute(Task<T> t){
00114                 //Append the child skeleton code to the stack
00115                 Vector<Instruction<T>> currentStack = t.getStack();
00116                 currentStack.addAll(child.getInstructionStack());
00117                 t.setStack(currentStack);
00118                 return t;
00119         }
00120         
00121         protected Task<T> divide(Task<T> parent) throws EnvironmentException{
00122                 
00123                 /*
00124                  * We pass the t.object to the div. Each result
00125                  * of divide will be then encapsulated by a Task, and 
00126                  * the Tasks will be held in an array  
00127                  */     
00128                 Timer timer = new Timer();
00129                 Vector<T> childObjects=div.divide(parent.getObject());
00130                 timer.stop();
00131                 
00132                 if(childObjects.size()<=0){
00133                         String msg="Parameter was divided into less than 1 part.";
00134                         logger.debug(msg);
00135                         throw new MuscleException(msg);
00136                 }
00137                 
00138                 for(T o:childObjects){
00139                         Task<T> child = new Task<T>(o);
00140                         child.pushInstruction(this); //To do divide or execute in the child
00141                         parent.addReadyChild(child); //parent holds it's children
00142                 }
00143 
00144                 //Now we put a DaC (conquer) on the instruction stack of the parent 
00145                 parent.pushInstruction(this);
00146                 parent.getStats().getWorkout().track(div, timer);
00147                 return parent;
00148         }
00149         
00150         protected Task<T> conquer(Task<T> parent) throws EnvironmentException {
00151                 
00158                 Vector<T> childObjects = new Vector<T>();
00159                 
00160                 while(parent.hasFinishedChild()){
00161                         
00162                         Task<T> child = parent.getFinishedChild();
00163                         childObjects.add(child.getObject());
00164                 }
00165 
00166                 if(childObjects.size() <=0 ){
00167                         String msg="Can't conquer less than one child parameter!";
00168                         logger.error(msg);
00169                         throw new MuscleException(msg);
00170                 }
00171                 
00172                 Timer timer = new Timer();
00173                 T resultObject=conq.conquer(parent.getObject(), childObjects);
00174                 timer.stop();
00175                 Task<T> resultTask=parent.reBirth(resultObject);
00176                 
00177                 resultTask.getStats().getWorkout().track(conq, timer);
00178                 return resultTask;
00179         }
00180 
00181         public String toString(){
00182                 return "D&C";
00183         }
00184 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1