source: trunk/ADOL-C/src/externfcts.cpp @ 71

Last change on this file since 71 was 42, checked in by awalther, 10 years ago

set svn keywords property

  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     externfcts.cpp
4 Revision: $Id: externfcts.cpp 42 2009-07-15 18:37:17Z awalther $
5 Contents: functions and data types for extern (differentiated) functions.
6 
7 Copyright (c) Andreas Kowarz
8 
9 This file is part of ADOL-C. This software is provided as open source.
10 Any use, reproduction, or distribution of the software constitutes
11 recipient's acceptance of the terms of the accompanying license file.
12         
13----------------------------------------------------------------------------*/
14
15#include <externfcts.h>
16#include <externfcts_p.h>
17#include <taping_p.h>
18#include <adouble.h>
19#include <oplate.h>
20#include <buffer_temp.h>
21
22#include <cstring>
23
24/****************************************************************************/
25/*                                    extern differentiated functions stuff */
26
27#define ADOLC_BUFFER_TYPE \
28   Buffer< ext_diff_fct, ADOLC_ext_fct, EDFCTS_BLOCK_SIZE >
29static ADOLC_BUFFER_TYPE buffer(init_ext_diff_fct);
30static int oldTraceFlag;
31
32ext_diff_fct *reg_ext_fct(ADOLC_ext_fct ext_fct) {
33    return buffer.append(ext_fct);
34}
35
36int call_ext_fct(ext_diff_fct *edfct,
37                 int n, double *xp, adouble *xa,
38                 int m, double *yp, adouble *ya)
39{
40    int i = 0, ret;
41    locint firstVal, numVals;
42    double *vals;
43    ADOLC_OPENMP_THREAD_NUMBER;
44    ADOLC_OPENMP_GET_THREAD_NUMBER;
45
46    if (edfct==NULL) fail(ADOLC_EXT_DIFF_NULLPOINTER_STRUCT);
47
48    if (ADOLC_CURRENT_TAPE_INFOS.traceFlag) {
49        put_op(ext_diff);
50        ADOLC_PUT_LOCINT(edfct->index);
51        ADOLC_PUT_LOCINT(n);
52        ADOLC_PUT_LOCINT(m);
53        ADOLC_PUT_LOCINT(xa[i].loc());
54        ADOLC_PUT_LOCINT(ya[i].loc());
55        ADOLC_PUT_LOCINT(0);               /* keep space for checkpointing index */
56
57        oldTraceFlag=ADOLC_CURRENT_TAPE_INFOS.traceFlag;
58        ADOLC_CURRENT_TAPE_INFOS.traceFlag=0;
59    } else oldTraceFlag=0;
60
61#if defined(ADOLC_SAFE_EXTERN)
62    firstVal = 0;
63#else
64    updateLocs();
65    firstVal = ADOLC_GLOBAL_TAPE_VARS.locMinUnused;
66#endif
67    numVals = ADOLC_GLOBAL_TAPE_VARS.numMaxAlive - firstVal;
68    vals = new double[numVals];
69    memcpy(vals, ADOLC_GLOBAL_TAPE_VARS.store + firstVal,
70            numVals * sizeof(double));
71
72    for (i=0; i<n; ++i) xp[i]=xa[i].getValue();
73    for (i=0; i<m; ++i) yp[i]=ya[i].getValue();
74
75    ret=edfct->function(n, xp, m, yp);
76
77    memcpy(ADOLC_GLOBAL_TAPE_VARS.store + firstVal, vals,
78            numVals * sizeof(double));
79    delete[] vals;
80
81    /* update taylor buffer if keep != 0 ; possible double counting as in
82     * adouble.cpp => correction in taping.c */
83
84    if (oldTraceFlag != 0) {
85        ADOLC_CURRENT_TAPE_INFOS.numTays_Tape += n;
86        ADOLC_CURRENT_TAPE_INFOS.numTays_Tape += m;
87        if (ADOLC_CURRENT_TAPE_INFOS.keepTaylors) {
88            for (i=0; i<n; ++i) ADOLC_WRITE_SCAYLOR(xa[i].getValue());
89            for (i=0; i<m; ++i) ADOLC_WRITE_SCAYLOR(ya[i].getValue());
90        }
91    }
92    /* write back */
93    for (i=0; i<n; ++i) xa[i].setValue(xp[i]);
94    for (i=0; i<m; ++i) ya[i].setValue(yp[i]);
95
96    ADOLC_CURRENT_TAPE_INFOS.traceFlag=oldTraceFlag;
97
98    return ret;
99}
100
101ext_diff_fct *get_ext_diff_fct( int index ) {
102    return buffer.getElement(index);
103}
104
105void init_ext_diff_fct(ext_diff_fct *edfct) {
106    char *ptr;
107
108    ptr = (char *)edfct;
109    for (unsigned int i = 0; i < sizeof(ext_diff_fct); ++i) ptr[i]=0;
110}
111
112/****************************************************************************/
113/*                                                               THAT'S ALL */
114
Note: See TracBrowser for help on using the repository browser.