Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2023 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed is distributed in the hope that it will be useful, 15 : but WITHOUT ANY WARRANTY; without even the implied warranty of 16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_core_ActionWithMatrix_h 23 : #define __PLUMED_core_ActionWithMatrix_h 24 : 25 : #include "ActionWithVector.h" 26 : #include <cstddef> 27 : 28 : namespace PLMD { 29 : 30 : //this class serves as a workaround for moving data with openacc without specifying --memory=managed 31 345 : class RequiredMatrixElements { 32 : std::vector<std::size_t> bookeeping{}; 33 : std::size_t* bookeeping_data=nullptr; 34 : public: 35 : std::size_t ncols=0; 36 : void update() { 37 26177 : bookeeping_data = bookeeping.data(); 38 : } 39 : void toACCDevice() const { 40 : //this assumes that update() has already been called 41 : #pragma acc enter data copyin(this[0:1], bookeeping_data[0:bookeeping.size()]) 42 : } 43 : void removeFromACCDevice() const { 44 : #pragma acc exit data delete(bookeeping_data[0:bookeeping.size()],this[0:1]) 45 : } 46 : std::size_t& operator[]( std::size_t i ) { 47 48637775 : return bookeeping_data[i]; 48 : } 49 : std::size_t operator[]( std::size_t i ) const { 50 46077399 : return bookeeping_data[i]; 51 : } 52 : std::size_t size() const { 53 : return bookeeping.size(); 54 : } 55 : void resize(std::size_t newSize) { 56 13119 : bookeeping.resize(newSize); 57 : update(); 58 : } 59 : }; 60 : 61 : class MatrixElementOutput { 62 : public: 63 : View<double> values; 64 : View2D<double> derivs; 65 : MatrixElementOutput( std::size_t nv, std::size_t nd, double* v, double* d ) : 66 : values( v, nv ), 67 : derivs( d, nv, nd ) { 68 : } 69 : }; 70 : 71 : class ActionWithMatrix : public ActionWithVector { 72 : private: 73 : /// List of columns in matrix that have non-zero elements 74 : std::vector<unsigned> column_list; 75 : protected: 76 : /// The number of elements in the longest column of the sparse matrix that is output by this action 77 : unsigned maxcolsize; 78 : /// Some actions have a flag that sets this to true. The elements on the diagonal of the resulting matrix are then set to zero 79 : bool diagzero; 80 : /// This flag turns on a low memory implemntation of the multi-threaded calculation 81 : bool no_thread_gather; 82 : /// This flag is activated when we run through the columns of the matrix when gathering forces in a thread safe way 83 : bool gatherForceOnColumns; 84 : /// Copy the matrix bookeeping for task list stuff from the first component 85 : void copyMatrixBookeepingFromFirstComponent( RequiredMatrixElements& outmat ); 86 : /// Implementation of transferStashToValues for both precisions 87 : template <typename T> 88 : void matrixStashToValuesImpl( const std::vector<unsigned>& partialTaskList, const std::vector<T>& stash ); 89 : /// Implementation of transferForcesToStash for both precisions 90 : template <typename T> 91 : void matrixForcesToStashImpl( const std::vector<unsigned>& partialTaskList, std::vector<T>& stash ) const ; 92 : /// Update all the arrays for doing bookeeping 93 : void updateBookeepingArrays( RequiredMatrixElements& mat ); 94 : /// Get an array that tells us which indices are in each column 95 : void getColumnBookeepingArrays( RequiredMatrixElements& outmat ); 96 : public: 97 : static void registerKeywords( Keywords& keys ); 98 : explicit ActionWithMatrix(const ActionOptions&); 99 : /// Get the list of active tasks for this action 100 : std::vector<unsigned>& getListOfActiveTasks() override ; 101 : /// Get the size to use for the value in the stash 102 : unsigned getSizeOfValueInStash( const Value* myval ) override ; 103 : /// Get the elements of the matrices into the output values 104 : void transferStashToValues( const std::vector<unsigned>& partialTaskList, const std::vector<double>& stash ) override ; 105 : /// Get the elements of the matrices into the output values 106 : void transferStashToValues( const std::vector<unsigned>& partialTaskList, const std::vector<float>& stash ) override ; 107 : /// Get the forces from the output values and transfer them to the stash 108 : void transferForcesToStash( const std::vector<unsigned>& partialTaskList, std::vector<double>& stash ) const override ; 109 : /// Get the forces from the output values and transfer them to the stash 110 : void transferForcesToStash( const std::vector<unsigned>& partialTaskList, std::vector<float>& stash ) const override ; 111 : }; 112 : 113 : } 114 : #endif