kCalculateCustomNonbondedForces.cu 16.8 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
/* -------------------------------------------------------------------------- *
 *                                   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: Scott Le Grand, 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 <stdio.h>
#include <cuda.h>
#include <vector_functions.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

#include "gputypes.h"
#include "cudatypes.h"

#define UNROLLXX 0
#define UNROLLXY 0

struct Atom {
    float x;
    float y;
    float z;
    float4 params;
    float fx;
    float fy;
    float fz;
};

static __constant__ cudaGmxSimulation cSim;
static __constant__ Expression<128> forceExp;
static __constant__ Expression<128> energyExp;
static __constant__ Expression<64> combiningRules[4];
56
static __constant__ float globalParams[8];
57

58
59
60
61
62
texture<float4, 1, cudaReadModeElementType> texRef0;
texture<float4, 1, cudaReadModeElementType> texRef1;
texture<float4, 1, cudaReadModeElementType> texRef2;
texture<float4, 1, cudaReadModeElementType> texRef3;

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
void SetCalculateCustomNonbondedForcesSim(gpuContext gpu)
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(cSim, &gpu->sim, sizeof(cudaGmxSimulation));
    RTERROR(status, "cudaMemcpyToSymbol: SetSim copy to cSim failed");
}

void GetCalculateCustomNonbondedForcesSim(gpuContext gpu)
{
    cudaError_t status;
    status = cudaMemcpyFromSymbol(&gpu->sim, cSim, sizeof(cudaGmxSimulation));
    RTERROR(status, "cudaMemcpyFromSymbol: SetSim copy from cSim failed");
}

void SetCustomNonbondedForceExpression(const Expression<128>& expression)
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(forceExp, &expression, sizeof(forceExp));
    RTERROR(status, "SetCustomNonbondedForceExpression: cudaMemcpyToSymbol failed");
}

void SetCustomNonbondedEnergyExpression(const Expression<128>& expression)
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(energyExp, &expression, sizeof(energyExp));
    RTERROR(status, "SetCustomNonbondedEnergyExpression: cudaMemcpyToSymbol failed");
}

void SetCustomNonbondedCombiningRules(const Expression<64>* expressions)
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(combiningRules, expressions, sizeof(combiningRules));
    RTERROR(status, "SetCustomNonbondedCombiningRules: cudaMemcpyToSymbol failed");
}

98
void SetCustomNonbondedGlobalParams(float* paramValues)
99
100
{
    cudaError_t status;
101
    status = cudaMemcpyToSymbol(globalParams, paramValues, sizeof(globalParams));
102
103
104
    RTERROR(status, "SetCustomNonbondedGlobalParams: cudaMemcpyToSymbol failed");
}

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

108
template<int SIZE>
109
__device__ float kEvaluateExpression_kernel(Expression<SIZE>* expression, float* stack, float* variables)
110
111
112
113
{
    int stackPointer = -1;
    for (int i = 0; i < expression->length; i++)
    {
114
        int op = expression->op[i];
115
116
117
118
119
120
121
122
        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;
123
                }
124
125
126
127
128
129
130
131
132
133
134
135
                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);
                }
Peter Eastman's avatar
Peter Eastman committed
136
                else /*if (op == POWER)*/ {
137
138
139
140
141
142
143
144
145
146
147
                    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]);
                }
Peter Eastman's avatar
Peter Eastman committed
148
                else /*if (op == ADD_CONSTANT)*/ {
149
                    STACK(stackPointer) += expression->arg[i];
150
                }
151
            }
152
            else {
153
154
                if (op == GLOBAL) {
                    STACK(++stackPointer) = globalParams[(int) expression->arg[i]];
155
                }
156
157
158
                else if (op == CONSTANT) {
                    STACK(++stackPointer) = expression->arg[i];
                }
Peter Eastman's avatar
Peter Eastman committed
159
                else /*if (op == CUSTOM || op == CUSTOM_DERIV)*/ {
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                    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
                    {
                        int index = floor((x-params.x)*params.z);
                        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);
                        x = (x-params.x)*params.z-index;
                        if (op == CUSTOM)
                            STACK(stackPointer) = coeff.x+x*(coeff.y+x*(coeff.z+x*coeff.w));
                        else
                            STACK(stackPointer) = (coeff.y+x*(2.0f*coeff.z+x*3.0f*coeff.w))*params.z;
182
183
                    }
                }
184
            }
185
186
        }
        else {
187
188
189
            if (op < SIN) {
                if (op == NEGATE) {
                    STACK(stackPointer) *= -1.0f;
190
                }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                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;
                }
Peter Eastman's avatar
Peter Eastman committed
207
                else /*if (op == CUBE)*/ {
208
209
                    float temp = STACK(stackPointer);
                    STACK(stackPointer) *= temp*temp;
210
                }
211
            }
212
            else {
213
214
                if (op == SIN) {
                    STACK(stackPointer) = sin(STACK(stackPointer));
215
                }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
                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));
                }
Peter Eastman's avatar
Peter Eastman committed
237
                else /*if (op == ATAN)*/ {
238
                    STACK(stackPointer) = atan(STACK(stackPointer));
239
                }
240
241
242
            }
        }
    }
243
    return STACK(stackPointer);
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
}

// Include versions of the kernels for N^2 calculations.

#define METHOD_NAME(a, b) a##N2##b
#include "kCalculateCustomNonbondedForces.h"
#define USE_OUTPUT_BUFFER_PER_WARP
#undef METHOD_NAME
#define METHOD_NAME(a, b) a##N2ByWarp##b
#include "kCalculateCustomNonbondedForces.h"

// Include versions of the kernels with cutoffs.

#undef METHOD_NAME
#undef USE_OUTPUT_BUFFER_PER_WARP
#define USE_CUTOFF
#define METHOD_NAME(a, b) a##Cutoff##b
#include "kCalculateCustomNonbondedForces.h"
#define USE_OUTPUT_BUFFER_PER_WARP
#undef METHOD_NAME
#define METHOD_NAME(a, b) a##CutoffByWarp##b
#include "kCalculateCustomNonbondedForces.h"

// Include versions of the kernels with periodic boundary conditions.

#undef METHOD_NAME
#undef USE_OUTPUT_BUFFER_PER_WARP
#define USE_PERIODIC
#define METHOD_NAME(a, b) a##Periodic##b
#include "kCalculateCustomNonbondedForces.h"
#define USE_OUTPUT_BUFFER_PER_WARP
#undef METHOD_NAME
#define METHOD_NAME(a, b) a##PeriodicByWarp##b
#include "kCalculateCustomNonbondedForces.h"

__global__ void kFindBlockBoundsCutoff_kernel();
__global__ void kFindBlocksWithInteractionsCutoff_kernel();
__global__ void kFindInteractionsWithinBlocksCutoff_kernel(unsigned int* workUnit);
__global__ void kFindBlockBoundsPeriodic_kernel();
__global__ void kFindBlocksWithInteractionsPeriodic_kernel();
__global__ void kFindInteractionsWithinBlocksPeriodic_kernel(unsigned int* workUnit);

void kCalculateCustomNonbondedForces(gpuContext gpu, bool neighborListValid)
{
//    printf("kCalculateCustomNonbondedCutoffForces\n");
289
290
291
292
293
294
295
296
297
298
299
300
301
    if (gpu->tabulatedFunctionsChanged)
    {
        cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float4>();
        if (gpu->tabulatedFunctions[0].coefficients != NULL)
            cudaBindTexture(NULL, &texRef0, gpu->tabulatedFunctions[0].coefficients->_pDevData, &channelDesc, gpu->tabulatedFunctions[0].coefficients->_length*sizeof(float4));
        if (gpu->tabulatedFunctions[1].coefficients != NULL)
            cudaBindTexture(NULL, &texRef1, gpu->tabulatedFunctions[1].coefficients->_pDevData, &channelDesc, gpu->tabulatedFunctions[1].coefficients->_length*sizeof(float4));
        if (gpu->tabulatedFunctions[2].coefficients != NULL)
            cudaBindTexture(NULL, &texRef2, gpu->tabulatedFunctions[2].coefficients->_pDevData, &channelDesc, gpu->tabulatedFunctions[2].coefficients->_length*sizeof(float4));
        if (gpu->tabulatedFunctions[3].coefficients != NULL)
            cudaBindTexture(NULL, &texRef3, gpu->tabulatedFunctions[3].coefficients->_pDevData, &channelDesc, gpu->tabulatedFunctions[3].coefficients->_length*sizeof(float4));
        gpu->tabulatedFunctionsChanged = false;
    }
302
    int sharedPerThread = sizeof(Atom)+gpu->sim.customExpressionStackSize*sizeof(float)+8*sizeof(float);
303
304
305
306
307
308
    if (gpu->sim.customNonbondedMethod != NO_CUTOFF)
        sharedPerThread += sizeof(float3);
    int threads = gpu->sim.nonbond_threads_per_block;
    int maxThreads = 16380/sharedPerThread;
    if (threads > maxThreads)
        threads = (maxThreads/32)*32;
309
310
311
312
    switch (gpu->sim.customNonbondedMethod)
    {
        case NO_CUTOFF:
            if (gpu->bOutputBufferPerWarp)
313
                kCalculateCustomNonbondedN2ByWarpForces_kernel<<<gpu->sim.nonbond_blocks, threads, sharedPerThread*threads>>>(gpu->sim.pWorkUnit);
314
            else
315
                kCalculateCustomNonbondedN2Forces_kernel<<<gpu->sim.nonbond_blocks, threads, sharedPerThread*threads>>>(gpu->sim.pWorkUnit);
316
            LAUNCHERROR("kCalculateCustomNonbondedN2Forces");
317
318
319
            kCalculateCustomNonbondedN2Exceptions_kernel<<<gpu->sim.blocks, gpu->sim.custom_exception_threads_per_block,
                    gpu->sim.customExpressionStackSize*sizeof(float)*gpu->sim.custom_exception_threads_per_block>>>();
            LAUNCHERROR("kCalculateCustomNonbondedN2Exceptions");
320
321
322
323
324
325
326
327
            break;
        case CUTOFF:
            if (!neighborListValid)
            {
                kFindBlockBoundsCutoff_kernel<<<(gpu->psGridBoundingBox->_length+63)/64, 64>>>();
                LAUNCHERROR("kFindBlockBoundsCutoff");
                kFindBlocksWithInteractionsCutoff_kernel<<<gpu->sim.interaction_blocks, gpu->sim.interaction_threads_per_block>>>();
                LAUNCHERROR("kFindBlocksWithInteractionsCutoff");
328
                compactStream(gpu->compactPlan, gpu->sim.pInteractingWorkUnit, gpu->sim.pWorkUnit, gpu->sim.pInteractionFlag, gpu->sim.workUnits, gpu->sim.pInteractionCount);
329
330
331
332
                kFindInteractionsWithinBlocksCutoff_kernel<<<gpu->sim.nonbond_blocks, gpu->sim.nonbond_threads_per_block,
                        sizeof(unsigned int)*gpu->sim.nonbond_threads_per_block>>>(gpu->sim.pInteractingWorkUnit);
            }
            if (gpu->bOutputBufferPerWarp)
333
                kCalculateCustomNonbondedCutoffByWarpForces_kernel<<<gpu->sim.nonbond_blocks, threads, sharedPerThread*threads>>>(gpu->sim.pInteractingWorkUnit);
334
            else
335
                kCalculateCustomNonbondedCutoffForces_kernel<<<gpu->sim.nonbond_blocks, threads, sharedPerThread*threads>>>(gpu->sim.pInteractingWorkUnit);
336
            LAUNCHERROR("kCalculateCustomNonbondedCutoffForces");
337
338
339
            kCalculateCustomNonbondedCutoffExceptions_kernel<<<gpu->sim.blocks, gpu->sim.custom_exception_threads_per_block,
                    gpu->sim.customExpressionStackSize*sizeof(float)*gpu->sim.custom_exception_threads_per_block>>>();
            LAUNCHERROR("kCalculateCustomNonbondedCutoffExceptions");
340
341
342
343
344
345
346
347
            break;
        case PERIODIC:
            if (!neighborListValid)
            {
                kFindBlockBoundsPeriodic_kernel<<<(gpu->psGridBoundingBox->_length+63)/64, 64>>>();
                LAUNCHERROR("kFindBlockBoundsPeriodic");
                kFindBlocksWithInteractionsPeriodic_kernel<<<gpu->sim.interaction_blocks, gpu->sim.interaction_threads_per_block>>>();
                LAUNCHERROR("kFindBlocksWithInteractionsPeriodic");
348
                compactStream(gpu->compactPlan, gpu->sim.pInteractingWorkUnit, gpu->sim.pWorkUnit, gpu->sim.pInteractionFlag, gpu->sim.workUnits, gpu->sim.pInteractionCount);
349
350
351
352
                kFindInteractionsWithinBlocksPeriodic_kernel<<<gpu->sim.nonbond_blocks, gpu->sim.nonbond_threads_per_block,
                        sizeof(unsigned int)*gpu->sim.nonbond_threads_per_block>>>(gpu->sim.pInteractingWorkUnit);
            }
            if (gpu->bOutputBufferPerWarp)
353
                kCalculateCustomNonbondedPeriodicByWarpForces_kernel<<<gpu->sim.nonbond_blocks, threads, sharedPerThread*threads>>>(gpu->sim.pInteractingWorkUnit);
354
            else
355
                kCalculateCustomNonbondedPeriodicForces_kernel<<<gpu->sim.nonbond_blocks, threads, sharedPerThread*threads>>>(gpu->sim.pInteractingWorkUnit);
356
            LAUNCHERROR("kCalculateCustomNonbondedPeriodicForces");
357
            kCalculateCustomNonbondedPeriodicExceptions_kernel<<<gpu->sim.blocks, gpu->sim.custom_exception_threads_per_block,
358
                    (gpu->sim.customExpressionStackSize+8)*sizeof(float)*gpu->sim.custom_exception_threads_per_block>>>();
359
            LAUNCHERROR("kCalculateCustomNonbondedPeriodicExceptions");
360
361
362
            break;
    }
}