CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
externalFunctionUserData.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <vector>
38
39#include "../../config.h"
40#include "../../misc/macros.hpp"
41
43namespace codi {
44
60 private:
61
62 struct DataItemBase {
63 public:
64 void* data;
65
66 virtual ~DataItemBase() {}
67
68 virtual DataItemBase* clone() = 0;
69 };
70
71 template<typename T_Type>
72 struct DataItem : public DataItemBase {
73 public:
74 using Type = CODI_DD(T_Type, CODI_ANY);
75
76 explicit DataItem(Type const& value) {
77 data = (void*)new Type(value);
78 }
79
80 ~DataItem() {
81 Type* pointer = (Type*)data;
82 delete pointer;
83 }
84
85 DataItemBase* clone() {
86 return new DataItem<Type>(*((Type*)data));
87 }
88 };
89
90 template<typename Type>
91 struct DataArray : public DataItemBase {
92 private:
93 int size;
94
95 public:
96 DataArray(Type const* value, int size) {
97 data = (void*)new Type[size];
98 this->size = size;
99 std::copy(value, &value[size], (Type*)data);
100 }
101
102 ~DataArray() {
103 Type* pointer = (Type*)data;
104 delete[] pointer;
105 }
106
107 DataItemBase* clone() {
108 return new DataArray<Type>(*((Type*)data), size);
109 }
110 };
111
112 std::vector<DataItemBase*> store;
113
114 size_t storePos;
115
116 public:
117
119 ExternalFunctionUserData() : storePos(0) {}
120
123 copyAll(other, *this);
124 }
125
130
133 this->clear();
134 copyAll(other, *this);
135
136 return *this;
137 }
138
140 void clear() {
141 for (size_t i = 0; i < store.size(); ++i) {
142 delete store[i];
143 }
144 store.clear();
145 }
146
150 template<typename Type>
151 size_t addData(Type const& value) {
152 store.push_back(new DataItem<Type>(value));
153 return store.size() - 1;
154 }
155
159 template<typename Type>
160 size_t addData(Type const* value, int const size) {
161 store.push_back(new DataArray<Type>(value, size));
162 return store.size() - 1;
163 }
164
165 /*******************************************************************************/
168
170 template<typename Type>
171 void getData(Type& value) {
172 getData<Type>(&value, 1);
173 }
174
176 template<typename Type>
177 Type const& getData() {
178 Type* data = nextStore<Type>();
179 return *data;
180 }
181
183 template<typename Type>
184 Type& getDataRef() {
185 Type* data = nextStore<Type>();
186 return *data;
187 }
188
190 template<typename Type>
191 void getData(Type* value, int const size) {
192 Type* convPointer = nextStore<Type>();
193
194 std::copy(convPointer, &convPointer[size], value);
195 }
196
198 template<typename Type>
199 Type const* getDataArray() {
200 Type* data = nextStore<Type>();
201 return *data;
202 }
203
205 void resetPos() {
206 storePos = 0;
207 }
208
210
211 /*******************************************************************************/
214
218 template<typename Type>
219 void getDataByIndex(Type& value, size_t pos) {
220 getDataByIndex<Type>(&value, 1, pos);
221 }
222
226 template<typename Type>
227 Type const& getDataByIndex(size_t pos) {
228 Type* data = getStore<Type>(pos);
229 return *data;
230 }
231
235 template<typename Type>
236 Type& getDataRefByIndex(size_t pos) {
237 Type* data = getStore<Type>(pos);
238 return *data;
239 }
240
244 template<typename Type>
245 void getDataByIndex(Type* value, int const size, size_t pos) {
246 Type* convPointer = getStore<Type>(pos);
247
248 std::copy(convPointer, &convPointer[size], value);
249 }
250
252 template<typename Type>
253 Type const* getDataArrayByIndex() {
254 Type* data = nextStore<Type>();
255 return *data;
256 }
257
259
260 private:
261
262 static void copyAll(ExternalFunctionUserData const& from, ExternalFunctionUserData& to) {
263 for (size_t i = 0; i < from.store.size(); ++i) {
264 to.store.push_back(from.store[i]->clone());
265 }
266 to.storePos = from.storePos;
267 }
268
269 template<typename Type>
270 Type* getStore(size_t pos) {
271 return (Type*)store[pos]->data;
272 }
273
274 template<typename Type>
275 Type* nextStore() {
276 Type* pointer = getStore<Type>(storePos);
277 storePos += 1;
278 if (storePos >= store.size()) {
279 storePos = 0;
280 }
281
282 return pointer;
283 }
284 };
285}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:98
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Ease of access structure for user-provided data on the tape for external functions....
Definition externalFunctionUserData.hpp:59
void getDataByIndex(Type *value, int const size, size_t pos)
Definition externalFunctionUserData.hpp:245
void getData(Type &value)
Get a copy of the next data item.
Definition externalFunctionUserData.hpp:171
ExternalFunctionUserData & operator=(ExternalFunctionUserData const &other)
Copy operator.
Definition externalFunctionUserData.hpp:132
void getDataByIndex(Type &value, size_t pos)
Definition externalFunctionUserData.hpp:219
Type const * getDataArrayByIndex()
Get the address of a data item based on the index. Intended for reference access to array items.
Definition externalFunctionUserData.hpp:253
Type & getDataRefByIndex(size_t pos)
Definition externalFunctionUserData.hpp:236
ExternalFunctionUserData()
Constructor.
Definition externalFunctionUserData.hpp:119
ExternalFunctionUserData(ExternalFunctionUserData const &other)
Constructor.
Definition externalFunctionUserData.hpp:122
~ExternalFunctionUserData()
Destructor.
Definition externalFunctionUserData.hpp:127
Type const & getDataByIndex(size_t pos)
Definition externalFunctionUserData.hpp:227
void getData(Type *value, int const size)
Get the next data item and copy it as an array. The target array must have the correct size.
Definition externalFunctionUserData.hpp:191
Type const * getDataArray()
Get the address of the next data item. Intended for reference access to array items.
Definition externalFunctionUserData.hpp:199
void resetPos()
Manually reset the position.
Definition externalFunctionUserData.hpp:205
void clear()
Delete all data entries.
Definition externalFunctionUserData.hpp:140
Type & getDataRef()
Get a reference to the next data item.
Definition externalFunctionUserData.hpp:184
Type const & getData()
Get a constant reference to the next data item.
Definition externalFunctionUserData.hpp:177
size_t addData(Type const &value)
Definition externalFunctionUserData.hpp:151
size_t addData(Type const *value, int const size)
Definition externalFunctionUserData.hpp:160