ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
medipacksupport.h
Go to the documentation of this file.
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File: ampisupportAdolc.cpp
4 Revision: $Id$
5
6 Copyright (c) Jean Utke
7
8 This file is part of ADOL-C. This software is provided as open source.
9 Any use, reproduction, or distribution of the software constitutes
10 recipient's acceptance of the terms of the accompanying license file.
11
12----------------------------------------------------------------------------*/
13
14#if !defined(ADOLC_MEDISUPPORTADOLC_H)
15#define ADOLC_MEDISUPPORTADOLC_H 1
16
17#include <adolc/adtb_types.h>
18
19#include <medi/adToolImplCommon.hpp>
20#include <medi/adjointInterface.hpp>
21#include <medi/ampi/types/indexTypeHelper.hpp>
22#include <medi/medi.hpp>
23
24void mediAddHandle(medi::HandleBase *h);
27
34struct AdolcTool final
35 : public medi::ADToolImplCommon<AdolcTool, true, true, double, double,
36 double, int> {
37 typedef adouble Type;
38 typedef void AdjointType;
39 typedef double ModifiedType;
40 typedef double PrimalType;
41 typedef int IndexType;
42
43 static MPI_Datatype MpiType;
44 static MPI_Datatype ModifiedMpiType;
45 static MPI_Datatype PrimalMpiType;
46 static MPI_Datatype AdjointMpiType;
47
48 typedef medi::MpiTypeDefault<AdolcTool> MediType;
50 static medi::AMPI_Datatype MPI_INT_TYPE;
51
52 static medi::OperatorHelper<
53 medi::FunctionHelper<adouble, double, double, int, double, AdolcTool>>
55
56 static void initTypes() {
57 // create the mpi type for ADOL-c
58 MPI_Type_contiguous(sizeof(adouble), MPI_BYTE, &MpiType);
59 MPI_Type_commit(&MpiType);
60
61 ModifiedMpiType = MPI_DOUBLE;
62 PrimalMpiType = MPI_DOUBLE;
63 AdjointMpiType = MPI_DOUBLE;
64 }
65
66 static void init() {
67 initTypes();
68
69 MPI_TYPE = new MediType();
70
72 MPI_INT_TYPE = operatorHelper.MPI_INT_TYPE;
73
75 }
76
77 static void finalizeTypes() {}
78
79 static void finalize() {
80
82
83 operatorHelper.finalize();
84
85 if (nullptr != MPI_TYPE) {
86 delete MPI_TYPE;
87 MPI_TYPE = nullptr;
88 }
89
91 }
92
93 AdolcTool(MPI_Datatype primalMpiType, MPI_Datatype adjointMpiType)
94 : medi::ADToolImplCommon<AdolcTool, true, true, double, double, double,
95 int>(primalMpiType, adjointMpiType) {}
96
97 inline bool isActiveType() const { return true; }
98
99 inline bool isHandleRequired() const { return isTaping(); }
100
101 inline bool isOldPrimalsRequired() const { return true; }
102
103 inline void startAssembly(medi::HandleBase *h) const { MEDI_UNUSED(h); }
104
105 inline void addToolAction(medi::HandleBase *h) const {
106 if (NULL != h) {
107 mediAddHandle(h);
108 }
109 }
110
111 inline void stopAssembly(medi::HandleBase *h) const { MEDI_UNUSED(h); }
112
113 medi::AMPI_Op convertOperator(medi::AMPI_Op op) const {
114 return operatorHelper.convertOperator(op);
115 }
116
117 inline void createPrimalTypeBuffer(PrimalType *&buf, size_t size) const {
118 buf = new PrimalType[size];
119 }
120
121 inline void createIndexTypeBuffer(IndexType *&buf, size_t size) const {
122 buf = new IndexType[size];
123 }
124
125 inline void deletePrimalTypeBuffer(PrimalType *&buf) const {
126 if (NULL != buf) {
127 delete[] buf;
128 buf = NULL;
129 }
130 }
131
132 inline void deleteIndexTypeBuffer(IndexType *&buf) const {
133 if (NULL != buf) {
134 delete[] buf;
135 buf = NULL;
136 }
137 }
138
139 static inline int getIndex(const Type &value) { return value.loc(); }
140
141 static inline void clearIndex(Type &value) {
142 // do nothing
143 }
144
145 static inline PrimalType getValue(const Type &value) { return value.value(); }
146
147 static inline void setIntoModifyBuffer(ModifiedType &modValue,
148 const Type &value) {
149 modValue = value.value();
150 }
151
152 static inline void getFromModifyBuffer(const ModifiedType &modValue,
153 Type &value) {
154 value.setValue(modValue);
155 }
156
157 static void createIndex(Type &value, IndexType &index) {
158 MEDI_UNUSED(value);
159 MEDI_UNUSED(index);
160
161 // do nothing indices are created in registerValue
162 }
163
164 static inline void registerValue(Type &value, PrimalType &oldPrimal,
165 IndexType &index) {
166 MEDI_UNUSED(oldPrimal);
167 // do nothing value should have an index
168
169 index = value.loc();
170 }
171
172 static PrimalType getPrimalFromMod(const ModifiedType &modValue) {
173 return modValue;
174 }
175
176 static void setPrimalToMod(ModifiedType &modValue, const PrimalType &value) {
177 modValue = value;
178 }
179
180 static void modifyDependency(ModifiedType &inval, ModifiedType &inoutval) {
181 MEDI_UNUSED(inval);
182 MEDI_UNUSED(inoutval);
183
184 // no dependency tracking possible
185 }
186};
187
188#endif
Taped active scalar for reusable forward and reverse differentiation.
Definition adtb_types.h:211
size_t loc() const
Retrieves the location of the adouble on the tape.
Definition adtb_types.h:332
double value() const
Retrieves the current value stored at the tape location.
Definition adtb_types.h:323
void mediFinalizeStatic()
void mediInitStatic()
void mediAddHandle(medi::HandleBase *h)
MeDiPack adapter that connects taped ADOL-C values with AMPI.
Definition medipacksupport.h:36
static PrimalType getValue(const Type &value)
Definition medipacksupport.h:145
static void modifyDependency(ModifiedType &inval, ModifiedType &inoutval)
Definition medipacksupport.h:180
static MPI_Datatype AdjointMpiType
Definition medipacksupport.h:46
bool isActiveType() const
Definition medipacksupport.h:97
static void clearIndex(Type &value)
Definition medipacksupport.h:141
void deletePrimalTypeBuffer(PrimalType *&buf) const
Definition medipacksupport.h:125
static void getFromModifyBuffer(const ModifiedType &modValue, Type &value)
Definition medipacksupport.h:152
static void init()
Definition medipacksupport.h:66
static void createIndex(Type &value, IndexType &index)
Definition medipacksupport.h:157
static MPI_Datatype MpiType
Definition medipacksupport.h:43
static MediType * MPI_TYPE
Definition medipacksupport.h:49
adouble Type
Definition medipacksupport.h:37
AdolcTool(MPI_Datatype primalMpiType, MPI_Datatype adjointMpiType)
Definition medipacksupport.h:93
static void registerValue(Type &value, PrimalType &oldPrimal, IndexType &index)
Definition medipacksupport.h:164
static medi::AMPI_Datatype MPI_INT_TYPE
Definition medipacksupport.h:50
bool isOldPrimalsRequired() const
Definition medipacksupport.h:101
medi::AMPI_Op convertOperator(medi::AMPI_Op op) const
Definition medipacksupport.h:113
static PrimalType getPrimalFromMod(const ModifiedType &modValue)
Definition medipacksupport.h:172
static void initTypes()
Definition medipacksupport.h:56
void addToolAction(medi::HandleBase *h) const
Definition medipacksupport.h:105
static void setPrimalToMod(ModifiedType &modValue, const PrimalType &value)
Definition medipacksupport.h:176
void stopAssembly(medi::HandleBase *h) const
Definition medipacksupport.h:111
int IndexType
Definition medipacksupport.h:41
void startAssembly(medi::HandleBase *h) const
Definition medipacksupport.h:103
static void finalize()
Definition medipacksupport.h:79
double ModifiedType
Definition medipacksupport.h:39
void createPrimalTypeBuffer(PrimalType *&buf, size_t size) const
Definition medipacksupport.h:117
void AdjointType
Definition medipacksupport.h:38
double PrimalType
Definition medipacksupport.h:40
static int getIndex(const Type &value)
Definition medipacksupport.h:139
static MPI_Datatype ModifiedMpiType
Definition medipacksupport.h:44
static MPI_Datatype PrimalMpiType
Definition medipacksupport.h:45
medi::MpiTypeDefault< AdolcTool > MediType
Definition medipacksupport.h:48
void createIndexTypeBuffer(IndexType *&buf, size_t size) const
Definition medipacksupport.h:121
static void finalizeTypes()
Definition medipacksupport.h:77
static medi::OperatorHelper< medi::FunctionHelper< adouble, double, double, int, double, AdolcTool > > operatorHelper
Definition medipacksupport.h:54
bool isHandleRequired() const
Definition medipacksupport.h:99
static void setIntoModifyBuffer(ModifiedType &modValue, const Type &value)
Definition medipacksupport.h:147
void deleteIndexTypeBuffer(IndexType *&buf) const
Definition medipacksupport.h:132