CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
primalBaseReaderWriter.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 "../../misc/demangleName.hpp"
41#include "commonReaderWriterBase.hpp"
42
44namespace codi {
54 template<typename T_Type>
56 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
57 using Tape = typename Type::Tape;
58
59 using Identifier = typename Type::Identifier;
60 using Real = typename Type::Real;
61 using EvalHandle = typename Tape::EvalHandle;
62
63 std::map<EvalHandle, size_t> existingEvalHandles;
65 std::vector<std::string> evalHandleStatements;
66
68 PrimalBaseTapeWriter(std::string const& name, std::vector<Identifier> const& in,
69 std::vector<Identifier> const& out)
70 : CommonBaseTapeWriter<T_Type>(name, in, out),
74
75 protected:
76
79 FILE* fileHandleCreator = nullptr;
80 std::string handleCreatorFileName = this->modifyFileName(".hpp");
81 this->openFile(fileHandleCreator, handleCreatorFileName, "w");
82
83 std::string functionName = this->modifyFileName("CreateEvalHandles");
84 std::string fileHeader =
85 "#include <codi.hpp>\n\n"
86 "template <typename Tape>\n"
87 "std::vector<typename Tape::EvalHandle> " +
88 functionName +
89 "(){\n\n"
90 " std::vector<typename Tape::EvalHandle> evalHandles;\n"
91 " using Impl = ";
92
93 // Add the Impl type to the fileHeader
94 fileHeader += demangleName<Tape>() + ";\n\n";
95 // Resize the evalHandles
96 fileHeader += " evalHandles.resize(" + std::to_string(evalHandleCount) + ");\n";
97
98 // Stmt strings
99 std::string frontOfStmt = " evalHandles[";
100 std::string backOfStmt = "] = Tape::StatementEvaluator::template createHandle<";
101
102 // Create fileFooter
103 std::string fileFooter = "return evalHandles;\n}";
104
105 // Header print
106 fprintf(fileHandleCreator, "%s", fileHeader.c_str());
107
108 // Use CreateHandle for all the statements stored in the lookup table.
109 for (size_t handleCounter = 0; handleCounter < evalHandleStatements.size(); handleCounter++) {
110 fprintf(fileHandleCreator, "%s%zu%s%s>();\n", frontOfStmt.c_str(), handleCounter, backOfStmt.c_str(),
111 evalHandleStatements[handleCounter].c_str());
112 }
113 // Footer print
114 fprintf(fileHandleCreator, "\n %s", fileFooter.c_str());
115 }
116
118 size_t getEvalHandleIndex(EvalHandle const evalHandle, std::string const& evalStatement) {
119 auto it = existingEvalHandles.find(evalHandle);
120 if (it != existingEvalHandles.end()) {
121 return it->second;
122 } else {
124 evalHandleStatements.push_back(evalStatement);
125 evalHandleCount += 1;
126 return evalHandleCount - 1;
127 }
128 }
129 };
130
131}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
This class is a common base for all the writers and produces a IO file that contains the input and ou...
Definition commonReaderWriterBase.hpp:84
std::string modifyFileName(std::string const &suffix)
Remove the file extension and replace it with a new suffix.
Definition commonReaderWriterBase.hpp:59
void openFile(FILE *&fileHandle, std::string const &name, std::string const &mode)
Definition commonReaderWriterBase.hpp:67
This base class is used to implement the automatic generation of the .hpp file that restores the eval...
Definition primalBaseReaderWriter.hpp:55
void generateHandleCreatorFile()
This method is used to generate an .hpp file which creates the necessary EvalHandles in the reading p...
Definition primalBaseReaderWriter.hpp:78
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition primalBaseReaderWriter.hpp:59
PrimalBaseTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition primalBaseReaderWriter.hpp:68
std::vector< std::string > evalHandleStatements
The unique evalHandleStatements.
Definition primalBaseReaderWriter.hpp:65
size_t evalHandleCount
Count the number of unique evalHandles.
Definition primalBaseReaderWriter.hpp:64
size_t getEvalHandleIndex(EvalHandle const evalHandle, std::string const &evalStatement)
Get the index for an evalHandle.
Definition primalBaseReaderWriter.hpp:118
typename Type::Real Real
See TapeWriterInterface.
Definition primalBaseReaderWriter.hpp:60
typename Type::Tape Tape
See TapeWriterInterface.
Definition primalBaseReaderWriter.hpp:57
T_Type Type
See TapeWriterInterface.
Definition primalBaseReaderWriter.hpp:56
std::map< EvalHandle, size_t > existingEvalHandles
Contains the existing handles and the assigned index.
Definition primalBaseReaderWriter.hpp:63
typename Tape::EvalHandle EvalHandle
See TapeWriterInterface.
Definition primalBaseReaderWriter.hpp:61