Skip to content

Commit

Permalink
Parallel task manager now calls static method from MultiColvarTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Aneurin Tribello committed Feb 5, 2025
1 parent 6482d08 commit 4125913
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
23 changes: 21 additions & 2 deletions src/colvar/MultiColvarTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ template <class T>
class MultiColvarTemplate : public ActionWithVector {
private:
/// The parallel task manager
ParallelTaskManager<T> taskmanager;
ParallelTaskManager<MultiColvarTemplate<T> > taskmanager;
/// An index that decides what we are calculating
unsigned mode;
/// Are we using pbc to calculate the CVs
Expand All @@ -65,6 +65,7 @@ class MultiColvarTemplate : public ActionWithVector {
void getInputData( std::vector<double>& 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<std::size_t>& der_indices, const bool noderiv, const bool haspbc, const Pbc& pbc, MultiValue& myvals );
};

Expand Down Expand Up @@ -131,7 +132,9 @@ MultiColvarTemplate<T>::MultiColvarTemplate(const ActionOptions&ao):
for(unsigned j=0; j<ablocks.size(); ++j) ind[i*ablocks.size() + j] = ablocks[j][i];
}
// Sets up the index list in the task manager
taskmanager.setupIndexList( ind );
taskmanager.setupIndexList( ind );
taskmanager.setPbcFlag( usepbc );
taskmanager.setMode( mode );
}

template <class T>
Expand Down Expand Up @@ -195,6 +198,22 @@ void MultiColvarTemplate<T>::performTask( const unsigned& task_index, MultiValue
performTask( mode, der_indices, doNotCalculateDerivatives(), usepbc, getPbc(), myvals );
}

template <class T>
void MultiColvarTemplate<T>::performTask( const ParallelActionsInput& input, MultiValue& myvals ) {
std::vector<double> & mass( myvals.getTemporyVector(0) );
std::vector<double> & charge( myvals.getTemporyVector(1) );
std::vector<Vector> & fpositions( myvals.getFirstAtomVector() );
for(unsigned i=0; i<fpositions.size(); ++i) {
std::size_t base = 5*fpositions.size()*input.task_index + 5*i;
fpositions[i][0] = input.inputdata[base + 0];
fpositions[i][1] = input.inputdata[base + 1];
fpositions[i][2] = input.inputdata[base + 2];
mass[i] = input.inputdata[base + 3];
charge[i] = input.inputdata[base + 4];
}
MultiColvarTemplate<T>::performTask( input.mode, input.indices, input.noderiv, input.usepbc, input.pbc, myvals );
}

template <class T>
void MultiColvarTemplate<T>::performTask( const unsigned& m, const std::vector<std::size_t>& der_indices, const bool noderiv, const bool haspbc, const Pbc& pbc, MultiValue& myvals ) {
// Retrieve the inputs
Expand Down
54 changes: 42 additions & 12 deletions src/core/ParallelTaskManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t> indices;
/// This holds all the input data that is required to calculate all values for all tasks
std::vector<double> inputdata;
ParallelActionsInput( const Pbc& box ) : usepbc(false), noderiv(false), pbc(box), mode(0), task_index(0) {}
};

template <class T>
class ParallelTaskManager {
private:
Expand All @@ -42,33 +56,46 @@ class ParallelTaskManager {
std::vector<double> buffer;
/// A tempory vector of MultiValue so we can avoid doing lots of resizes
std::vector<MultiValue> myvals;
/// This holds indices for creating derivatives
std::vector<std::size_t> indices;
/// This holds all the input data that is required to calculate all values for all tasks
std::vector<double> 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<std::size_t>& 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 <class T>
ParallelTaskManager<T>::ParallelTaskManager(ActionWithVector* av):
action(av),
comm(av->comm),
ismatrix(false)
ismatrix(false),
myinput(av->getPbc())
{
ActionWithMatrix* am=dynamic_cast<ActionWithMatrix*>(av);
if(am) ismatrix=true;
}

template <class T>
void ParallelTaskManager<T>::setMode( const unsigned val ) {
myinput.mode = val;
}

template <class T>
void ParallelTaskManager<T>::setPbcFlag( const bool val ) {
myinput.usepbc = val;
}

template <class T>
void ParallelTaskManager<T>::setupIndexList( const std::vector<std::size_t>& ind ) {
indices.resize( ind.size() ); for(unsigned i=0; i<ind.size(); ++i) indices[i] = ind[i];
myinput.indices.resize( ind.size() ); for(unsigned i=0; i<ind.size(); ++i) myinput.indices[i] = ind[i];
}

template <class T>
Expand Down Expand Up @@ -98,7 +125,8 @@ void ParallelTaskManager<T>::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)
{
Expand All @@ -113,7 +141,8 @@ void ParallelTaskManager<T>::runAllTasks( const unsigned& natoms ) {
#pragma omp for nowait
for(unsigned i=rank; i<nactive_tasks; i+=stride) {
// Calculate the stuff in the loop for this action
runTask( partialTaskList[i], myvals[t] );
myinput.task_index = partialTaskList[i];
runTask( myinput, myvals[t] );

// Clear the value
myvals[t].clearAll();
Expand All @@ -129,14 +158,15 @@ void ParallelTaskManager<T>::runAllTasks( const unsigned& natoms ) {
}

template <class T>
void ParallelTaskManager<T>::runTask( const unsigned& current, MultiValue& myvals ) const {
void ParallelTaskManager<T>::runTask( const ParallelActionsInput& locinp, MultiValue& myvals ) const {
const ActionWithMatrix* am = dynamic_cast<const ActionWithMatrix*>(action);
myvals.setTaskIndex(locinp.task_index); T::performTask( locinp, myvals );
if( am ) return ;
myvals.setTaskIndex(current); action->performTask( current, myvals );

for(unsigned i=0; i<action->getNumberOfComponents(); ++i) {
const Value* myval = action->getConstPntrToComponent(i);
if( myval->hasDerivatives() || (action->getName()=="RMSD_VECTOR" && myval->getRank()==2) ) continue;
Value* myv = const_cast<Value*>( myval ); myv->set( current, myvals.get( i ) );
Value* myv = const_cast<Value*>( myval ); myv->set( locinp.task_index, myvals.get( i ) );
}
}

Expand Down

0 comments on commit 4125913

Please sign in to comment.