Skip to content

Commit 623152f

Browse files
committed
fix bugs avoid global variable
1 parent f774899 commit 623152f

11 files changed

Lines changed: 40 additions & 41 deletions

File tree

Common/include/basic_types/ad_structure.hpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,15 @@ namespace AD{
251251

252252
/*!
253253
* \brief Start a passive region, i.e. stop recording.
254+
* \return True is tape was active.
254255
*/
255-
inline void BeginPassive() {}
256+
inline bool BeginPassive() { return false; }
256257

257258
/*!
258259
* \brief End a passive region, i.e. start recording if we were recording before.
260+
* \param[in] wasActive - Whether we were recording before entering the passive region.
259261
*/
260-
inline void EndPassive() {}
262+
inline void EndPassive(bool wasActive) {}
261263

262264
#else
263265
using CheckpointHandler = codi::DataStore;
@@ -498,19 +500,15 @@ namespace AD{
498500

499501
FORCEINLINE void EndExtFunc() { delete FuncHelper; }
500502

501-
FORCEINLINE void BeginPassive() {
503+
FORCEINLINE bool BeginPassive() {
502504
if(AD::globalTape.isActive()) {
503-
AD::globalTape.setPassive();
504-
AD::Status = true;
505+
StopRecording();
506+
return true;
505507
}
508+
return false;
506509
}
507510

508-
FORCEINLINE void EndPassive() {
509-
if(AD::Status) {
510-
AD::globalTape.setActive();
511-
AD::Status = false;
512-
}
513-
}
511+
FORCEINLINE void EndPassive(bool wasActive) { if(wasActive) StartRecording(); }
514512

515513
#endif // CODI_REVERSE_TYPE
516514

Common/src/adt_structure.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void CADTPointsOnlyClass::DetermineNearestNode_impl(vector<unsigned long>& front
332332
unsigned long &pointID,
333333
int &rankID) const {
334334

335-
AD::BeginPassive();
335+
const bool wasActive = AD::BeginPassive();
336336

337337
/*--------------------------------------------------------------------------*/
338338
/*--- Step 1: Initialize the nearest node to the central node of the ---*/
@@ -448,7 +448,7 @@ void CADTPointsOnlyClass::DetermineNearestNode_impl(vector<unsigned long>& front
448448
if(frontLeaves.size() == 0) break;
449449
}
450450

451-
AD::EndPassive();
451+
AD::EndPassive(wasActive);
452452

453453
/* Recompute the distance to get the correct dependency if we use AD */
454454
coorTarget = coorPoints.data() + nDimADT*minIndex;
@@ -790,7 +790,7 @@ void CADTElemClass::DetermineNearestElement_impl(vector<CBBoxTargetClass>& BBoxT
790790
unsigned long &elemID,
791791
int &rankID) const {
792792

793-
AD::BeginPassive();
793+
const bool wasActive = AD::BeginPassive();
794794

795795
/*----------------------------------------------------------------------------*/
796796
/*--- Step 1: Initialize the distance (squared) to the quaranteed distance ---*/
@@ -953,7 +953,7 @@ void CADTElemClass::DetermineNearestElement_impl(vector<CBBoxTargetClass>& BBoxT
953953
}
954954
}
955955

956-
AD::EndPassive();
956+
AD::EndPassive(wasActive);
957957

958958
/* At the moment the square of the distance is stored in dist. Compute
959959
the correct value. */

Common/src/basic_types/ad_structure.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* SU2 Project Website: https://su2code.github.io
88
*
9-
* The SU2 Project is maintained by the SU2 Foundation
9+
* The SU2 Project is maintained by the SU2 Foundation
1010
* (http://su2foundation.org)
1111
*
1212
* Copyright 2012-2020, SU2 Contributors (cf. AUTHORS.md)
@@ -41,7 +41,6 @@ namespace AD {
4141
su2double::TapeType::Position StartPosition, EndPosition;
4242
std::vector<su2double::TapeType::Position> TapePositions;
4343

44-
bool Status = false;
4544
bool PreaccActive = false;
4645
bool PreaccEnabled = true;
4746

Common/src/geometry/CGeometry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ void CGeometry::ComputeAirfoil_Section(su2double *Plane_P0, su2double *Plane_Nor
18901890
vector<su2double> &Zcoord_Airfoil, vector<su2double> &Variable_Airfoil,
18911891
bool original_surface, CConfig *config) {
18921892

1893-
AD::BeginPassive();
1893+
const bool wasActive = AD::BeginPassive();
18941894

18951895
unsigned short iMarker, iNode, jNode, iDim, Index = 0;
18961896
bool intersect;
@@ -2609,7 +2609,7 @@ void CGeometry::ComputeAirfoil_Section(su2double *Plane_P0, su2double *Plane_Nor
26092609

26102610
}
26112611

2612-
AD::EndPassive();
2612+
AD::EndPassive(wasActive);
26132613

26142614
}
26152615

Common/src/geometry/CPhysicalGeometry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7264,7 +7264,7 @@ void CPhysicalGeometry::SetMaxLength(CConfig* config) {
72647264
* neighbor, then enable it again and recompute the distance.
72657265
* This reduces the overhead of storing irrelevant computations. ---*/
72667266

7267-
AD::BeginPassive();
7267+
const bool wasActive = AD::BeginPassive();
72687268

72697269
su2double max_delta=0;
72707270
auto max_neighbor = iPoint;
@@ -7285,7 +7285,7 @@ void CPhysicalGeometry::SetMaxLength(CConfig* config) {
72857285
}
72867286
}
72877287

7288-
AD::EndPassive();
7288+
AD::EndPassive(wasActive);
72897289

72907290
/*--- Recompute and set. ---*/
72917291
const su2double* Coord_j = nodes->GetCoord(max_neighbor);

Common/src/interface_interpolation/CIsoparametric.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ int CIsoparametric::QuadrilateralIsoparameters(const su2double X[][3], const su2
385385
/*--- Finding Xi and Eta is a "third order" effect that we do not
386386
* differentiate (also because we need to iterate). ---*/
387387

388-
AD::BeginPassive();
388+
const bool wasActive = AD::BeginPassive();
389389

390390
for (int iter = 0; iter < NITER; ++iter) {
391391

@@ -431,7 +431,7 @@ int CIsoparametric::QuadrilateralIsoparameters(const su2double X[][3], const su2
431431
if (eps < tol) break;
432432
}
433433

434-
AD::EndPassive();
434+
AD::EndPassive(wasActive);
435435

436436
int outOfBounds = 0;
437437

SU2_CFD/include/limiters/computeLimiters_impl.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ void computeLimiters_impl(CSolver* solver,
9999
#endif
100100

101101
/*--- If limiters are frozen do not record the computation ---*/
102+
bool wasActive = false;
102103
if (config.GetDiscrete_Adjoint() && config.GetFrozen_Limiter_Disc()) {
103-
AD::BeginPassive();
104+
wasActive = AD::BeginPassive();
104105
}
105106

106107
CLimiterDetails<LimiterKind> limiterDetails;
@@ -237,6 +238,6 @@ void computeLimiters_impl(CSolver* solver,
237238
solver->CompleteComms(&geometry, &config, kindMpiComm);
238239
}
239240

240-
AD::EndPassive();
241+
AD::EndPassive(wasActive);
241242

242243
}

SU2_CFD/src/iteration_structure.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,11 @@ void CIteration::SetMesh_Deformation(CGeometry **geometry,
231231

232232
/*--- Perform the elasticity mesh movement ---*/
233233

234+
bool wasActive = false;
234235
if ((kind_recording != MESH_DEFORM) && !config->GetMultizone_Problem()) {
235236
/*--- In a primal run, AD::TapeActive returns a false ---*/
236237
/*--- In any other recordings, the tape is passive during the deformation. ---*/
237-
AD::BeginPassive();
238+
wasActive = AD::BeginPassive();
238239
}
239240

240241
/*--- Set the stiffness of each element mesh into the mesh numerics ---*/
@@ -246,7 +247,7 @@ void CIteration::SetMesh_Deformation(CGeometry **geometry,
246247
solver[MESH_SOL]->DeformMesh(geometry, numerics[MESH_SOL], config);
247248

248249
/*--- Continue recording. ---*/
249-
AD::EndPassive();
250+
AD::EndPassive(wasActive);
250251

251252
}
252253

SU2_CFD/src/numerics/CNumerics.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void CNumerics::GetInviscidIncProjFlux(const su2double *val_density,
334334
void CNumerics::GetInviscidProjJac(const su2double *val_velocity, const su2double *val_energy,
335335
const su2double *val_normal, su2double val_scale,
336336
su2double **val_Proj_Jac_Tensor) const {
337-
AD::BeginPassive();
337+
const bool wasActive = AD::BeginPassive();
338338
unsigned short iDim, jDim;
339339
su2double sqvel, proj_vel, phi, a1, a2;
340340

@@ -365,15 +365,15 @@ void CNumerics::GetInviscidProjJac(const su2double *val_velocity, const su2doubl
365365
for (iDim = 0; iDim < nDim; iDim++)
366366
val_Proj_Jac_Tensor[nDim+1][iDim+1] = val_scale*(val_normal[iDim]*a1-a2*val_velocity[iDim]*proj_vel);
367367
val_Proj_Jac_Tensor[nDim+1][nDim+1] = val_scale*Gamma*proj_vel;
368-
AD::EndPassive();
368+
AD::EndPassive(wasActive);
369369
}
370370

371371

372372
void CNumerics::GetInviscidProjJac(const su2double *val_velocity, const su2double *val_enthalpy,
373373
const su2double *val_chi, const su2double *val_kappa,
374374
const su2double *val_normal, su2double val_scale,
375375
su2double **val_Proj_Jac_Tensor) const {
376-
AD::BeginPassive();
376+
const bool wasActive = AD::BeginPassive();
377377
unsigned short iDim, jDim;
378378
su2double sqvel, proj_vel, phi, a1, a2;
379379

@@ -404,15 +404,15 @@ void CNumerics::GetInviscidProjJac(const su2double *val_velocity, const su2doubl
404404
for (iDim = 0; iDim < nDim; iDim++)
405405
val_Proj_Jac_Tensor[nDim+1][iDim+1] = val_scale*(val_normal[iDim]*a1-a2*val_velocity[iDim]*proj_vel);
406406
val_Proj_Jac_Tensor[nDim+1][nDim+1] = val_scale*(a2+1)*proj_vel;
407-
AD::EndPassive();
407+
AD::EndPassive(wasActive);
408408
}
409409

410410
void CNumerics::GetInviscidIncProjJac(const su2double *val_density, const su2double *val_velocity,
411411
const su2double *val_betainc2, const su2double *val_cp,
412412
const su2double *val_temperature, const su2double *val_dRhodT,
413413
const su2double *val_normal, su2double val_scale,
414414
su2double **val_Proj_Jac_Tensor) const {
415-
AD::BeginPassive();
415+
const bool wasActive = AD::BeginPassive();
416416
unsigned short iDim;
417417
su2double proj_vel;
418418

@@ -475,7 +475,7 @@ void CNumerics::GetInviscidIncProjJac(const su2double *val_density, const su2dou
475475
val_Proj_Jac_Tensor[4][4] = val_scale*((*val_cp)*((*val_temperature)*(*val_dRhodT) + (*val_density))*proj_vel);
476476

477477
}
478-
AD::EndPassive();
478+
AD::EndPassive(wasActive);
479479
}
480480

481481
void CNumerics::GetPreconditioner(const su2double *val_density, const su2double *val_velocity,

SU2_CFD/src/solvers/CFEASolver.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ void CFEASolver::Compute_MassMatrix(CGeometry *geometry, CNumerics **numerics, c
10811081
const su2double simp_minstiff = config->GetSIMP_MinStiffness();
10821082

10831083
/*--- Never record this method as the mass matrix is passive (but the mass residual is not). ---*/
1084-
AD::BeginPassive();
1084+
const bool wasActive = AD::BeginPassive();
10851085

10861086
/*--- Start OpenMP parallel region. ---*/
10871087

@@ -1156,7 +1156,7 @@ void CFEASolver::Compute_MassMatrix(CGeometry *geometry, CNumerics **numerics, c
11561156

11571157
} // end SU2_OMP_PARALLEL
11581158

1159-
AD::EndPassive();
1159+
AD::EndPassive(wasActive);
11601160

11611161
}
11621162

@@ -1329,7 +1329,7 @@ void CFEASolver::Compute_NodalStressRes(CGeometry *geometry, CNumerics **numeric
13291329
void CFEASolver::Compute_NodalStress(CGeometry *geometry, CNumerics **numerics, const CConfig *config) {
13301330

13311331
/*--- Never record this method as atm it is not differentiable. ---*/
1332-
AD::BeginPassive();
1332+
const bool wasActive = AD::BeginPassive();
13331333

13341334
const bool prestretch_fem = config->GetPrestretch();
13351335

@@ -1608,7 +1608,7 @@ void CFEASolver::Compute_NodalStress(CGeometry *geometry, CNumerics **numerics,
16081608

16091609
}
16101610

1611-
AD::EndPassive();
1611+
AD::EndPassive(wasActive);
16121612

16131613
}
16141614

0 commit comments

Comments
 (0)