From 94d33a9d2406273e94554936c208a98c25172fed Mon Sep 17 00:00:00 2001 From: Gareth Aneurin Tribello Date: Thu, 18 Jul 2024 10:32:35 +0100 Subject: [PATCH] Added some optimisations in AdjacencyMatrixBase --- src/adjmat/AdjacencyMatrixBase.cpp | 24 ++++++++++++++---------- src/core/ActionWithVector.h | 6 ++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/adjmat/AdjacencyMatrixBase.cpp b/src/adjmat/AdjacencyMatrixBase.cpp index 98eb5ed620..8b569829da 100644 --- a/src/adjmat/AdjacencyMatrixBase.cpp +++ b/src/adjmat/AdjacencyMatrixBase.cpp @@ -308,10 +308,12 @@ void AdjacencyMatrixBase::performTask( const std::string& controller, const unsi // Update dynamic list indices for virial unsigned base = 3*getNumberOfAtoms(); for(unsigned j=0; j<9; ++j) myvals.updateIndex( w_ind, base+j ); // And the indices for the derivatives of the row of the matrix - unsigned nmat = getConstPntrToComponent(0)->getPositionInMatrixStash(), nmat_ind = myvals.getNumberOfMatrixRowDerivatives( nmat ); - std::vector& matrix_indices( myvals.getMatrixRowDerivativeIndices( nmat ) ); - matrix_indices[nmat_ind+0]=3*index2+0; matrix_indices[nmat_ind+1]=3*index2+1; matrix_indices[nmat_ind+2]=3*index2+2; - myvals.setNumberOfMatrixRowDerivatives( nmat, nmat_ind+3 ); + if( chainContinuesAfterThisAction() ) { + unsigned nmat = getConstPntrToComponent(0)->getPositionInMatrixStash(), nmat_ind = myvals.getNumberOfMatrixRowDerivatives( nmat ); + std::vector& matrix_indices( myvals.getMatrixRowDerivativeIndices( nmat ) ); + matrix_indices[nmat_ind+0]=3*index2+0; matrix_indices[nmat_ind+1]=3*index2+1; matrix_indices[nmat_ind+2]=3*index2+2; + myvals.setNumberOfMatrixRowDerivatives( nmat, nmat_ind+3 ); + } } // Calculate the components if we need them @@ -352,18 +354,20 @@ void AdjacencyMatrixBase::performTask( const std::string& controller, const unsi myvals.addDerivative( z_index, base+1, 0 ); myvals.addDerivative( z_index, base+4, 0 ); myvals.addDerivative( z_index, base+7, 0 ); myvals.addDerivative( z_index, base+2, -atom[0] ); myvals.addDerivative( z_index, base+5, -atom[1] ); myvals.addDerivative( z_index, base+8, -atom[2] ); for(unsigned k=0; k<9; ++k) { myvals.updateIndex( x_index, base+k ); myvals.updateIndex( y_index, base+k ); myvals.updateIndex( z_index, base+k ); } - for(unsigned k=1; k<4; ++k) { - unsigned nmat = getConstPntrToComponent(k)->getPositionInMatrixStash(), nmat_ind = myvals.getNumberOfMatrixRowDerivatives( nmat ); - std::vector& matrix_indices( myvals.getMatrixRowDerivativeIndices( nmat ) ); - matrix_indices[nmat_ind+0]=3*index2+0; matrix_indices[nmat_ind+1]=3*index2+1; matrix_indices[nmat_ind+2]=3*index2+2; - myvals.setNumberOfMatrixRowDerivatives( nmat, nmat_ind+3 ); + if( chainContinuesAfterThisAction() ) { + for(unsigned k=1; k<4; ++k) { + unsigned nmat = getConstPntrToComponent(k)->getPositionInMatrixStash(), nmat_ind = myvals.getNumberOfMatrixRowDerivatives( nmat ); + std::vector& matrix_indices( myvals.getMatrixRowDerivativeIndices( nmat ) ); + matrix_indices[nmat_ind+0]=3*index2+0; matrix_indices[nmat_ind+1]=3*index2+1; matrix_indices[nmat_ind+2]=3*index2+2; + myvals.setNumberOfMatrixRowDerivatives( nmat, nmat_ind+3 ); + } } } } } void AdjacencyMatrixBase::runEndOfRowJobs( const unsigned& ind, const std::vector & indices, MultiValue& myvals ) const { - if( doNotCalculateDerivatives() ) return; + if( doNotCalculateDerivatives() || !chainContinuesAfterThisAction() ) return; for(int k=0; kgetPositionInMatrixStash(), nmat_ind = myvals.getNumberOfMatrixRowDerivatives( nmat ); diff --git a/src/core/ActionWithVector.h b/src/core/ActionWithVector.h index 6327715da5..c8daa8980b 100644 --- a/src/core/ActionWithVector.h +++ b/src/core/ActionWithVector.h @@ -125,6 +125,7 @@ class ActionWithVector: bool doNotCalculateDerivatives() const override ; /// Are we running this command in a chain bool actionInChain() const ; + bool chainContinuesAfterThisAction() const ; /// This is overwritten within ActionWithMatrix and is used to build the chain of just matrix actions virtual void finishChainBuild( ActionWithVector* act ); /// Check if there are any stored values in arguments @@ -185,6 +186,11 @@ bool ActionWithVector::actionInChain() const { return (action_to_do_before!=NULL); } +inline +bool ActionWithVector::chainContinuesAfterThisAction() const { + return (action_to_do_after!=NULL); +} + inline bool ActionWithVector::runInSerial() const { return serial;