Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Tag / debug tape recordings #2343

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3801e4d
Throw error message in case the multizone discrete adjoint solver wil…
oleburghardt Aug 14, 2024
0e5f95b
Softcode indentifier/index.
oleburghardt Aug 15, 2024
dc38cc3
Add tag type option to build system.
oleburghardt Aug 15, 2024
19a1a70
Add option to use RealReverseTag as AD type.
oleburghardt Aug 25, 2024
36c922e
Temporarily adjust/hardcode type size for memory alignment in vectori…
oleburghardt Aug 25, 2024
84e97f4
added turbo obj funcs
joshkellyjak Aug 29, 2024
e68f844
Merge branch 'develop' into feature_mz_adjoint_for_turbo
joshkellyjak Aug 29, 2024
bb0831d
thems the breaks
joshkellyjak Aug 29, 2024
4327942
Merge branch 'feature_mz_adjoint_for_turbo' of https://github.com/su2…
joshkellyjak Aug 29, 2024
9f1b07c
Merge remote-tracking branch 'upstream/feature_tag_debug_tape' into f…
joshkellyjak Sep 2, 2024
6e65dd1
Add config option and indicator for a debug discrete adjoint run.
oleburghardt Sep 24, 2024
82f69e9
Add routines for setting tags to AD structure.
oleburghardt Sep 24, 2024
9de8e93
Add kinds of recording for tag tapes.
oleburghardt Sep 24, 2024
57fbcea
Comment out checks in AD structure whether an identifier is active. T…
oleburghardt Sep 24, 2024
3da17d8
Add first WIP version of the tag debug mode to the (multizone) discre…
oleburghardt Sep 24, 2024
b0e7c86
Fix preaccumulation error in ComputeVorticityAndStrainMag (first debu…
oleburghardt Sep 24, 2024
688797a
Merge branch 'develop' into feature_tag_debug_tape
joshkellyjak Sep 24, 2024
8556c2a
Revert small change (will be added to CoDi soon).
oleburghardt Sep 24, 2024
683de77
Resolves tag errors in turbo mode
joshkellyjak Sep 26, 2024
13ca3f5
Revert "Resolves tag errors in turbo mode"
joshkellyjak Sep 26, 2024
1169606
Merge branch 'feature_tag_debug_tape' of https://github.com/su2code/S…
joshkellyjak Sep 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Common/include/CConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,8 @@ class CConfig {
long ParMETIS_pointWgt; /*!< \brief Load balancing weight given to points. */
long ParMETIS_edgeWgt; /*!< \brief Load balancing weight given to edges. */
unsigned short DirectDiff; /*!< \brief Direct Differentation mode. */
bool DiscreteAdjoint; /*!< \brief AD-based discrete adjoint mode. */
bool DiscreteAdjoint, /*!< \brief AD-based discrete adjoint mode. */
DiscreteAdjointDebug; /*!< \brief Discrete adjoint debug mode using tags. */
su2double Const_DES; /*!< \brief Detached Eddy Simulation Constant. */
WINDOW_FUNCTION Kind_WindowFct; /*!< \brief Type of window (weight) function for objective functional. */
unsigned short Kind_HybridRANSLES; /*!< \brief Kind of Hybrid RANS/LES. */
Expand Down Expand Up @@ -8904,6 +8905,12 @@ class CConfig {
*/
bool GetDiscrete_Adjoint(void) const { return DiscreteAdjoint; }

/*!
* \brief Get the indicator whether a debug run for the discrete adjoint solver will be started.
* \return the discrete adjoint debug indicator.
*/
bool GetDiscrete_Adjoint_Debug(void) const { return DiscreteAdjointDebug; }

/*!
* \brief Get the number of subiterations while a ramp is applied.
* \return Number of FSI subiters.
Expand Down
56 changes: 39 additions & 17 deletions Common/include/basic_types/ad_structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
*/
namespace AD {
#ifndef CODI_REVERSE_TYPE

using Identifier = int;

/*!
* \brief Start the recording of the operations and involved variables.
* If called, the computational graph of all operations occuring after the call will be stored,
Expand Down Expand Up @@ -101,14 +104,20 @@
* \param[in] index - Position in the adjoint vector.
* \param[in] val - adjoint value to be set.
*/
inline void SetDerivative(int index, const double val) {}
inline void SetDerivative(Identifier index, const double val) {}

/*!
* \brief Extracts the adjoint value at index
* \param[in] index - position in the adjoint vector where the derivative will be extracted.
* \return Derivative value.
*/
inline double GetDerivative(int index) { return 0.0; }
inline double GetDerivative(Identifier index) { return 0.0; }

/*!
* \brief Returns the identifier that represents an inactive variable.
* \return Passive index.
*/
inline Identifier GetPassiveIndex() { return 0; }

/*!
* \brief Clears the currently stored adjoints but keeps the computational graph.
Expand Down Expand Up @@ -259,7 +268,13 @@
* \param[in] data - variable whose gradient information will be extracted.
* \param[in] index - where obtained gradient information will be stored.
*/
inline void SetIndex(int& index, const su2double& data) {}
inline void SetIndex(Identifier& index, const su2double& data) {}

/*!
* \brief Sets the tag tape to a specific tag.
* \param[in] tag - the number to which the tag is set.
*/
inline void SetTag(int tag) {}

/*!
* \brief Pushes back the current tape position to the tape position's vector.
Expand Down Expand Up @@ -304,6 +319,7 @@
using CheckpointHandler = codi::ExternalFunctionUserData;

using Tape = su2double::Tape;
using Identifier = su2double::Identifier;

#ifdef HAVE_OPDI
using ExtFuncHelper = codi::OpenMPExternalFunctionHelper<su2double>;
Expand Down Expand Up @@ -470,14 +486,16 @@

FORCEINLINE void EndUseAdjoints() { AD::getTape().endUseAdjointVector(); }

FORCEINLINE void SetIndex(int& index, const su2double& data) { index = data.getIdentifier(); }
FORCEINLINE void SetIndex(Identifier& index, const su2double& data) { index = data.getIdentifier(); }

FORCEINLINE void SetTag(int tag) { AD::getTape().setCurTag(tag); }

// WARNING: For performance reasons, this method does not perform bounds checking.
// When using it, please ensure sufficient adjoint vector size by a call to AD::ResizeAdjoints().
// This method does not perform locking either.
// It should be safeguarded by calls to AD::BeginUseAdjoints() and AD::EndUseAdjoints().
FORCEINLINE void SetDerivative(int index, const double val) {
if (index == 0) // Allow multiple threads to "set the derivative" of passive variables without causing data races.
FORCEINLINE void SetDerivative(Identifier index, const double val) {
if (!AD::getTape().isIdentifierActive(index)) // Allow multiple threads to "set the derivative" of passive variables without causing data races.
return;

AD::getTape().setGradient(index, val, codi::AdjointsManagement::Manual);
Expand All @@ -488,10 +506,12 @@
// Otherwise, please ensure sufficient adjoint vector size by a call to AD::ResizeAdjoints().
// This method does not perform locking either.
// It should be safeguarded by calls to AD::BeginUseAdjoints() and AD::EndUseAdjoints().
FORCEINLINE double GetDerivative(int index) {
FORCEINLINE double GetDerivative(Identifier index) {
return AD::getTape().getGradient(index, codi::AdjointsManagement::Manual);
}

FORCEINLINE Identifier GetPassiveIndex() { return AD::getTape().getPassiveIndex(); }

FORCEINLINE bool IsIdentifierActive(su2double const& value) {
return getTape().isIdentifierActive(value.getIdentifier());
}
Expand All @@ -502,7 +522,8 @@
template <class T, class... Ts, su2enable_if<std::is_same<T, su2double>::value> = 0>
FORCEINLINE void SetPreaccIn(const T& data, Ts&&... moreData) {
if (!PreaccActive) return;
if (IsIdentifierActive(data)) PreaccHelper.addInput(data);
// if (IsIdentifierActive(data))
PreaccHelper.addInput(data);
SetPreaccIn(moreData...);
}

Expand All @@ -515,9 +536,9 @@
FORCEINLINE void SetPreaccIn(const T& data, const int size) {
if (PreaccActive) {
for (int i = 0; i < size; i++) {
if (IsIdentifierActive(data[i])) {
// if (IsIdentifierActive(data[i])) {

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
PreaccHelper.addInput(data[i]);
}
// }

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
}
}
}
Expand All @@ -527,9 +548,9 @@
if (!PreaccActive) return;
for (int i = 0; i < size_x; i++) {
for (int j = 0; j < size_y; j++) {
if (IsIdentifierActive(data[i][j])) {
// if (IsIdentifierActive(data[i][j])) {

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
PreaccHelper.addInput(data[i][j]);
}
// }

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
}
}
}
Expand All @@ -547,17 +568,18 @@
template <class T, class... Ts, su2enable_if<std::is_same<T, su2double>::value> = 0>
FORCEINLINE void SetPreaccOut(T& data, Ts&&... moreData) {
if (!PreaccActive) return;
if (IsIdentifierActive(data)) PreaccHelper.addOutput(data);
// if (IsIdentifierActive(data))
PreaccHelper.addOutput(data);
SetPreaccOut(moreData...);
}

template <class T>
FORCEINLINE void SetPreaccOut(T&& data, const int size) {
if (PreaccActive) {
for (int i = 0; i < size; i++) {
if (IsIdentifierActive(data[i])) {
// if (IsIdentifierActive(data[i])) {

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
PreaccHelper.addOutput(data[i]);
}
// }

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
}
}
}
Expand All @@ -567,9 +589,9 @@
if (!PreaccActive) return;
for (int i = 0; i < size_x; i++) {
for (int j = 0; j < size_y; j++) {
if (IsIdentifierActive(data[i][j])) {
// if (IsIdentifierActive(data[i][j])) {

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
PreaccHelper.addOutput(data[i][j]);
}
// }

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Common/include/code_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ using su2double = codi::RealReversePrimal;
using su2double = codi::RealReversePrimalIndexGen<double, double, codi::ReuseIndexManager<int> >;
#elif defined(CODI_PRIMAL_MULTIUSE_TAPE)
using su2double = codi::RealReversePrimalIndex;
#elif defined(CODI_TAG_TAPE)
using su2double = codi::RealReverseTag;
#else
#error "Please define a CoDiPack tape."
#endif
Expand Down
5 changes: 2 additions & 3 deletions Common/include/geometry/dual_grid/CPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ class CPoint {
su2activevector MaxLength; /*!< \brief The maximum cell-center to cell-center length. */
su2activevector RoughnessHeight; /*!< \brief Roughness of the nearest wall. */

su2matrix<int> AD_InputIndex; /*!< \brief Indices of Coord variables in the adjoint vector. */
su2matrix<int>
AD_OutputIndex; /*!< \brief Indices of Coord variables in the adjoint vector after having been updated. */
su2matrix<AD::Identifier> AD_InputIndex; /*!< \brief Indices of Coord variables in the adjoint vector. */
su2matrix<AD::Identifier> AD_OutputIndex; /*!< \brief Indices of Coord variables in the adjoint vector after having been updated. */

/*!
* \brief Allocate fields required by the minimal constructor.
Expand Down
2 changes: 2 additions & 0 deletions Common/include/option_structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2493,6 +2493,8 @@ enum class RECORDING {
MESH_COORDS,
MESH_DEFORM,
SOLUTION_AND_MESH,
TAG_INIT_SOLUTION_VARIABLES,
TAG_CHECK_SOLUTION_VARIABLES
};

/*!
Expand Down
4 changes: 2 additions & 2 deletions Common/include/parallelization/vectorization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ class Array : public CVecExpr<Array<Scalar_t, N>, Scalar_t> {
public:
using Scalar = Scalar_t;
enum : size_t { Size = N };
enum : size_t { Align = Size * sizeof(Scalar) };
enum : size_t { Align = Size * 32 };
static constexpr bool StoreAsRef = true;

private:
alignas(Size * sizeof(Scalar)) Scalar x_[N];
alignas(Size * 32) Scalar x_[N];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be okay with replacing 32 by a function to find the next power of 2? (Requirement of alignas.) Or could this be done any better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what type is the result not a power of 2?
If we use a fixed number instead of sizeof, arrays of this type may take more space than necessary.

Copy link
Contributor Author

@oleburghardt oleburghardt Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reverse tag type is 24 I think. If sizeof(Scalar) is already a power of 2 that function should of course not change it :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so the tag doesn't have a float/double part? Then it should be ok to round Size * sizeof(Scalar) to the next power of 2.
Or maybe using Size * alignof(Scalar), that may be more generic and achieve the desired result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it does contain at least a double for the primal value (show must go on). I'll check or ask what the remaining bytes are for besides two ints for index and tag. But sizeof(RealReverseTag) = 24.


public:
#define ARRAY_BOILERPLATE \
Expand Down
10 changes: 9 additions & 1 deletion Common/src/CConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,11 +1105,19 @@ void CConfig::SetConfig_Options() {
addBoolOption("MULTIZONE", Multizone_Problem, NO);
/*!\brief PHYSICAL_PROBLEM \n DESCRIPTION: Physical governing equations \n Options: see \link Solver_Map \endlink \n DEFAULT: NONE \ingroup Config*/
addEnumOption("MULTIZONE_SOLVER", Kind_MZSolver, Multizone_Map, ENUM_MULTIZONE::MZ_BLOCK_GAUSS_SEIDEL);
#ifdef CODI_REVERSE_TYPE
#if defined (CODI_REVERSE_TYPE)
const bool discAdjDefault = true;
# if defined (CODI_TAG_TAPE)
const bool discAdjDebugDefault = true;
# else
const bool discAdjDebugDefault = false;
# endif
#else
const bool discAdjDefault = false;
const bool discAdjDebugDefault = false;
#endif
// TODO Set the indicator through MATH_PROBLEM
DiscreteAdjointDebug = discAdjDebugDefault;
/*!\brief MATH_PROBLEM \n DESCRIPTION: Mathematical problem \n Options: DIRECT, ADJOINT \ingroup Config*/
addMathProblemOption("MATH_PROBLEM", ContinuousAdjoint, false, DiscreteAdjoint, discAdjDefault, Restart_Flow, discAdjDefault);
/*!\brief KIND_TURB_MODEL \n DESCRIPTION: Specify turbulence model \n Options: see \link Turb_Model_Map \endlink \n DEFAULT: NONE \ingroup Config*/
Expand Down
4 changes: 2 additions & 2 deletions Common/src/geometry/dual_grid/CPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void CPoint::FullAllocation(unsigned short imesh, const CConfig* config) {
}

if (config->GetDiscrete_Adjoint()) {
AD_InputIndex.resize(npoint, nDim) = 0;
AD_OutputIndex.resize(npoint, nDim) = 0;
AD_InputIndex.resize(npoint, nDim) = AD::GetPassiveIndex();
AD_OutputIndex.resize(npoint, nDim) = AD::GetPassiveIndex();
}

/*--- Multigrid structures. ---*/
Expand Down
9 changes: 7 additions & 2 deletions SU2_CFD/include/drivers/CDiscAdjMultizoneDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CDiscAdjMultizoneDriver : public CMultizoneDriver {
};

/*!
* \brief Kinds of recordings (three different ones).
* \brief Kinds of recordings.
*/
enum class Kind_Tape {
FULL_TAPE, /*!< \brief Entire derivative information for a coupled adjoint
Expand All @@ -96,7 +96,7 @@ class CDiscAdjMultizoneDriver : public CMultizoneDriver {

bool eval_transfer = false; /*!< \brief Evaluate the transfer section of the tape. */
su2double ObjFunc; /*!< \brief Value of the objective function. */
int ObjFunc_Index; /*!< \brief Index of the value of the objective function. */
AD::Identifier ObjFunc_Index; /*!< \brief Index of the value of the objective function. */

CIteration*** direct_iteration; /*!< \brief Array of pointers to the direct iterations. */
COutput** direct_output; /*!< \brief Array of pointers to the direct outputs. */
Expand Down Expand Up @@ -141,6 +141,11 @@ class CDiscAdjMultizoneDriver : public CMultizoneDriver {
*/
void StartSolver() override;

/*!
* \brief [Overload] Launch the debug mode for the discrete adjoint multizone solver.
*/
void DebugRun() override;

/*!
* \brief Preprocess the multizone iteration
*/
Expand Down
5 changes: 5 additions & 0 deletions SU2_CFD/include/drivers/CDriver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ class CDriver : public CDriverBase {
*/
virtual void StartSolver() {}

/*!
* \brief Launch a debug run of the solver.
*/
virtual void DebugRun() {}

/*!
* \brief Deallocation routine
*/
Expand Down
4 changes: 2 additions & 2 deletions SU2_CFD/include/solvers/CFVMFlowSolverBase.inl
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,11 @@ void CFVMFlowSolverBase<V, R>::ComputeVorticityAndStrainMag(const CConfig& confi
StrainMag(iPoint) = sqrt(2.0*StrainMag(iPoint));
AD::SetPreaccOut(StrainMag(iPoint));

AD::EndPreacc();

/*--- Max is not differentiable, so we not register them for preacc. ---*/
strainMax = max(strainMax, StrainMag(iPoint));
omegaMax = max(omegaMax, GeometryToolbox::Norm(3, Vorticity));

AD::EndPreacc();
}
END_SU2_OMP_FOR

Expand Down
12 changes: 6 additions & 6 deletions SU2_CFD/include/variables/CVariable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class CVariable {

MatrixType Solution_BGS_k; /*!< \brief Old solution container for BGS iterations. */

su2matrix<int> AD_InputIndex; /*!< \brief Indices of Solution variables in the adjoint vector. */
su2matrix<int> AD_OutputIndex; /*!< \brief Indices of Solution variables in the adjoint vector after having been updated. */
su2matrix<AD::Identifier> AD_InputIndex; /*!< \brief Indices of Solution variables in the adjoint vector. */
su2matrix<AD::Identifier> AD_OutputIndex; /*!< \brief Indices of Solution variables in the adjoint vector after having been updated. */

VectorType SolutionExtra; /*!< \brief Stores adjoint solution for extra solution variables.
Currently only streamwise periodic pressure-drop for massflow prescribed flows. */
Expand All @@ -118,7 +118,7 @@ class CVariable {
assert(false && "A base method of CVariable was used, but it should have been overridden by the derived class.");
}

void RegisterContainer(bool input, su2activematrix& variable, su2matrix<int>* ad_index = nullptr) {
void RegisterContainer(bool input, su2activematrix& variable, su2matrix<AD::Identifier>* ad_index = nullptr) {
const auto nPoint = variable.rows();
SU2_OMP_FOR_STAT(roundUpDiv(nPoint,omp_get_num_threads()))
for (unsigned long iPoint = 0; iPoint < nPoint; ++iPoint) {
Expand All @@ -133,7 +133,7 @@ class CVariable {
END_SU2_OMP_FOR
}

void RegisterContainer(bool input, su2activematrix& variable, su2matrix<int>& ad_index) {
void RegisterContainer(bool input, su2activematrix& variable, su2matrix<AD::Identifier>& ad_index) {
RegisterContainer(input, variable, &ad_index);
}

Expand Down Expand Up @@ -2180,15 +2180,15 @@ class CVariable {
}

inline void GetAdjointSolution_time_n(unsigned long iPoint, su2double *adj_sol) const {
int index = 0;
AD::Identifier index = AD::GetPassiveIndex();
for (unsigned long iVar = 0; iVar < Solution_time_n.cols(); iVar++) {
AD::SetIndex(index, Solution_time_n(iPoint, iVar));
adj_sol[iVar] = AD::GetDerivative(index);
}
}

inline void GetAdjointSolution_time_n1(unsigned long iPoint, su2double *adj_sol) const {
int index = 0;
AD::Identifier index = AD::GetPassiveIndex();
for (unsigned long iVar = 0; iVar < Solution_time_n1.cols(); iVar++) {
AD::SetIndex(index, Solution_time_n1(iPoint, iVar));
adj_sol[iVar] = AD::GetDerivative(index);
Expand Down
1 change: 1 addition & 0 deletions SU2_CFD/src/SU2_CFD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ int main(int argc, char *argv[]) {
and perform all the preprocessing. ---*/

const bool disc_adj = config.GetDiscrete_Adjoint();
const bool disc_adj_debug = config.GetDiscrete_Adjoint_Debug();
const bool multizone = config.GetMultizone_Problem();
const bool harmonic_balance = (config.GetTime_Marching() == TIME_MARCHING::HARMONIC_BALANCE);

Expand Down
Loading
Loading