ColPack
|
00001 /************************************************************************************ 00002 Copyright (C) 2005-2008 Assefaw H. Gebremedhin, Arijit Tarafdar, Duc Nguyen, 00003 Alex Pothen 00004 00005 This file is part of ColPack. 00006 00007 ColPack is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published 00009 by the Free Software Foundation, either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 ColPack is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with ColPack. If not, see <http://www.gnu.org/licenses/>. 00019 ************************************************************************************/ 00020 00021 #ifndef EXTRA_CPP 00022 #define EXTRA_CPP 00023 00024 #include "extra.h" 00025 #include "Pause.h" 00026 #include "mmio.h" 00027 #include <cmath> 00028 00029 int WriteMatrixMarket_ADOLCInput(string s_postfix, int i_mode, ...) { 00030 unsigned int ** uip2_SparsityPattern; 00031 int i_Matrix_Row; 00032 int i_Matrix_Col; 00033 double** dp2_CompressedMatrix; 00034 int i_CompressedMatrix_Row; 00035 int i_CompressedMatrix_Col; 00036 double** dp2_Values; 00037 00038 string s_BaseName = "-ColPack_debug.mtx"; 00039 00040 va_list ap; /*will point to each unnamed argument in turn*/ 00041 va_start(ap,i_mode); /* point to first element after i_mode*/ 00042 00043 if (i_mode == 0) { 00044 uip2_SparsityPattern = va_arg(ap,unsigned int **); 00045 i_Matrix_Row = va_arg(ap,int); 00046 i_Matrix_Col = va_arg(ap,int); 00047 00048 string s_MatrixName = "pattern"+s_postfix+s_BaseName; 00049 00050 ofstream out_Matrix (s_MatrixName.c_str()); 00051 if(!out_Matrix) { 00052 cout<<"Error creating file: \""<<out_Matrix<<"\""<<endl; 00053 exit(1); 00054 } 00055 00056 int i_NumOfLines = 0; 00057 00058 //Count i_NumOfLines 00059 for(int i = 0; i<i_Matrix_Row;i++) { 00060 i_NumOfLines += uip2_SparsityPattern[i][0]; 00061 } 00062 00063 out_Matrix<<"%%MatrixMarket matrix coordinate real general"<<endl; 00064 out_Matrix<<i_Matrix_Row<<" "<<i_Matrix_Col<<" "<< i_NumOfLines<<endl; 00065 00066 out_Matrix<<setprecision(10)<<scientific<<showpoint; 00067 for(int i = 0; i<i_Matrix_Row;i++) { 00068 for(int j = 1; j<=uip2_SparsityPattern[i][0];j++) { 00069 out_Matrix<<i+1<<" "<<uip2_SparsityPattern[i][j]+1; 00070 out_Matrix<<endl; 00071 } 00072 } 00073 00074 out_Matrix.close(); 00075 } 00076 else if (i_mode == 1) { 00077 uip2_SparsityPattern = va_arg(ap,unsigned int **); 00078 i_Matrix_Row = va_arg(ap,int); 00079 i_Matrix_Col = va_arg(ap,int); 00080 dp2_CompressedMatrix = va_arg(ap,double**); 00081 i_CompressedMatrix_Row = va_arg(ap,int); 00082 i_CompressedMatrix_Col = va_arg(ap,int); 00083 00084 string s_MatrixName = "pattern"+s_postfix+s_BaseName; 00085 ofstream out_Matrix (s_MatrixName.c_str()); 00086 if(!out_Matrix) { 00087 cout<<"Error creating file: \""<<out_Matrix<<"\""<<endl; 00088 exit(1); 00089 } 00090 00091 int i_NumOfLines = 0; 00092 00093 //Count i_NumOfLines 00094 for(int i = 0; i<i_Matrix_Row;i++) { 00095 i_NumOfLines += uip2_SparsityPattern[i][0]; 00096 } 00097 00098 out_Matrix<<"%%MatrixMarket matrix coordinate real general"<<endl; 00099 out_Matrix<<i_Matrix_Row<<" "<<i_Matrix_Col<<" "<< i_NumOfLines<<endl; 00100 00101 out_Matrix<<setprecision(10)<<scientific<<showpoint; 00102 for(int i = 0; i<i_Matrix_Row;i++) { 00103 for(int j = 1; j<=uip2_SparsityPattern[i][0];j++) { 00104 out_Matrix<<i+1<<" "<<uip2_SparsityPattern[i][j]+1; 00105 out_Matrix<<endl; 00106 } 00107 } 00108 00109 out_Matrix.close(); 00110 00111 string s_CompressedMatrixName = "CompressedMatrix"+s_postfix+s_BaseName; 00112 ofstream out_CompressedMatrix (s_CompressedMatrixName.c_str()); 00113 if(!out_CompressedMatrix) { 00114 cout<<"Error creating file: \""<<out_CompressedMatrix<<"\""<<endl; 00115 exit(1); 00116 } 00117 00118 out_CompressedMatrix<<"%%MatrixMarket matrix coordinate real general"<<endl; 00119 out_CompressedMatrix<<i_CompressedMatrix_Row<<" "<<i_CompressedMatrix_Col<<" "<< i_CompressedMatrix_Row*i_CompressedMatrix_Col<<endl; 00120 00121 out_CompressedMatrix<<setprecision(10)<<scientific<<showpoint; 00122 for(int i = 0; i<i_CompressedMatrix_Row;i++) { 00123 for(int j = 0; j<i_CompressedMatrix_Col;j++) { 00124 out_CompressedMatrix<<i+1<<" "<<j+1<<" "<<dp2_CompressedMatrix[i][j]; 00125 out_CompressedMatrix<<endl; 00126 } 00127 } 00128 00129 out_CompressedMatrix.close(); 00130 } 00131 else if (i_mode == 2) { 00132 uip2_SparsityPattern = va_arg(ap,unsigned int **); 00133 i_Matrix_Row = va_arg(ap,int); 00134 i_Matrix_Col = va_arg(ap,int); 00135 dp2_CompressedMatrix = va_arg(ap,double**); 00136 i_CompressedMatrix_Row = va_arg(ap,int); 00137 i_CompressedMatrix_Col = va_arg(ap,int); 00138 dp2_Values = va_arg(ap,double**); 00139 00140 string s_MatrixName = "pattern_value"+s_postfix+s_BaseName; 00141 ofstream out_Matrix (s_MatrixName.c_str()); 00142 if(!out_Matrix) { 00143 cout<<"Error creating file: \""<<out_Matrix<<"\""<<endl; 00144 exit(1); 00145 } 00146 00147 int i_NumOfLines = 0; 00148 00149 //Count i_NumOfLines 00150 for(int i = 0; i<i_Matrix_Row;i++) { 00151 i_NumOfLines += uip2_SparsityPattern[i][0]; 00152 } 00153 00154 out_Matrix<<"%%MatrixMarket matrix coordinate real general"<<endl; 00155 out_Matrix<<i_Matrix_Row<<" "<<i_Matrix_Col<<" "<< i_NumOfLines<<endl; 00156 00157 out_Matrix<<setprecision(10)<<scientific<<showpoint; 00158 for(int i = 0; i<i_Matrix_Row;i++) { 00159 for(int j = 1; j<=uip2_SparsityPattern[i][0];j++) { 00160 out_Matrix<<i+1<<" "<<uip2_SparsityPattern[i][j]+1<<" "<<dp2_Values[i][j]; 00161 out_Matrix<<endl; 00162 } 00163 } 00164 00165 out_Matrix.close(); 00166 00167 string s_CompressedMatrixName = "CompressedMatrix"+s_postfix+s_BaseName; 00168 ofstream out_CompressedMatrix (s_CompressedMatrixName.c_str()); 00169 if(!out_CompressedMatrix) { 00170 cout<<"Error creating file: \""<<out_CompressedMatrix<<"\""<<endl; 00171 exit(1); 00172 } 00173 00174 out_CompressedMatrix<<"%%MatrixMarket matrix coordinate real general"<<endl; 00175 out_CompressedMatrix<<i_CompressedMatrix_Row<<" "<<i_CompressedMatrix_Col<<" "<< i_CompressedMatrix_Row*i_CompressedMatrix_Col<<endl; 00176 00177 out_CompressedMatrix<<setprecision(10)<<scientific<<showpoint; 00178 for(int i = 0; i<i_CompressedMatrix_Row;i++) { 00179 for(int j = 0; j<i_CompressedMatrix_Col;j++) { 00180 out_CompressedMatrix<<i+1<<" "<<j+1<<" "<<dp2_CompressedMatrix[i][j]; 00181 out_CompressedMatrix<<endl; 00182 } 00183 } 00184 00185 out_CompressedMatrix.close(); 00186 } 00187 else { 00188 cerr<<"ERR: WriteMatrixMarket_ADOLCInput(): i_mode =\""<< i_mode <<"\" unknown or unspecified"<<endl; 00189 00190 va_end(ap); //cleanup 00191 return 1; 00192 } 00193 00194 va_end(ap); //cleanup 00195 return 0; 00196 } 00197 00198 int displayGraph(map< int, map<int,bool> > *graph, vector<int>* vi_VertexColors,int i_RunInBackground , int filter ) { 00199 static int ranNum = rand(); 00200 static int seq = 0; 00201 seq++; 00202 vector<string> ListOfColors = getListOfColors(""); 00203 string fileName = "/tmp/."; 00204 fileName = fileName + "ColPack_"+ itoa(ranNum)+"_"+itoa(seq)+".dot"; 00205 00206 //build the dot file of the graph 00207 if(vi_VertexColors == NULL) { 00208 //build dot file represents graph without color info 00209 buildDotWithoutColor(graph, ListOfColors, fileName); 00210 } else { 00211 //build dot file represents graph with color 00212 buildDotWithColor(graph, vi_VertexColors, ListOfColors, fileName); 00213 } 00214 00215 //display the graph using xdot 00216 string command; 00217 switch (filter) { 00218 case NEATO: command="xdot -f neato "; break; 00219 case TWOPI: command="xdot -f twopi "; break; 00220 case CIRCO: command="xdot -f circo "; break; 00221 case FDP: command="xdot -f fdp "; break; 00222 default: command="xdot -f dot "; // case DOT 00223 } 00224 00225 command = command + fileName; 00226 if(i_RunInBackground) command = command + " &"; 00227 int i_ReturnValue = system(command.c_str()); 00228 return i_ReturnValue; 00229 } 00230 00231 int buildDotWithoutColor(map< int, map<int,bool> > *graph, vector<string> &ListOfColors, string fileName) { 00232 cerr<<"IN buildDotWithoutColor"<<endl; 00233 ofstream OutputStream (fileName.c_str()); 00234 if(!OutputStream){ 00235 cout<<"CAN'T create File "<<fileName<<endl; 00236 return 1; 00237 } else { 00238 cout<<"Create File "<<fileName<<endl; 00239 } 00240 00241 string line=""; 00242 00243 //build header 00244 OutputStream<<"graph g {"<<endl; 00245 00246 //build body 00247 map< int, map<int,bool> >::iterator itr = graph->begin(); 00248 for(; itr != graph->end(); itr++) { 00249 map<int,bool>::iterator itr2 = (itr->second).begin(); 00250 for(; itr2 != (itr->second).end(); itr2++) { 00251 if(itr2->first<=itr->first) continue; 00252 line = ""; 00253 line = line + "v"+itoa(itr->first)+" -- v"+ itoa(itr2->first) +" ;"; 00254 OutputStream<<line<<endl; 00255 } 00256 } 00257 00258 //build footer 00259 OutputStream<<"}"<<endl; 00260 00261 OutputStream.close(); 00262 cout<<"\t File created"<<endl; 00263 00264 return 0; 00265 } 00266 00267 int buildDotWithColor(map< int, map<int,bool> > *graph, vector<int>* vi_VertexColors, vector<string> &ListOfColors, string fileName) { 00268 cerr<<"IN buildDotWithColor"<<endl; 00269 ofstream OutputStream (fileName.c_str()); 00270 if(!OutputStream){ 00271 cout<<"CAN'T create File "<<fileName<<endl; 00272 return 1; 00273 } else { 00274 cout<<"Create File "<<fileName<<endl; 00275 } 00276 00277 //vector<int> m_vi_Vertices, m_vi_Edges, m_vi_VertexColors; 00278 //g.GetVertices(m_vi_Vertices); 00279 //g.GetEdges(m_vi_Edges); 00280 //g.GetVertexColors(m_vi_VertexColors); 00281 //int i_VertexCount = STEP_DOWN((signed) m_vi_Vertices.size()); 00282 int i_NumberOfColors = ListOfColors.size(); 00283 string line="", color_str="", colorID_str="", colorID_str2=""; 00284 00285 //build header 00286 OutputStream<<"graph g {"<<endl; 00287 00288 //build node colors 00289 map< int, map<int,bool> >::iterator itr = graph->begin(); 00290 for(; itr != graph->end(); itr++) { 00291 line=""; 00292 if((*vi_VertexColors)[itr->first] != _UNKNOWN) { 00293 color_str = ListOfColors[(*vi_VertexColors)[itr->first]%i_NumberOfColors]; 00294 colorID_str = itoa((*vi_VertexColors)[itr->first]); 00295 } 00296 else { 00297 color_str="green"; 00298 colorID_str = "_"; 00299 } 00300 //a_1 [color=aliceblue] 00301 line = line + "v"+itoa(itr->first)+"_c"+ colorID_str +" [style=filled fillcolor="+color_str+"]"; 00302 OutputStream<<line<<endl; 00303 } 00304 cout<<endl<<endl; 00305 00306 00307 //build body 00308 itr = graph->begin(); 00309 for(; itr != graph->end(); itr++) { 00310 map<int,bool>::iterator itr2 = (itr->second).begin(); 00311 for(; itr2 != (itr->second).end(); itr2++) { 00312 if(itr2->first <= itr->first) continue; 00313 00314 if((*vi_VertexColors)[itr->first] != _UNKNOWN) { 00315 colorID_str = itoa((*vi_VertexColors)[itr->first]); 00316 } 00317 else { 00318 colorID_str = "_"; 00319 } 00320 00321 if((*vi_VertexColors)[itr2->first] != _UNKNOWN) { 00322 colorID_str2 = itoa((*vi_VertexColors)[itr2->first]); 00323 } 00324 else { 00325 colorID_str2 = "_"; 00326 } 00327 00328 line = ""; 00329 line = line + "v"+itoa(itr->first)+"_c"+colorID_str+" -- v"+ itoa(itr2->first)+"_c"+colorID_str2 ; 00330 OutputStream<<line<<" ;"<<endl; 00331 } 00332 } 00333 00334 //build footer 00335 OutputStream<<"}"<<endl; 00336 00337 OutputStream.close(); 00338 cout<<"\t File created"<<endl; 00339 return 0; 00340 } 00341 00342 00343 int ConvertHarwellBoeingDouble(string & num_string) { 00344 for(int i=num_string.size()-1; i>=0; i--) { 00345 if(num_string[i] == 'D') { 00346 num_string[i]='E'; 00347 return 1; 00348 } 00349 } 00350 return 0; 00351 } 00352 00353 string itoa(int i) { 00354 string s; 00355 stringstream out; 00356 out << i; 00357 s = out.str(); 00358 00359 return s; 00360 } 00361 00362 vector<string> getListOfColors(string s_InputFile) { 00363 if (s_InputFile.size()==0 || s_InputFile == "" ) s_InputFile="list_of_colors.txt"; 00364 ifstream InputStream (s_InputFile.c_str()); 00365 if(!InputStream){ 00366 cout<<"Not Found File "<<s_InputFile<<endl; 00367 } else { 00368 cout<<"Found File "<<s_InputFile<<endl; 00369 } 00370 00371 string line; 00372 getline(InputStream, line); 00373 vector<string> ListOfColors; 00374 00375 while(!InputStream.eof() && line != "*") { 00376 ListOfColors.push_back(line); 00377 getline(InputStream, line); 00378 } 00379 00380 return ListOfColors; 00381 } 00382 00383 00384 int buildDotWithoutColor(ColPack::BipartiteGraphPartialColoringInterface &g, vector<string> &ListOfColors, string fileName) { 00385 cerr<<"IN buildDotWithoutColor - BipartiteGraphPartialColoring"<<endl; 00386 ofstream OutputStream (fileName.c_str()); 00387 if(!OutputStream){ 00388 cout<<"CAN'T create File "<<fileName<<endl; 00389 return 1; 00390 } else { 00391 cout<<"Create File "<<fileName<<endl; 00392 } 00393 00394 vector<int> m_vi_Vertices, m_vi_Edges; 00395 g.GetLeftVertices(m_vi_Vertices); 00396 g.GetEdges(m_vi_Edges); 00397 int i_VertexCount = STEP_DOWN((signed) m_vi_Vertices.size()); 00398 string line=""; 00399 00400 //build header 00401 OutputStream<<"graph g {"<<endl; 00402 00403 //build body 00404 for(int i=0; i < i_VertexCount; i++) { 00405 for(int j=m_vi_Vertices[i] ; j< m_vi_Vertices[i + 1]; j++) { 00406 line = ""; 00407 line = line + "v"+itoa(i)+" -- v"+ itoa(m_vi_Edges[j] + i_VertexCount) +" ;"; 00408 OutputStream<<line<<endl; 00409 } 00410 } 00411 00412 //build footer 00413 OutputStream<<"}"<<endl; 00414 00415 OutputStream.close(); 00416 cout<<"\t File created"<<endl; 00417 00418 return 0; 00419 } 00420 00421 int buildDotWithColor(ColPack::BipartiteGraphPartialColoringInterface &g, vector<string> &ListOfColors, string fileName) { 00422 cerr<<"IN buildDotWithColor - BipartiteGraphPartialColoringInterface"<<endl; 00423 ofstream OutputStream (fileName.c_str()); 00424 if(!OutputStream){ 00425 cout<<"CAN'T create File "<<fileName<<endl; 00426 return 1; 00427 } else { 00428 cout<<"Create File "<<fileName<<endl; 00429 } 00430 00431 vector<int> m_vi_Vertices, m_vi_Edges, m_vi_LeftVertexColors, m_vi_RightVertexColors; 00432 g.GetLeftVertices(m_vi_Vertices); 00433 //cout<<"displayVector(m_vi_Vertices);"<<endl; 00434 //displayVector(m_vi_Vertices); 00435 g.GetEdges(m_vi_Edges); 00436 //cout<<"displayVector(m_vi_Edges);"<<endl; 00437 //displayVector(m_vi_Edges); 00438 g.GetLeftVertexColors(m_vi_LeftVertexColors); 00439 //cout<<"displayVector(m_vi_LeftVertexColors);"<<endl; 00440 //displayVector(m_vi_LeftVertexColors); 00441 g.GetRightVertexColors(m_vi_RightVertexColors); 00442 //cout<<"displayVector(m_vi_RightVertexColors);"<<endl; 00443 //displayVector(m_vi_RightVertexColors); 00444 int i_VertexCount = STEP_DOWN((signed) m_vi_Vertices.size()); 00445 int i_RightVertexCount = g.GetRightVertexCount(); 00446 //cout<<"i_RightVertexCount="<<i_RightVertexCount<<endl; 00447 int i_NumberOfColors = ListOfColors.size(); 00448 string line="", color_str=""; 00449 00450 //build header 00451 OutputStream<<"graph g {"<<endl; 00452 00453 //build node colors 00454 //colors for left vertices 00455 for(int i=0; i < i_VertexCount; i++) { 00456 line=""; 00457 if(m_vi_LeftVertexColors.size()>0) { 00458 color_str = ListOfColors[m_vi_LeftVertexColors[i]%i_NumberOfColors]; 00459 //v2_c4 [color=aliceblue] 00460 line = line + "v"+itoa(i)+"_c"+itoa(m_vi_LeftVertexColors[i])+" [style=filled fillcolor="+color_str+"]"; 00461 } 00462 else { 00463 color_str = ListOfColors[0]; 00464 line = line + "v"+itoa(i)+"_c_"+" [style=filled fillcolor="+color_str+"]"; 00465 } 00466 OutputStream<<line<<endl; 00467 } 00468 //colors for right vertices 00469 for(int i=0; i < i_RightVertexCount; i++) { 00470 line=""; 00471 if(m_vi_RightVertexColors.size()>0) { 00472 color_str = ListOfColors[m_vi_RightVertexColors[i]%i_NumberOfColors]; 00473 //v2_c4 [color=aliceblue] 00474 line = line + "v"+itoa(i+i_VertexCount)+"_c"+itoa(m_vi_RightVertexColors[i])+" [style=filled fillcolor="+color_str+"]"; 00475 } 00476 else { 00477 color_str = ListOfColors[0]; 00478 line = line + "v"+itoa(i+i_VertexCount)+"_c_"+" [style=filled fillcolor="+color_str+"]"; 00479 } 00480 OutputStream<<line<<endl; 00481 } 00482 cout<<endl<<endl; 00483 00484 //Find conflicts 00485 vector<bool> m_vi_ConflictEdges; 00486 /* 00487 vector<vector<int> > ListOfConflicts; 00488 g.GetStarColoringConflicts(ListOfConflicts); 00489 00490 //Mark conflict edge 00491 m_vi_ConflictEdges.resize(m_vi_Edges.size(),false); 00492 if(ListOfConflicts.size()>0) { 00493 for(int i=0; i<ListOfConflicts.size();i++) { 00494 for(int j=0; j<ListOfConflicts[i].size()-1;j++) { 00495 int Vertex1 = ListOfConflicts[i][j]; 00496 int Vertex2 = ListOfConflicts[i][j+1]; 00497 if(Vertex1 > Vertex2) { //swap order 00498 for(int k=m_vi_Vertices[Vertex2]; k < m_vi_Vertices[Vertex2+1]; k++) { 00499 if(m_vi_Edges[k] == Vertex1) { 00500 m_vi_ConflictEdges[ k ]=true; 00501 break; 00502 } 00503 } 00504 } 00505 else { 00506 for(int k=m_vi_Vertices[Vertex1]; k < m_vi_Vertices[Vertex1+1]; k++) { 00507 if(m_vi_Edges[k] == Vertex2) { 00508 m_vi_ConflictEdges[ k ]=true; 00509 break; 00510 } 00511 } 00512 } 00513 00514 } 00515 } 00516 } 00517 //*/ 00518 00519 //build body 00520 for(int i=0; i < i_VertexCount; i++) { 00521 for(int j=m_vi_Vertices[i] ; j< m_vi_Vertices[i + 1]; j++) { 00522 line = ""; 00523 line = line + "v"+itoa(i)+"_c"; 00524 00525 if(m_vi_LeftVertexColors.size() > 0) { 00526 line = line + itoa(m_vi_LeftVertexColors[i]); 00527 } 00528 else { 00529 line = line + '_'; 00530 } 00531 00532 line = line + " -- v"+ itoa(m_vi_Edges[j] + i_VertexCount)+"_c"; 00533 00534 if(m_vi_RightVertexColors.size() > 0) { 00535 line = line + itoa(m_vi_RightVertexColors[m_vi_Edges[j]]); 00536 } 00537 else { 00538 line = line + '_'; 00539 } 00540 00541 if(m_vi_ConflictEdges.size()>0 && m_vi_ConflictEdges[j]) { // make the line bolder if the edge is conflict 00542 line = line + "[style=\"setlinewidth(3)\"]"; 00543 } 00544 OutputStream<<line<<" ;"<<endl; 00545 } 00546 } 00547 00548 //build footer 00549 OutputStream<<"}"<<endl; 00550 00551 OutputStream.close(); 00552 cout<<"\t File created"<<endl; 00553 return 0; 00554 } 00555 00556 int buildDotWithoutColor(ColPack::BipartiteGraphBicoloringInterface &g, vector<string> &ListOfColors, string fileName) { 00557 cerr<<"Function to be built! int buildDotWithoutColor(ColPack::BipartiteGraphBicoloringInterface &g, vector<string> &ListOfColors, string fileName)"<<endl; 00558 Pause(); 00559 return 0; 00560 } 00561 00562 int buildDotWithColor(ColPack::BipartiteGraphBicoloringInterface &g, vector<string> &ListOfColors, string fileName) { 00563 cerr<<"Function to be built! int buildDotWithColor(ColPack::BipartiteGraphBicoloringInterface &g, vector<string> &ListOfColors, string fileName)"<<endl; 00564 Pause(); 00565 return 0; 00566 } 00567 00568 00569 00570 int buildDotWithoutColor(ColPack::GraphColoringInterface &g, vector<string> &ListOfColors, string fileName) { 00571 cerr<<"IN buildDotWithoutColor"<<endl; 00572 ofstream OutputStream (fileName.c_str()); 00573 if(!OutputStream){ 00574 cout<<"CAN'T create File "<<fileName<<endl; 00575 return 1; 00576 } else { 00577 cout<<"Create File "<<fileName<<endl; 00578 } 00579 00580 vector<int> m_vi_Vertices, m_vi_Edges; 00581 g.GetVertices(m_vi_Vertices); 00582 g.GetEdges(m_vi_Edges); 00583 int i_VertexCount = STEP_DOWN((signed) m_vi_Vertices.size()); 00584 string line=""; 00585 00586 //build header 00587 OutputStream<<"graph g {"<<endl; 00588 00589 //build body 00590 for(int i=0; i < i_VertexCount; i++) { 00591 for(int j=m_vi_Vertices[i] ; j< m_vi_Vertices[i + 1]; j++) { 00592 if(m_vi_Edges[j]<=i) continue; 00593 line = ""; 00594 line = line + "v"+itoa(i)+" -- v"+ itoa(m_vi_Edges[j]) +" ;"; 00595 OutputStream<<line<<endl; 00596 } 00597 } 00598 00599 //build footer 00600 OutputStream<<"}"<<endl; 00601 00602 OutputStream.close(); 00603 cout<<"\t File created"<<endl; 00604 00605 return 0; 00606 } 00607 00608 int buildDotWithColor(ColPack::GraphColoringInterface &g, vector<string> &ListOfColors, string fileName) { 00609 cerr<<"IN buildDotWithColor"<<endl; 00610 ofstream OutputStream (fileName.c_str()); 00611 if(!OutputStream){ 00612 cout<<"CAN'T create File "<<fileName<<endl; 00613 return 1; 00614 } else { 00615 cout<<"Create File "<<fileName<<endl; 00616 } 00617 00618 vector<int> m_vi_Vertices, m_vi_Edges, m_vi_VertexColors; 00619 g.GetVertices(m_vi_Vertices); 00620 g.GetEdges(m_vi_Edges); 00621 g.GetVertexColors(m_vi_VertexColors); 00622 int i_VertexCount = STEP_DOWN((signed) m_vi_Vertices.size()); 00623 int i_NumberOfColors = ListOfColors.size(); 00624 string line="", color_str="", colorID_str="", colorID_str2=""; 00625 00626 //build header 00627 OutputStream<<"graph g {"<<endl; 00628 00629 //build node colors 00630 for(int i=0; i < i_VertexCount; i++) { 00631 line=""; 00632 if(m_vi_VertexColors[i] != _UNKNOWN) { 00633 color_str = ListOfColors[m_vi_VertexColors[i]%i_NumberOfColors]; 00634 colorID_str = itoa(m_vi_VertexColors[i]); 00635 } 00636 else { 00637 color_str="green"; 00638 colorID_str = "_"; 00639 } 00640 //a_1 [color=aliceblue] 00641 line = line + "v"+itoa(i)+"_c"+ colorID_str +" [style=filled fillcolor="+color_str+"]"; 00642 OutputStream<<line<<endl; 00643 } 00644 cout<<endl<<endl; 00645 00646 //Find conflicts 00647 vector<vector<int> > ListOfConflicts; 00648 g.GetStarColoringConflicts(ListOfConflicts); 00649 00650 //Mark conflict edge 00651 vector<bool> m_vi_ConflictEdges; 00652 m_vi_ConflictEdges.resize(m_vi_Edges.size(),false); 00653 if(ListOfConflicts.size()>0) { 00654 for(int i=0; i<ListOfConflicts.size();i++) { 00655 for(int j=0; j<ListOfConflicts[i].size()-1;j++) { 00656 int Vertex1 = ListOfConflicts[i][j]; 00657 int Vertex2 = ListOfConflicts[i][j+1]; 00658 if(Vertex1 > Vertex2) { //swap order 00659 for(int k=m_vi_Vertices[Vertex2]; k < m_vi_Vertices[Vertex2+1]; k++) { 00660 if(m_vi_Edges[k] == Vertex1) { 00661 m_vi_ConflictEdges[ k ]=true; 00662 break; 00663 } 00664 } 00665 } 00666 else { 00667 for(int k=m_vi_Vertices[Vertex1]; k < m_vi_Vertices[Vertex1+1]; k++) { 00668 if(m_vi_Edges[k] == Vertex2) { 00669 m_vi_ConflictEdges[ k ]=true; 00670 break; 00671 } 00672 } 00673 } 00674 00675 } 00676 } 00677 } 00678 00679 //build body 00680 for(int i=0; i < i_VertexCount; i++) { 00681 for(int j=m_vi_Vertices[i] ; j< m_vi_Vertices[i + 1]; j++) { 00682 if(m_vi_Edges[j]<=i) continue; 00683 00684 if(m_vi_VertexColors[i] != _UNKNOWN) { 00685 colorID_str = itoa(m_vi_VertexColors[i]); 00686 } 00687 else { 00688 colorID_str = "_"; 00689 } 00690 00691 if(m_vi_VertexColors[m_vi_Edges[j]] != _UNKNOWN) { 00692 colorID_str2 = itoa(m_vi_VertexColors[m_vi_Edges[j]]); 00693 } 00694 else { 00695 colorID_str2 = "_"; 00696 } 00697 00698 line = ""; 00699 line = line + "v"+itoa(i)+"_c"+colorID_str+" -- v"+ itoa(m_vi_Edges[j])+"_c"+colorID_str2 ; 00700 if(m_vi_ConflictEdges.size()>0 && m_vi_ConflictEdges[j]) { // make the line bolder if the edge is conflict 00701 line = line + "[style=\"setlinewidth(3)\"]"; 00702 } 00703 OutputStream<<line<<" ;"<<endl; 00704 } 00705 } 00706 00707 //build footer 00708 OutputStream<<"}"<<endl; 00709 00710 OutputStream.close(); 00711 cout<<"\t File created"<<endl; 00712 return 0; 00713 } 00714 00715 00716 bool isValidOrdering(vector<int> & ordering, int offset) { 00717 vector<bool> isExist, index; 00718 int orderingNum = 0; 00719 isExist.resize(ordering.size(), false); 00720 index.resize(ordering.size(), false); 00721 for(int i=0; i<ordering.size(); i++) { 00722 orderingNum = ordering[i] - offset; 00723 if(orderingNum<0 || orderingNum>= ordering.size()) { 00724 cerr<<" This vertex # is not in the valid range [0, ordering.size()]. ordering[i]: "<<ordering[i]<<endl; 00725 return false; 00726 } 00727 00728 if(isExist[ orderingNum ]) { 00729 cerr<<"This vertex id "<<orderingNum<<" has been seen before at ordering["<<index [orderingNum]<<"] and ordering["<<i<<"]. We have duplication!"<<endl; 00730 return false; 00731 } 00732 00733 isExist[ orderingNum ] = true; 00734 index [orderingNum] = i; 00735 } 00736 00737 return true; 00738 } 00739 00740 int ReadRowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, int& rowCount, int& columnCount) { 00741 string line; 00742 int lineCounter = 0,nz_counter = 0, nonzeros = 0, nnz_per_row = 0; 00743 unsigned int num = 0; 00744 istringstream in2; 00745 ifstream in (s_InputFile.c_str()); 00746 00747 if(!in) { 00748 cout<<s_InputFile<<" not Found!"<<endl; 00749 exit(1); 00750 } 00751 00752 getline(in,line); 00753 lineCounter++; 00754 in2.str(line); 00755 in2 >> rowCount >> columnCount >> nonzeros; 00756 00757 (*uip3_SparsityPattern) = new unsigned int*[rowCount]; 00758 00759 for(int i=0;i < rowCount; i++) { 00760 getline(in, line); 00761 lineCounter++; 00762 if(line!="") 00763 { 00764 in2.clear(); 00765 in2.str(line); 00766 in2>>nnz_per_row; 00767 (*uip3_SparsityPattern)[i] = new unsigned int[nnz_per_row + 1]; 00768 (*uip3_SparsityPattern)[i][0] = nnz_per_row; 00769 00770 for(int j=1; j<nnz_per_row+1; j++) { 00771 in2>>num; 00772 (*uip3_SparsityPattern)[i][j] = num; 00773 nz_counter++; 00774 } 00775 } 00776 else 00777 { 00778 cerr<<"* WARNING: ReadRowCompressedFormat()"<<endl; 00779 cerr<<"*\t line == \"\" at row "<<lineCounter<<". Empty line. Wrong input format. Can't process."<<endl; 00780 cerr<<"\t total non-zeros so far: "<<nz_counter<<endl; 00781 exit( -1); 00782 } 00783 } 00784 00785 if(nz_counter<nonzeros) { //nz_counter should be == nonzeros 00786 cerr<<"* WARNING: ReadRowCompressedFormat()"<<endl; 00787 cerr<<"*\t nz_counter<nonzeros+1. Wrong input format. Can't process."<<endl; 00788 cerr<<"\t total non-zeros so far: "<<nz_counter<<endl; 00789 exit( -1); 00790 } 00791 00792 00793 00794 return 0; 00795 00796 } 00797 00798 int ConvertRowCompressedFormat2SparseSolversFormat_StructureOnly (unsigned int ** uip2_HessianSparsityPattern, unsigned int ui_rowCount, unsigned int** ip2_RowIndex, unsigned int** ip2_ColumnIndex) { 00799 00800 //first, count the number of non-zeros in the upper triangular and also populate *ip2_RowIndex array 00801 unsigned int nnz = 0; 00802 unsigned int nnz_in1Row = 0; 00803 (*ip2_RowIndex) = (unsigned int*) malloc( (ui_rowCount + 1) * sizeof(unsigned int)); 00804 for (unsigned int i=0; i < ui_rowCount; i++) { 00805 nnz_in1Row = uip2_HessianSparsityPattern[i][0]; 00806 (*ip2_RowIndex)[i] = nnz; 00807 for (unsigned int j = 1; j <= nnz_in1Row ; j++) { 00808 if (i <= uip2_HessianSparsityPattern[i][j]) nnz++; 00809 } 00810 } 00811 (*ip2_RowIndex)[ui_rowCount] = nnz; 00812 //cout<<"nnz = "<<nnz<<endl; 00813 00814 //displayVector(*ip2_RowIndex,ui_rowCount+1); 00815 00816 // populate *ip2_ColumnIndex array 00817 (*ip2_ColumnIndex) = (unsigned int*) malloc( (nnz) * sizeof(unsigned int)); 00818 unsigned int count = 0; 00819 for (unsigned int i=0; i < ui_rowCount; i++) { 00820 nnz_in1Row = uip2_HessianSparsityPattern[i][0]; 00821 for (unsigned int j = 1; j <= nnz_in1Row ; j++) { 00822 if (i <= uip2_HessianSparsityPattern[i][j]) { 00823 (*ip2_ColumnIndex)[count] = uip2_HessianSparsityPattern[i][j]; 00824 count++; 00825 } 00826 } 00827 } 00828 if(count != nnz) { 00829 cerr<<"!!! count != nnz. count = "<<count<<endl; 00830 Pause(); 00831 } 00832 00833 return nnz; 00834 } 00835 00836 int ConvertCoordinateFormat2RowCompressedFormat(unsigned int* uip1_RowIndex, unsigned int* uip1_ColumnIndex, double* dp1_HessianValue, int i_RowCount, int i_NonZeroCount, unsigned int *** dp3_Pattern, double*** dp3_Values ) { 00837 (*dp3_Pattern) = (unsigned int**) malloc( (i_RowCount) * sizeof(unsigned int*)); 00838 (*dp3_Values) = (double**) malloc( (i_RowCount) * sizeof(double*)); 00839 00840 //Allocate memory for (*dp3_Pattern) and (*dp3_Values) 00841 int count=1; 00842 for(int i=1; i<i_NonZeroCount; i++) { 00843 if(uip1_RowIndex[i] != uip1_RowIndex[i-1]) { 00844 (*dp3_Pattern)[ uip1_RowIndex[i-1] ] = (unsigned int*) malloc( (count + 1) * sizeof(unsigned int)); 00845 (*dp3_Pattern)[ uip1_RowIndex[i-1] ][0] = count; 00846 (*dp3_Values)[ uip1_RowIndex[i-1] ] = (double*) malloc( (count + 1) * sizeof(double)); 00847 (*dp3_Values)[ uip1_RowIndex[i-1] ][0] = (double)count; 00848 00849 count=1; 00850 } else { //uip1_RowIndex[i] == uip1_RowIndex[i-1] 00851 count++; 00852 } 00853 } 00854 (*dp3_Pattern)[ uip1_RowIndex[i_NonZeroCount-1] ] = (unsigned int*) malloc( (count + 1) * sizeof(unsigned int)); 00855 (*dp3_Pattern)[ uip1_RowIndex[i_NonZeroCount-1] ][0] = count; 00856 (*dp3_Values)[ uip1_RowIndex[i_NonZeroCount-1] ] = (double*) malloc( (count + 1) * sizeof(double)); 00857 (*dp3_Values)[ uip1_RowIndex[i_NonZeroCount-1] ][0] = (double) count; 00858 00859 //Populate values of (*dp3_Pattern) and (*dp3_Values) 00860 count=0; 00861 for(int i=0; i<i_RowCount; i++) { 00862 for(int j=1; j<= (*dp3_Pattern)[i][0]; j++) { 00863 (*dp3_Pattern)[i][j] = uip1_ColumnIndex[count]; 00864 (*dp3_Values)[i][j] = dp1_HessianValue[count]; 00865 count++; 00866 } 00867 } 00868 00869 if(count != i_NonZeroCount) { 00870 cerr<<"count != i_NonZeroCount"<<endl; 00871 exit(1); 00872 } 00873 00874 00875 return 0; 00876 } 00877 00878 void ConvertFileDIMACSFormat2MatrixMarketFormat(string fileNameNoExt) { 00879 string inFileName = fileNameNoExt + ".gr"; 00880 string outFileName = fileNameNoExt + ".mtx"; 00881 string line, temp; 00882 ifstream in(inFileName.c_str()); 00883 ofstream out(outFileName.c_str()); 00884 istringstream iin; 00885 00886 while(in) { 00887 getline(in, line); 00888 if(line=="") break; 00889 switch(line[0]) { 00890 case 'a': 00891 //Line has this format "a <in_node> <out_node> <edge_weight>" 00892 out<<line.substr(2,line.size()-2)<<endl; 00893 break; 00894 case 'c': // comment line 00895 break; 00896 default: // 'p' 00897 //Heading. Line has this format "p sp <num_of_node> <num_of_edges == num_of_line after this line>" 00898 iin.str(line); 00899 iin>>temp>>temp>>temp;out<<temp<<" "<<temp<<" "; 00900 iin>>temp;out<<temp<<endl; 00901 break; 00902 } 00903 } 00904 00905 in.close(); 00906 out.close(); 00907 } 00908 00909 void randomOrdering(vector<int>& ordering) { 00910 srand(time(NULL)); 00911 int size = ordering.size(); 00912 int ran_num = 0; 00913 for(int i=0; i < size; i++) { 00914 //Get a random number in range [i, size] 00915 ran_num = (int)(((float) rand() / RAND_MAX) * (size -1 - i)) + i; 00916 swap(ordering[i],ordering[ran_num]); 00917 } 00918 } 00919 00920 string toUpper(string input) { 00921 string output = input; 00922 00923 for(int i = input.size() - 1; i>=0; i--) { 00924 if(input[i]==' ' || input[i]=='\t' || input[i]=='\n') { 00925 output[i] = '_'; 00926 } 00927 else { 00928 output[i] = toupper(input[i]); 00929 } 00930 } 00931 00932 return output; 00933 } 00934 00935 //just manipulate the value of dp2_Values a little bit 00936 int Times2Plus1point5(double** dp2_Values, int i_RowCount, int i_ColumnCount) { 00937 for(int i=0; i < i_RowCount; i++) { 00938 for(int j=0; j < i_ColumnCount; j++) { 00939 if(dp2_Values[i][j] != 0.) dp2_Values[i][j] = dp2_Values[i][j]*2 + 1.5; //for each non-zero entry in the matrix, do the manipulation. 00940 } 00941 00942 } 00943 return 0; 00944 } 00945 int Times2(double** dp2_Values, int i_RowCount, int i_ColumnCount) { 00946 for(int i=0; i < i_RowCount; i++) { 00947 for(int j=0; j < i_ColumnCount; j++) { 00948 if(dp2_Values[i][j] != 0.) dp2_Values[i][j] = dp2_Values[i][j]*2; 00949 } 00950 00951 } 00952 return 0; 00953 } 00954 00955 int GenerateValues(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value) { 00956 //srand(time(NULL)); 00957 srand(0); 00958 00959 (*dp3_Value) = new double*[rowCount]; 00960 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 00961 unsigned int numOfNonZeros = uip2_SparsityPattern[i][0]; 00962 (*dp3_Value)[i] = new double[numOfNonZeros + 1]; 00963 (*dp3_Value)[i][0] = (double)numOfNonZeros; 00964 for(unsigned int j=1; j <= numOfNonZeros; j++) { 00965 (*dp3_Value)[i][j] = (rand()%2001 - 1000)/1000.0; 00966 //printf("(*dp3_Value)[%d][%d] = (%d % 2001 - 1000)/1000.0 = %7.2f \n",i,j,rand(),(*dp3_Value)[i][j]); 00967 } 00968 } 00969 00970 return 0; 00971 } 00972 00973 int GenerateValuesForSymmetricMatrix(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value) { 00974 //srand(time(NULL)); 00975 srand(0); 00976 00977 int * nnzCount = new int[rowCount]; // keep track of the # of non-zeros in each row 00978 for(unsigned int i=0; i < (unsigned int)rowCount; i++) nnzCount[i] = 0; 00979 00980 (*dp3_Value) = new double*[rowCount]; 00981 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 00982 unsigned int numOfNonZeros = uip2_SparsityPattern[i][0]; 00983 (*dp3_Value)[i] = new double[numOfNonZeros + 1]; 00984 (*dp3_Value)[i][0] = (double)numOfNonZeros; 00985 for(unsigned int j=1; j <= numOfNonZeros; j++) { 00986 if (uip2_SparsityPattern[i][j] >i) break; 00987 (*dp3_Value)[i][j] = (rand()%2001 - 1000)/1000.0; nnzCount[i]++; 00988 if (uip2_SparsityPattern[i][j] <i) { // copy the value from the low triangular to the upper triangular 00989 (*dp3_Value)[uip2_SparsityPattern[i][j]][nnzCount[uip2_SparsityPattern[i][j]]+1] = (*dp3_Value)[i][j]; nnzCount[uip2_SparsityPattern[i][j]]++; 00990 } 00991 //printf("(*dp3_Value)[%d][%d] = (%d % 2001 - 1000)/1000.0 = %7.2f \n",i,j,rand(),(*dp3_Value)[i][j]); 00992 } 00993 } 00994 00995 delete[] nnzCount; 00996 00997 return 0; 00998 } 00999 01000 int ConvertRowCompressedFormat2ADIC(unsigned int ** uip2_SparsityPattern_RowCompressedFormat, int i_rowCount , double** dp2_Value, std::list<std::set<int> > &lsi_valsetlist, std::list<std::vector<double> > &lvd_Value) { 01001 for(int i=0; i<i_rowCount; i++) { 01002 std::set<int> valset; 01003 std::vector<double> valuevector; 01004 valuevector.reserve(uip2_SparsityPattern_RowCompressedFormat[i][0]); 01005 for(int j= 1; j <= uip2_SparsityPattern_RowCompressedFormat[i][0]; j++) { 01006 valset.insert(uip2_SparsityPattern_RowCompressedFormat[i][j]); 01007 valuevector.push_back(dp2_Value[i][j]); 01008 } 01009 (lsi_valsetlist).push_back(valset); 01010 (lvd_Value).push_back(valuevector); 01011 } 01012 01013 return 0; 01014 } 01015 01016 int ConvertRowCompressedFormat2CSR(unsigned int ** uip2_SparsityPattern_RowCompressedFormat, int i_rowCount, int** ip_RowIndex, int** ip_ColumnIndex) { 01017 (*ip_RowIndex) = new int[i_rowCount+1]; 01018 int nnz = 0; 01019 for(int i=0; i < i_rowCount; i++) { 01020 (*ip_RowIndex)[i] = nnz; 01021 nnz += uip2_SparsityPattern_RowCompressedFormat[i][0]; 01022 01023 //cout<<"Display *ip_RowIndex"<<endl; 01024 //displayVector(*ip_RowIndex,i_rowCount+1); 01025 01026 } 01027 (*ip_RowIndex)[i_rowCount] = nnz; 01028 01029 (*ip_ColumnIndex) = new int[nnz]; 01030 int nz_count=0; 01031 for(int i=0; i < i_rowCount; i++) { 01032 for(int j=1; j<= uip2_SparsityPattern_RowCompressedFormat[i][0];j++) { 01033 (*ip_ColumnIndex)[nz_count] = uip2_SparsityPattern_RowCompressedFormat[i][j]; 01034 nz_count++; 01035 } 01036 //cout<<"Display *ip_ColumnIndex"<<endl; 01037 //displayVector(*ip_ColumnIndex, (*ip_RowIndex)[i_rowCount]); 01038 } 01039 01040 if(nz_count != nnz) { 01041 cerr<<"IN ConvertRowCompressedFormat2CSR, nz_count ("<<nz_count<<") != nnz ("<<nnz<<")"<<endl; 01042 } 01043 return 0; 01044 } 01045 01046 int ConvertMatrixMarketFormat2RowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, double*** dp3_Value, int &rowCount, int &columnCount) { 01047 01048 string m_s_InputFile=s_InputFile; 01049 01050 //initialize local data 01051 int rowCounter=0, nonzeros=0, rowIndex=0, colIndex=0, nz_counter=0, entries=0; 01052 //int num=0, numCount=0; 01053 float value; 01054 bool b_getValue, b_symmetric; 01055 istringstream in2; 01056 string line=""; 01057 map<int,vector<int> > nodeList; 01058 map<int,vector<float> > valueList; 01059 01060 //READ IN BANNER 01061 MM_typecode matcode; 01062 FILE *f; 01063 if ((f = fopen(m_s_InputFile.c_str(), "r")) == NULL) { 01064 cout<<m_s_InputFile<<" not Found!"<<endl; 01065 exit(1); 01066 } 01067 else cout<<"Found file "<<m_s_InputFile<<endl; 01068 01069 if (mm_read_banner(f, &matcode) != 0) 01070 { 01071 printf("Could not process Matrix Market banner.\n"); 01072 exit(1); 01073 } 01074 01075 if( mm_is_pattern(matcode) ) { 01076 b_getValue = false; 01077 } 01078 else b_getValue = true; 01079 01080 if(mm_is_symmetric(matcode)) { 01081 b_symmetric = true; 01082 } 01083 else b_symmetric = false; 01084 01085 //Check and make sure that the input file is supported 01086 char * result = mm_typecode_to_str(matcode); 01087 printf("Graph of Market Market type: [%s]\n", result); 01088 free(result); 01089 if (b_getValue) printf("\t Graph structure and VALUES will be read\n"); 01090 else { 01091 printf("\t Read graph struture only. Values will NOT be read. dp3_Value will NOT be allocated memory, so don't try to use it!!!\n"); 01092 Pause(); 01093 } 01094 if( !( mm_is_coordinate(matcode) && (mm_is_symmetric(matcode) || mm_is_general(matcode) ) && ( mm_is_real(matcode) || mm_is_pattern(matcode) || mm_is_integer(matcode) ) ) ) { 01095 printf("Sorry, this application does not support this type."); 01096 exit(1); 01097 } 01098 01099 fclose(f); 01100 //DONE - READ IN BANNER 01101 01102 // FIND OUT THE SIZE OF THE MATRIX 01103 ifstream in (m_s_InputFile.c_str()); 01104 if(!in) { 01105 cout<<m_s_InputFile<<" not Found!"<<endl; 01106 exit(1); 01107 } 01108 else { 01109 //cout<<"Found file "<<m_s_InputFile<<endl; 01110 } 01111 01112 getline(in,line); 01113 rowCounter++; 01114 while(line.size()>0&&line[0]=='%') {//ignore comment line 01115 getline(in,line); 01116 } 01117 in2.str(line); 01118 in2 >> rowCount >> columnCount >> entries; 01119 //cout<<"rowCount="<<rowCount<<"; columnCount="<<columnCount<<"; nonzeros="<<nonzeros<<endl; 01120 // DONE - FIND OUT THE SIZE OF THE MATRIX 01121 01122 while(!in.eof() && rowCounter<=entries) //there should be (nonzeros+1) lines in the input file 01123 { 01124 getline(in,line); 01125 if(line!="") 01126 { 01127 rowCounter++; 01128 //cout<<"Line "<<rowCounter<<"="<<line<<endl; 01129 01130 in2.clear(); 01131 in2.str(line); 01132 in2>>rowIndex>>colIndex; 01133 rowIndex--; 01134 colIndex--; 01135 01136 if(b_symmetric) { 01137 if(rowIndex > colIndex) { 01138 01139 //cout<<"\t"<<setw(4)<<rowIndex<<setw(4)<<colIndex<<setw(4)<<nz_counter<<endl; 01140 nodeList[rowIndex].push_back(colIndex); 01141 nodeList[colIndex].push_back(rowIndex); 01142 nz_counter += 2; 01143 01144 if(b_getValue) { 01145 in2>>value; 01146 //cout<<"Value = "<<value<<endl; 01147 valueList[rowIndex].push_back(value); 01148 valueList[colIndex].push_back(value); 01149 } 01150 } 01151 else if (rowIndex == colIndex) { 01152 //cout<<"\t"<<setw(4)<<rowIndex<<setw(4)<<colIndex<<setw(4)<<nz_counter<<endl; 01153 nodeList[rowIndex].push_back(rowIndex); 01154 nz_counter++; 01155 if(b_getValue) { 01156 in2>>value; 01157 valueList[rowIndex].push_back(value); 01158 } 01159 } 01160 else { //rowIndex < colIndex 01161 cerr<<"* WARNING: ConvertMatrixMarketFormatToRowCompressedFormat()"<<endl; 01162 cerr<<"\t Found a nonzero in the upper triangular. A symmetric Matrix Market file format should only specify the nonzeros in the lower triangular."<<endl; 01163 exit( -1); 01164 } 01165 } 01166 else { // !b_symmetric 01167 //cout<<"\t"<<setw(4)<<rowIndex<<setw(4)<<colIndex<<setw(4)<<nz_counter<<endl; 01168 nodeList[rowIndex].push_back(colIndex); 01169 nz_counter++; 01170 if(b_getValue) { 01171 in2>>value; 01172 //cout<<"Value = "<<value<<endl; 01173 valueList[rowIndex].push_back(value); 01174 } 01175 } 01176 01177 } 01178 else 01179 { 01180 cerr<<"* WARNING: ConvertMatrixMarketFormatToRowCompressedFormat()"<<endl; 01181 cerr<<"*\t line == \"\" at row "<<rowCounter<<". Empty line. Wrong input format. Can't process."<<endl; 01182 cerr<<"\t total non-zeros so far: "<<nz_counter<<endl; 01183 exit( -1); 01184 } 01185 } 01186 01187 01188 (*uip3_SparsityPattern) = new unsigned int*[rowCount]; 01189 if(b_getValue) (*dp3_Value) = new double*[rowCount]; 01190 for(int i=0;i<rowCount; i++) { 01191 unsigned int numOfNonZeros = nodeList[i].size(); 01192 //printf("row = %d \t numOfNonZeros = %d : ", i, (int)numOfNonZeros); 01193 01194 //Allocate memory for each row 01195 (*uip3_SparsityPattern)[i] = new unsigned int[numOfNonZeros+1]; 01196 (*uip3_SparsityPattern)[i][0] = numOfNonZeros; 01197 01198 if(b_getValue) { 01199 (*dp3_Value)[i] = new double[numOfNonZeros+1]; 01200 (*dp3_Value)[i][0] = (double)numOfNonZeros; 01201 } 01202 01203 for(unsigned int j=0; j < numOfNonZeros; j++) { 01204 (*uip3_SparsityPattern)[i][j+1] = nodeList[i][j]; 01205 //printf("\t %d", (int) nodeList[i][j]); 01206 } 01207 01208 if(b_getValue) for(unsigned int j=0; j < numOfNonZeros; j++) { 01209 (*dp3_Value)[i][j+1] = valueList[i][j]; 01210 } 01211 //printf("\n"); 01212 } 01213 01214 01215 return(0); 01216 } 01217 01218 int MatrixMultiplication_VxS__usingVertexPartialColors(std::list<std::set<int> > &lsi_SparsityPattern, std::list<std::vector<double> > &lvd_Value, int columnCount, vector<int> &vi_VertexPartialColors, int colorCount, double*** dp3_CompressedMatrix) { 01219 unsigned int rowCount = lsi_SparsityPattern.size(); 01220 01221 //Allocate memory for (*dp3_CompressedMatrix)[rowCount][colorCount] 01222 //cout<<"Allocate memory for (*dp3_CompressedMatrix)[rowCount][colorCount]"<<endl; 01223 (*dp3_CompressedMatrix) = new double*[rowCount]; 01224 for(unsigned int i=0; i < rowCount; i++) { 01225 (*dp3_CompressedMatrix)[i] = new double[colorCount]; 01226 for(unsigned int j=0; j < (unsigned int)colorCount; j++) { 01227 (*dp3_CompressedMatrix)[i][j] = 0.; 01228 } 01229 } 01230 01231 //do the multiplication 01232 //cout<<"Do the multiplication"<<endl; 01233 std::list<std::set<int> >::iterator valsetlistiter = lsi_SparsityPattern.begin(); 01234 std::list<std::vector<double> >::iterator valuelistlistiter = lvd_Value.begin(); 01235 for (unsigned int i=0; i< rowCount; valsetlistiter++, valuelistlistiter++, i++){ 01236 unsigned int numOfNonZeros = (*valsetlistiter).size(); 01237 std::set<int>::iterator valsetiter = (*valsetlistiter).begin(); 01238 for(unsigned int j=0; j < numOfNonZeros; valsetiter++, j++) { 01239 (*dp3_CompressedMatrix)[i][vi_VertexPartialColors[*valsetiter] ] += (*valuelistlistiter)[j]; 01240 } 01241 } 01242 01243 return 0; 01244 } 01245 01246 int MatrixMultiplication_VxS(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix) { 01247 01248 //Allocate memory for (*dp3_CompressedMatrix)[rowCount][colorCount] 01249 #if DEBUG == 2 01250 cout<<"Allocate memory for (*dp3_CompressedMatrix)[rowCount][colorCount]"<<endl; 01251 #endif 01252 (*dp3_CompressedMatrix) = new double*[rowCount]; 01253 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 01254 (*dp3_CompressedMatrix)[i] = new double[colorCount]; 01255 for(unsigned int j=0; j < (unsigned int)colorCount; j++) { 01256 (*dp3_CompressedMatrix)[i][j] = 0.; 01257 } 01258 } 01259 01260 //do the multiplication 01261 #if DEBUG == 2 01262 cout<<"Do the multiplication"<<endl; 01263 Pause(); 01264 #endif 01265 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 01266 unsigned int numOfNonZeros = uip3_SparsityPattern[i][0]; 01267 for(unsigned int j=1; j <= numOfNonZeros; j++) { 01268 for(unsigned int k=0; k < (unsigned int)colorCount; k++) { 01269 #if DEBUG == 2 01270 printf("i=%d\tj=%d\tuip3_SparsityPattern[i][j]=%d\tk=%d\n", i, j, uip3_SparsityPattern[i][j], k); 01271 cout<<"\trowCount="<<rowCount<<"; numOfNonZeros="<<numOfNonZeros<<"; colorCount="<<colorCount<<endl; 01272 if(i==256 && j==1 && k==0) { 01273 cout<<"blah"<<endl; 01274 } 01275 #endif 01276 (*dp3_CompressedMatrix)[i][k] += dp3_Value[i][j]*dp2_seed[uip3_SparsityPattern[i][j]][k]; 01277 } 01278 } 01279 } 01280 01281 return 0; 01282 } 01283 01284 int MatrixMultiplication_SxV(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix) { 01285 01286 //Allocate memory for (*dp3_CompressedMatrix)[colorCount][columnCount] 01287 //cout<<"Allocate memory for (*dp3_CompressedMatrix)[colorCount][columnCount]"<<endl; 01288 (*dp3_CompressedMatrix) = new double*[colorCount]; 01289 for(unsigned int i=0; i < (unsigned int)colorCount; i++) { 01290 (*dp3_CompressedMatrix)[i] = new double[columnCount]; 01291 for(unsigned int j=0; j < (unsigned int)columnCount; j++) { 01292 (*dp3_CompressedMatrix)[i][j] = 0.; 01293 } 01294 } 01295 01296 //do the multiplication 01297 //cout<<"Do the multiplication"<<endl; 01298 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 01299 unsigned int numOfNonZeros = uip3_SparsityPattern[i][0]; 01300 for(unsigned int j=1; j <= numOfNonZeros; j++) { 01301 for(unsigned int k=0; k < (unsigned int)colorCount; k++) { 01302 //printf("i=%d\tj=%d\tuip3_SparsityPattern[i][j]=%d\tk=%d\n", i, j, uip3_SparsityPattern[i][j], k); 01303 (*dp3_CompressedMatrix)[k][uip3_SparsityPattern[i][j]] += dp2_seed[k][i]*dp3_Value[i][j]; 01304 } 01305 } 01306 } 01307 01308 return 0; 01309 } 01310 bool ADICMatricesAreEqual(std::list<std::vector<double> >& lvd_Value, std::list<std::vector<double> >& lvd_NewValue, bool compare_exact, bool print_all) { 01311 double ratio = 1.; 01312 int none_equal_count = 0; 01313 int rowCount = lvd_Value.size(); 01314 std::list<std::vector<double> >::iterator lvdi_Value = lvd_Value.begin(), lvdi_NewValue = lvd_NewValue.begin() ; 01315 01316 for(unsigned int i=0; i < (unsigned int)rowCount; lvdi_Value++, lvdi_NewValue++, i++) { 01317 unsigned int numOfNonZeros = (unsigned int)(*lvdi_Value).size(); 01318 if (numOfNonZeros != (unsigned int)(*lvdi_NewValue).size()) { 01319 printf("Number of non-zeros in row %d are not equal. (*lvdi_Value).size() = %d; (*lvdi_NewValue).size() = %d; \n",i,(unsigned int)(*lvdi_Value).size(),(unsigned int)(*lvdi_NewValue).size()); 01320 if (print_all) { 01321 none_equal_count++; 01322 continue; 01323 } 01324 else return false; 01325 } 01326 for(unsigned int j=0; j < numOfNonZeros; j++) { 01327 if (compare_exact) { 01328 if ((*lvdi_Value)[j] != (*lvdi_NewValue)[j]) { 01329 printf("At row %d, column %d, (*lvdi_Value)[j](%f) != (*lvdi_NewValue)[j](%f) \n",i,j,(*lvdi_Value)[j],(*lvdi_NewValue)[j]); 01330 if (print_all) { 01331 none_equal_count++; 01332 } 01333 else { 01334 printf("You may want to set the flag \"compare_exact\" to 0 to compare the values approximately\n"); 01335 return false; 01336 } 01337 } 01338 } 01339 else { 01340 if((*lvdi_NewValue)[j] == 0.) { 01341 if((*lvdi_Value)[j] != 0.) { 01342 printf("At row %d, column %d, (*lvdi_Value)[j](%f) != (*lvdi_NewValue)[j](0) \n",i,j,(*lvdi_Value)[j]); 01343 if (print_all) { 01344 none_equal_count++; 01345 } 01346 else return false; 01347 } 01348 } 01349 else { 01350 ratio = (*lvdi_Value)[j] / (*lvdi_NewValue)[j]; 01351 if( ratio < .99 || ratio > 1.02) { 01352 printf("At row %d, column %d, (*lvdi_Value)[j](%f) != (*lvdi_NewValue)[j](%f) ; (*lvdi_Value)[j] / (*lvdi_NewValue)[j]=%f\n",i,j,(*lvdi_Value)[j],(*lvdi_NewValue)[j], ratio); 01353 if (print_all) { 01354 none_equal_count++; 01355 } 01356 else return false; 01357 } 01358 } 01359 } 01360 } 01361 } 01362 01363 if(none_equal_count!=0) { 01364 printf("Total: %d lines. (The total # of non-equals can be greater)\n",none_equal_count); 01365 if (compare_exact) printf("You may want to set the flag \"compare_exact\" to 0 to compare the values approximately\n"); 01366 return false; 01367 } 01368 else return true; 01369 } 01370 01371 bool CompressedRowMatricesAreEqual(double** dp3_Value, double** dp3_NewValue, int rowCount, bool compare_exact, bool print_all) { 01372 double ratio = 1.; 01373 int none_equal_count = 0; 01374 01375 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 01376 unsigned int numOfNonZeros = (unsigned int)dp3_Value[i][0]; 01377 if (numOfNonZeros != (unsigned int)dp3_NewValue[i][0]) { 01378 printf("Number of non-zeros in row %d are not equal. dp3_Value[i][0] = %d; dp3_NewValue[i][0] = %d; \n",i,(unsigned int)dp3_Value[i][0],(unsigned int)dp3_NewValue[i][0]); 01379 if (print_all) { 01380 none_equal_count++; 01381 continue; 01382 } 01383 else return false; 01384 } 01385 for(unsigned int j=0; j <= numOfNonZeros; j++) { 01386 if (compare_exact) { 01387 if (dp3_Value[i][j] != dp3_NewValue[i][j]) { 01388 printf("At row %d, column %d, dp3_Value[i][j](%f) != dp3_NewValue[i][j](%f) \n",i,j,dp3_Value[i][j],dp3_NewValue[i][j]); 01389 if (print_all) { 01390 none_equal_count++; 01391 } 01392 else { 01393 printf("You may want to set the flag \"compare_exact\" to 0 to compare the values approximately\n"); 01394 return false; 01395 } 01396 } 01397 } 01398 else { 01399 if(dp3_NewValue[i][j] == 0.) { 01400 if(fabs(dp3_Value[i][j]) > 1e-10) { 01401 printf("At row %d, column %d, dp3_Value[i][j](%f) != dp3_NewValue[i][j](0) \n",i,j,dp3_Value[i][j]); 01402 cout<<scientific<<" dp3_Value="<< dp3_Value[i][j] <<endl; 01403 if (print_all) { 01404 none_equal_count++; 01405 } 01406 else return false; 01407 } 01408 } 01409 else { 01410 ratio = fabs(dp3_Value[i][j]) / fabs(dp3_NewValue[i][j]); 01411 if( fabs(dp3_Value[i][j]) > 1e-10 && (ratio < .99 || ratio > 1.02) ) { 01412 printf("At row %d, column %d, dp3_Value[i][j](%f) != dp3_NewValue[i][j](%f) ; dp3_Value[i][j] / dp3_NewValue[i][j]=%f\n",i,j,dp3_Value[i][j],dp3_NewValue[i][j], ratio); 01413 cout<<scientific<<" dp3_Value="<< dp3_Value[i][j] <<", dp3_NewValue="<< dp3_NewValue[i][j] <<endl; 01414 if (print_all) { 01415 none_equal_count++; 01416 } 01417 else return false; 01418 } 01419 } 01420 } 01421 } 01422 } 01423 01424 if(none_equal_count!=0) { 01425 printf("Total: %d lines. (The total # of non-equals can be greater)\n",none_equal_count); 01426 if (compare_exact) printf("You may want to set the flag \"compare_exact\" to 0 to compare the values approximately\n"); 01427 return false; 01428 } 01429 else return true; 01430 } 01431 01432 int DisplayADICFormat_Sparsity(std::list<std::set<int> > &lsi_valsetlist) { 01433 int size = (lsi_valsetlist).size(); 01434 int rowIndex=-1, colIndex=-1; 01435 std::list<std::set<int> >::iterator valsetlistiter = (lsi_valsetlist).begin(); 01436 01437 unsigned int estimateColumnCount = 20; 01438 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"] "; 01439 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(4)<<j; 01440 cout<<endl; 01441 01442 for (; valsetlistiter != (lsi_valsetlist).end(); valsetlistiter++){ 01443 rowIndex++; 01444 std::set<int>::iterator valsetiter = (*valsetlistiter).begin(); 01445 cout<<setw(4)<<"["<<setw(3)<<rowIndex<<"]"; 01446 cout<<" ("<<setw(3)<<(*valsetlistiter).size()<<")"; 01447 for (; valsetiter != (*valsetlistiter).end() ; valsetiter++) { 01448 colIndex = *valsetiter; 01449 cout<<setw(4)<<colIndex; 01450 } 01451 cout<<endl<<flush; 01452 } 01453 cout<<endl<<endl; 01454 01455 return 0; 01456 } 01457 01458 int DisplayADICFormat_Value(std::list<std::vector<double> > &lvd_Value) { 01459 int size = (lvd_Value).size(); 01460 int rowIndex=-1; 01461 double value=0.; 01462 std::list<std::vector<double> >::iterator valsetlistiter = (lvd_Value).begin(); 01463 01464 unsigned int estimateColumnCount = 20; 01465 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"] "; 01466 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j; 01467 cout<<endl; 01468 01469 for (; valsetlistiter != (lvd_Value).end(); valsetlistiter++){ 01470 rowIndex++; 01471 std::vector<double>::iterator valsetiter = (*valsetlistiter).begin(); 01472 cout<<setw(4)<<"["<<setw(3)<<rowIndex<<"]"; 01473 cout<<" ("<<setw(3)<<(*valsetlistiter).size()<<")"; 01474 for (; valsetiter != (*valsetlistiter).end() ; valsetiter++) { 01475 value = *valsetiter; 01476 cout<<setw(9)<<value; 01477 } 01478 cout<<endl<<flush; 01479 } 01480 cout<<endl<<endl; 01481 01482 return 0; 01483 } 01484 01485 #endif 01486