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
00032
00033
00034
00035
00036
00037
00038
00039 #include <RCM.h>
00040 #include <Graph.h>
00041 #include <Vertex.h>
00042 #include <VertexIter.h>
00043 #include <ID.h>
00044 #include <Channel.h>
00045 #include <FEM_ObjectBroker.h>
00046
00047
00048 RCM::RCM(bool gps)
00049 :GraphNumberer(GraphNUMBERER_TAG_RCM),
00050 numVertex(-1), theRefResult(0), GPS(gps)
00051 {
00052
00053 }
00054
00055
00056 RCM::~RCM()
00057 {
00058 if (theRefResult != 0)
00059 delete theRefResult;
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 const ID &
00076 RCM::number(Graph &theGraph, int startVertex)
00077 {
00078
00079 if (numVertex != theGraph.getNumVertex()) {
00080
00081
00082 if (theRefResult != 0)
00083 delete theRefResult;
00084
00085 numVertex = theGraph.getNumVertex();
00086 theRefResult = new ID(numVertex);
00087
00088 if (theRefResult == 0) {
00089 cerr << "ERROR: RCM::number - Out of Memory\n";
00090 theRefResult = new ID(0);
00091 numVertex = 0;
00092 return *theRefResult;
00093 }
00094 }
00095
00096
00097
00098 if (numVertex == 0)
00099 return *theRefResult;
00100
00101
00102
00103
00104
00105 Vertex *vertexPtr;
00106 VertexIter &vertexIter = theGraph.getVertices();
00107
00108 while ((vertexPtr = vertexIter()) != 0)
00109 vertexPtr->setTmp(-1);
00110
00111
00112 int startVertexTag;
00113 startVertexTag = startVertex;
00114
00115 if (startVertexTag != -1) {
00116 vertexPtr = theGraph.getVertexPtr(startVertexTag);
00117 if (vertexPtr == 0) {
00118 cerr << "WARNING: RCM::number - No vertex with tag ";
00119 cerr << startVertexTag << "Exists - using first come from iter\n";
00120 startVertexTag = -1;
00121 }
00122 }
00123
00124
00125 VertexIter &vertexIter2 = theGraph.getVertices();
00126 Vertex *start;
00127
00128 if (startVertexTag == -1) {
00129 vertexPtr = vertexIter2();
00130 start = vertexPtr;
00131
00132
00133
00134
00135 if (GPS == true) {
00136
00137 int currentMark = numVertex-1;
00138 int nextMark = currentMark -1;
00139 int startLastLevelSet = nextMark;
00140 (*theRefResult)(currentMark) = vertexPtr->getTag();
00141 vertexPtr->setTmp(currentMark);
00142
00143
00144
00145 while (nextMark >= 0) {
00146
00147
00148 vertexPtr = theGraph.getVertexPtr((*theRefResult)(currentMark));
00149 const ID &adjacency = vertexPtr->getAdjacency();
00150
00151
00152
00153
00154 int size = adjacency.Size();
00155 for (int i=0; i<size; i++) {
00156
00157 int vertexTag = adjacency(i);
00158 vertexPtr = theGraph.getVertexPtr(vertexTag);
00159 if ((vertexPtr->getTmp()) == -1) {
00160 vertexPtr->setTmp(nextMark);
00161 (*theRefResult)(nextMark--) = vertexTag;
00162 }
00163 }
00164
00165
00166
00167
00168 currentMark--;
00169
00170 if (startLastLevelSet == currentMark)
00171 startLastLevelSet = nextMark;
00172
00173
00174
00175 if ((currentMark == nextMark) && (currentMark >= 0)) {
00176
00177
00178 while (((vertexPtr = vertexIter2()) != 0) &&
00179 (vertexPtr->getTmp() != -1))
00180 ;
00181
00182 nextMark--;
00183 startLastLevelSet = nextMark;
00184 vertexPtr->setTmp(currentMark);
00185 (*theRefResult)(currentMark) = vertexPtr->getTag();
00186
00187 }
00188
00189 }
00190
00191
00192 if (startLastLevelSet > 0) {
00193 ID lastLevelSet(startLastLevelSet);
00194 for (int i=0; i<startLastLevelSet; i++)
00195 lastLevelSet(i) = (*theRefResult)(i);
00196
00197 return this->number(theGraph,lastLevelSet);
00198 }
00199
00200 }
00201 else
00202 vertexPtr = start;
00203 }
00204
00205
00206 VertexIter &vertexIter3 = theGraph.getVertices();
00207 Vertex *otherPtr;
00208
00209
00210 while ((otherPtr = vertexIter3()) != 0)
00211 otherPtr->setTmp(-1);
00212
00213
00214 VertexIter &vertexIter4 = theGraph.getVertices();
00215
00216 int currentMark = numVertex-1;
00217 int nextMark = currentMark -1;
00218 (*theRefResult)(currentMark) = vertexPtr->getTag();
00219 vertexPtr->setTmp(currentMark);
00220
00221
00222 while (nextMark >= 0) {
00223
00224 vertexPtr = theGraph.getVertexPtr((*theRefResult)(currentMark));
00225 const ID &adjacency = vertexPtr->getAdjacency();
00226
00227
00228
00229
00230 int size = adjacency.Size();
00231 for (int i=0; i<size; i++) {
00232
00233 int vertexTag = adjacency(i);
00234 vertexPtr = theGraph.getVertexPtr(vertexTag);
00235 if ((vertexPtr->getTmp()) == -1) {
00236 vertexPtr->setTmp(nextMark);
00237 (*theRefResult)(nextMark--) = vertexTag;
00238 }
00239 }
00240
00241
00242
00243
00244 currentMark--;
00245
00246
00247
00248 if ((currentMark == nextMark) && (currentMark >= 0)) {
00249 cerr << "WARNING: RCM::number - Disconnected graph -2 \n ";
00250
00251
00252
00253 while (((vertexPtr = vertexIter4()) != 0) &&
00254 (vertexPtr->getTmp() != -1))
00255 ;
00256
00257 nextMark--;
00258 vertexPtr->setTmp(currentMark);
00259 (*theRefResult)(currentMark) = vertexPtr->getTag();
00260 }
00261
00262 }
00263
00264
00265
00266
00267 for (int i=0; i<numVertex; i++) {
00268 int vertexTag = (*theRefResult)(i);
00269 vertexPtr = theGraph.getVertexPtr(vertexTag);
00270 vertexPtr->setTmp(i+1);
00271 (*theRefResult)(i) = vertexPtr->getRef();
00272 }
00273
00274 return *theRefResult;
00275 }
00276
00277
00278
00279 int
00280 RCM::sendSelf(int commitTag, Channel &theChannel)
00281 {
00282 return 0;
00283 }
00284
00285 int
00286 RCM::recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker)
00287 {
00288 return 0;
00289 }
00290
00291
00292 const ID &
00293 RCM::number(Graph &theGraph, const ID &startVertices)
00294 {
00295
00296
00297 if (numVertex != theGraph.getNumVertex()) {
00298
00299
00300 if (theRefResult != 0)
00301 delete theRefResult;
00302
00303 numVertex = theGraph.getNumVertex();
00304 theRefResult = new ID(numVertex);
00305
00306 if (theRefResult == 0) {
00307 cerr << "ERROR: RCM::number - Out of Memory\n";
00308 theRefResult = new ID(0);
00309 numVertex = 0;
00310 return *theRefResult;
00311 }
00312 }
00313
00314
00315
00316 if (numVertex == 0)
00317 return *theRefResult;
00318
00319
00320 int minStartVertexTag =0;
00321 int minAvgProfile = 0;
00322 int startVerticesSize = startVertices.Size();
00323 Vertex *vertexPtr;
00324
00325 int startVertexTag =0;
00326
00327 for (int i=0; i<startVerticesSize; i++) {
00328
00329
00330 VertexIter &vertexIter = theGraph.getVertices();
00331
00332 while ((vertexPtr = vertexIter()) != 0)
00333 vertexPtr->setTmp(-1);
00334
00335 startVertexTag = startVertices(i);
00336
00337 if (startVertexTag != -1) {
00338 vertexPtr = theGraph.getVertexPtr(startVertexTag);
00339 if (vertexPtr == 0) {
00340 cerr << "WARNING: RCM::number - No vertex with tag ";
00341 cerr << startVertexTag << "Exists - using first come from iter\n";
00342 startVertexTag = -1;
00343 }
00344 }
00345
00346 int currentMark = numVertex-1;
00347 int nextMark = currentMark -1;
00348 (*theRefResult)(currentMark) = vertexPtr->getTag();
00349 vertexPtr->setTmp(currentMark);
00350 int avgProfile = 0;
00351 VertexIter &vertexIter2 = theGraph.getVertices();
00352
00353
00354
00355 while (nextMark >= 0) {
00356
00357
00358 vertexPtr = theGraph.getVertexPtr((*theRefResult)(currentMark));
00359 const ID &adjacency = vertexPtr->getAdjacency();
00360
00361
00362
00363
00364 int size = adjacency.Size();
00365 for (int j=0; j<size; j++) {
00366
00367 int vertexTag = adjacency(j);
00368 vertexPtr = theGraph.getVertexPtr(vertexTag);
00369 if ((vertexPtr->getTmp()) == -1) {
00370 vertexPtr->setTmp(nextMark);
00371 avgProfile += (currentMark - nextMark);
00372 (*theRefResult)(nextMark--) = vertexTag;
00373 }
00374 }
00375
00376
00377
00378
00379 currentMark--;
00380
00381
00382
00383 if ((currentMark == nextMark) && (currentMark >= 0)) {
00384
00385
00386
00387 while (((vertexPtr = vertexIter2()) != 0) &&
00388 (vertexPtr->getTmp() != -1))
00389 ;
00390 nextMark--;
00391
00392 vertexPtr->setTmp(currentMark);
00393 (*theRefResult)(currentMark) = vertexPtr->getTag();
00394 }
00395
00396 }
00397
00398 if (i == 0 || minAvgProfile > avgProfile) {
00399 minStartVertexTag = startVertexTag;
00400 minAvgProfile = avgProfile;
00401 }
00402
00403 }
00404
00405
00406
00407 minAvgProfile = 0;
00408
00409 if (minStartVertexTag != startVertexTag) {
00410
00411
00412
00413 startVertexTag = minStartVertexTag;
00414
00415
00416 VertexIter &vertexIter = theGraph.getVertices();
00417 while ((vertexPtr = vertexIter()) != 0)
00418 vertexPtr->setTmp(-1);
00419
00420 vertexPtr = theGraph.getVertexPtr(startVertexTag);
00421
00422 int currentMark = numVertex-1;
00423 int nextMark = currentMark -1;
00424 (*theRefResult)(currentMark) = vertexPtr->getTag();
00425 vertexPtr->setTmp(currentMark);
00426 VertexIter &vertexIter2 = theGraph.getVertices();
00427
00428
00429
00430 while (nextMark >= 0) {
00431
00432
00433
00434 vertexPtr = theGraph.getVertexPtr((*theRefResult)(currentMark));
00435 const ID &adjacency = vertexPtr->getAdjacency();
00436
00437
00438
00439
00440 int size = adjacency.Size();
00441 for (int j=0; j<size; j++) {
00442
00443 int vertexTag = adjacency(j);
00444 vertexPtr = theGraph.getVertexPtr(vertexTag);
00445 if ((vertexPtr->getTmp()) == -1) {
00446 vertexPtr->setTmp(nextMark);
00447 minAvgProfile += (currentMark-nextMark);
00448 (*theRefResult)(nextMark--) = vertexTag;
00449 }
00450 }
00451
00452
00453
00454
00455 currentMark--;
00456
00457
00458 if ((currentMark == nextMark) && (currentMark >= 0)) {
00459 cerr << "WARNING: RCM::number - Disconnected graph\n";
00460
00461 while (((vertexPtr = vertexIter2()) != 0) &&
00462 (vertexPtr->getTmp() != -1))
00463 ;
00464
00465 nextMark--;
00466 vertexPtr->setTmp(currentMark);
00467 (*theRefResult)(currentMark) = vertexPtr->getTag();
00468 }
00469 }
00470 }
00471
00472
00473
00474
00475
00476 for (int j=0; j<numVertex; j++) {
00477 int vertexTag = (*theRefResult)(j);
00478 vertexPtr = theGraph.getVertexPtr(vertexTag);
00479 vertexPtr->setTmp(j+1);
00480 (*theRefResult)(j) = vertexPtr->getRef();
00481 }
00482
00483 return *theRefResult;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493