OpenCLExpressionUtilities.h 7.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_OPENCLEXPRESSIONUTILITIES_H_
#define OPENMM_OPENCLEXPRESSIONUTILITIES_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-2014 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * 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/>.      *
 * -------------------------------------------------------------------------- */

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

namespace OpenMM {

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

47
class OPENMM_EXPORT_OPENCL OpenCLExpressionUtilities {
48
public:
49
50
    OpenCLExpressionUtilities(OpenCLContext& context) : context(context) {
    }
51
52
53
54
    /**
     * 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)
55
56
     * @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
57
58
     * @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
59
60
     * @param prefix         a prefix to put in front of temporary variables
     * @param functionParams the variable name containing the parameters for each tabulated function
61
     * @param tempType       the type of value to use for temporary variables (defaults to "real")
62
     */
63
    std::string createExpressions(const std::map<std::string, Lepton::ParsedExpression>& expressions, const std::map<std::string, std::string>& variables,
peastman's avatar
peastman committed
64
65
            const std::vector<const TabulatedFunction*>& functions, const std::vector<std::pair<std::string, std::string> >& functionNames,
            const std::string& prefix, const std::string& functionParams, const std::string& tempType="real");
66
67
68
69
70
71
    /**
     * 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
72
73
     * @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
74
75
     * @param prefix         a prefix to put in front of temporary variables
     * @param functionParams the variable name containing the parameters for each tabulated function
76
     * @param tempType       the type of value to use for temporary variables (defaults to "float")
77
     */
78
    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
79
80
            const std::vector<const TabulatedFunction*>& functions, const std::vector<std::pair<std::string, std::string> >& functionNames,
            const std::string& prefix, const std::string& functionParams, const std::string& tempType="float");
81
82
83
    /**
     * Calculate the spline coefficients for a tabulated function that appears in expressions.
     *
84
     * @param function   the function for which to compute coefficients
peastman's avatar
peastman committed
85
     * @param width      on output, the number of floats used for each value
86
87
     * @return the spline coefficients
     */
peastman's avatar
peastman committed
88
    std::vector<float> computeFunctionCoefficients(const TabulatedFunction& function, int& width);
89
90
91
92
93
94
95
    /**
     * Given the list of TabulatedFunctions used by a Force, create the parameter array describing them.
     *
     * @param functions   the list of functions to include in the array
     * @return the parameter array
     */
    std::vector<mm_float4> computeFunctionParameters(const std::vector<const TabulatedFunction*>& functions);
96
    class FunctionPlaceholder;
97
private:
98
    void processExpression(std::stringstream& out, const Lepton::ExpressionTreeNode& node,
99
            std::vector<std::pair<Lepton::ExpressionTreeNode, std::string> >& temps,
peastman's avatar
peastman committed
100
101
            const std::vector<const TabulatedFunction*>& functions, const std::vector<std::pair<std::string, std::string> >& functionNames,
            const std::string& prefix, const std::string& functionParams, const std::vector<Lepton::ParsedExpression>& allExpressions, const std::string& tempType);
102
103
    std::string getTempName(const Lepton::ExpressionTreeNode& node, const std::vector<std::pair<Lepton::ExpressionTreeNode, std::string> >& temps);
    void findRelatedTabulatedFunctions(const Lepton::ExpressionTreeNode& node, const Lepton::ExpressionTreeNode& searchNode,
104
            const Lepton::ExpressionTreeNode*& valueNode, const Lepton::ExpressionTreeNode*& derivNode);
105
    void findRelatedPowers(const Lepton::ExpressionTreeNode& node, const Lepton::ExpressionTreeNode& searchNode,
106
            std::map<int, const Lepton::ExpressionTreeNode*>& powers);
107
    OpenCLContext& context;
108
109
};

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
 * This class serves as a placeholder for custom functions in expressions.
 */

class OpenCLExpressionUtilities::FunctionPlaceholder : public Lepton::CustomFunction {
public:
    int getNumArguments() const {
        return 1;
    }
    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();
    }
};

130
131
132
} // namespace OpenMM

#endif /*OPENMM_OPENCLEXPRESSIONUTILITIES_H_*/