Skip to content

Commit

Permalink
Added some helper pacakages that will be needed for next step
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Aneurin Tribello committed Feb 5, 2025
1 parent e3153cc commit 6482d08
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/colvar/MultiColvarTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class MultiColvarTemplate : public ActionWithVector {
unsigned getNumberOfAtomsPerTask() const override ;
void addValueWithDerivatives( const std::vector<unsigned>& shape=std::vector<unsigned>() ) override ;
void addComponentWithDerivatives( const std::string& name, const std::vector<unsigned>& shape=std::vector<unsigned>() ) override ;
void getInputData( std::vector<double>& inputdata ) const override ;
void performTask( const unsigned&, MultiValue& ) const override ;
void calculate() override;
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 @@ -124,6 +125,13 @@ MultiColvarTemplate<T>::MultiColvarTemplate(const ActionOptions&ao):

// Setup the values
mode = T::getModeAndSetupValues( this );
// This sets up an array in the parallel task manager to hold all the indices
std::vector<std::size_t> ind( ablocks.size()*ablocks[0].size() );
for(unsigned i=0; i<ablocks[0].size(); ++i) {
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 );
}

template <class T>
Expand Down Expand Up @@ -154,6 +162,23 @@ unsigned MultiColvarTemplate<T>::getNumberOfAtomsPerTask() const {
return ablocks.size();
}

template <class T>
void MultiColvarTemplate<T>::getInputData( std::vector<double>& inputdata ) const {
unsigned ntasks = ablocks[0].size(); std::size_t k=0;
if( inputdata.size()!=5*ablocks.size()*ntasks ) inputdata.resize( 5*ablocks.size()*ntasks );
for(unsigned i=0; i<ntasks; ++i) {
for(unsigned j=0; j<ablocks.size(); ++j) {
Vector mypos( getPosition( ablocks[j][i] ) );
inputdata[k] = mypos[0];
inputdata[k+1] = mypos[1];
inputdata[k+2] = mypos[2];
inputdata[k+3] = getMass( ablocks[j][i] );
inputdata[k+4] = getCharge( ablocks[j][i] );
k+=5;
}
}
}

template <class T>
void MultiColvarTemplate<T>::performTask( const unsigned& task_index, MultiValue& myvals ) const {
// Retrieve the positions
Expand Down
14 changes: 14 additions & 0 deletions src/core/ParallelTaskManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ 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;
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 );
/// This runs all the tasks
void runAllTasks( const unsigned& natoms=0 );
/// This runs each of the tasks
Expand All @@ -60,6 +66,11 @@ ParallelTaskManager<T>::ParallelTaskManager(ActionWithVector* av):
if(am) ismatrix=true;
}

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];
}

template <class T>
void ParallelTaskManager<T>::runAllTasks( const unsigned& natoms ) {
unsigned stride=comm.Get_size();
Expand All @@ -86,6 +97,9 @@ void ParallelTaskManager<T>::runAllTasks( const unsigned& natoms ) {
// Clear buffer
buffer.assign( buffer.size(), 0.0 );

// Get all the input data so we can broadcast it to the GPU
action->getInputData( inputdata );

#pragma omp parallel num_threads(nt)
{
std::vector<double> omp_buffer;
Expand Down

0 comments on commit 6482d08

Please sign in to comment.