Changeset 736


Ignore:
Timestamp:
May 30, 2017 4:48:05 AM (6 months ago)
Author:
kulshres
Message:

design a C++ object oriented wrapper for external functions

it is easier to work with classes, inheritence and overloaded member
functions in scripting languages like python and R instead of raw
pointers to functions in order to call arbitrary code.

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

Location:
trunk/ADOL-C
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/ADOL-C/examples/additional_examples/ext_diff_func/Makefile.am

    r623 r736  
    2121
    2222if ADDEXA
    23 noinst_PROGRAMS        = ext_diff_func
     23noinst_PROGRAMS        = ext_diff_func edfootest
    2424endif
    2525
    2626ext_diff_func_SOURCES  = ext_diff_func.cpp
     27
     28edfootest_SOURCES      = edfootest.cpp
  • trunk/ADOL-C/include/adolc/Makefile.am

    r710 r736  
    1818                       revolve.h advector.h \
    1919                       adolc_fatalerror.h \
    20                        adtl.h adoublecuda.h param.h externfcts2.h
     20                       adtl.h adoublecuda.h \
     21                       param.h externfcts2.h \
     22                       edfclasses.h
    2123
    2224SUBDIRS = internal drivers tapedoc lie
  • trunk/ADOL-C/include/adolc/adolc.h

    r656 r736  
    2828#  include <adolc/adouble.h>
    2929#  include <adolc/externfcts.h>
    30 #  include <adolc/externfcts2.h>
    3130#  include <adolc/checkpointing.h>
    3231#  include <adolc/fixpoint.h>
  • trunk/ADOL-C/include/adolc/externfcts.h

    r735 r736  
    268268END_C_DECLS
    269269
     270#include <adolc/externfcts2.h>
     271
    270272#if defined(__cplusplus)
    271273/****************************************************************************/
     
    288290ADOLC_DLL_EXPORT void edf_zero(ext_diff_fct *edfct);
    289291
     292#include <adolc/edfclasses.h>
     293
    290294#endif /* __CPLUSPLUS */
    291295
  • trunk/ADOL-C/src/externfcts.cpp

    r735 r736  
    196196  for (int i=0; i<n; ++i) edfct->dp_x[i]=xa[i].getValue();
    197197  if (edfct->dp_y_priorRequired) for (int i=0; i<m; ++i) edfct->dp_y[i]=ya[i].getValue();
     198  ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index = edfct->index;
    198199}
    199200
     
    260261}
    261262
     263static int edfoo_wrapper_function(int n, double *x, int m, double* y) {
     264    ext_diff_fct* edf;
     265    EDFobject* ebase;
     266    ADOLC_OPENMP_THREAD_NUMBER;
     267    ADOLC_OPENMP_GET_THREAD_NUMBER;
     268    // figure out which edf
     269    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     270    ebase = reinterpret_cast<EDFobject*>(edf->obj);
     271    return ebase->function(n,x,m,y);
     272}
     273static int edfoo_wrapper_zos_forward(int n, double *x, int m, double* y) {
     274    ext_diff_fct* edf;
     275    EDFobject* ebase;
     276    ADOLC_OPENMP_THREAD_NUMBER;
     277    ADOLC_OPENMP_GET_THREAD_NUMBER;
     278    // figure out which edf
     279    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     280    ebase = reinterpret_cast<EDFobject*>(edf->obj);
     281    return ebase->zos_forward(n,x,m,y);
     282}
     283
     284static int edfoo_wrapper_fos_forward(int n, double *dp_x, double *dp_X, int m, double *dp_y, double *dp_Y) {
     285    ext_diff_fct* edf;
     286    EDFobject* ebase;
     287    ADOLC_OPENMP_THREAD_NUMBER;
     288    ADOLC_OPENMP_GET_THREAD_NUMBER;
     289    // figure out which edf
     290    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     291    ebase = reinterpret_cast<EDFobject*>(edf->obj);
     292    return ebase->fos_forward(n,dp_x,dp_X,m,dp_y,dp_Y);
     293}
     294
     295static int edfoo_wrapper_fov_forward(int n, double *dp_x, int p, double **dpp_X, int m, double *dp_y, double **dpp_Y) {
     296    ext_diff_fct* edf;
     297    EDFobject* ebase;
     298    ADOLC_OPENMP_THREAD_NUMBER;
     299    ADOLC_OPENMP_GET_THREAD_NUMBER;
     300    // figure out which edf
     301    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     302    ebase = reinterpret_cast<EDFobject*>(edf->obj);
     303    return ebase->fov_forward(n,dp_x,p,dpp_X,m,dp_y,dpp_Y);   
     304}
     305
     306static int edfoo_wrapper_fos_reverse(int m, double *dp_U, int n, double *dp_Z, double *dp_x, double *dp_y) {
     307    ext_diff_fct* edf;
     308    EDFobject* ebase;
     309    ADOLC_OPENMP_THREAD_NUMBER;
     310    ADOLC_OPENMP_GET_THREAD_NUMBER;
     311    // figure out which edf
     312    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     313    ebase = reinterpret_cast<EDFobject*>(edf->obj);
     314    return ebase->fos_reverse(m,dp_U,n,dp_Z,dp_x,dp_y);   
     315}
     316static int edfoo_wrapper_fov_reverse(int m, int p, double **dpp_U, int n, double **dpp_Z, double *dp_x, double *dp_y) {
     317    ext_diff_fct* edf;
     318    EDFobject* ebase;
     319    ADOLC_OPENMP_THREAD_NUMBER;
     320    ADOLC_OPENMP_GET_THREAD_NUMBER;
     321    // figure out which edf
     322    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     323    ebase = reinterpret_cast<EDFobject*>(edf->obj);
     324    return ebase->fov_reverse(m,p,dpp_U,n,dpp_Z,dp_x,dp_y);   
     325}
     326
     327void EDFobject::init_edf(EDFobject* ebase) {
     328    edf = buffer.append();
     329    edf->obj = reinterpret_cast<void*>(ebase);
     330    edf->function = edfoo_wrapper_function;
     331    edf->zos_forward = edfoo_wrapper_zos_forward;
     332    edf->fos_forward = edfoo_wrapper_fos_forward;
     333    edf->fov_forward = edfoo_wrapper_fov_forward;
     334    edf->fos_reverse = edfoo_wrapper_fos_reverse;
     335    edf->fov_reverse = edfoo_wrapper_fov_reverse;   
     336}
     337
     338static int edfoo_iarr_wrapper_function(int iArrLength, int *iArr, int n, double *x, int m, double* y) {
     339    ext_diff_fct* edf;
     340    EDFobject_iArr* ebase;
     341    ADOLC_OPENMP_THREAD_NUMBER;
     342    ADOLC_OPENMP_GET_THREAD_NUMBER;
     343    // figure out which edf
     344    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     345    ebase = reinterpret_cast<EDFobject_iArr*>(edf->obj);
     346    return ebase->function(iArrLength,iArr,n,x,m,y);
     347}
     348static int edfoo_iarr_wrapper_zos_forward(int iArrLength, int *iArr, int n, double *x, int m, double* y) {
     349    ext_diff_fct* edf;
     350    EDFobject_iArr* ebase;
     351    ADOLC_OPENMP_THREAD_NUMBER;
     352    ADOLC_OPENMP_GET_THREAD_NUMBER;
     353    // figure out which edf
     354    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     355    ebase = reinterpret_cast<EDFobject_iArr*>(edf->obj);
     356    return ebase->zos_forward(iArrLength,iArr,n,x,m,y);
     357}
     358
     359static int edfoo_iarr_wrapper_fos_forward(int iArrLength, int *iArr, int n, double *dp_x, double *dp_X, int m, double *dp_y, double *dp_Y) {
     360    ext_diff_fct* edf;
     361    EDFobject_iArr* ebase;
     362    ADOLC_OPENMP_THREAD_NUMBER;
     363    ADOLC_OPENMP_GET_THREAD_NUMBER;
     364    // figure out which edf
     365    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     366    ebase = reinterpret_cast<EDFobject_iArr*>(edf->obj);
     367    return ebase->fos_forward(iArrLength,iArr,n,dp_x,dp_X,m,dp_y,dp_Y);
     368}
     369
     370static int edfoo_iarr_wrapper_fov_forward(int iArrLength, int *iArr, int n, double *dp_x, int p, double **dpp_X, int m, double *dp_y, double **dpp_Y) {
     371    ext_diff_fct* edf;
     372    EDFobject_iArr* ebase;
     373    ADOLC_OPENMP_THREAD_NUMBER;
     374    ADOLC_OPENMP_GET_THREAD_NUMBER;
     375    // figure out which edf
     376    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     377    ebase = reinterpret_cast<EDFobject_iArr*>(edf->obj);
     378    return ebase->fov_forward(iArrLength,iArr,n,dp_x,p,dpp_X,m,dp_y,dpp_Y);   
     379}
     380
     381static int edfoo_iarr_wrapper_fos_reverse(int iArrLength, int *iArr, int m, double *dp_U, int n, double *dp_Z, double *dp_x, double *dp_y) {
     382    ext_diff_fct* edf;
     383    EDFobject_iArr* ebase;
     384    ADOLC_OPENMP_THREAD_NUMBER;
     385    ADOLC_OPENMP_GET_THREAD_NUMBER;
     386    // figure out which edf
     387    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     388    ebase = reinterpret_cast<EDFobject_iArr*>(edf->obj);
     389    return ebase->fos_reverse(iArrLength,iArr,m,dp_U,n,dp_Z,dp_x,dp_y);   
     390}
     391static int edfoo_iarr_wrapper_fov_reverse(int iArrLength, int *iArr, int m, int p, double **dpp_U, int n, double **dpp_Z, double *dp_x, double *dp_y) {
     392    ext_diff_fct* edf;
     393    EDFobject_iArr* ebase;
     394    ADOLC_OPENMP_THREAD_NUMBER;
     395    ADOLC_OPENMP_GET_THREAD_NUMBER;
     396    // figure out which edf
     397    edf = get_ext_diff_fct(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     398    ebase = reinterpret_cast<EDFobject_iArr*>(edf->obj);
     399    return ebase->fov_reverse(iArrLength,iArr,m,p,dpp_U,n,dpp_Z,dp_x,dp_y);   
     400}
     401
     402void EDFobject_iArr::init_edf(EDFobject_iArr* ebase) {
     403    edf = buffer.append();
     404    edf->obj = reinterpret_cast<void*>(ebase);
     405    edf->function = edfoo_wrapper_function;
     406    edf->zos_forward_iArr = edfoo_iarr_wrapper_zos_forward;
     407    edf->fos_forward_iArr = edfoo_iarr_wrapper_fos_forward;
     408    edf->fov_forward_iArr = edfoo_iarr_wrapper_fov_forward;
     409    edf->fos_reverse_iArr = edfoo_iarr_wrapper_fos_reverse;
     410    edf->fov_reverse_iArr = edfoo_iarr_wrapper_fov_reverse;   
     411}
     412
    262413/****************************************************************************/
    263414/*                                                               THAT'S ALL */
  • trunk/ADOL-C/src/externfcts2.cpp

    r735 r736  
    1414
    1515#include "taping_p.h"
    16 #include <adolc/externfcts2.h>
     16#include <adolc/externfcts.h>
    1717#include "externfcts_p.h"
    1818#include <adolc/adouble.h>
     
    178178                edfct->y[i][j] = y[i][j].getValue();
    179179
     180    ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index = edfct->index;
    180181    ret=edfct->function(iArrLen,iArr,nin,nout,insz,edfct->x,outsz,edfct->y,edfct->context);
    181182
     
    201202    return buffer.getElement(index);
    202203}
     204
     205static int edfoo_v2_wrapper_function(int iArrLen, int *iArr, int nin, int nout, int *insz, double **x, int *outsz, double **y, void* ctx) {
     206    ext_diff_fct_v2* edf;
     207    EDFobject_v2* ebase;
     208    ADOLC_OPENMP_THREAD_NUMBER;
     209    ADOLC_OPENMP_GET_THREAD_NUMBER;
     210    // figure out which edf
     211    edf = get_ext_diff_fct_v2(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     212    ebase = reinterpret_cast<EDFobject_v2*>(edf->obj);
     213    return ebase->function(iArrLen,iArr,nin,nout,insz,x,outsz,y,ctx);
     214}
     215static int edfoo_v2_wrapper_zos_forward(int iArrLen, int *iArr, int nin, int nout, int *insz, double **x, int *outsz, double **y, void* ctx) {
     216    ext_diff_fct_v2* edf;
     217    EDFobject_v2* ebase;
     218    ADOLC_OPENMP_THREAD_NUMBER;
     219    ADOLC_OPENMP_GET_THREAD_NUMBER;
     220    // figure out which edf
     221    edf = get_ext_diff_fct_v2(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     222    ebase = reinterpret_cast<EDFobject_v2*>(edf->obj);
     223    return ebase->zos_forward(iArrLen,iArr,nin,nout,insz,x,outsz,y,ctx);
     224}
     225static int edfoo_v2_wrapper_fos_forward(int iArrLen, int* iArr, int nin, int nout, int *insz, double **x, double **xp, int *outsz, double **y, double **yp, void *ctx) {
     226    ext_diff_fct_v2* edf;
     227    EDFobject_v2* ebase;
     228    ADOLC_OPENMP_THREAD_NUMBER;
     229    ADOLC_OPENMP_GET_THREAD_NUMBER;
     230    // figure out which edf
     231    edf = get_ext_diff_fct_v2(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     232    ebase = reinterpret_cast<EDFobject_v2*>(edf->obj);
     233    return ebase->fos_forward(iArrLen,iArr,nin,nout,insz,x,xp,outsz,y,yp,ctx);
     234}
     235static int edfoo_v2_wrapper_fov_forward(int iArrLen, int* iArr, int nin, int nout, int *insz, double **x, int ndir, double ***Xp, int *outsz, double **y, double ***Yp, void* ctx) {
     236    ext_diff_fct_v2* edf;
     237    EDFobject_v2* ebase;
     238    ADOLC_OPENMP_THREAD_NUMBER;
     239    ADOLC_OPENMP_GET_THREAD_NUMBER;
     240    // figure out which edf
     241    edf = get_ext_diff_fct_v2(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     242    ebase = reinterpret_cast<EDFobject_v2*>(edf->obj);
     243    return ebase->fov_forward(iArrLen,iArr,nin,nout,insz,x,ndir,Xp,outsz,y,Yp,ctx);
     244}
     245static int edfoo_v2_wrapper_fos_reverse(int iArrLen, int* iArr, int nout, int nin, int *outsz, double **up, int *insz, double **zp, double **x, double **y, void *ctx) {
     246    ext_diff_fct_v2* edf;
     247    EDFobject_v2* ebase;
     248    ADOLC_OPENMP_THREAD_NUMBER;
     249    ADOLC_OPENMP_GET_THREAD_NUMBER;
     250    // figure out which edf
     251    edf = get_ext_diff_fct_v2(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     252    ebase = reinterpret_cast<EDFobject_v2*>(edf->obj);
     253    return ebase->fos_reverse(iArrLen,iArr,nout,nin,outsz,up,insz,zp,x,y,ctx);
     254}
     255static int edfoo_v2_wrapper_fov_reverse(int iArrLen, int* iArr, int nout, int nin, int *outsz, int dir, double ***Up, int *insz, double ***Zp, double **x, double **y, void* ctx) {
     256    ext_diff_fct_v2* edf;
     257    EDFobject_v2* ebase;
     258    ADOLC_OPENMP_THREAD_NUMBER;
     259    ADOLC_OPENMP_GET_THREAD_NUMBER;
     260    // figure out which edf
     261    edf = get_ext_diff_fct_v2(ADOLC_CURRENT_TAPE_INFOS.ext_diff_fct_index);
     262    ebase = reinterpret_cast<EDFobject_v2*>(edf->obj);
     263    return ebase->fov_reverse(iArrLen,iArr,nout,nin,outsz,dir,Up,insz,Zp,x,y,ctx);
     264}
     265
     266void EDFobject_v2::init_edf(EDFobject_v2* ebase) {
     267    edf = buffer.append();
     268    edf->obj = reinterpret_cast<void*>(ebase);
     269    edf->function = edfoo_v2_wrapper_function;
     270    edf->zos_forward = edfoo_v2_wrapper_zos_forward;
     271    edf->fos_forward = edfoo_v2_wrapper_fos_forward;
     272    edf->fov_forward = edfoo_v2_wrapper_fov_forward;
     273    edf->fos_reverse = edfoo_v2_wrapper_fos_reverse;
     274    edf->fov_reverse = edfoo_v2_wrapper_fov_reverse;   
     275}
Note: See TracChangeset for help on using the changeset viewer.