-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDE.h
286 lines (234 loc) · 7.88 KB
/
DE.h
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
/******************************************************************
* Author: Mahesh Narayanamurthi
* e- Mail : mahesh.mach@gmail.com
* Description: Differentiation Exercise - GSoC
* Created with: Geany
* Libraries: Hashlib by Charles B. Falconer
* Adapted Ideas: Simple Reverse Mode Automatic Diff.
* from "Evaluating Derivatives - Principles and Techniques of Algorithmic
* Differentiation" by Andreas Griewank
******************************************************************/
#ifndef _DE_H_
#define _DE_H_
#ifndef _MATH_H
#include <math.h>
#endif
#ifndef _STDLIB_H
#include <stdlib.h>
#endif
#ifndef _STRING_H
#include <string.h>
#endif
#ifndef _STDIO_H
#include <stdio.h>
#endif
#ifndef hashlib_h
#include "hashlib/hashlib.h"
#endif
/* The maximum number of characters in one line of an input file */
#ifndef MAX_CHAR_LINE
#define MAX_CHAR_LINE 20
#endif
/* The maximum depth of stack allowed for function evaluation */
#ifndef MAX_STACK_SIZE
#define MAX_STACK_SIZE 100
#endif
/* The maximum length of Trace allowed for adjoint evaluation */
#ifndef MAX_TRACE_SIZE
#define MAX_TRACE_SIZE 1000
#endif
/************************ Structures & Enums *************************/
/* Description: This enum contains the various opcodes.To extend this enum,
* simply add new opcodes to the list. This has been maintained this way to
* support scalability.
* */
typedef enum opcode{
emptyv,
constv,
indepv,
bplusv,
bminusv,
bmultv,
divv,
recipv,
sinv,
cosv,
powv,
funcv
}opcode;
/* Description: This structure holds the various tokens that form the
* equation. It is later used by function evaluate to evaluate the value
* of the function.
* */
typedef struct {
opcode type;
char* token;
struct Equation* next;
}Equation,*EquationP;
/* Description: This structure is used by the Hashmap to keep record of a
* variable as a key,value pair. The Hashlib used by this code has been
* written by Charles B. Falconer and is licensed under GPL. It has O(1)
* storage and retrieval performance.
* */
typedef struct{
char* key;
double value;
}varMap,*varMapP;
/* Description: This structure is used for maintaining Trace information
* of all the operations performed during function evaluation. It is
* later used for adjoint calculation during the return sweep.
* This structure idea has been adapted from the book
* "Evaluating Derivatives - Principles and Techniques of Algorithmic
* Differentiation by Andreas Griewank"
* and modified suitably.
* */
typedef struct{
double val;
double bar;
char* varName;
opcode operation;
struct elements *arg1;
struct elements *arg2;
}elements;
/* Description: This structure is used for maintaining pointer to trace
* information of all the operations performed during function evaluation.
* This structure idea has been adapted from the book
* "Evaluating Derivatives - Principles and Techniques of Algorithmic
* Differentiation by Andreas Griewank"
* */
typedef struct{
elements* ref;
}redouble;
/*Description: This structure is used for maintaining the stack during
*function evaluation.
* */
typedef struct{
redouble element[MAX_STACK_SIZE];
int topOfStack;
}stack, *stackP;
/* Description: This structure is used to store the values of the first
* partials of the function which can later be used for further calculations.
* */
typedef struct{
double* partials;
int index;
int count;
char** varName;
}firstPartials;
/********* These functions help access and modify the stack **********/
/* Description: This function is used to push values of type redouble onto
* the stack during function evaluation phase.
* Returns: Exits with 1 on failure and returns 0 on success
* */
int push(stackP,redouble);
/* Description: This function is used to pop values of type redouble* off
* the stack during function evaluation phase.
* Returns: Returns the pointer to the object of type redouble at the top
* of the stack.
* Exits with 1 on underflow.
* */
redouble* pop(stackP);
/* Description: This function is used to create a stack. It returns a stack
* pointer to the new stack.
* */
stackP createStack();
/* Description: This function is used to kill an existing stack.
* Arguments: The pointer to the stack that is to be killed.
* */
int killStack(stackP);
/* Description: This function is used to print the contents of an existing
* stack. Its used for debugging purpose.
* Arguments: The pointer to the stack that is to be printed.
* */
void printStack(stackP);
/*********************************************************************/
/****** These Functions help acess and modify the variable map *******/
/* Description: varCmp is used to compare two items belonging to the same
* variable map and returns 1, 0, -1 according to 1>2,1==2,1<2
* */
int varCmp(void*,void*);
/* Description: varDup is used to create a copy of the element that is passed
* to it. It is used by Hashlib library. For more details look at hashusage.txt
* in hashlib folder at the root directory.
* */
void* varDup(void*);
/* Description: varFree is used to free the allocated memory on the hashmap
* It is used by Hashlib library. For more details look at hashusage.txt in
* hashlib folder at the root directory.
* */
void varFree(void*);
/* Description: varHash and varReHash are used by Hashlib library.
* For more details look at hashusage.txt in hashlib folder at the
* root directory.
* */
unsigned long varHash(void*);
unsigned long varReHash(void*);
/*********************************************************************/
/* Description: During the return sweep, the partial adjoints of each
* input is calulated and this has to be summed up before I can get the
* adjoint of that particular input.
* */
int sumEachAdjoint(void*,void*,void*);
/* Description: Calculates all the partial adjoints during return sweep
* */
int reverseSweep();
/* Description: Reads the equation file and sotres it in a Equation Linked List
* */
Equation* readEquation(char* , hshtbl* );
/* Description: Reads the variables from the variable file and updates the hashmap
* */
int readVariables(char* , hshtbl*);
/* Description: Prints the read Variables to screen for Debugging
* */
void printVariables(hshtbl*);
int printEachVariable(void*, void* , void* );
/* Description: Prints the alread read Equation to a file named eqnchk.txt
* for debugging
* */
void printEquation(Equation* );
/* Description: This function is used to get the type of token that is
* passed to it.
* Arguments: char* to the token.
* Returns: The type of the token
* empty variable 0
* constant variable 1
* independent variable 2
* bplusv 3
* bminusv 4
* bmultv 5
* divv 6
* recipv 7
* sinv 8
* powv 9
* */
int getType(char* );
/* Description: Returns the number of distinct variables does the eqn have
* */
int getNumberVariables(hshtbl*);
/* Description: Evaluation is computing F = left-hand-side - right-hand-side
* (the residual error if the variable values given do not satify
* the equation).
* */
double evaluate(Equation*, hshtbl*);
/* Description: Evaluate dF/d(all-variables) partial derivatives).
* and return the results as a firstPartials object
* */
firstPartials evaluateFirstPartials(hshtbl*);
/* Description: Makes independent variables to be pushed onto the stack
* which are later used during the evaluation phase.
* */
redouble makeIndepv(double,char*);
/* Description: Makes constant entries to be pushed onto the stack which
* are later used during the evaluation phase
* */
redouble makeConstv(double,char*);
/* Description: Returns a new table to hold key,value pairs for variables.
* */
hshtbl* getNewTable();
/*Description: This function deallocates the resources that have been
* allocated during the formation of the Equation Linked List.
* This function has to be called after a call to hshkill()
* */
void killEquation(Equation*);
/**********************************************************************************************/
#endif