00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00032
00033 #include <list>
00034 #include <map>
00035 #include <string>
00036 #include <vector>
00037
00038 using namespace std;
00039
00040
00041
00042
00043
00044
00046
00050 int ReadRowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, int& rowCount, int& columnCount);
00051
00053
00057 bool isValidOrdering(vector<int> & ordering, int offset = 0);
00058
00059
00060 void randomOrdering(vector<int>& ordering);
00061
00063 string toUpper(string input);
00064
00066
00079 int RowCompressedFormat_2_SparseSolversFormat_StructureOnly(unsigned int ** uip2_HessianSparsityPattern, unsigned int ui_rowCount, unsigned int** ip2_RowIndex, unsigned int** ip2_ColumnIndex);
00080
00082
00089 void ConvertDIMACSFormat2MatrixMarketFormat(string fileNameNoExt);
00090
00092
00096 int ConvertMatrixMarketFormatToRowCompressedFormat(string s_InputFile, unsigned int *** uip3_SparsityPattern, double*** dp3_Value, int &rowCount, int &columnCount);
00097
00099
00101 int MatrixMultiplication_VxS(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix);
00102
00104
00106 int MatrixMultiplication_SxV(unsigned int ** uip3_SparsityPattern, double** dp3_Value, int rowCount, int columnCount, double** dp2_seed, int colorCount, double*** dp3_CompressedMatrix);
00107
00109
00113 bool CompressedRowMatricesREqual(double** dp3_Value, double** dp3_NewValue, int rowCount, bool compare_exact = 1, bool print_all = 0);
00114
00116 int Times2Plus1point5(double** dp2_Values, int i_RowCount, int i_ColumnCount);
00117
00119 int Times2(double** dp2_Values, int i_RowCount, int i_ColumnCount);
00120
00122 int GenerateValues(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value);
00123
00125 int GenerateValuesForSymmetricMatrix(unsigned int ** uip2_SparsityPattern, int rowCount, double*** dp3_Value);
00126
00127 #ifndef EXTRA_H_TEMPLATE_FUNCTIONS
00128 #define EXTRA_H_TEMPLATE_FUNCTIONS
00129
00131 template<class T>
00132 int diffArrays(T* array1, T* array2, int rowCount, bool compare_exact = 1, bool print_all = 0) {
00133 double ratio = 0.;
00134 int none_equal_count = 0;
00135 for(int i = 0; i < rowCount; i++) {
00136 if (compare_exact) {
00137 if(array1[i]!=array2[i]) {
00138 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<";\t array2[] = "<<array2[i]<<endl;
00139 none_equal_count++;
00140 if(!print_all) return 1;
00141 }
00142 }
00143 else {
00144 ratio = array1[i] / array2[i];
00145 if(ratio < .99 || ratio > 1.02) {
00146 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<";\t array2[] = "<<array2[i]<<endl;
00147 none_equal_count++;
00148 if(!print_all) return 1;
00149 }
00150 }
00151 }
00152
00153 return none_equal_count;
00154 }
00155
00157 template<class T>
00158 int diffVectors(vector<T> array1, vector<T> array2, bool compare_exact = 1, bool print_all = 0) {
00159 double ratio = 0.;
00160 int none_equal_count = 0;
00161
00162 if(array1.size() != array2.size()) {
00163 cout<<"array1.size() "<<array1.size()<<" != array2.size()"<<array2.size()<<endl;
00164 none_equal_count++;
00165 }
00166
00167 int min_array_size = (array1.size() < array2.size())?array1.size():array2.size();
00168
00169 for(int i = 0; i < min_array_size; i++) {
00170 if (compare_exact) {
00171 if(array1[i]!=array2[i]) {
00172 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<<";\t array2[] = "<<array2[i]<<endl;
00173 none_equal_count++;
00174 if(!print_all) return none_equal_count;
00175 }
00176 }
00177 else {
00178 ratio = array1[i] / array2[i];
00179 if(ratio < .99 || ratio > 1.02) {
00180 cout<<"At index i="<<i<<"\t array1[] = "<<array1[i]<<";\t array2[] = "<<array2[i]<<endl;
00181 none_equal_count++;
00182 if(!print_all) return none_equal_count;
00183 }
00184 }
00185 }
00186
00187 return none_equal_count;
00188 }
00189
00190 template<class T>
00191 int deleteMatrix(T** xp2_matrix, int rowCount) {
00192
00193
00194
00195 for(int i = 0; i < rowCount; i++) {
00196
00197 delete xp2_matrix[i];
00198 }
00199
00200 delete xp2_matrix;
00201
00202 return 0;
00203 }
00204
00205 template<class T>
00206 int deleteMatrix(T*** xp3_matrix, int rowCount) {
00207
00208 deleteMatrix(*xp3_matrix,rowCount);
00209
00210 delete xp3_matrix;
00211
00212 return 0;
00213 }
00214 template<class T>
00215 void displayCompressedRowMatrix(T** xp2_Value, int rowCount, bool structureOnly = false) {
00216 unsigned int estimateColumnCount = 30;
00217 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]";
00218 if(structureOnly) {
00219 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(4)<<j;
00220 }
00221 else {
00222 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j;
00223 }
00224 cout<<endl;
00225
00226 for(unsigned int i=0; i < (unsigned int)rowCount; i++) {
00227 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00228 unsigned int numOfNonZeros = (unsigned int)xp2_Value[i][0];
00229 if(structureOnly) {
00230 for(unsigned int j=0; j <= numOfNonZeros; j++) {
00231 if (j==0) printf(" (%d)",(int)xp2_Value[i][j]);
00232 else printf(" %d",(int)xp2_Value[i][j]);
00233 }
00234 }
00235 else {
00236
00237 for(unsigned int j=0; j <= numOfNonZeros; j++) {
00238 if(j==0) printf(" (%7.2f)",(float)xp2_Value[i][j]);
00239 else printf(" %7.2f",(float)xp2_Value[i][j]);
00240 }
00241 }
00242 cout<<endl<<flush;
00243 }
00244 cout<<endl<<endl;
00245 }
00246
00247 template<class T>
00248 void displayMatrix(T** xp2_Value, int rowCount, int columnCount, bool structureOnly = false) {
00249 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]";
00250 if(structureOnly) {
00251 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(3)<<j;
00252 }
00253 else {
00254 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(9)<<j;
00255 }
00256 cout<<endl;
00257
00258 for(unsigned int i=0; i < (unsigned int)rowCount; i++) {
00259 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00260 if(structureOnly) {
00261 for(unsigned int j=0; j < (unsigned int)columnCount; j++) cout<<setw(3)<<(bool)xp2_Value[i][j];
00262 }
00263 else {
00264 for(unsigned int j=0; j < (unsigned int)columnCount; j++) printf(" %7.2f",(float)xp2_Value[i][j]);
00265
00266 }
00267 cout<<endl<<flush;
00268 }
00269 cout<<endl<<endl;
00270 }
00271
00272 template<class T>
00273 void displayVector(T* xp2_Value, int size, bool structureOnly = false) {
00274 if(structureOnly) {
00275 for(unsigned int i=0; i < (unsigned int)size; i++) {
00276 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00277 cout<<setw(3)<<(bool)xp2_Value[i];
00278 cout<<endl<<flush;
00279 }
00280 }
00281 else {
00282 for(unsigned int i=0; i < (unsigned int)size; i++) {
00283 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00284 printf(" %7.2f",(float)xp2_Value[i]);
00285
00286 cout<<endl<<flush;
00287 }
00288 }
00289 cout<<endl<<endl;
00290 }
00291
00293 template<class T>
00294 void displayAdjacencyMatrix(vector< vector<T> > &xp2_Value, bool structureOnly = false) {
00295 unsigned int estimateColumnCount = 30;
00296 cout<<setw(4)<<"["<<setw(3)<<"\\"<<"]";
00297 if(structureOnly) {
00298 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(3)<<j;
00299 }
00300 else {
00301 for(unsigned int j=0; j < estimateColumnCount; j++) cout<<setw(9)<<j;
00302 }
00303 cout<<endl;
00304
00305 unsigned int rowCount = xp2_Value.size();
00306 for(unsigned int i=0; i < rowCount; i++) {
00307 cout<<setw(4)<<"["<<setw(3)<<i<<"]";
00308 unsigned int numOfNonZeros = (int)xp2_Value[i].size();
00309 cout<<"("<<setw(5)<<numOfNonZeros<<")";
00310 if(structureOnly) {
00311 for(unsigned int j=0; j < numOfNonZeros; j++) cout<<setw(3)<<(bool)xp2_Value[i][j];
00312 }
00313 else {
00314 for(unsigned int j=0; j < numOfNonZeros; j++) cout<<setw(9)<<xp2_Value[i][j];
00315 }
00316 cout<<endl<<flush;
00317 }
00318 cout<<endl<<endl;
00319 }
00320
00321
00322 #endif //EXTRA_H_TEMPLATE_FUNCTIONS
00323
00324 #endif //EXTRA_H
00325
00326
00327