kCalculateCustomExternalForces.cu 5.51 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
/* -------------------------------------------------------------------------- *
 *                                   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"

static __constant__ cudaGmxSimulation cSim;
40
41
42
43
static __constant__ Expression<256> forceExpX;
static __constant__ Expression<256> forceExpY;
static __constant__ Expression<256> forceExpZ;
static __constant__ Expression<256> energyExp;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include "kEvaluateExpression.h"

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

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

61
void SetCustomExternalForceExpressions(const Expression<256>& expressionX, const Expression<256>& expressionY, const Expression<256>& expressionZ)
62
63
64
65
66
67
68
69
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(forceExpX, &expressionX, sizeof(forceExpX));
    status = cudaMemcpyToSymbol(forceExpY, &expressionY, sizeof(forceExpY));
    status = cudaMemcpyToSymbol(forceExpZ, &expressionZ, sizeof(forceExpZ));
    RTERROR(status, "SetCustomExternalForceExpression: cudaMemcpyToSymbol failed");
}

70
void SetCustomExternalEnergyExpression(const Expression<256>& expression)
71
72
73
74
75
76
{
    cudaError_t status;
    status = cudaMemcpyToSymbol(energyExp, &expression, sizeof(energyExp));
    RTERROR(status, "SetCustomExternalEnergyExpression: cudaMemcpyToSymbol failed");
}

Peter Eastman's avatar
Peter Eastman committed
77
void SetCustomExternalGlobalParams(const vector<float>& paramValues)
78
79
{
    cudaError_t status;
Peter Eastman's avatar
Peter Eastman committed
80
    status = cudaMemcpyToSymbol(globalParams, &paramValues[0], paramValues.size()*sizeof(float));
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    RTERROR(status, "SetCustomExternalGlobalParams: cudaMemcpyToSymbol failed");
}


__global__ void kCalculateCustomExternalForces_kernel()
{
    extern __shared__ float stack[];
    float* variables = (float*) &stack[cSim.customExpressionStackSize*blockDim.x];
    unsigned int index = blockIdx.x * blockDim.x + threadIdx.x;
    float totalEnergy = 0.0f;

    while (index < cSim.customExternals)
    {
        int atom        = cSim.pCustomExternalID[index];
        float4 params   = cSim.pCustomExternalParams[index];
        float4 pos      = cSim.pPosq[atom];
        VARIABLE(0)     = pos.x;
        VARIABLE(1)     = pos.y;
        VARIABLE(2)     = pos.z;
        VARIABLE(3)     = params.x;
        VARIABLE(4)     = params.y;
        VARIABLE(5)     = params.z;
        VARIABLE(6)     = params.w;
        totalEnergy       += kEvaluateExpression_kernel(&energyExp, stack, variables);;
        float4 force       = cSim.pForce4[atom];
        force.x           -= kEvaluateExpression_kernel(&forceExpX, stack, variables);
        force.y           -= kEvaluateExpression_kernel(&forceExpY, stack, variables);
        force.z           -= kEvaluateExpression_kernel(&forceExpZ, stack, variables);
        cSim.pForce4[atom] = force;
        index             += blockDim.x * gridDim.x;
    }
    cSim.pEnergy[blockIdx.x * blockDim.x + threadIdx.x] += totalEnergy;
}

void kCalculateCustomExternalForces(gpuContext gpu)
{
//    printf("kCalculateCustomExternalForces\n");
    kCalculateCustomExternalForces_kernel<<<gpu->sim.blocks, gpu->sim.localForces_threads_per_block,
            gpu->sim.customExpressionStackSize*sizeof(float)*gpu->sim.localForces_threads_per_block>>>();
    LAUNCHERROR("kCalculateCustomExternalForces");
}