kEvaluateExpression.h 9.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

/**
 * This file contains the routine for evaluating a custom expression.
 */

static __constant__ float globalParams[8];

texture<float4, 1, cudaReadModeElementType> texRef0;
texture<float4, 1, cudaReadModeElementType> texRef1;
texture<float4, 1, cudaReadModeElementType> texRef2;
texture<float4, 1, cudaReadModeElementType> texRef3;

#define STACK(y) stack[(y)*blockDim.x+threadIdx.x]
#define VARIABLE(y) variables[(y)*blockDim.x+threadIdx.x]

template<int SIZE>
__device__ float kEvaluateExpression_kernel(Expression<SIZE>* expression, float* stack, float* variables)
{
    int stackPointer = -1;
    for (int i = 0; i < expression->length; i++)
    {
        int op = expression->op[i];
        if (op < MULTIPLY) {
            STACK(++stackPointer) = VARIABLE(op-VARIABLE0);
        }
        else if (op < NEGATE) {
            if (op < MULTIPLY_CONSTANT) {
                if (op == MULTIPLY) {
                    float temp = STACK(stackPointer);
                    STACK(--stackPointer) *= temp;
                }
                else if (op == DIVIDE) {
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) = temp/STACK(--stackPointer);
                }
                else if (op == ADD) {
                    float temp = STACK(stackPointer);
                    STACK(--stackPointer) += temp;
                }
                else if (op == SUBTRACT) {
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) = temp-STACK(--stackPointer);
                }
                else /*if (op == POWER)*/ {
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) = pow(temp, STACK(--stackPointer));
                }
            }
            else if (op < GLOBAL) {
                if (op == MULTIPLY_CONSTANT) {
                    STACK(stackPointer) *= expression->arg[i];
                }
                else if (op == POWER_CONSTANT) {
                    STACK(stackPointer) = pow(STACK(stackPointer), expression->arg[i]);
                }
                else /*if (op == ADD_CONSTANT)*/ {
                    STACK(stackPointer) += expression->arg[i];
                }
            }
            else {
                if (op == GLOBAL) {
                    STACK(++stackPointer) = globalParams[(int) expression->arg[i]];
                }
                else if (op == CONSTANT) {
                    STACK(++stackPointer) = expression->arg[i];
                }
                else /*if (op == CUSTOM || op == CUSTOM_DERIV)*/ {
                    int function = (int) expression->arg[i];
                    float x = STACK(stackPointer);
                    float4 params = cSim.pTabulatedFunctionParams[function];
                    if (x < params.x || x > params.y)
                        STACK(stackPointer) = 0.0f;
                    else
                    {
100
101
102
                        x = (x-params.x)*params.z;
                        int index = floor(x);
                        index = min(index, (int) params.w);
103
104
105
106
107
108
109
110
111
                        float4 coeff;
                        if (function == 0)
                            coeff = tex1Dfetch(texRef0, index);
                        else if (function == 1)
                            coeff = tex1Dfetch(texRef1, index);
                        else if (function == 2)
                            coeff = tex1Dfetch(texRef2, index);
                        else
                            coeff = tex1Dfetch(texRef3, index);
112
113
                        float b = x-index;
                        float a = 1.0f-b;
114
                        if (op == CUSTOM)
115
                            STACK(stackPointer) = a*coeff.x+b*coeff.y+((a*a*a-a)*coeff.z+(b*b*b-b)*coeff.w)/(params.z*params.z);
116
                        else
117
                            STACK(stackPointer) = (coeff.y-coeff.x)*params.z+((1.0f-3.0f*a*a)*coeff.z+(3.0f*b*b-1.0f)*coeff.w)/params.z;
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
                    }
                }
            }
        }
        else {
            if (op < SIN) {
                if (op == NEGATE) {
                    STACK(stackPointer) *= -1.0f;
                }
                else if (op == RECIPROCAL) {
                    STACK(stackPointer) = 1.0f/STACK(stackPointer);
                }
                else if (op == SQRT) {
                    STACK(stackPointer) = sqrt(STACK(stackPointer));
                }
                else if (op == EXP) {
                    STACK(stackPointer) = exp(STACK(stackPointer));
                }
                else if (op == LOG) {
                    STACK(stackPointer) = log(STACK(stackPointer));
                }
                else if (op == SQUARE) {
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) *= temp;
                }
143
                else if (op == CUBE) {
144
145
146
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) *= temp*temp;
                }
147
148
149
                else /*if (op == STEP)*/ {
                    STACK(stackPointer) = (STACK(stackPointer) >= 0.0f ? 1.0f : 0.0f);
                }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
            }
            else {
                if (op == SIN) {
                    STACK(stackPointer) = sin(STACK(stackPointer));
                }
                else if (op == COS) {
                    STACK(stackPointer) = cos(STACK(stackPointer));
                }
                else if (op == SEC) {
                    STACK(stackPointer) = 1.0f/cos(STACK(stackPointer));
                }
                else if (op == CSC) {
                    STACK(stackPointer) = 1.0f/sin(STACK(stackPointer));
                }
                else if (op == TAN) {
                    STACK(stackPointer) = tan(STACK(stackPointer));
                }
                else if (op == COT) {
                    STACK(stackPointer) = 1.0f/tan(STACK(stackPointer));
                }
                else if (op == ASIN) {
                    STACK(stackPointer) = asin(STACK(stackPointer));
                }
                else if (op == ACOS) {
                    STACK(stackPointer) = acos(STACK(stackPointer));
                }
                else if (op == ATAN) {
                    STACK(stackPointer) = atan(STACK(stackPointer));
                }
                else if (op == SINH) {
                    STACK(stackPointer) = sinh(STACK(stackPointer));
                }
                else if (op == COSH) {
                    STACK(stackPointer) = cosh(STACK(stackPointer));
                }
185
                else if (op == TANH) {
186
187
                    STACK(stackPointer) = tanh(STACK(stackPointer));
                }
188
189
190
                else if (op == ERF) {
                    STACK(stackPointer) = erf(STACK(stackPointer));
                }
191
                else if (op == ERFC) {
192
193
                    STACK(stackPointer) = erfc(STACK(stackPointer));
                }
194
195
196
197
198
199
200
201
202
203
204
                else if (op == MIN) {
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) = min(temp, STACK(--stackPointer));
                }
                else if (op == MAX) {
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) = max(temp, STACK(--stackPointer));
                }
                else /*if (op == ABS)*/ {
                    STACK(stackPointer) = fabs(STACK(stackPointer));
                }
205
206
207
208
209
            }
        }
    }
    return STACK(stackPointer);
}