File: sparse_jac_hess.cpp

package info (click to toggle)
colpack 1.0.10-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,704 kB
  • sloc: cpp: 49,807; ansic: 1,231; makefile: 419; sh: 13
file content (374 lines) | stat: -rw-r--r-- 9,243 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "math.h"
#include "stdlib.h"
#include "stdio.h"

#include "adolc.h"
#include "sparse/sparsedrivers.h"

#define repnum 10

//------------------------------------------------------------------------------------
// for time measurements

#include <sys/time.h>
#include "ColPackHeaders.h"

using namespace ColPack;

double k_getTime() {
   struct timeval v;
   struct timezone z;
   gettimeofday(&v, &z);
   return ((double)v.tv_sec)+((double)v.tv_usec)/1000000;
}

//------------------------------------------------------------------------------------
// required for second method

using namespace std;

#include <list>
#include <map>
#include <string>
#include <vector>

//------------------------------------------------------------------------------------
// as before

#define tag_f 1
#define tag_red 2
#define tag_HP 3

#define tag_c 4

// problem definition -> eval_fun.c
void init_dim(int *n, int *m);
void init_startpoint(double *x, int n);
double  feval(double *x, int n);
adouble feval_ad(double *x, int n);
void ceval(double *x, double *c, int n);
void ceval_ad(double *x, adouble *c, int n);
adouble feval_ad_mod(double *x, int n);
adouble feval_ad_modHP(double *x, int n);

void printmat(char* kette, int n, int m, double** M);
void printmatint(char* kette, int n, int m, int** M);
void printmatint_c(char* kette, int m,unsigned int** M);


int main()
{
  int i, j, k, l, sum, n, m, nnz, direct = 1, found;
  double f;
  double *x, *c;
  adouble fad, *xad, *cad;
  //double** Zpp;
  //double** Zppf;
  double** J;
  //double* s;
  //int p_H_dir, p_H_indir;
  size_t tape_stats[11];
  int num;
  FILE *fp_JP;

  double **Seed_J;
  double **Jc;
  int p_J;

  int recover = 1;
  int jac_vec = 1;
  int compute_full = 1;
  int output_sparsity_pattern_J = 0;
  //int output_sparsity_pattern_H = 1;
  //int use_direct_method = 1;
  //int output_direct = 0;
  //int use_indirect_method = 1;
  //int output_indirect = 0;

  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;
  //double test;
  unsigned int *rind;
  unsigned int *cind;
  double *values;

  //tring s_InputFile = "test_mat.mtx";
  //string s_InputFile = "jac_pat.mtx";


//------------------------------------------------------------------------------------
// problem definition + evaluation

  init_dim(&n,&m); // initialize n and m

  printf(" n = %d m = %d\n",n,m);

  x =   (double*)  malloc(n*sizeof(double)); // x: vector input for function evaluation
  c =   (double*)  malloc(m*sizeof(double)); // c: constraint vector
  cad = new adouble[m];

  init_startpoint(x,n);

  t_f_1 = k_getTime();
  for(i=0;i<repnum;i++)
    f = feval(x,n);
  t_f_2 = k_getTime();
  div_f = (t_f_2 - t_f_1)*1.0/repnum;
  printf("XXX The time needed for function evaluation:  %10.6f \n \n", div_f);


  t_f_1 = k_getTime();
  for(i=0;i<repnum;i++)
    ceval(x,c,n);
  t_f_2 = k_getTime();
  div_c = (t_f_2 - t_f_1)*1.0/repnum;
  printf("XXX The time needed for constraint evaluation:  %10.6f \n \n", div_c);


  trace_on(tag_f);

    fad = feval_ad(x, n); // feval_ad: derivative of feval

    fad >>= f;

  trace_off();

  trace_on(tag_c);

    ceval_ad(x, cad, n); //ceval_ad: derivative of ceval

    for(i=0;i<m;i++)
      cad[i] >>= f;

  trace_off();
  //return 1;

  tapestats(tag_c,tape_stats);              // reading of tape statistics
  printf("\n    independents   %ld\n",(long)tape_stats[0]);
  printf("    dependents     %ld\n",(long)tape_stats[1]);
  printf("    operations     %ld\n",(long)tape_stats[5]);
  printf("    buffer size    %ld\n",(long)tape_stats[4]);
  printf("    maxlive        %ld\n",(long)tape_stats[2]);
  printf("    valstack size  %ld\n\n",(long)tape_stats[3]);


//------------------------------------------------------------------------------------
// full Jacobian:

  div_J = -1;

  if(compute_full == 1)
    {
      J =  myalloc2(m,n);

      t_f_1 = k_getTime();
      jacobian(tag_c,m,n,x,J);
      t_f_2 = k_getTime();
      div_J = (t_f_2 - t_f_1);

      printf("XXX The time needed for full Jacobian:  %10.6f \n \n", div_J);
      printf("XXX runtime ratio:  %10.6f \n \n", div_J/div_c);

      //save the matrix into a file (non-zero entries only)

	fp_JP = fopen("jac_full.mtx","w");

	fprintf(fp_JP,"%d %d\n",m,n);

	for (i=0;i<m;i++)
	  {
	    for (j=0;j<n;j++)
	      if(J[i][j]!=0.0) fprintf(fp_JP,"%d %d %10.6f\n",i,j,J[i][j] );
	  }
	fclose(fp_JP);
    }

//------------------------------------------------------------------------------------
  printf("XXX THE 4 STEP TO COMPUTE SPARSE MATRICES USING ColPack \n \n");
// STEP 1: Determination of sparsity pattern of Jacobian JP:

  unsigned int  *rb=NULL;          /* dependent variables          */
  unsigned int  *cb=NULL;          /* independent variables        */
  unsigned int  **JP=NULL;         /* compressed block row storage */
  int ctrl[2];

  JP = (unsigned int **) malloc(m*sizeof(unsigned int*));
  ctrl[0] = 0; ctrl[1] = 0;


  t_f_1 = k_getTime();
  jac_pat(tag_c, m, n, x, JP, ctrl);	//ADOL-C calculate the sparsity pattern
  t_f_2 = k_getTime();
  div_JP = (t_f_2 - t_f_1);

  printf("XXX STEP 1: The time needed for Jacobian pattern:  %10.6f \n \n", div_JP);
  printf("XXX STEP 1: runtime ratio:  %10.6f \n \n", div_JP/div_c);


  nnz = 0;
  for (i=0;i<m;i++)
    nnz += JP[i][0];

  printf(" nnz %d \n",nnz);
  printf(" hier 1a\n");


//------------------------------------------------------------------------------------
// STEP 2: Determination of Seed matrix:

  double tg_C;
  int dummy;

  t_f_1 = k_getTime();

  BipartiteGraphPartialColoringInterface * gGraph = new BipartiteGraphPartialColoringInterface(SRC_MEM_ADOLC, JP, m, n);
  //gGraph->PrintBipartiteGraph();
  t_f_2 = k_getTime();

  printf("XXX STEP 2: The time needed for Graph construction:  %10.6f \n \n", (t_f_2-t_f_1) );
  printf("XXX STEP 2: runtime ratio:  %10.6f \n \n", (t_f_2-t_f_1)/div_c);

  t_f_1 = k_getTime();
  //gGraph->GenerateSeedJacobian(&Seed_J, &dummy, &p_J,
  //                          "NATURAL", "COLUMN_PARTIAL_DISTANCE_TWO");
  gGraph->PartialDistanceTwoColoring("NATURAL", "COLUMN_PARTIAL_DISTANCE_TWO");
  t_f_2 = k_getTime();

  printf("XXX STEP 2: The time needed for Coloring:  %10.6f \n \n", (t_f_2-t_f_1));
  printf("XXX STEP 2: runtime ratio:  %10.6f \n \n", (t_f_2-t_f_1)/div_c);

  t_f_1 = k_getTime();
  Seed_J = gGraph->GetSeedMatrix(&dummy, &p_J);
  t_f_2 = k_getTime();
  tg_C = t_f_2 - t_f_1;


  printf("XXX STEP 2: The time needed for Seed generation:  %10.6f \n \n", tg_C);
  printf("XXX STEP 2: runtime ratio:  %10.6f \n \n", tg_C/div_c);

  //*/

//------------------------------------------------------------------------------------
// STEP 3: Jacobian-matrix product:

// ADOL-C:
//*
  if (jac_vec == 1)
    {

      Jc = myalloc2(m,p_J);
      t_f_1 = k_getTime();
      printf(" hier 1\n");
      fov_forward(tag_c,m,n,p_J,x,Seed_J,c,Jc);
      printf(" hier 2\n");
      t_f_2 = k_getTime();
      div_Jc = (t_f_2 - t_f_1);

      printf("XXX STEP 3: The time needed for Jacobian-matrix product:  %10.6f \n \n", div_Jc);
      printf("XXX STEP 3: runtime ratio:  %10.6f \n \n", div_Jc/div_c);


    }

//------------------------------------------------------------------------------------
// STEP 4: computed Jacobians/ recovery


  if (recover == 1)
    {


      JacobianRecovery1D jr1d;

      printf("m = %d, n = %d, p_J = %d \n",m,n,p_J);
      //printmatint_c("JP Jacobian Pattern",m,JP);
      //printmat("Jc Jacobian compressed",m,p_J,Jc);

      t_f_1 = k_getTime();
      jr1d.RecoverD2Cln_CoordinateFormat (gGraph, Jc, JP, &rind, &cind, &values);
      t_f_2 = k_getTime();
      div_rec_C = (t_f_2 - t_f_1);

      printf("XXX STEP 4: The time needed for Recovery:  %10.6f \n \n", div_rec_C);
      printf("XXX STEP 4: runtime ratio:  %10.6f \n \n", div_rec_C/div_c);

      //save recovered matrix into file

	fp_JP = fopen("jac_recovered.mtx","w");

	fprintf(fp_JP,"%d %d %d\n",m,n,nnz);

	for (i=0;i<nnz;i++)
	  {
	      fprintf(fp_JP,"%d %d %10.6f\n",rind[i],cind[i],values[i] );
	  }
	fclose(fp_JP);

    }

    /*By this time, if you compare the 2 output files: jac_full.mtx and jac_recovered.mtx
    You should be able to see that the non-zero entries are identical
    */


  free(JP);
  delete[] cad;
  free(c);
  free(x);
  delete gGraph;
  if(jac_vec == 1) {
    myfree2(Jc);
  }
  if(compute_full == 1)
    {
      myfree2(J);
    }

  return 0;

}




/***************************************************************************/

void printmat(char* kette, int n, int m, double** M)
{ int i,j;

  printf("%s \n",kette);
  for(i=0; i<n ;i++)
  {
    printf("\n %d: ",i);
    for(j=0;j<m ;j++)
	printf(" %10.4f ", M[i][j]);
  }
  printf("\n");
}

void printmatint(char* kette, int n, int m, int** M)
{ int i,j;

  printf("%s \n",kette);
  for(i=0; i<n ;i++)
  {
    printf("\n %d: ",i);
    for(j=0;j<m ;j++)
      printf(" %d ", M[i][j]);
  }
  printf("\n");
}


void printmatint_c(char* kette, int m,unsigned int** M)
{ int i;
  unsigned int j;

  printf("%s \n",kette);
  for (i=0;i<m;i++)
    {
    printf("\n %d (%d): ",i,M[i][0]);
      for (j=1;j<=M[i][0];j++)
	printf("\t%d ",M[i][j]);
    }
  printf("\n");
}