File: checkpointing.cpp

package info (click to toggle)
adolc 2.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,684 kB
  • ctags: 3,333
  • sloc: cpp: 18,988; ansic: 15,599; sh: 11,184; makefile: 483
file content (151 lines) | stat: -rw-r--r-- 3,000 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
/*----------------------------------------------------------------------------
 ADOL-C -- Automatic Differentiation by Overloading in C++
 File:     checkpointing.cpp
 Revision: $Id: checkpointing.cpp 171 2010-10-04 13:57:19Z kulshres $
 Contents: example for checkpointing

 Copyright (c) Andrea Walther
  
 This file is part of ADOL-C. This software is provided as open source.
 Any use, reproduction, or distribution of the software constitutes 
 recipient's acceptance of the terms of the accompanying license file.
 
---------------------------------------------------------------------------*/
#include <math.h>
#include <adolc/adolc.h>

#define h 0.01
#define steps 100

// time step function
// double version 
int euler_step(int n, double *y);

// adouble version 
int euler_step_act(int n, adouble *y);

int tag_full, tag_part, tag_check;


int main()
{
  // time interval
  double t0, tf;

  // state, double and adouble version
  adouble y[2];
  int n;

  // control, double and adouble version
  adouble con[2];
  double conp[2];

  // target value;
  double f;

  //variables for derivative caluclation
  double grad[2];

  int i;
  
  // tape identifiers
  tag_full = 1;
  tag_part = 2;
  tag_check = 3;

  // two input and output variables for checkpointing function
  n = 2;

  // time interval
  t0 = 0.0;
  tf = 1.0;

  //control
  conp[0] = 1.0;
  conp[1] = 1.0;

  // basis variant: full taping of time step loop

  trace_on(tag_full);
    con[0] <<= conp[0];
    con[1] <<= conp[1];
    y[0] = con[0];
    y[1] = con[1];
 
    for(i=0;i<steps;i++)
      {
	euler_step_act(n,y);
      }
    
    y[0] + y[1] >>= f;
  trace_off(1);

  gradient(tag_full,2,conp,grad);
  
  printf(" full taping:\n gradient=( %f, %f)\n\n",grad[0],grad[1]);

  // Now using checkpointing facilities

  // define checkpointing procedure

  // generate checkpointing context => define active variante of the time step
  CP_Context cpc(euler_step_act);

  // double variante of the time step function
  cpc.setDoubleFct(euler_step);

  // number of time steps to perform
  cpc.setNumberOfSteps(steps);

  // number of checkpoint
  cpc.setNumberOfCheckpoints(5);

  // dimension of input/output
  cpc.setDimensionXY(n);
  // input vector
  cpc.setInput(y);
  // output vector
  cpc.setOutput(y);
  // tape number for checkpointing
  cpc.setTapeNumber(tag_check);
  // always retape or not ?
  cpc.setAlwaysRetaping(false);

  trace_on(tag_part);
    con[0] <<= conp[0];
    con[1] <<= conp[1];
    y[0] = con[0];
    y[1] = con[1];
  
    cpc.checkpointing();

    y[0] + y[1] >>= f;
  trace_off(1);
  gradient(tag_part,2,conp,grad);
  
  printf(" taping with checkpointing facility:\n gradient=( %f, %f)\n\n",grad[0],grad[1]);

  return 0;
}

int euler_step(int n, double *y)
{

 // Euler step, double version
 y[0] = y[0]+h*y[0];
 y[1] = y[1]+h*2*y[1];

 return 1;
}

int euler_step_act(int n, adouble *y)
{

 // Euler step, adouble version
 
 y[0] = y[0]+h*y[0];
 y[1] = y[1]+h*2*y[1];

 return 1;
}