Line data Source code
1 : #ifndef __PLUMED_lepton_CompiledExpression_h 2 : #define __PLUMED_lepton_CompiledExpression_h 3 : 4 : /* -------------------------------------------------------------------------- * 5 : * lepton * 6 : * -------------------------------------------------------------------------- * 7 : * This is part of the lepton expression parser. * 8 : * See https://openmm.org/development. * 9 : * * 10 : * Portions copyright (c) 2013-2022 Stanford University and the Authors. * 11 : * Authors: Peter Eastman * 12 : * Contributors: * 13 : * * 14 : * Permission is hereby granted, free of charge, to any person obtaining a * 15 : * copy of this software and associated documentation files (the "Software"), * 16 : * to deal in the Software without restriction, including without limitation * 17 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 18 : * and/or sell copies of the Software, and to permit persons to whom the * 19 : * Software is furnished to do so, subject to the following conditions: * 20 : * * 21 : * The above copyright notice and this permission notice shall be included in * 22 : * all copies or substantial portions of the Software. * 23 : * * 24 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 25 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 26 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 27 : * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 28 : * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 29 : * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 30 : * USE OR OTHER DEALINGS IN THE SOFTWARE. * 31 : * -------------------------------------------------------------------------- */ 32 : 33 : #include "ExpressionTreeNode.h" 34 : #include "windowsIncludes.h" 35 : #include <map> 36 : #include <set> 37 : #include <string> 38 : #include <utility> 39 : #include <vector> 40 : 41 : namespace PLMD { 42 : namespace lepton { 43 : 44 : bool useAsmJit(); 45 : 46 : // Utility class. 47 : // Implement an unique pointer to asmjit::JitRuntime. 48 : // Needed to decouple asmjit header file from this one. 49 : class AsmJitRuntimePtr { 50 : /// if ASMJIT is not defined, just set the pointer to null 51 : void* ptr=nullptr; 52 : public: 53 : /// constructor 54 : AsmJitRuntimePtr(); 55 : /// destructor 56 : ~AsmJitRuntimePtr(); 57 : /// deleted copy constructor 58 : AsmJitRuntimePtr(const AsmJitRuntimePtr&) = delete; 59 : /// deleted assignment 60 : AsmJitRuntimePtr & operator=(const AsmJitRuntimePtr&) = delete; 61 : /// get the pointer 62 : void* get() { 63 27492 : return ptr; 64 : } 65 : }; 66 : 67 : class Operation; 68 : class ParsedExpression; 69 : 70 : /** 71 : * A CompiledExpression is a highly optimized representation of an expression for cases when you want to evaluate 72 : * it many times as quickly as possible. You should treat it as an opaque object; none of the internal representation 73 : * is visible. 74 : * 75 : * A CompiledExpression is created by calling createCompiledExpression() on a ParsedExpression. 76 : * 77 : * WARNING: CompiledExpression is NOT thread safe. You should never access a CompiledExpression from two threads at 78 : * the same time. 79 : */ 80 : 81 : class LEPTON_EXPORT CompiledExpression { 82 : public: 83 : CompiledExpression(); 84 : CompiledExpression(const CompiledExpression& expression); 85 : ~CompiledExpression(); 86 : CompiledExpression& operator=(const CompiledExpression& expression); 87 : /** 88 : * Get the names of all variables used by this expression. 89 : */ 90 : const std::set<std::string>& getVariables() const; 91 : /** 92 : * Get a reference to the memory location where the value of a particular variable is stored. This can be used 93 : * to set the value of the variable before calling evaluate(). 94 : */ 95 : double& getVariableReference(const std::string& name); 96 : /** 97 : * You can optionally specify the memory locations from which the values of variables should be read. 98 : * This is useful, for example, when several expressions all use the same variable. You can then set 99 : * the value of that variable in one place, and it will be seen by all of them. 100 : */ 101 : void setVariableLocations(std::map<std::string, double*>& variableLocations); 102 : /** 103 : * Evaluate the expression. The values of all variables should have been set before calling this. 104 : */ 105 : double evaluate() const; 106 : private: 107 : friend class ParsedExpression; 108 : CompiledExpression(const ParsedExpression& expression); 109 : void compileExpression(const ExpressionTreeNode& node, std::vector<std::pair<ExpressionTreeNode, int> >& temps); 110 : int findTempIndex(const ExpressionTreeNode& node, std::vector<std::pair<ExpressionTreeNode, int> >& temps); 111 : std::map<std::string, double*> variablePointers; 112 : std::vector<std::pair<double*, double*> > variablesToCopy; 113 : std::vector<std::vector<int> > arguments; 114 : std::vector<int> target; 115 : std::vector<Operation*> operation; 116 : std::map<std::string, int> variableIndices; 117 : std::set<std::string> variableNames; 118 : mutable std::vector<double> workspace; 119 : mutable std::vector<double> argValues; 120 : std::map<std::string, double> dummyVariables; 121 : double (*jitCode)(); 122 : void generateJitCode(); 123 : void findPowerGroups(std::vector<std::vector<int> >& groups, std::vector<std::vector<int> >& groupPowers, std::vector<int>& stepGroup); 124 : std::vector<double> constants; 125 : AsmJitRuntimePtr runtimeptr; 126 : }; 127 : 128 : } // namespace lepton 129 : } // namespace PLMD 130 : 131 : 132 : #endif // __PLUMED_lepton_CompiledExpression_h