org/objectweb/proactive/loadbalancing/util/JacobiDispatcher.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.loadbalancing.util;
00032 
00033 
00034 import java.util.Vector;
00035 
00036 import org.objectweb.proactive.ActiveObjectCreationException;
00037 import org.objectweb.proactive.ProActive;
00038 import org.objectweb.proactive.core.ProActiveException;
00039 import org.objectweb.proactive.core.config.ProActiveConfiguration;
00040 import org.objectweb.proactive.core.node.Node;
00041 import org.objectweb.proactive.core.node.NodeException;
00042 import org.objectweb.proactive.p2p.service.P2PService;
00043 import org.objectweb.proactive.p2p.service.node.P2PNodeLookup;
00044 
00045 
00046 
00051 public class JacobiDispatcher {
00052 
00053         static final public double boudaryValue = 0;
00054         static final public double initialValue = 1000000000;
00055         protected Vector nodes;
00056         protected int nodesBooked;
00057         protected P2PNodeLookup p2pNodeLookup;
00058         
00059         static public void createVirtualGrid(JacobiWorker[] workers, int workerGridSize){
00060                         System.out.println("[JACOBI] Creating workers virtual grid...");
00061                         int nbWorker = workers.length;
00062                         for (int i=0;i<nbWorker;i++){
00063                                         System.out.println("[JACOBI] Initializing worker " + i + "..."); 
00064                                         JacobiWorker currentWorker = workers[i];
00065                                         JacobiWorker up=null, down=null, left=null, right=null;
00066                                         if (nbWorker == 1) continue;
00067                                         else 
00068                                         if (i==0) { // upper left
00069                                                 left=null;
00070                                                 up=null;
00071                                                 right=workers[1];
00072                                                 down=workers[workerGridSize];
00073                                         } else if ( i==(workerGridSize-1) ){ // upper right
00074                                                 up=null;
00075                                                 right=null;
00076                                                 left=workers[i-1];
00077                                                 down=workers[i+workerGridSize]; 
00078                                         } else if (i==(nbWorker-workerGridSize)){ //lower left
00079                                                 left=null;
00080                                                 down=null;
00081                                                 up=workers[i-workerGridSize];
00082                                                 right=workers[i+1];
00083                                         } else if (i==nbWorker-1) { //lower right
00084                                                 down=null;
00085                                                 right=null;
00086                                                 up=workers[i-workerGridSize];
00087                                                 left=workers[i-1];
00088                                         } else if ( (i>0) && (i<(workerGridSize-1)) ) { // up
00089                                                 up=null;
00090                                                 right=workers[i+1];
00091                                                 left=workers[i-1];
00092                                                 if (i+workerGridSize < nbWorker) down=workers[i+workerGridSize];
00093                                         } else if ( (i>(workerGridSize*(workerGridSize-1))) && (i<(workerGridSize*workerGridSize-1)) ) { // down
00094                                                 up=workers[i-workerGridSize];
00095                                                 right=workers[i+1];
00096                                                 left=workers[i-1];
00097                                                 down=null;
00098                                         } else if ( i%workerGridSize == 0 ){ // left
00099                                                 left=null;
00100                                                 right=workers[i+1];
00101                                                 up=workers[i-workerGridSize];
00102                                                 if (i+workerGridSize < nbWorker) down=workers[i+workerGridSize];
00103                                         } else if ( i%workerGridSize == (workerGridSize-1)) {//right
00104                                                 right=null;
00105                                                 left=workers[i-1];
00106                                                 if (i+workerGridSize < nbWorker) down=workers[i+workerGridSize];
00107                                                 up=workers[i-workerGridSize];
00108                                         } else { // inside
00109                                                 up=workers[i-workerGridSize];
00110                                                 if (i+workerGridSize < nbWorker)        down=workers[i+workerGridSize];
00111                                                 else down=null;
00112                                                 left=workers[i-1];
00113                                                 right=workers[i+1];
00114                                         }
00115                                         currentWorker.setNeighbours(up, down, left, right);
00116                                 }
00117                         System.out.println("[JACOBI] Virtual grid is created.");
00118                 }
00119 
00120 
00121         static public void createAndSplitMatrix(JacobiWorker[] workers, int workerGridSize, int subMatrixSize, int globalMatrixSize){
00122                 System.out.println("[JACOBI] Creating and spliting global matrix");
00123                 for (int currentW=0 ; currentW<workers.length ; currentW++){
00124                         //create submatrix
00125                         double[][] currentSubMatrix = new double[subMatrixSize][subMatrixSize];
00126                         for (int i=0;i<subMatrixSize;i++){
00127                                 for(int j=0;j<subMatrixSize;j++){
00128                                         // **** HERE MATRIX VALUES ****
00129                                         //currentSubMatrix[i][j] = (double)( (i*j*(currentW+1)) );
00130                                         currentSubMatrix[i][j] = JacobiDispatcher.initialValue;
00131                                 }
00132                         }
00133                         // compute upperLeft values
00134                         int upperLeftX = (currentW % workerGridSize)*subMatrixSize;
00135                         int upperLeftY = (currentW / workerGridSize)*subMatrixSize;
00136                         
00137                         //send submatrix to worker
00138                         workers[currentW].setSubMatrix(globalMatrixSize,subMatrixSize,upperLeftX,upperLeftY,
00139                                                                                         currentSubMatrix);                      
00140                 }
00141                 System.out.println("[JACOBI]Global Matrix created and splitted");
00142         
00143         }
00144 
00145 
00146 
00147         
00148 
00149 /*      public static void main(String[] args) throws IOException, ProActiveException, AlreadyBoundException {
00150                 
00151                 // cmd : java Start globalSize nbWorker maxIter
00152                 
00153                 if (args.length != 3){
00154                         System.err.println("java Start globalSize nbWorker maxIter");
00155                         System.exit(1);
00156                 }
00157                 Node n = NodeFactory.getNode("rmi://anaconda:2805/StartTest");
00158                 P2PTest myTest= (P2PTest) ProActive.newActive(P2PTest.class.getName(),args,n);;
00159         }
00160 */
00161         public JacobiDispatcher() {}
00162         
00163 public JacobiDispatcher(String s1, String s2, String s3, P2PService serviceP2P) throws ProActiveException {
00164                 int globalSize = Integer.parseInt((String) s1);
00165                 int nbWorker =  Integer.parseInt((String) s2);
00166                 int maxIter =  Integer.parseInt((String) s3);
00167         
00168                 
00169                 int workerGridSize = (int) Math.sqrt((double)(nbWorker));
00170                 int submatrixSize = globalSize/workerGridSize;
00171                 JacobiWorker[] workers = new JacobiWorker[nbWorker];
00172                 nodesBooked = 0;
00173                 
00174                 System.out.println("[JACOBI] Initialization with :");
00175                 System.out.println("         * global matrix size = " + globalSize);
00176                 System.out.println("         * sub matrix size    = " + submatrixSize);
00177                 System.out.println("         * # of workers       = " + nbWorker);
00178                 System.out.println("         * worker grid size   = " + workerGridSize);
00179                 System.out.println("         * # of iterations    = " + maxIter);
00180                 System.out.println("         * boundary value     = " + JacobiDispatcher.boudaryValue);
00181                 
00182                 try {
00183                         
00184                         ProActiveConfiguration.load();
00185                                                 
00186                         int n = 8;
00187                         P2PNodeLookup p2pNodeLookup = serviceP2P.getNodes(n, "JacobiNode", "Jacobi");
00188                  
00189                         Vector nodes = p2pNodeLookup.getNodes();
00190                         
00191                         System.out.println("[JACOBI] Deploying Workers in "+nodes.size()+" nodes");
00192                         for (int i=0; i<nbWorker ;i++){
00193                                 int fix = (int) (Math.random()*n);
00194                                 workers[i]=(JacobiWorker)(ProActive.newActive(JacobiWorker.class.getName(), 
00195                                                                                 new Object[]{new Integer(i), new Double(JacobiDispatcher.boudaryValue), new Integer(maxIter)}, 
00196                                                                                 (Node) nodes.get(fix)));
00197                         }
00198                         System.out.println("[JACOBI] Workers are deployed");
00199 
00200                         //initializing workers
00201                         JacobiDispatcher.createVirtualGrid(workers, workerGridSize);
00202                         JacobiDispatcher.createAndSplitMatrix(workers, workerGridSize, submatrixSize, globalSize);
00203                         
00204                         for (int i=0;i<nbWorker;i++){
00205                                 workers[i].computeNewSubMatrix();
00206                         }
00207                         
00208                         
00209                 } catch (NodeException e) {
00210                         e.printStackTrace();
00211                 } catch (ActiveObjectCreationException e) {
00212                         e.printStackTrace();
00213                 } catch (Exception e) {
00214                         e.printStackTrace();
00215                 } 
00216                 
00217         } 
00218 }

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