CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
readerWriterHelpers.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 <map>
38
39#include "../../config.h"
40#include "graphWriters.hpp"
41#include "jacobianBinaryReaderWriter.hpp"
42#include "jacobianTextReaderWriter.hpp"
43#include "mathRepWriter.hpp"
44#include "primalBinaryReaderWriter.hpp"
45#include "primalTextReaderWriter.hpp"
46#include "tapeReaderWriterInterface.hpp"
47
49namespace codi {
50
52 template<typename T, typename... Args>
53 std::unique_ptr<T> make_unique_helper(Args&&... args) {
54 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
55 }
56
68 template<typename T_Type>
69 std::unique_ptr<TapeWriterInterface<T_Type>> createWriter(const std::string& fileName,
70 std::vector<typename T_Type::Identifier>& inputVariables,
71 std::vector<typename T_Type::Identifier>& outputVariables,
72 FileType selectedType) {
73 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
74 using Tape = typename Type::Tape;
75
76 std::unique_ptr<TapeWriterInterface<T_Type>> writer = nullptr;
77 static constexpr bool IsPrimalValueTape = TapeTraits::IsPrimalValueTape<Tape>::value;
78
79 switch (selectedType) {
80 case FileType::Text:
81 if (IsPrimalValueTape) {
82 writer = make_unique_helper<codi::PrimalTextTapeWriter<Type>>(fileName, inputVariables, outputVariables, true,
83 true);
84 } else {
85 writer = make_unique_helper<codi::JacobianTextTapeWriter<Type>>(fileName, inputVariables, outputVariables,
86 true, true);
87 }
88 break;
89
90 case FileType::Binary:
91 if (IsPrimalValueTape) {
92 writer = make_unique_helper<codi::PrimalBinaryTapeWriter<Type>>(fileName, inputVariables, outputVariables);
93 } else {
94 writer = make_unique_helper<codi::JacobianBinaryTapeWriter<Type>>(fileName, inputVariables, outputVariables);
95 }
96 break;
97
98 case FileType::Graph:
99 if (IsPrimalValueTape) {
100 writer = make_unique_helper<codi::PrimalGraphTapeWriter<Type>>(fileName, inputVariables, outputVariables);
101 } else {
102 writer =
103 make_unique_helper<codi::JacobianGraphTapeWriter<Type>>(fileName, inputVariables, outputVariables, true);
104 }
105 break;
106
107 case FileType::Math:
108 if (IsPrimalValueTape) {
109 writer = make_unique_helper<codi::MathRepWriter<Type>>(fileName, inputVariables, outputVariables);
110 } else {
111 CODI_EXCEPTION("The MathRepWriter is not supported for Jacobian tapes.");
112 }
113 break;
114
115 default:
116 CODI_EXCEPTION("A valid file format was not selected.");
117 break;
118 }
119
120 return writer;
121 };
122
131 template<typename T_Type>
132 std::unique_ptr<TapeReaderInterface<T_Type>> readTapeFile(std::string const& fileName) {
133 // This function uses the file extension provided in the fileName to determine the type of TapeReader.
134
136 std::unique_ptr<TapeReaderInterface<T_Type>> reader = nullptr;
137 std::string fileExtension;
138
139 size_t dotPosition = fileName.find_last_of(".");
140 if (dotPosition != std::string::npos) {
141 fileExtension = fileName.substr(dotPosition + 1);
142 }
143
144 if (fileExtension == "txt") {
145 reader = make_unique_helper<JacobianTextTapeReader<T_Type>>();
146 } else if (fileExtension == "dat") {
147 reader = make_unique_helper<JacobianBinaryTapeReader<T_Type>>();
148 } else {
149 CODI_EXCEPTION("The given file type is not supported.");
150 }
151 reader->readFile(fileName);
152
153 return reader;
154 };
155
165 template<typename T_Type>
166 std::unique_ptr<TapeReaderInterface<T_Type>> readTapeFile(
167 std::string const& fileName, std::vector<typename T_Type::Tape::EvalHandle> const& evalHandles) {
168 // This function uses the file extension provided in the fileName to determine the type of TapeReader.
169
171 std::unique_ptr<TapeReaderInterface<T_Type>> reader = nullptr;
172 std::string fileExtension;
173
174 size_t dotPosition = fileName.find_last_of(".");
175 if (dotPosition != std::string::npos) {
176 fileExtension = fileName.substr(dotPosition + 1);
177 }
178
179 if (fileExtension == "txt") {
180 reader = make_unique_helper<PrimalTextTapeReader<T_Type>>(evalHandles);
181 } else if (fileExtension == "dat") {
182 reader = make_unique_helper<PrimalBinaryTapeReader<T_Type>>(evalHandles);
183 } else {
184 CODI_EXCEPTION("The given file type is not supported.");
185 }
186
187 reader->readFile(fileName);
188
189 return reader;
190 };
191}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
std::unique_ptr< TapeWriterInterface< T_Type > > createWriter(const std::string &fileName, std::vector< typename T_Type::Identifier > &inputVariables, std::vector< typename T_Type::Identifier > &outputVariables, FileType selectedType)
The createWriter() function is used to generate an automatic writer using the FileType and the TapeTr...
Definition readerWriterHelpers.hpp:69
std::unique_ptr< TapeReaderInterface< T_Type > > readTapeFile(std::string const &fileName)
Uses the fileName to read and restore a Jacobian tape. The file extension is used to determine wether...
Definition readerWriterHelpers.hpp:132
FileType
Used to select the type of writer that should be generated.
Definition tapeReaderWriterInterface.hpp:47
std::unique_ptr< T > make_unique_helper(Args &&... args)
Helper for creating a unique pointer. (std::make_unique is not available in C++11....
Definition readerWriterHelpers.hpp:53
If the tape inherits from PrimalValueBaseTape.
Definition tapeTraits.hpp:97