Actual source code: ad_grad.h

      1: /*
      2:   THIS PROGRAM DISCLOSES MATERIAL PROTECTABLE UNDER COPYRIGHT
      3:   LAWS OF THE UNITED STATES.  FOR LICENSING INFORMATION CONTACT:

      5:   Paul Hovland and Boyana Norris, Mathematics and Computer Science Division,
      6:   Argonne National Laboratory, 9700 S. Cass Avenue, Argonne IL 60439,
      7:   {hovland,norris}@mcs.anl.gov.
      8: */

     10: #include "ad_grad_macro_axpys.h"

     12: #if !defined(AD_GRAD_H)
     13: #define AD_GRAD_H

     15: #include <string.h>

     21: #   if defined(__cplusplus)
     23: #   endif
     24:
     25: #define ad_AD_GradInitMPI(pargc, pargv) \
     26:     { \
     27:         ad_mpi_init(pargc, pargv, &ad_total_grad_size); \
     28:     }

     31: #define ad_AD_GradFinalMPI() \
     32:     { \
     33:         ad_mpi_final(); \
     34:     }

     36: #define ad_AD_GradFinal() \
     37:     { \
     38:         ad_grad_size = 0; \
     39:     }

     43: #define ad_AD_GradInit(n) \
     44:     { \
     45:         if (n == -1) \
     46:            ad_grad_size = ad_GRAD_MAX; \
     47:         else \
     48:            ad_grad_size = n; \
     49:         ad_grad_size_shadow = 0; \
     50:     }

     53: #define ad_AD_ClearGrad(gz) memset((char*)gz, 0, ad_GRAD_MAX*sizeof(double));

     55: #define ad_AD_ClearGrad2(gz)\
     56:     {\
     57:         int iWiLlNeVeRCoNfLiCt0;\
     58:         for (iWiLlNeVeRCoNfLiCt0 = 0 ; iWiLlNeVeRCoNfLiCt0 < ad_GRAD_MAX; \
     59:              iWiLlNeVeRCoNfLiCt0++) {\
     60:             gz[iWiLlNeVeRCoNfLiCt0] = 0.0;\
     61:         }\
     62:     }

     64: #define ad_AD_ClearGradArray(ggz,size)\
     65:     {\
     66:         int iWiLlNeVeRCoNfLiCt0;\
     67:         for (iWiLlNeVeRCoNfLiCt0 = 0 ; iWiLlNeVeRCoNfLiCt0 < size; \
     68:              iWiLlNeVeRCoNfLiCt0++) {\
     69:             ad_AD_ClearGrad(DERIV_grad((ggz)[iWiLlNeVeRCoNfLiCt0])); \
     70:         }\
     71:     }

     73: #define ad_AD_CopyGrad(gz,gx) \
     74:     {\
     75:         int iWiLlNeVeRCoNfLiCt0;\
     76:         for (iWiLlNeVeRCoNfLiCt0 = 0 ; iWiLlNeVeRCoNfLiCt0 < ad_GRAD_MAX;\
     77:              iWiLlNeVeRCoNfLiCt0++) {\
     78:             gz[iWiLlNeVeRCoNfLiCt0] = gx[iWiLlNeVeRCoNfLiCt0];\
     79:         }\
     80:     }

     82: #   define ad_AD_GetTotalGradSize() ad_grad_size
     83: #   define ad_AD_SetTotalGradSize(x) ad_grad_size = x

     85: #   define ad_AD_IncrementTotalGradSize(x) \
     86:     { \
     87:          if (x + ad_grad_size_shadow > ad_GRAD_MAX) {\
     88:                 SETERRQ2(PETSC_ERR_USER, "The number of independent variables exceeds the maximum compiled for!\n Edit your program and change Process adiC(%d) to Process adiC(%d)", x + ad_grad_size_shadow,ad_GRAD_MAX);\
     89:         }\
     90:         ad_grad_size_shadow += x;\
     91:     }

     93: #   define ad_AD_ExtractGrad(a, var) \
     94:     { \
     95:         int pOsItIoN; \
     96:         for (pOsItIoN = 0; pOsItIoN < ad_grad_size; pOsItIoN++) {\
     97:             (a)[pOsItIoN] = DERIV_grad(var)[pOsItIoN];  \
     98:         }\
     99:     }
    100: #   define ad_AD_ExtractVal(a, var) \
    101:     { \
    102:         a = DERIV_val(var); \
    103:     }
    104: #   define ad_AD_SetGrad(a, var) \
    105:     { \
    106:         int pOsItIoN; \
    107:         for (pOsItIoN = 0; pOsItIoN < ad_grad_size; pOsItIoN++) {\
    108:             DERIV_grad(var)[pOsItIoN] = (a)[pOsItIoN];  \
    109:         }\
    110:     }

    112: #   define ad_AD_SetIndepDone() ad_AD_CommitShadowVar()
    113: #   define ad_AD_ResetIndep() ad_AD_ResetShadowVar()
    114: #   define ad_AD_SetIndep(var) \
    115:     { \
    116:         int pOsItIoN = ad_AD_IncrShadowVar(); \
    117:         if (pOsItIoN > ad_GRAD_MAX) {\
    118:           SETERRQ2(PETSC_ERR_USER, "The number of independent variables exceeds the maximum compiled for!\n Edit your program and change Process adiC(%d) to Process adiC(%d)",pOsItIoN ,ad_GRAD_MAX);\
    119:         }\
    120:         ad_AD_ClearGrad(DERIV_grad(var)); \
    121:         DERIV_grad(var)[pOsItIoN] = 1; \
    122:     }
    123: #   define ad_AD_SetIndepArray(vars, size) \
    124:     { \
    125:         int iWiLlNeVeRCoNfLiCt; \
    126:         for (iWiLlNeVeRCoNfLiCt = 0; iWiLlNeVeRCoNfLiCt < size; \
    127:                iWiLlNeVeRCoNfLiCt++) { \
    128:             ad_AD_ClearGrad(DERIV_grad((vars)[iWiLlNeVeRCoNfLiCt])); \
    129:             DERIV_grad((vars)[iWiLlNeVeRCoNfLiCt])[ad_AD_IncrShadowVar()] = 1; \
    130:         } \
    131:     }

    133: #   define ad_AD_SetIndepArrayElement(var, index) \
    134:     { \
    135:        ad_AD_ClearGrad(DERIV_grad((var)[index])); \
    136:        DERIV_grad((var)[index])[ad_AD_IncrShadowVar()] = 1; \
    137:     }

    139: #   define ad_AD_SetIndepArrayColored(vars, size, colors) \
    140:     { \
    141:         int iWiLlNeVeRCoNfLiCt; \
    142:         for (iWiLlNeVeRCoNfLiCt = 0; iWiLlNeVeRCoNfLiCt < size; \
    143:                iWiLlNeVeRCoNfLiCt++) { \
    144:             ad_AD_ClearGrad2(DERIV_grad((vars)[iWiLlNeVeRCoNfLiCt])); \
    145:             DERIV_grad((vars)[iWiLlNeVeRCoNfLiCt])[ad_grad_size_shadow+(colors)[iWiLlNeVeRCoNfLiCt]] = 1; \
    146:         } \
    147:     }

    149: /* values array is the same length as vars */
    150: #   define ad_AD_SetIndepVector(vars, size, values) \
    151:     { \
    152:         int iWiLlNeVeRCoNfLiCt; \
    153:         for (iWiLlNeVeRCoNfLiCt = 0; iWiLlNeVeRCoNfLiCt < size; \
    154:                iWiLlNeVeRCoNfLiCt++) { \
    155:             ad_AD_ClearGrad(DERIV_grad((vars)[iWiLlNeVeRCoNfLiCt])); \
    156:             DERIV_grad((vars)[iWiLlNeVeRCoNfLiCt])[ad_grad_size_shadow] = (values)[iWiLlNeVeRCoNfLiCt]; \
    157:         } \
    158:     }

    160: #define ad_AD_SetValArray(vars, size, values) \
    161:     { \
    162:         int iWiLlNeVeRCoNfLiCt; \
    163:         for (iWiLlNeVeRCoNfLiCt = 0; iWiLlNeVeRCoNfLiCt < size; \
    164:                iWiLlNeVeRCoNfLiCt++) { \
    165:             DERIV_val((vars)[iWiLlNeVeRCoNfLiCt]) = (values)[iWiLlNeVeRCoNfLiCt]; \
    166:         } \
    167:     }

    174: #   if defined(__cplusplus)
    175:         }
    176: #  endif
    177: #endif /*AD_GRAD_H*/