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_H 00022 #define EXTRA_H 00023 00024 #include <iostream> 00025 #include <fstream> 00026 #include <sstream> 00027 #include <string> 00028 #include <iomanip> 00029 #include <ctime> 00030 #include <cstdlib> 00031 #include <stdarg.h> // support for variadic functions 00032 //#include <cctype> //for toupper() 00033 00034 #include <list> 00035 #include <set> 00036 #include <map> 00037 #include <string> 00038 #include <vector> 00039 00040 #include "ColPackHeaders.h" 00041 00042 using namespace std; 00043 00044 /* 00045 #include "Definitions.h" 00046 #include "Pause.h" 00047 */ 00048 00049 00050 //Definition for dot 00051 #define DOT 1 00052 #define NEATO 2 00053 #define TWOPI 3 00054 #define CIRCO 4 00055 #define FDP 5 00056 00058 00069 int WriteMatrixMarket_ADOLCInput(string s_postfix, int i_mode, ...); 00070 00072 00080 int ConvertHarwellBoeingDouble(string & num_string); 00081 00082 string itoa(int i); 00083 00084 vector<string> getListOfColors(string s_InputFile); 00085 00086 int buildDotWithoutColor(ColPack::GraphColoringInterface &g, vector<string> &ListOfColors, string fileName); 00087 00089 00092 int buildDotWithColor(ColPack::GraphColoringInterface &g, vector<string> &ListOfColors, string fileName); 00093 00094 int buildDotWithoutColor(ColPack::BipartiteGraphPartialColoringInterface &g, vector<string> &ListOfColors, string fileName); 00095 // !!! TODO: enable conflict detection 00096 int buildDotWithColor(ColPack::BipartiteGraphPartialColoringInterface &g, vector<string> &ListOfColors, string fileName); 00097 00098 // !!! TO BE BUILT 00099 int buildDotWithoutColor(ColPack::BipartiteGraphBicoloringInterface &g, vector<string> &ListOfColors, string fileName); 00100 // !!! TO BE BUILT 00101 int buildDotWithColor(ColPack::BipartiteGraphBicoloringInterface &g, vector<string> &ListOfColors, string fileName); 00102 00103 00105 00109 int ReadRowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, int& rowCount, int& columnCount); 00110 00112 00116 bool isValidOrdering(vector<int> & ordering, int offset = 0); 00117 00118 //Re-order the values randomly 00119 void randomOrdering(vector<int>& ordering); 00120 00122 string toUpper(string input); 00123 00125 00138 int ConvertRowCompressedFormat2SparseSolversFormat_StructureOnly(unsigned int ** uip2_HessianSparsityPattern, unsigned int ui_rowCount, unsigned int** ip2_RowIndex, unsigned int** ip2_ColumnIndex); 00139 00141 00156 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 ); 00157 00158 00160 00167 void ConvertFileDIMACSFormat2MatrixMarketFormat(string fileNameNoExt); 00168 00170 00174 int ConvertMatrixMarketFormat2RowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, double*** dp3_Value, int &rowCount, int &columnCount); 00175 00176 /* !!! the documentation here may not be accurate 00177 "zero-based indexing, 3-array variation CSR format (used by ADIC)" 00178 Does ADIC use zero-based indexing, 3-array variation CSR format any more? 00179 //*/ 00181 00184 // !!! need to be fixed to accomodate dp2_Value parameter 00185 int ConvertRowCompressedFormat2CSR(unsigned int ** uip2_SparsityPattern_RowCompressedFormat, int i_rowCount, int** ip_RowIndex, int** ip_ColumnIndex); 00186 00187 int ConvertRowCompressedFormat2ADIC(unsigned int ** uip2_SparsityPattern_RowCompressedFormat, int i_rowCount , double** dp2_Value, std::list<std::set<int> > &lsi_SparsityPattern, std::list<std::vector<double> > &lvd_Value); 00188 00190 00192 int MatrixMultiplication_VxS(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix); 00193 00194 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); 00195 00197 00199 int MatrixMultiplication_SxV(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix); 00200 00202 00206 bool CompressedRowMatricesAreEqual(double** dp3_Value, double** dp3_NewValue, int rowCount, bool compare_exact = 1, bool print_all = 0); 00207 00208 bool ADICMatricesAreEqual(std::list<std::vector<double> >& lvd_Value, std::list<std::vector<double> >& lvd_NewValue, bool compare_exact = 1, bool print_all = 0); 00209 00211 int Times2Plus1point5(double** dp2_Values, int i_RowCount, int i_ColumnCount); 00212 00214 int Times2(double** dp2_Values, int i_RowCount, int i_ColumnCount); 00215 00217 int GenerateValues(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value); 00218 00220 int GenerateValuesForSymmetricMatrix(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value); 00221 00222 int DisplayADICFormat_Sparsity(std::list<std::set<int> > &lsi_SparsityPattern); 00223 int DisplayADICFormat_Value(std::list<std::vector<double> > &lvd_Value); 00224 00225 int displayGraph(map< int, map<int,bool> > *graph, vector<int>* vi_VertexColors=NULL,int i_RunInBackground = false, int filter = DOT); 00226 int buildDotWithoutColor(map< int, map<int,bool> > *graph, vector<string> &ListOfColors, string fileName); 00227 int buildDotWithColor(map< int, map<int,bool> > *graph, vector<int>* vi_VertexColors, vector<string> &ListOfColors, string fileName); 00228 00229 #ifndef EXTRA_H_TEMPLATE_FUNCTIONS 00230 #define EXTRA_H_TEMPLATE_FUNCTIONS 00231 00232 template<class T> 00233 int displayGraph(T &g,int i_RunInBackground = false, int filter = DOT) { 00234 static int ranNum = rand(); 00235 static int seq = 0; 00236 seq++; 00237 vector<string> ListOfColors = getListOfColors(""); 00238 string fileName = "/tmp/."; 00239 fileName = fileName + "ColPack_"+ itoa(ranNum)+"_"+itoa(seq)+".dot"; 00240 00241 //build the dot file of the graph 00242 string m_s_VertexColoringVariant = g.GetVertexColoringVariant(); 00243 if(m_s_VertexColoringVariant.empty() || m_s_VertexColoringVariant=="Unknown") { 00244 //build dot file represents graph without color info 00245 buildDotWithoutColor(g, ListOfColors, fileName); 00246 } else { 00247 //build dot file represents graph with color 00248 buildDotWithColor(g, ListOfColors, fileName); 00249 } 00250 00251 //display the graph using xdot 00252 string command; 00253 switch (filter) { 00254 case NEATO: command="xdot -f neato "; break; 00255 case TWOPI: command="xdot -f twopi "; break; 00256 case CIRCO: command="xdot -f circo "; break; 00257 case FDP: command="xdot -f fdp "; break; 00258 default: command="xdot -f dot "; // case DOT 00259 } 00260 00261 command = command + fileName; 00262 if(i_RunInBackground) command = command + " &"; 00263 int i_ReturnValue = system(command.c_str()); 00264 return i_ReturnValue; 00265 } 00266 00267 00269 template<class T> 00270 int diffArrays(T* array1, T* array2, int rowCount, bool compare_exact = 1, bool print_all = 0) { 00271 double ratio = 0.; 00272 int none_equal_count = 0; 00273 for(int i = 0; i < rowCount; i++) { 00274 if (compare_exact) { 00275 if(array1[i]!=array2[i]) { // found a difference 00276 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<";\t array2[] = "<<array2[i]<<endl; 00277 none_equal_count++; 00278 if(!print_all) return 1; 00279 } 00280 } 00281 else { 00282 ratio = array1[i] / array2[i]; 00283 if(ratio < .99 || ratio > 1.02) { // found a difference 00284 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<";\t array2[] = "<<array2[i]<<endl; 00285 none_equal_count++; 00286 if(!print_all) return 1; 00287 } 00288 } 00289 } 00290 00291 return none_equal_count; 00292 } 00293 00295 template<class T> 00296 int diffVectors(vector<T> array1, vector<T> array2, bool compare_exact = 1, bool print_all = 0) { 00297 double ratio = 0.; 00298 int none_equal_count = 0; 00299 00300 if(array1.size() != array2.size()) { 00301 cout<<"array1.size() "<<array1.size()<<" != array2.size()"<<array2.size()<<endl; 00302 none_equal_count++; 00303 } 00304 00305 int min_array_size = (array1.size() < array2.size())?array1.size():array2.size(); 00306 00307 for(int i = 0; i < min_array_size; i++) { 00308 if (compare_exact) { 00309 if(array1[i]!=array2[i]) { // found a difference 00310 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<<";\t array2[] = "<<array2[i]<<endl; 00311 none_equal_count++; 00312 if(!print_all) return none_equal_count; 00313 } 00314 } 00315 else { 00316 ratio = array1[i] / array2[i]; 00317 if(ratio < .99 || ratio > 1.02) { // found a difference 00318 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<<";\t array2[] = "<<array2[i]<<endl; 00319 none_equal_count++; 00320 if(!print_all) return none_equal_count; 00321 } 00322 } 00323 } 00324 00325 return none_equal_count; 00326 } 00327 00328 template<class T> 00329 int freeMatrix(T** xp2_matrix, int rowCount) { 00330 //cout<<"IN deleteM 2"<<endl<<flush; 00331 //printf("* deleteMatrix rowCount=%d \n",rowCount); 00332 //Pause(); 00333 for(int i = 0; i < rowCount; i++) { 00334 //printf("delete xp2_matrix[%d][0] = %7.2f \n", i, (float) xp2_matrix[i][0]); 00335 free( xp2_matrix[i]); 00336 } 00337 //cout<<"MID deleteM 2"<<endl<<flush; 00338 free( xp2_matrix); 00339 //cout<<"OUT deleteM 2"<<endl<<flush; 00340 return 0; 00341 } 00342 00343 template<class T> 00344 int freeMatrix(T*** xp3_matrix, int rowCount) { 00345 //cout<<"IN deleteM 3"<<endl<<flush; 00346 freeMatrix(*xp3_matrix,rowCount); 00347 //cout<<"MID deleteM 3"<<endl<<flush; 00348 free( xp3_matrix); 00349 //cout<<"OUT deleteM 3"<<endl<<flush; 00350 return 0; 00351 } 00352 00353 template<class T> 00354 int deleteMatrix(T** xp2_matrix, int rowCount) { 00355 //cout<<"IN deleteM 2"<<endl<<flush; 00356 //printf("* deleteMatrix rowCount=%d \n",rowCount); 00357 //Pause(); 00358 for(int i = 0; i < rowCount; i++) { 00359 //printf("delete xp2_matrix[%d][0] = %7.2f \n", i, (float) xp2_matrix[i][0]); 00360 delete xp2_matrix[i]; 00361 } 00362 //cout<<"MID deleteM 2"<<endl<<flush; 00363 delete xp2_matrix; 00364 //cout<<"OUT deleteM 2"<<endl<<flush; 00365 return 0; 00366 } 00367 00368 template<class T> 00369 int deleteMatrix(T*** xp3_matrix, int rowCount) { 00370 //cout<<"IN deleteM 3"<<endl<<flush; 00371 deleteMatrix(*xp3_matrix,rowCount); 00372 //cout<<"MID deleteM 3"<<endl<<flush; 00373 delete xp3_matrix; 00374 //cout<<"OUT deleteM 3"<<endl<<flush; 00375 return 0; 00376 } 00377 00378 template<class T> 00379 void displayCompressedRowMatrix(T** xp2_Value, int rowCount, bool structureOnly = false) { 00380 unsigned int estimateColumnCount = 20; 00381 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"] "; 00382 if(structureOnly) { 00383 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(4)<<j; 00384 } 00385 else { 00386 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j; 00387 } 00388 cout<<endl; 00389 00390 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 00391 cout<<setw(4)<<"["<<setw(3)<<i<<"]"; 00392 unsigned int numOfNonZeros = (unsigned int)xp2_Value[i][0]; 00393 cout<<" ("<<setw(3)<<numOfNonZeros<<")"; 00394 if(structureOnly) { 00395 for(unsigned int j=1; j <= numOfNonZeros; j++) cout<<setw(4)<<(int)xp2_Value[i][j]; 00396 //for(unsigned int j=1; j <= numOfNonZeros; j++) { 00397 // printf(" %d",(int)xp2_Value[i][j]); 00398 //} 00399 } 00400 else { 00401 for(unsigned int j=1; j <= numOfNonZeros; j++) cout<<setw(9)<<(float)xp2_Value[i][j]; 00402 //for(unsigned int j=1; j <= numOfNonZeros; j++) { 00403 // printf(" %7.2f",(float)xp2_Value[i][j]); 00404 //} 00405 } 00406 cout<<endl<<flush; 00407 } 00408 cout<<endl<<endl; 00409 } 00410 00411 template<class T> 00412 void displayMatrix(T** xp2_Value, int rowCount, int columnCount, bool structureOnly = false) { 00413 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]"; 00414 if(structureOnly) { 00415 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(3)<<j; 00416 } 00417 else { 00418 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(9)<<j; 00419 } 00420 cout<<endl; 00421 00422 for(unsigned int i=0; i < (unsigned int)rowCount; i++) { 00423 cout<<setw(4)<<"["<<setw(3)<<i<<"]"; 00424 if(structureOnly) { 00425 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(3)<<(bool)xp2_Value[i][j]; 00426 } 00427 else { 00428 for(unsigned int j=0; j < (unsigned int)columnCount; j++) printf(" %7.2f",(float)xp2_Value[i][j]); 00429 //for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(8)<<xp2_Value[i][j]; 00430 } 00431 cout<<endl<<flush; 00432 } 00433 cout<<endl<<endl; 00434 } 00435 00436 template<class T> 00437 void displayVector(T* xp2_Value, int size, bool structureOnly = false) { 00438 if(structureOnly) { 00439 for(unsigned int i=0; i < (unsigned int)size; i++) { 00440 cout<<setw(4)<<"["<<setw(3)<<i<<"]"; 00441 cout<<setw(3)<<(bool)xp2_Value[i]; 00442 cout<<endl<<flush; 00443 } 00444 } 00445 else { 00446 for(unsigned int i=0; i < (unsigned int)size; i++) { 00447 cout<<setw(4)<<"["<<setw(3)<<i<<"]"; 00448 printf(" %7.2f",(float)xp2_Value[i]); 00449 //cout<<setw(8)<<xp2_Value[i]; 00450 cout<<endl<<flush; 00451 } 00452 } 00453 cout<<endl<<endl; 00454 } 00455 00456 template<class T> 00457 int displayVector(vector<T> v) { 00458 for (unsigned int i=0; i < v.size(); i++) { 00459 cout<<setw(4)<<"["<<setw(3)<<i<<"]"; 00460 printf(" %7.2f",(float)v[i]); 00461 cout<<endl<<flush; 00462 } 00463 return 0; 00464 } 00465 00466 00468 template<class T> 00469 void displayAdjacencyMatrix(vector< vector<T> > &xp2_Value, bool structureOnly = false) { 00470 unsigned int estimateColumnCount = 20; 00471 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]"; 00472 if(structureOnly) { 00473 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(3)<<j; 00474 } 00475 else { 00476 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j; 00477 } 00478 cout<<endl; 00479 00480 unsigned int rowCount = xp2_Value.size(); 00481 for(unsigned int i=0; i < rowCount; i++) { 00482 cout<<setw(4)<<"["<<setw(3)<<i<<"]"; 00483 unsigned int numOfNonZeros = (int)xp2_Value[i].size(); 00484 cout<<"("<<setw(5)<<numOfNonZeros<<")"; 00485 if(structureOnly) { 00486 for(unsigned int j=0; j < numOfNonZeros; j++) cout<<setw(3)<<(bool)xp2_Value[i][j]; 00487 } 00488 else { 00489 for(unsigned int j=0; j < numOfNonZeros; j++) cout<<setw(9)<<xp2_Value[i][j]; 00490 } 00491 cout<<endl<<flush; 00492 } 00493 cout<<endl<<endl; 00494 } 00495 00496 00497 #endif //EXTRA_H_TEMPLATE_FUNCTIONS 00498 00499 #endif //EXTRA_H 00500 00501 00502