From 4125913e8182110341fc3ebf5ed55d7c0edcd379 Mon Sep 17 00:00:00 2001 From: Gareth Aneurin Tribello Date: Wed, 5 Feb 2025 15:06:22 +0100 Subject: [PATCH] Parallel task manager now calls static method from MultiColvarTemplate --- src/colvar/MultiColvarTemplate.h | 23 ++++++++++++-- src/core/ParallelTaskManager.h | 54 +++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/colvar/MultiColvarTemplate.h b/src/colvar/MultiColvarTemplate.h index f58bfaa0bb..bc66f17575 100644 --- a/src/colvar/MultiColvarTemplate.h +++ b/src/colvar/MultiColvarTemplate.h @@ -46,7 +46,7 @@ template class MultiColvarTemplate : public ActionWithVector { private: /// The parallel task manager - ParallelTaskManager taskmanager; + ParallelTaskManager > taskmanager; /// An index that decides what we are calculating unsigned mode; /// Are we using pbc to calculate the CVs @@ -65,6 +65,7 @@ class MultiColvarTemplate : public ActionWithVector { void getInputData( std::vector& inputdata ) const override ; void performTask( const unsigned&, MultiValue& ) const override ; void calculate() override; + static void performTask( const ParallelActionsInput& input, MultiValue& myvals ); static void performTask( const unsigned& m, const std::vector& der_indices, const bool noderiv, const bool haspbc, const Pbc& pbc, MultiValue& myvals ); }; @@ -131,7 +132,9 @@ MultiColvarTemplate::MultiColvarTemplate(const ActionOptions&ao): for(unsigned j=0; j @@ -195,6 +198,22 @@ void MultiColvarTemplate::performTask( const unsigned& task_index, MultiValue performTask( mode, der_indices, doNotCalculateDerivatives(), usepbc, getPbc(), myvals ); } +template +void MultiColvarTemplate::performTask( const ParallelActionsInput& input, MultiValue& myvals ) { + std::vector & mass( myvals.getTemporyVector(0) ); + std::vector & charge( myvals.getTemporyVector(1) ); + std::vector & fpositions( myvals.getFirstAtomVector() ); + for(unsigned i=0; i::performTask( input.mode, input.indices, input.noderiv, input.usepbc, input.pbc, myvals ); +} + template void MultiColvarTemplate::performTask( const unsigned& m, const std::vector& der_indices, const bool noderiv, const bool haspbc, const Pbc& pbc, MultiValue& myvals ) { // Retrieve the inputs diff --git a/src/core/ParallelTaskManager.h b/src/core/ParallelTaskManager.h index b0e5bbe209..a9231d6145 100644 --- a/src/core/ParallelTaskManager.h +++ b/src/core/ParallelTaskManager.h @@ -29,6 +29,20 @@ namespace PLMD { +class ParallelActionsInput { +public: + bool usepbc; + bool noderiv; + const Pbc& pbc; + unsigned mode; + unsigned task_index; +/// This holds indices for creating derivatives + std::vector indices; +/// This holds all the input data that is required to calculate all values for all tasks + std::vector inputdata; + ParallelActionsInput( const Pbc& box ) : usepbc(false), noderiv(false), pbc(box), mode(0), task_index(0) {} +}; + template class ParallelTaskManager { private: @@ -42,33 +56,46 @@ class ParallelTaskManager { std::vector buffer; /// A tempory vector of MultiValue so we can avoid doing lots of resizes std::vector myvals; -/// This holds indices for creating derivatives - std::vector indices; -/// This holds all the input data that is required to calculate all values for all tasks - std::vector inputdata; +/// An action to hold data that we pass to and from the static function + ParallelActionsInput myinput; public: ParallelTaskManager(ActionWithVector* av); /// Setup an array to hold all the indices that are used for derivatives void setupIndexList( const std::vector& ind ); +/// Set the mode for the calculation + void setMode( const unsigned val ); +/// Set the value of the pbc flag + void setPbcFlag( const bool val ); /// This runs all the tasks void runAllTasks( const unsigned& natoms=0 ); /// This runs each of the tasks - void runTask( const unsigned& current, MultiValue& myvals ) const ; + void runTask( const ParallelActionsInput& locinp, MultiValue& myvals ) const ; }; template ParallelTaskManager::ParallelTaskManager(ActionWithVector* av): action(av), comm(av->comm), - ismatrix(false) + ismatrix(false), + myinput(av->getPbc()) { ActionWithMatrix* am=dynamic_cast(av); if(am) ismatrix=true; } +template +void ParallelTaskManager::setMode( const unsigned val ) { + myinput.mode = val; +} + +template +void ParallelTaskManager::setPbcFlag( const bool val ) { + myinput.usepbc = val; +} + template void ParallelTaskManager::setupIndexList( const std::vector& ind ) { - indices.resize( ind.size() ); for(unsigned i=0; i @@ -98,7 +125,8 @@ void ParallelTaskManager::runAllTasks( const unsigned& natoms ) { buffer.assign( buffer.size(), 0.0 ); // Get all the input data so we can broadcast it to the GPU - action->getInputData( inputdata ); + myinput.noderiv = true; + action->getInputData( myinput.inputdata ); #pragma omp parallel num_threads(nt) { @@ -113,7 +141,8 @@ void ParallelTaskManager::runAllTasks( const unsigned& natoms ) { #pragma omp for nowait for(unsigned i=rank; i::runAllTasks( const unsigned& natoms ) { } template -void ParallelTaskManager::runTask( const unsigned& current, MultiValue& myvals ) const { +void ParallelTaskManager::runTask( const ParallelActionsInput& locinp, MultiValue& myvals ) const { const ActionWithMatrix* am = dynamic_cast(action); + myvals.setTaskIndex(locinp.task_index); T::performTask( locinp, myvals ); if( am ) return ; - myvals.setTaskIndex(current); action->performTask( current, myvals ); + for(unsigned i=0; igetNumberOfComponents(); ++i) { const Value* myval = action->getConstPntrToComponent(i); if( myval->hasDerivatives() || (action->getName()=="RMSD_VECTOR" && myval->getRank()==2) ) continue; - Value* myv = const_cast( myval ); myv->set( current, myvals.get( i ) ); + Value* myv = const_cast( myval ); myv->set( locinp.task_index, myvals.get( i ) ); } }