00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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) {
00069 left=null;
00070 up=null;
00071 right=workers[1];
00072 down=workers[workerGridSize];
00073 } else if ( i==(workerGridSize-1) ){
00074 up=null;
00075 right=null;
00076 left=workers[i-1];
00077 down=workers[i+workerGridSize];
00078 } else if (i==(nbWorker-workerGridSize)){
00079 left=null;
00080 down=null;
00081 up=workers[i-workerGridSize];
00082 right=workers[i+1];
00083 } else if (i==nbWorker-1) {
00084 down=null;
00085 right=null;
00086 up=workers[i-workerGridSize];
00087 left=workers[i-1];
00088 } else if ( (i>0) && (i<(workerGridSize-1)) ) {
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)) ) {
00094 up=workers[i-workerGridSize];
00095 right=workers[i+1];
00096 left=workers[i-1];
00097 down=null;
00098 } else if ( i%workerGridSize == 0 ){
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)) {
00104 right=null;
00105 left=workers[i-1];
00106 if (i+workerGridSize < nbWorker) down=workers[i+workerGridSize];
00107 up=workers[i-workerGridSize];
00108 } else {
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
00125 double[][] currentSubMatrix = new double[subMatrixSize][subMatrixSize];
00126 for (int i=0;i<subMatrixSize;i++){
00127 for(int j=0;j<subMatrixSize;j++){
00128
00129
00130 currentSubMatrix[i][j] = JacobiDispatcher.initialValue;
00131 }
00132 }
00133
00134 int upperLeftX = (currentW % workerGridSize)*subMatrixSize;
00135 int upperLeftY = (currentW / workerGridSize)*subMatrixSize;
00136
00137
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
00150
00151
00152
00153
00154
00155
00156
00157
00158
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
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 }