ColPack
SampleDrivers/Matrix_Compression_and_Recovery/SMB/sparse_jac_hess.cpp
Go to the documentation of this file.
00001 #include "math.h"
00002 #include "stdlib.h"
00003 #include "stdio.h"
00004 
00005 #include "adolc.h"
00006 #include "sparse/sparsedrivers.h"
00007 
00008 #define repnum 10
00009 
00010 //------------------------------------------------------------------------------------
00011 // for time measurements
00012 
00013 #include <sys/time.h>
00014 #include "ColPackHeaders.h"
00015 
00016 using namespace ColPack;
00017 
00018 double k_getTime() {
00019    struct timeval v;
00020    struct timezone z;
00021    gettimeofday(&v, &z);
00022    return ((double)v.tv_sec)+((double)v.tv_usec)/1000000;
00023 }
00024 
00025 //------------------------------------------------------------------------------------
00026 // required for second method
00027 
00028 using namespace std;
00029 
00030 #include <list>
00031 #include <map>
00032 #include <string>
00033 #include <vector>
00034 
00035 //------------------------------------------------------------------------------------
00036 // as before
00037 
00038 #define tag_f 1
00039 #define tag_red 2
00040 #define tag_HP 3
00041 
00042 #define tag_c 4
00043 
00044 // problem definition -> eval_fun.c
00045 void init_dim(int *n, int *m);
00046 void init_startpoint(double *x, int n);
00047 double  feval(double *x, int n);
00048 adouble feval_ad(double *x, int n);
00049 void ceval(double *x, double *c, int n);
00050 void ceval_ad(double *x, adouble *c, int n);
00051 adouble feval_ad_mod(double *x, int n);
00052 adouble feval_ad_modHP(double *x, int n);
00053 
00054 void printmat(char* kette, int n, int m, double** M);
00055 void printmatint(char* kette, int n, int m, int** M);
00056 void printmatint_c(char* kette, int m,unsigned int** M);
00057 
00058 
00059 int main()
00060 {
00061   int i, j, k, l, sum, n, m, nnz, direct = 1, found;
00062   double f;
00063   double *x, *c;
00064   adouble fad, *xad, *cad;
00065   //double** Zpp;
00066   //double** Zppf;
00067   double** J;
00068   //double* s;
00069   //int p_H_dir, p_H_indir;
00070   int tape_stats[11];
00071   int num;
00072   FILE *fp_JP;
00073 
00074   double **Seed_J;
00075   double **Jc;
00076   int p_J;
00077 
00078   int recover = 1;
00079   int jac_vec = 1;
00080   int compute_full = 1;
00081   int output_sparsity_pattern_J = 0;
00082   //int output_sparsity_pattern_H = 1;
00083   //int use_direct_method = 1;
00084   //int output_direct = 0;
00085   //int use_indirect_method = 1;
00086   //int output_indirect = 0;
00087 
00088   double t_f_1, t_f_2, div_f=0, div_c=0, div_JP=0, div_JP2=0, div_Seed=0, div_Seed_C=0, div_Jc=0, div_Jc_C=0, div_rec=0, div_rec_C=0, div_J=0;
00089   //double test;
00090   unsigned int *rind;
00091   unsigned int *cind;
00092   double *values;
00093 
00094   //tring s_InputFile = "test_mat.mtx";
00095   //string s_InputFile = "jac_pat.mtx";
00096 
00097 
00098 //------------------------------------------------------------------------------------
00099 // problem definition + evaluation
00100 
00101   init_dim(&n,&m); // initialize n and m
00102 
00103   printf(" n = %d m = %d\n",n,m);
00104 
00105   x =   (double*)  malloc(n*sizeof(double)); // x: vector input for function evaluation
00106   c =   (double*)  malloc(m*sizeof(double)); // c: constraint vector
00107   cad = new adouble[m];
00108 
00109   init_startpoint(x,n);
00110 
00111   t_f_1 = k_getTime();
00112   for(i=0;i<repnum;i++)
00113     f = feval(x,n);
00114   t_f_2 = k_getTime();
00115   div_f = (t_f_2 - t_f_1)*1.0/repnum;
00116   printf("XXX The time needed for function evaluation:  %10.6f \n \n", div_f);
00117 
00118 
00119   t_f_1 = k_getTime();
00120   for(i=0;i<repnum;i++)
00121     ceval(x,c,n);
00122   t_f_2 = k_getTime();
00123   div_c = (t_f_2 - t_f_1)*1.0/repnum;
00124   printf("XXX The time needed for constraint evaluation:  %10.6f \n \n", div_c);
00125 
00126 
00127   trace_on(tag_f);
00128 
00129     fad = feval_ad(x, n); // feval_ad: derivative of feval
00130 
00131     fad >>= f;
00132 
00133   trace_off();
00134 
00135   trace_on(tag_c);
00136 
00137     ceval_ad(x, cad, n); //ceval_ad: derivative of ceval
00138 
00139     for(i=0;i<m;i++)
00140       cad[i] >>= f;
00141 
00142   trace_off();
00143   //return 1;
00144 
00145   tapestats(tag_c,tape_stats);              // reading of tape statistics
00146   printf("\n    independents   %d\n",tape_stats[0]);
00147   printf("    dependents     %d\n",tape_stats[1]);
00148   printf("    operations     %d\n",tape_stats[5]);
00149   printf("    buffer size    %d\n",tape_stats[4]);
00150   printf("    maxlive        %d\n",tape_stats[2]);
00151   printf("    valstack size  %d\n\n",tape_stats[3]);
00152 
00153 
00154 //------------------------------------------------------------------------------------
00155 // full Jacobian:
00156 
00157   div_J = -1;
00158 
00159   if(compute_full == 1)
00160     {
00161       J =  myalloc2(m,n);
00162 
00163       t_f_1 = k_getTime();
00164       jacobian(tag_c,m,n,x,J);
00165       t_f_2 = k_getTime();
00166       div_J = (t_f_2 - t_f_1);
00167 
00168       printf("XXX The time needed for full Jacobian:  %10.6f \n \n", div_J);
00169       printf("XXX runtime ratio:  %10.6f \n \n", div_J/div_c);
00170 
00171       //save the matrix into a file (non-zero entries only)
00172 
00173         fp_JP = fopen("jac_full.mtx","w");
00174 
00175         fprintf(fp_JP,"%d %d\n",m,n);
00176 
00177         for (i=0;i<m;i++)
00178           {
00179             for (j=0;j<n;j++)
00180               if(J[i][j]!=0.0) fprintf(fp_JP,"%d %d %10.6f\n",i,j,J[i][j] );
00181           }
00182         fclose(fp_JP);
00183     }
00184 
00185 //------------------------------------------------------------------------------------
00186   printf("XXX THE 4 STEP TO COMPUTE SPARSE MATRICES USING ColPack \n \n");
00187 // STEP 1: Determination of sparsity pattern of Jacobian JP:
00188 
00189   unsigned int  *rb=NULL;          /* dependent variables          */
00190   unsigned int  *cb=NULL;          /* independent variables        */
00191   unsigned int  **JP=NULL;         /* compressed block row storage */
00192   int ctrl[2];
00193 
00194   JP = (unsigned int **) malloc(m*sizeof(unsigned int*));
00195   ctrl[0] = 0; ctrl[1] = 0;
00196 
00197 
00198   t_f_1 = k_getTime();
00199   jac_pat(tag_c, m, n, x, JP, ctrl);    //ADOL-C calculate the sparsity pattern
00200   t_f_2 = k_getTime();
00201   div_JP = (t_f_2 - t_f_1);
00202 
00203   printf("XXX STEP 1: The time needed for Jacobian pattern:  %10.6f \n \n", div_JP);
00204   printf("XXX STEP 1: runtime ratio:  %10.6f \n \n", div_JP/div_c);
00205 
00206 
00207   nnz = 0;
00208   for (i=0;i<m;i++)
00209     nnz += JP[i][0];
00210 
00211   printf(" nnz %d \n",nnz);
00212   printf(" hier 1a\n");
00213 
00214 
00215 //------------------------------------------------------------------------------------
00216 // STEP 2: Determination of Seed matrix:
00217 
00218   double tg_C;
00219   int dummy;
00220 
00221   t_f_1 = k_getTime();
00222 
00223   BipartiteGraphPartialColoringInterface * gGraph = new BipartiteGraphPartialColoringInterface(SRC_MEM_ADOLC, JP, m, n);
00224   //gGraph->PrintBipartiteGraph();
00225   t_f_2 = k_getTime();
00226 
00227   printf("XXX STEP 2: The time needed for Graph construction:  %10.6f \n \n", (t_f_2-t_f_1) );
00228   printf("XXX STEP 2: runtime ratio:  %10.6f \n \n", (t_f_2-t_f_1)/div_c);
00229   
00230   t_f_1 = k_getTime();
00231   //gGraph->GenerateSeedJacobian(&Seed_J, &dummy, &p_J,
00232   //                          "NATURAL", "COLUMN_PARTIAL_DISTANCE_TWO");
00233   gGraph->PartialDistanceTwoColoring("NATURAL", "COLUMN_PARTIAL_DISTANCE_TWO");
00234   t_f_2 = k_getTime();
00235 
00236   printf("XXX STEP 2: The time needed for Coloring:  %10.6f \n \n", (t_f_2-t_f_1));
00237   printf("XXX STEP 2: runtime ratio:  %10.6f \n \n", (t_f_2-t_f_1)/div_c);
00238   
00239   t_f_1 = k_getTime();
00240   Seed_J = gGraph->GetSeedMatrix(&dummy, &p_J);
00241   t_f_2 = k_getTime();
00242   tg_C = t_f_2 - t_f_1;
00243 
00244 
00245   printf("XXX STEP 2: The time needed for Seed generation:  %10.6f \n \n", tg_C);
00246   printf("XXX STEP 2: runtime ratio:  %10.6f \n \n", tg_C/div_c);
00247 
00248   //*/
00249 
00250 //------------------------------------------------------------------------------------
00251 // STEP 3: Jacobian-matrix product:
00252 
00253 // ADOL-C:
00254 //*
00255   if (jac_vec == 1)
00256     {
00257 
00258       Jc = myalloc2(m,p_J);
00259       t_f_1 = k_getTime();
00260       printf(" hier 1\n");
00261       fov_forward(tag_c,m,n,p_J,x,Seed_J,c,Jc);
00262       printf(" hier 2\n");
00263       t_f_2 = k_getTime();
00264       div_Jc = (t_f_2 - t_f_1);
00265 
00266       printf("XXX STEP 3: The time needed for Jacobian-matrix product:  %10.6f \n \n", div_Jc);
00267       printf("XXX STEP 3: runtime ratio:  %10.6f \n \n", div_Jc/div_c);
00268 
00269 
00270     }
00271 
00272 //------------------------------------------------------------------------------------
00273 // STEP 4: computed Jacobians/ recovery
00274 
00275 
00276   if (recover == 1)
00277     {
00278 
00279 
00280       JacobianRecovery1D jr1d;
00281 
00282       printf("m = %d, n = %d, p_J = %d \n",m,n,p_J);
00283       //printmatint_c("JP Jacobian Pattern",m,JP);
00284       //printmat("Jc Jacobian compressed",m,p_J,Jc);
00285 
00286       t_f_1 = k_getTime();
00287       jr1d.RecoverD2Cln_CoordinateFormat (gGraph, Jc, JP, &rind, &cind, &values);
00288       t_f_2 = k_getTime();
00289       div_rec_C = (t_f_2 - t_f_1);
00290 
00291       printf("XXX STEP 4: The time needed for Recovery:  %10.6f \n \n", div_rec_C);
00292       printf("XXX STEP 4: runtime ratio:  %10.6f \n \n", div_rec_C/div_c);
00293 
00294       //save recovered matrix into file
00295 
00296         fp_JP = fopen("jac_recovered.mtx","w");
00297 
00298         fprintf(fp_JP,"%d %d %d\n",m,n,nnz);
00299 
00300         for (i=0;i<nnz;i++)
00301           {
00302               fprintf(fp_JP,"%d %d %10.6f\n",rind[i],cind[i],values[i] );
00303           }
00304         fclose(fp_JP);
00305 
00306     }
00307 
00308     /*By this time, if you compare the 2 output files: jac_full.mtx and jac_recovered.mtx
00309     You should be able to see that the non-zero entries are identical
00310     */
00311 
00312 
00313   free(JP);
00314   delete[] cad;
00315   free(c);
00316   free(x);
00317   delete gGraph;
00318   if(jac_vec == 1) {
00319     myfree2(Jc);
00320   }
00321   if(compute_full == 1)
00322     {
00323       myfree2(J);
00324     }
00325 
00326   return 0;
00327 
00328 }
00329 
00330 
00331 
00332 
00333 /***************************************************************************/
00334 
00335 void printmat(char* kette, int n, int m, double** M)
00336 { int i,j;
00337 
00338   printf("%s \n",kette);
00339   for(i=0; i<n ;i++)
00340   {
00341     printf("\n %d: ",i);
00342     for(j=0;j<m ;j++)
00343         printf(" %10.4f ", M[i][j]);
00344   }
00345   printf("\n");
00346 }
00347 
00348 void printmatint(char* kette, int n, int m, int** M)
00349 { int i,j;
00350 
00351   printf("%s \n",kette);
00352   for(i=0; i<n ;i++)
00353   {
00354     printf("\n %d: ",i);
00355     for(j=0;j<m ;j++)
00356       printf(" %d ", M[i][j]);
00357   }
00358   printf("\n");
00359 }
00360 
00361 
00362 void printmatint_c(char* kette, int m,unsigned int** M)
00363 { int i,j;
00364 
00365   printf("%s \n",kette);
00366   for (i=0;i<m;i++)
00367     {
00368     printf("\n %d (%d): ",i,M[i][0]);
00369       for (j=1;j<=M[i][0];j++)
00370         printf("\t%d ",M[i][j]);
00371     }
00372   printf("\n");
00373 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines