source: trunk/ADOL-C/include/adolc/adouble.h @ 748

Last change on this file since 748 was 748, checked in by kulshres, 15 months ago

Merge branch 'activity' of 'gitclone' into 'svn'

The following commits were merged:
commit fc84dcabde90416ff32e8b08a06b7dad775e06bf
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Aug 4 10:36:17 2017 +0200

more debugging activity analysis

sign of coval is important in all cases, so I can't change it only
in one case.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit b5f80a477d085d84c67ee71e2cf84ce6d926b858
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Thu Aug 3 18:24:40 2017 +0200

correct some sign changes

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit e8642818244e33f0a4204fdffa936cc7195fc5bc
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Thu Aug 3 14:57:46 2017 +0200

add activity tracking to parameters

parameters are always active since they can be changed after tracing
is complete using set_param_vec, just like independent variables.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit ae9f196ebbf225cd9a5e62e26266c2cbbbc8a5b2
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Wed Aug 2 17:58:15 2017 +0200

add activity tracking to conditional assignments

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit c4b610de2500e949271d3bf60f04b5d34a229807
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Jul 31 13:50:05 2017 +0200

simplify some operations during activity tracking

especially assignment from adub. make that equivalent to construction
from adub, by stealing rhs location. The problem with not stealing that
location would be to restore rhs previous activity before rhs was created
so that we can replace lhs into the previous operation. Since we don't have
a way of recreating activities from before, it's better to steal the
location of rhs and free the lhs location for reuse later.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 8e2ad08100254ee4309209d6c807928d6cab94ee
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jul 28 14:06:30 2017 +0200

add activity checking code in the operators and functions for adouble

conditional assignments do not track activity at the moment. Next step
is to make the parameters active too.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit b8b6411ba29892d10d36bfe7a4d2abcbeb59ff67
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jul 28 13:59:36 2017 +0200

add a buffer in globalTapeVars to track activity of live variables

it is managed by the StoreManager? class for allocation and deallocation

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 569a7f28227991f8029e89b989c6f04415e8bd1a
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jul 28 13:58:32 2017 +0200

Add option to enable activity tracking in adouble class

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1/* ---------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3
4 Revision: $Id: adouble.h 748 2018-02-28 12:16:05Z kulshres $
5 Contents: adouble.h contains the basis for the class of adouble
6           included here are all the possible functions defined on
7           the adouble class.  Notice that, as opposed to ealier versions,
8           both the class adub and the class adouble are derived from a base
9           class (badouble).  See below for further explanation.
10
11 Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
12               Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel,
13               Benjamin Letschert Kshitij Kulshreshtha
14
15 This file is part of ADOL-C. This software is provided as open source.
16 Any use, reproduction, or distribution of the software constitutes
17 recipient's acceptance of the terms of the accompanying license file.
18 
19---------------------------------------------------------------------------*/
20
21#if !defined(ADOLC_ADOUBLE_H)
22#define ADOLC_ADOUBLE_H 1
23
24/****************************************************************************/
25/*                                                         THIS FILE IS C++ */
26#ifdef __cplusplus
27#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)
28#define COMPILER_HAS_CXX11
29#else
30#error "please use -std=c++11 compiler flag with a C++11 compliant compiler"
31#endif
32
33#include <cstdio>
34#include <cstdlib>
35#include <iostream>
36#include <cmath>
37#include <stdexcept>
38
39using std::cout;
40using std::cin;
41using std::cerr;
42using std::ostream;
43using std::istream;
44using std::logic_error;
45
46#include <adolc/internal/common.h>
47
48/* NOTICE: There are automatic includes at the end of this file! */
49
50/****************************************************************************/
51/*                                             FORWARD DECLARATIONS (TAPES) */
52
53/*--------------------------------------------------------------------------*/
54class adouble;
55class adub;
56class badouble;
57class pdouble;
58
59/*--------------------------------------------------------------------------*/
60void ADOLC_DLL_EXPORT condassign( double &res, const double &cond,
61                                  const double &arg1, const double &arg2 );
62void ADOLC_DLL_EXPORT condassign( double &res, const double &cond,
63                                  const double &arg );
64
65void ADOLC_DLL_EXPORT condeqassign( double &res, const double &cond,
66                                    const double &arg1, const double &arg2 );
67void ADOLC_DLL_EXPORT condeqassign( double &res, const double &cond,
68                                    const double &arg );
69
70/****************************************************************************/
71/*                                                           CLASS BADOUBLE */
72
73/**
74   The class badouble contains the basic definitions for
75   the arithmetic operations, comparisons, etc.
76   This is a basic class from which the adub and adouble are
77   derived.  Notice that the constructors/destructors for
78   the class badouble are of the trivial variety.  This is the
79   main difference among badoubles, adubs, and adoubles.
80*/
81class ADOLC_DLL_EXPORT badouble {
82    friend ADOLC_DLL_EXPORT class pdouble;
83protected:
84    locint location;
85    badouble( void ) {};
86    // Copy constructor:
87    // must be public when using gcc >= 3.4 and gcc <= 4.3.0
88    // (see GCC 3.4 Release Series - Changes, New Features, and Fixes)
89    // so we make it protected for newer compilers again.
90    badouble( const badouble& a ) {};           /* ctor */
91    explicit badouble( locint lo ) {
92        location = lo;
93        isInit = true;
94    };
95
96    bool isInit;  // marker if the badouble is properly initialized
97
98public:
99    /*--------------------------------------------------------------------------*/   
100    inline locint loc( void ) const;                         /* Helpful stuff */
101
102    /*------------------------------------------------------------------------*/
103    badouble& operator >>= ( double& );                        /* Assignments */
104    badouble& operator <<= ( double );
105    void declareIndependent ();
106    void declareDependent ();
107    badouble& operator = ( double );
108    badouble& operator = ( const badouble& );
109    badouble& operator = ( const adub& );
110    double getValue() const;
111    inline double value() const {
112        return getValue();
113    }
114    explicit operator double();
115    explicit operator double const&() const;
116    explicit operator double&&();
117    void setValue ( const double );
118    /* badouble& operator = ( const adouble& );
119       !!! olvo 991210: was the same as badouble-assignment */
120
121    /*--------------------------------------------------------------------------*/
122    friend ADOLC_DLL_EXPORT std::ostream& operator << ( std::ostream&, const badouble& );  /* IO friends */
123    friend ADOLC_DLL_EXPORT std::istream& operator >> ( std::istream&, const badouble& );
124
125    /*------------------------------------------------------------------------*/
126    badouble& operator += ( double );               /* Operation + Assignment */
127    badouble& operator += ( const badouble& );
128    badouble& operator -= ( double y );
129    badouble& operator -= ( const badouble& );
130    badouble& operator *= ( double );
131    badouble& operator *= ( const badouble& );
132    badouble& operator /= ( double );
133    badouble& operator /= ( const badouble& );
134    /* olvo 991122 n2l: new special op_codes */
135    badouble& operator += ( const adub& );
136    badouble& operator -= ( const adub& );
137
138    /*--------------------------------------------------------------------------*/
139    badouble& operator = (const pdouble&);
140    badouble& operator += (const pdouble&);
141    badouble& operator -= (const pdouble&);
142    badouble& operator *= (const pdouble&);
143    inline badouble& operator /= (const pdouble&);
144    /*--------------------------------------------------------------------------*/
145    /* Comparison (friends) */
146#if !defined(ADOLC_ADVANCED_BRANCHING)
147    inline friend int operator != ( const badouble&, const badouble& );
148    inline friend int operator == ( const badouble&, const badouble& );
149    inline friend int operator <= ( const badouble&, const badouble& );
150    inline friend int operator >= ( const badouble&, const badouble& );
151    inline friend int operator >  ( const badouble&, const badouble& );
152    inline friend int operator <  ( const badouble&, const badouble& );
153#endif
154    inline friend int operator != ( double, const badouble& );
155    friend ADOLC_DLL_EXPORT int operator != ( const badouble&, double );
156    inline friend int operator == ( double, const badouble& );
157    friend ADOLC_DLL_EXPORT int operator == ( const badouble&, double );
158    inline friend int operator <= ( double, const badouble& );
159    friend ADOLC_DLL_EXPORT int operator <= ( const badouble&, double );
160    inline friend int operator >= ( double, const badouble& );
161    friend ADOLC_DLL_EXPORT int operator >= ( const badouble&, double );
162    inline friend int operator >  ( double, const badouble& );
163    friend ADOLC_DLL_EXPORT int operator >  ( const badouble&, double );
164    inline friend int operator <  ( double, const badouble& );
165    friend ADOLC_DLL_EXPORT int operator <  ( const badouble&, double );
166
167
168    /*--------------------------------------------------------------------------*/
169    /* Functions friends with both badouble and adub */
170#define _IN_CLASS_ 1
171#define _IN_BADOUBLE_ 1
172#include <adolc/internal/adubfunc.h>
173#undef _IN_BADOUBLE_
174#undef _IN_CLASS_
175
176    /*--------------------------------------------------------------------------*/
177    /* special operators (friends) */
178    friend ADOLC_DLL_EXPORT adouble atan2 ( const badouble&, const badouble& );
179    /* uses condassign internally */
180    friend ADOLC_DLL_EXPORT adouble pow   ( const badouble&, const badouble& );
181    friend ADOLC_DLL_EXPORT adouble pow   ( double, const badouble& );
182    /* User defined version of logarithm to test extend_quad macro */
183    friend ADOLC_DLL_EXPORT adouble myquad( const badouble& );
184
185    /*--------------------------------------------------------------------------*/
186    /* Conditionals */
187    friend ADOLC_DLL_EXPORT void condassign( adouble &res, const badouble &cond,
188            const badouble &arg1, const badouble &arg2 );
189    friend ADOLC_DLL_EXPORT void condassign( adouble &res, const badouble &cond,
190            const badouble &arg );
191    friend ADOLC_DLL_EXPORT void condeqassign( adouble &res, const badouble &cond,
192            const badouble &arg1, const badouble &arg2 );
193    friend ADOLC_DLL_EXPORT void condeqassign( adouble &res, const badouble &cond,
194            const badouble &arg );
195
196#define _IN_CLASS_ 1
197#define _IN_BADOUBLE_ 1
198#include <adolc/internal/paramfunc.h>
199#undef _IN_BADOUBLE_
200#undef _IN_CLASS_
201
202};
203
204
205
206/****************************************************************************/
207/*                                                               CLASS ADUB */
208
209/*
210   The class Adub
211   ---- Basically used as a temporary result.  The address for an
212        adub is usually generated within an operation.  That address
213        is "freed" when the adub goes out of scope (at destruction time).
214   ---- operates just like a badouble, but it has a destructor defined for it.
215*/
216ADOLC_DLL_EXPORT adub* adubp_from_adub(const adub&);
217/* s = adolc_vec_dot(x,y,size); <=> s = <x,y>_2 */
218ADOLC_DLL_EXPORT adub adolc_vec_dot(const adouble*const, const adouble*const, locint);
219
220class ADOLC_DLL_EXPORT adub:public badouble {
221    friend ADOLC_DLL_EXPORT class adouble;
222    friend ADOLC_DLL_EXPORT class advector;
223    friend ADOLC_DLL_EXPORT class adubref;
224    friend ADOLC_DLL_EXPORT class pdouble;
225    friend adub* adubp_from_adub(const adub&);
226private:
227    adub( adub const &) {
228        isInit = false;
229        fprintf(DIAG_OUT,"ADOL-C error: illegal copy construction of adub"
230                " variable\n          ... adub objects must never be copied\n");
231        throw logic_error("illegal constructor call, errorcode=-2");
232    }
233    adub( void ) {
234        isInit = false;
235        fprintf(DIAG_OUT,"ADOL-C error: illegal default construction of adub"
236                " variable\n");
237        throw logic_error("illegal constructor call, errorcode=-2");
238    }
239    explicit adub( double ) {
240        isInit = false;
241        fprintf(DIAG_OUT,"ADOL-C error: illegal  construction of adub variable"
242                " from double\n");
243        throw logic_error("illegal constructor call, errorcode=-2");
244    }
245protected:
246   /* this is the only logically legal constructor, which can be called by
247    * friend classes and functions
248    */
249   adub( locint lo ) : badouble(lo) {} 
250
251public:
252
253    explicit operator adub*() const { return adubp_from_adub(*this); }
254    /*--------------------------------------------------------------------------*/
255    /* s = adolc_vec_dot(x,y,size); <=> s = <x,y>_2 */
256    friend adub adolc_vec_dot(const adouble*const, const adouble*const, locint);
257    /* Functions friends with both badouble and adub */
258#define _IN_CLASS_ 1
259#define _IN_ADUB_ 1
260#include <adolc/internal/adubfunc.h>
261#undef _IN_ADUB_
262#undef _IN_CLASS_
263
264    /*--------------------------------------------------------------------------*/
265    /* Parameter dependent functions (friends) */
266#define _IN_CLASS_ 1
267#define _IN_ADUB_ 1
268#include <adolc/internal/paramfunc.h>
269#undef _IN_ADUB_
270#undef _IN_CLASS_
271
272    ~adub();
273};
274
275BEGIN_C_DECLS
276ADOLC_DLL_EXPORT void ensureContiguousLocations(size_t n);
277END_C_DECLS
278
279/****************************************************************************/
280/*                                                            CLASS ADOUBLE */
281/*
282  The class adouble.
283  ---Derived from badouble.  Contains the standard constructors/destructors.
284  ---At construction, it is given a new address, and at destruction, that
285     address is freed.
286*/
287class ADOLC_DLL_EXPORT adouble:public badouble {
288    friend ADOLC_DLL_EXPORT class advector;
289    friend ADOLC_DLL_EXPORT class pdouble;
290protected:
291    void initInternal(void); // Init for late initialization
292public:
293    adouble( const adub& );
294    adouble( const adouble& );
295    adouble( void );
296    adouble( double );
297    /* adub prevents postfix operators to occur on the left
298       side of an assignment which would not work  */
299    adub operator++( int );
300    adub operator--( int );
301    badouble& operator++( void );
302    badouble& operator--( void );
303    /*   inline double value(); */
304    ~adouble();
305
306    adouble& operator = ( double );
307    adouble& operator = ( const badouble& );
308    adouble& operator = ( const adouble& );
309    adouble& operator = ( const adub& );
310    adouble& operator = (const pdouble&);
311   
312    inline locint loc(void) const;
313
314#if defined(ADOLC_DEFAULT_CONTIG_LOC)
315    void *operator new[](size_t sz) {
316        void *p = ::new char[sz];
317        size_t n = (sz - sizeof(size_t))/sizeof(adouble);
318        ensureContiguousLocations(n);
319        return p;
320    }
321    void operator delete[](void* p) {
322        ::delete[] (char*)p;
323    }
324#endif
325};
326
327#endif /* __cplusplus */
328
329#include <adolc/param.h>
330#include <adolc/advector.h>
331
332#ifdef __cplusplus
333/****************************************************************************/
334/*                                                       INLINE DEFINITIONS */
335
336/*--------------------------------------------------------------------------*/
337inline locint badouble::loc( void ) const {
338    return location;
339}
340
341inline locint adouble::loc( void ) const {
342    const_cast<adouble*>(this)->initInternal();
343    return location;
344}
345
346/*--------------------------------------------------------------------------*/
347/* Comparison */
348
349#if !defined(ADOLC_ADVANCED_BRANCHING)
350inline int operator != ( const badouble& u, const badouble& v ) {
351    return (u-v != 0);
352}
353
354inline int operator == ( const badouble& u, const badouble& v ) {
355    return (u-v == 0);
356}
357
358inline int operator <= ( const badouble& u, const badouble& v ) {
359    return (u-v <= 0);
360}
361
362inline int operator >= ( const badouble& u, const badouble& v ) {
363    return (u-v >= 0);
364}
365
366inline int operator > ( const badouble& u, const badouble& v ) {
367    return (u-v > 0);
368}
369
370inline int operator < ( const badouble& u, const badouble& v ) {
371    return (u-v < 0);
372}
373#endif
374
375inline int operator != ( double coval, const badouble& v) {
376    if (coval)
377        return (-coval+v != 0);
378    else
379        return (v != 0);
380}
381
382inline int operator == ( double coval, const badouble& v) {
383    if (coval)
384        return (-coval+v == 0);
385    else
386        return (v == 0);
387}
388
389inline int operator <= ( double coval, const badouble& v ) {
390    if (coval)
391        return (-coval+v >= 0);
392    else
393        return (v >= 0);
394}
395
396inline int operator >= ( double coval, const badouble& v ) {
397    if (coval)
398        return (-coval+v <= 0);
399    else
400        return (v <= 0);
401}
402
403inline int operator > ( double coval, const badouble& v ) {
404    if (coval)
405        return (-coval+v < 0);
406    else
407        return (v < 0);
408}
409
410inline int operator < ( double coval, const badouble& v ) {
411    if (coval)
412        return (-coval+v > 0);
413    else
414        return (v > 0);
415}
416
417/*--------------------------------------------------------------------------*/
418/* Adding a floating point from an adouble  */
419inline adub operator + ( const badouble& x , double coval ) {
420    return coval + x;
421}
422
423/* Subtract a floating point from an adouble  */
424inline adub operator - ( const badouble& x , double coval ) {
425    return (-coval) + x;
426}
427
428/*--------------------------------------------------------------------------*/
429/* Multiply an adouble by a floating point */
430inline adub operator * (const badouble& x, double coval) {
431    return coval * x;
432}
433
434/*--------------------------------------------------------------------------*/
435/* Divide an adouble by a floating point */
436inline adub operator / (const badouble& x, double coval) {
437    return (1.0/coval) * x;
438}
439
440
441inline badouble& badouble::operator /= (const pdouble& p) {
442    *this *= recipr(p);
443    return *this;
444}
445/****************************************************************************/
446/*                                                                THAT'S ALL*/
447#endif /* __cplusplus */
448#endif /* ADOLC_ADOUBLE_H */
Note: See TracBrowser for help on using the repository browser.