1 package org.objectweb.proactive.examples.matrix; 2 3 4 import org.apache.log4j.Logger; 5 import org.objectweb.proactive.ActiveObjectCreationException; 6 import org.objectweb.proactive.ProActive; 7 import org.objectweb.proactive.core.body.future.FutureProxy; 8 import org.objectweb.proactive.core.group.ProActiveGroup; 9 import org.objectweb.proactive.core.node.Node; 10 import org.objectweb.proactive.core.node.NodeException; 11 12 13 public class Matrix implements java.io.Serializable { 14 15 static Logger logger = Logger.getLogger(Matrix.class.getName()); 16 private int width; 17 private int height; 18 private double[][] tab; 19 boolean migration = true; 20 21 22 26 public Matrix () {} 27 28 public Matrix (int w, int h) { 29 width = w; 30 height = h; 31 tab = new double[w][h]; 32 } 33 34 35 public Matrix (double[][] table) { 36 width = table.length; 37 height = table[0].length; 38 tab = new double[width][]; 39 for (int i=0; i < width ; i++) 40 tab[i] = table[i]; 41 } 42 43 public Matrix(Matrix mr, int w) { 47 48 int index = 0; 49 int width = 0; 50 Matrix result = null; 51 52 int size = ProActiveGroup.size(mr) ; 53 59 60 61 this.width = w; 62 this.height = w; 64 80 81 tab = new double[this.width][]; 82 83 for (int i=0 ; i < size ; i++) { 84 result = ((Matrix)ProActiveGroup.get(mr,i)); 85 86 90 int widthTmp = result.getWidth(); 91 for (int j=0 ; j < widthTmp ; j++) { 92 tab[index] = result.getColumn(j); 94 index++; 95 } 96 } 97 } 98 99 public Matrix (Matrix mr) { 100 int index = 0; 101 int width = 0; 102 Matrix result = null; 103 104 int size = ProActiveGroup.size(mr) ; 105 106 for (int i=0 ; i < size ; i++) { 107 width += ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()).getWidth(); 108 } 109 110 height = ((Matrix)((FutureProxy)ProActiveGroup.get(mr,0)).getResult()).getHeight(); 111 tab = new double[width][]; 112 for (int i=0 ; i < size ; i++) { 113 result = ((Matrix)((FutureProxy)ProActiveGroup.get(mr,i)).getResult()); 114 width = result.getWidth(); 115 for (int j=0 ; j < width ; j++) { 116 tab[index] = result.getColumn(j); 117 index++; 118 } 119 } 120 } 121 122 123 125 127 128 129 130 131 138 139 140 141 142 143 144 150 153 155 157 168 169 public Matrix (Matrix[] mr) { 170 int w = 0; 171 for (int i=0 ; i < mr.length ; i++) 172 w += mr[i].getWidth(); 173 174 width = w; 175 height = mr[0].getHeight(); 176 177 tab = new double[width][]; 178 179 int index = 0; 180 for (int i=0 ; i < mr.length ; i++) { 181 for (int j=0 ; j < mr[i].getWidth() ; j++) { 182 tab[index] = mr[i].getColumn(j); 183 index++; 184 } 185 } 186 } 187 188 189 190 191 192 196 public int getWidth() { 197 return width; 198 } 199 200 public void setWidth(int w) { 201 width = w; 202 } 203 204 public int getHeight() { 205 return height; 206 } 207 208 public void setHeight(int h) { 209 height = h; 210 } 211 212 public double getWH(int w, int h) { 213 return tab[w][h]; 214 } 215 216 public void setWH(int w, int h, double val) { 217 tab[w][h] = val; 218 } 219 220 public double[][] getTab() { 221 return tab; 222 } 223 224 public double[] getColumn(int w) { 225 return tab[w]; 226 } 227 228 229 233 static int k=0; 234 235 public void initializeWithRandomValues() { 236 237 for (int i = 0 ; i < tab.length ; i++) 239 for (int j = 0 ; j < tab[i].length ; j++) 240 setWH(i,j,k++); 241 242 } 246 247 public String toString() { 249 String s = new String (""); 251 int height = this.getHeight(); 252 int width = this.getWidth(); 253 for (int i = 0 ; i < height ; i++) { 254 for (int j = 0 ; j < width ; j++) { 255 s += Double.toString(getWH(j,i)); 257 s += " "; 258 } 259 s += "\n"; 260 } 261 return s; 262 } 263 264 265 266 public Matrix getVerticalSubMatrix (int widthStart, int widthStop) { 267 double[][] d = new double[widthStop-widthStart][]; 268 for (int i=0 ; i < widthStop-widthStart ; i++) 269 d[i] = tab[widthStart+i]; 270 return new Matrix(d); 271 } 272 273 274 public Matrix getActiveVerticalSubMatrix (int widthStart, int widthStop, Node node) { 275 Matrix vsm = null; 276 277 double[][] d = new double[widthStop-widthStart][]; 278 for (int i=0 ; i < widthStop-widthStart ; i++) 279 d[i] = tab[widthStart+i]; 280 281 Object [] params = new Object [1]; 282 params[0] = d; 283 284 try { 285 vsm = (Matrix) ProActive.newActive("org.objectweb.proactive.examples.matrix.Matrix", params, node); } 286 catch (ActiveObjectCreationException e) { 287 logger.error("Error create Active Vertical Sub Matrix : ActiveObjectCreationException\n"); } 288 catch (NodeException e) { 289 logger.error("Error create Active Vertical Sub Matrix : NodeException\n"); } 290 return vsm; 291 } 292 293 294 public Matrix transformIntoActiveVerticalSubMatrixGroup(Node[] nodeList) { 295 Matrix result = null; 297 int widthSubMatrix; 298 int more; 299 boolean pile; 300 if ((getWidth() % nodeList.length) == 0) { 301 widthSubMatrix = getWidth() / nodeList.length; 302 more = 0; 303 pile = true; 304 } 305 else { 306 widthSubMatrix = (getWidth() / nodeList.length)+1; 307 more = getWidth() % widthSubMatrix; 308 pile = false; 309 } 310 311 Object [][] params = new Object [nodeList.length][]; 312 313 for (int i=0 ; i < nodeList.length ; i++) { 314 Object [] po = new Object [1]; 315 double[][] d; 316 if ((!pile) && (i == nodeList.length-1)) 317 d = new double[more][]; 318 else 319 d = new double[widthSubMatrix][]; 320 321 for (int j=0 ; j < d.length ; j++) 322 d[j] = tab[(i*widthSubMatrix)+j]; 323 324 po[0] = d; 325 326 params[i] = po; 327 328 329 330 331 332 346 347 348 349 350 351 352 353 } 354 355 try { 356 result = (Matrix) ProActiveGroup.newGroup("org.objectweb.proactive.examples.matrix.Matrix",params,nodeList); 357 } 358 catch (Exception e) { e.printStackTrace();} 359 360 return result; 361 } 362 363 364 public Matrix localMultiplyForGroup (Matrix m) { 365 369 370 Matrix res = new Matrix(getWidth(),m.getHeight()); 371 int height=res.getHeight() ; 372 int width = res.getWidth(); 373 for (int line=0 ; line < height ; line++) { 374 for (int column=0 ; column < width ; column++) { 375 double val = 0; 376 for (int index=0 ; index < height ; index++) 377 { 378 379 380 382 383 val += m.getWH(index,line) * getWH(column,index); 384 } 385 res.setWH(column,line,val); 387 } 388 } 389 390 391 394 395 return res; 396 } 397 398 399 400 public Matrix distributedMultiply (Matrix m, Node[] nodeList) { 401 if (getWidth() != m.getHeight()) { 402 logger.error("Error : no compatible Matrix"); 403 return null; 404 } 405 else { 406 Matrix verticalSubMatrixGroup = null; 407 408 verticalSubMatrixGroup = m.transformIntoActiveVerticalSubMatrixGroup(nodeList); 409 410 Matrix mr = verticalSubMatrixGroup.localMultiplyForGroup(this); 411 return new Matrix(mr); 412 } 413 } 414 415 416 public Matrix distributedMultiply (Matrix m) { 417 Node[] nodeList = new Node[1]; 418 nodeList[0] = null; 419 return distributedMultiply(m,nodeList); 420 } 421 422 423 424 public Matrix localMultiply (Matrix m) { 425 return m.localMultiplyForGroup(this); 426 } 427 428 429 430 431 432 public Matrix[] transformIntoActiveMatrixTable(Node[] nodeList) { 433 Matrix[] result = new Matrix[nodeList.length]; 435 int widthSubMatrix; 436 int more; 437 boolean pile; 438 if ((getWidth() % nodeList.length) == 0) { 439 widthSubMatrix = getWidth() / nodeList.length; 440 more = 0; 441 pile = true; 442 } 443 else { 444 widthSubMatrix = (getWidth() / nodeList.length)+1; 445 more = getWidth() % widthSubMatrix; 446 pile = false; 447 } 448 449 for (int i=0 ; i < nodeList.length ; i++) { 450 Object [] po = new Object [1]; 451 double[][] d; 452 if ((!pile) && (i == nodeList.length-1)) 453 d = new double[more][]; 454 else 455 d = new double[widthSubMatrix][]; 456 457 for (int j=0 ; j < d.length ; j++) 458 d[j] = tab[(i*widthSubMatrix)+j]; 459 460 po[0] = d; 461 462 try { 463 result[i] = (Matrix) ProActive.newActive("org.objectweb.proactive.examples.matrix.Matrix",po,nodeList[i]); 464 } 465 catch (Exception e) { e.printStackTrace();} 466 } 467 468 return result; 469 } 470 471 472 public Matrix distributedMultiplyWithOutGroup (Matrix m, Node[] nodeList) { 473 if (getWidth() != m.getHeight()) { 474 logger.error("Error : no compatible Matrix"); 475 return null; 476 } 477 else { 478 Matrix[] verticalSubMatrixTable; 479 480 verticalSubMatrixTable = m.transformIntoActiveMatrixTable(nodeList); 481 482 Matrix[] mr = new Matrix[verticalSubMatrixTable.length]; 483 484 for (int i=0 ; i < verticalSubMatrixTable.length ; i++) 485 mr[i] = verticalSubMatrixTable[i].localMultiplyForGroup(this); 486 487 return new Matrix(mr); 488 } 489 } 490 491 492 493 495 private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException { 496 499 501 out.defaultWriteObject(); 502 503 } 506 507 508 512 513 514 515 516 517 } 518 | Popular Tags |