"platforms/vscode:/vscode.git/clone" did not exist on "05ad28f23dba23340416e9197d9c14cb275b37a5"
CudaExpressionUtilities.h 7.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_CUDAEXPRESSIONUTILITIES_H_
#define OPENMM_CUDAEXPRESSIONUTILITIES_H_

/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
12
 * Portions copyright (c) 2009-2015 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include "CudaContext.h"
31
#include "openmm/TabulatedFunction.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "lepton/CustomFunction.h"
#include "lepton/ExpressionTreeNode.h"
#include "lepton/ParsedExpression.h"
#include <map>
#include <sstream>
#include <string>
#include <utility>

namespace OpenMM {

/**
 * This class is used by various classes to generate CUDA source code implementing
 * user defined mathematical expressions.
 */

47
class OPENMM_EXPORT_CUDA CudaExpressionUtilities {
48
public:
49
    CudaExpressionUtilities(CudaContext& context);
50
51
52
53
54
55
    /**
     * Generate the source code for calculating a set of expressions.
     *
     * @param expressions    the expressions to generate code for (keys are the variables to store the output values in)
     * @param variables      defines the source code to generate for each variable that may appear in the expressions.  Keys are
     *                       variable names, and the values are the code to generate for them.
peastman's avatar
peastman committed
56
57
     * @param functions      the tabulated functions that may appear in the expressions
     * @param functionNames  defines the variable name for each tabulated function that may appear in the expressions
58
     * @param prefix         a prefix to put in front of temporary variables
59
     * @param tempType       the type of value to use for temporary variables (defaults to "real")
60
     */
61
    std::string createExpressions(const std::map<std::string, Lepton::ParsedExpression>& expressions, const std::map<std::string, std::string>& variables,
peastman's avatar
peastman committed
62
            const std::vector<const TabulatedFunction*>& functions, const std::vector<std::pair<std::string, std::string> >& functionNames,
63
            const std::string& prefix, const std::string& tempType="real");
64
65
66
67
68
69
    /**
     * Generate the source code for calculating a set of expressions.
     *
     * @param expressions    the expressions to generate code for (keys are the variables to store the output values in)
     * @param variables      defines the source code to generate for each variable or precomputed sub-expression that may appear in the expressions.
     *                       Each entry is an ExpressionTreeNode, and the code to generate wherever an identical node appears.
peastman's avatar
peastman committed
70
71
     * @param functions      the tabulated functions that may appear in the expressions
     * @param functionNames  defines the variable name for each tabulated function that may appear in the expressions
72
     * @param prefix         a prefix to put in front of temporary variables
73
     * @param tempType       the type of value to use for temporary variables (defaults to "real")
74
     */
75
    std::string createExpressions(const std::map<std::string, Lepton::ParsedExpression>& expressions, const std::vector<std::pair<Lepton::ExpressionTreeNode, std::string> >& variables,
peastman's avatar
peastman committed
76
            const std::vector<const TabulatedFunction*>& functions, const std::vector<std::pair<std::string, std::string> >& functionNames,
77
            const std::string& prefix, const std::string& tempType="real");
78
79
80
    /**
     * Calculate the spline coefficients for a tabulated function that appears in expressions.
     *
81
     * @param function   the function for which to compute coefficients
peastman's avatar
peastman committed
82
     * @param width      on output, the number of floats used for each value
83
84
     * @return the spline coefficients
     */
peastman's avatar
peastman committed
85
    std::vector<float> computeFunctionCoefficients(const TabulatedFunction& function, int& width);
86
87
88
89
90
91
    /**
     * Get a Lepton::CustomFunction that can be used to represent a TabulatedFunction when parsing expressions.
     * 
     * @param function   the function for which to get a placeholder
     */
    Lepton::CustomFunction* getFunctionPlaceholder(const TabulatedFunction& function);
92
93
94
95
    /**
     * Get a Lepton::CustomFunction that can be used to represent the periodicdistance() function when parsing expressions.
     */
    Lepton::CustomFunction* getPeriodicDistancePlaceholder();
96
private:
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    class FunctionPlaceholder : public Lepton::CustomFunction {
        public:
            FunctionPlaceholder(int numArgs) : numArgs(numArgs) {
            }
            int getNumArguments() const {
                return numArgs;
            }
            double evaluate(const double* arguments) const {
                return 0.0;
            }
            double evaluateDerivative(const double* arguments, const int* derivOrder) const {
                return 0.0;
            }
            CustomFunction* clone() const {
                return new FunctionPlaceholder(numArgs);
            }
        private:
            int numArgs;
    };
116
    void processExpression(std::stringstream& out, const Lepton::ExpressionTreeNode& node,
117
            std::vector<std::pair<Lepton::ExpressionTreeNode, std::string> >& temps,
peastman's avatar
peastman committed
118
            const std::vector<const TabulatedFunction*>& functions, const std::vector<std::pair<std::string, std::string> >& functionNames,
119
            const std::string& prefix, const std::vector<std::vector<double> >& functionParams, const std::vector<Lepton::ParsedExpression>& allExpressions, const std::string& tempType);
120
    std::string getTempName(const Lepton::ExpressionTreeNode& node, const std::vector<std::pair<Lepton::ExpressionTreeNode, std::string> >& temps);
121
    void findRelatedCustomFunctions(const Lepton::ExpressionTreeNode& node, const Lepton::ExpressionTreeNode& searchNode,
122
            std::vector<const Lepton::ExpressionTreeNode*>& nodes);
123
    void findRelatedPowers(const Lepton::ExpressionTreeNode& node, const Lepton::ExpressionTreeNode& searchNode,
124
            std::map<int, const Lepton::ExpressionTreeNode*>& powers);
125
    void callFunction(std::stringstream& out, std::string singleFn, std::string doubleFn, const std::string& arg, const std::string& tempType);
126
    std::vector<std::vector<double> > computeFunctionParameters(const std::vector<const TabulatedFunction*>& functions);
127
    CudaContext& context;
128
    FunctionPlaceholder fp1, fp2, fp3, periodicDistance;
129
130
131
132
133
};

} // namespace OpenMM

#endif /*OPENMM_CUDAEXPRESSIONUTILITIES_H_*/