CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
jacobianBinaryReaderWriter.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 "../../config.h"
38#include "jacobianBaseReaderWriter.hpp"
39
41namespace codi {
56 template<typename T_Type>
58 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
59 using Tape = typename Type::Tape;
60 using Identifier = typename Type::Identifier;
61 using Real = typename Type::Real;
62
63 FILE* fileHandleBin = nullptr;
64
66 JacobianBinaryTapeWriter(std::string const& name, std::vector<Identifier> const& in,
67 std::vector<Identifier> const& out)
68 : CommonBaseTapeWriter<T_Type>(name, in, out) {};
69
71 void start(Tape& tape) {
72 this->printIoBinary(tape);
73
74 this->openFile(fileHandleBin, this->fileName, "wb");
75 }
76
81 void writeStatement(Identifier const& curLhsIdentifier, size_t& curJacobianPos, Real const* const rhsJacobians,
82 Identifier const* const rhsIdentifiers, Config::ArgumentSize const& nJacobians) {
83 fwrite(&curLhsIdentifier, sizeof(Identifier), 1, fileHandleBin);
84 fwrite(&nJacobians, sizeof(Config::ArgumentSize), 1, fileHandleBin);
85 if (nJacobians == Config::StatementInputTag) CODI_Unlikely {
86 // Do nothing.
87 } else CODI_Likely {
88 fwrite(&rhsIdentifiers[curJacobianPos], sizeof(Identifier), nJacobians, fileHandleBin);
89 fwrite(&rhsJacobians[curJacobianPos], sizeof(Real), nJacobians, fileHandleBin);
90 }
91 }
92
94 void finish() {
95 fclose(fileHandleBin);
96 }
97 };
98
114 template<typename T_Type>
116 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
117 using Tape = typename Type::Tape;
118 using Identifier = typename Type::Identifier;
119 using Real = typename Type::Real;
120
122
124 void readFile(std::string const& name) {
125 FILE* fileHandleReadBin = nullptr;
126
127 bool isFirstIdentifier = true;
128 Identifier lowestIndex = 0;
129
130 Identifier lhsIdentifier;
132
133 std::vector<Identifier> rhsIdentifiers(Config::MaxArgumentSize, 0);
134 std::vector<Real> rhsJacobians(Config::MaxArgumentSize, 0);
135
136 this->fileName = name;
137 this->restoreIoBinary();
138 this->tape.getIndexManager().updateLargestCreatedIndex(this->largestIndex);
139
140 this->openFile(fileHandleReadBin, this->fileName, "rb");
141
142 while (fread(&lhsIdentifier, sizeof(Identifier), 1, fileHandleReadBin) == 1) {
143 fread(&nArgs, sizeof(Config::ArgumentSize), 1, fileHandleReadBin);
145 // TODO.
146 } else if (nArgs == Config::StatementInputTag) CODI_Unlikely {
147 // Do nothing.
148 } else CODI_Likely {
149 fread(rhsIdentifiers.data(), sizeof(Identifier), nArgs, fileHandleReadBin);
150 fread(rhsJacobians.data(), sizeof(Real), nArgs, fileHandleReadBin);
151 }
152 this->registerStatement(lhsIdentifier, nArgs, rhsIdentifiers, rhsJacobians, lowestIndex, isFirstIdentifier);
153 }
154
155 /* Update the user provided IO with a potential offset in the linear case. The registerStatement() returns
156 the offset.*/
157 this->updateUserIO(lowestIndex);
158
159 fclose(fileHandleReadBin);
160 }
161 };
162}
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:399
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:397
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
size_t constexpr StatementLowLevelFunctionTag
Statement tag for low level functions.
Definition config.h:126
size_t constexpr MaxArgumentSize
Maximum number of arguments in a statement.
Definition config.h:120
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
size_t constexpr StatementInputTag
Tag for statements that are inputs. Used in linear index management context.
Definition config.h:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
void updateUserIO(Identifier const &linearOffset)
This method is used to remove any offset and to update the largest created index.
Definition commonReaderWriterBase.hpp:209
Tape tape
The newly resorted tape.
Definition commonReaderWriterBase.hpp:153
Identifier largestIndex
The largest index on the stored tape.
Definition commonReaderWriterBase.hpp:155
void restoreIoBinary()
Restore the IO for the binary readers.
Definition commonReaderWriterBase.hpp:180
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
void printIoBinary(Tape &tape)
Generate the IO file in a binary format.
Definition commonReaderWriterBase.hpp:117
std::string fileName
The base file name provided by the user.
Definition commonReaderWriterBase.hpp:51
void openFile(FILE *&fileHandle, std::string const &name, std::string const &mode)
Definition commonReaderWriterBase.hpp:67
Used to register a statement for a Jacobian tape.
Definition jacobianBaseReaderWriter.hpp:48
void registerStatement(Identifier const &lhsIdentifier, Config::ArgumentSize const &nArgs, std::vector< Identifier > const &rhsIdentifiers, std::vector< Real > const &rhsJacobians, Identifier &lowestIndex, bool &isFirstIdentifier)
Constructor.
Definition jacobianBaseReaderWriter.hpp:57
Reads and restores a binary file for a Jacobian tape.
Definition jacobianBinaryReaderWriter.hpp:115
typename Type::Identifier Identifier
See TapeReaderInterface.
Definition jacobianBinaryReaderWriter.hpp:118
typename Type::Real Real
See TapeReaderInterface.
Definition jacobianBinaryReaderWriter.hpp:119
typename Type::Tape Tape
See TapeReaderInterface.
Definition jacobianBinaryReaderWriter.hpp:117
void readFile(std::string const &name)
This method uses the the fileName to reproduce a valid tape.
Definition jacobianBinaryReaderWriter.hpp:124
T_Type Type
See TapeReaderInterface.
Definition jacobianBinaryReaderWriter.hpp:116
JacobianBinaryTapeReader()
Constructor.
Definition jacobianBinaryReaderWriter.hpp:121
Writes a Jacobian tape in a binary format.
Definition jacobianBinaryReaderWriter.hpp:57
typename Type::Real Real
See TapeWriterInterface.
Definition jacobianBinaryReaderWriter.hpp:61
typename Type::Identifier Identifier
See TapeWriterInterface.
Definition jacobianBinaryReaderWriter.hpp:60
T_Type Type
See TapeWriterInterface.
Definition jacobianBinaryReaderWriter.hpp:58
FILE * fileHandleBin
File handle.
Definition jacobianBinaryReaderWriter.hpp:63
JacobianBinaryTapeWriter(std::string const &name, std::vector< Identifier > const &in, std::vector< Identifier > const &out)
Constructor.
Definition jacobianBinaryReaderWriter.hpp:66
void start(Tape &tape)
Destructor.
Definition jacobianBinaryReaderWriter.hpp:71
void finish()
After all the statements have been written, the finish method finalizes the writing process.
Definition jacobianBinaryReaderWriter.hpp:94
void writeStatement(Identifier const &curLhsIdentifier, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nJacobians)
Called for each statement. The method writes the current statement to the file. This overload is used...
Definition jacobianBinaryReaderWriter.hpp:81
typename Type::Tape Tape
See TapeWriterInterface.
Definition jacobianBinaryReaderWriter.hpp:59