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