OpenCLRpmdKernels.cpp 21.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
/* -------------------------------------------------------------------------- *
 *                                   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) 2011 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "OpenCLRpmdKernels.h"
#include "OpenCLRpmdKernelSources.h"
#include "openmm/internal/ContextImpl.h"
#include "OpenCLIntegrationUtilities.h"
#include "OpenCLExpressionUtilities.h"
#include "OpenCLFFT3D.h"
Peter Eastman's avatar
Peter Eastman committed
38
#include "OpenCLNonbondedUtilities.h"
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "../src/SimTKUtilities/SimTKOpenMMRealType.h"

using namespace OpenMM;
using namespace std;

OpenCLIntegrateRPMDStepKernel::~OpenCLIntegrateRPMDStepKernel() {
    if (forces != NULL)
        delete forces;
    if (positions != NULL)
        delete positions;
    if (velocities != NULL)
        delete velocities;
}
void OpenCLIntegrateRPMDStepKernel::initialize(const System& system, const RPMDIntegrator& integrator) {
    cl.getPlatformData().initializeContexts(system);
    numCopies = integrator.getNumCopies();
    numParticles = system.getNumParticles();
    workgroupSize = numCopies;
Peter Eastman's avatar
Peter Eastman committed
57
    while (workgroupSize <= 128-numCopies)
58
59
60
61
        workgroupSize += numCopies;
    if (numCopies != OpenCLFFT3D::findLegalDimension(numCopies))
        throw OpenMMException("RPMDIntegrator: the number of copies must be a multiple of powers of 2, 3, and 5.");
    int paddedParticles = cl.getPaddedNumAtoms();
62
63
64
    forces = OpenCLArray::create<mm_float4>(cl, numCopies*paddedParticles, "rpmdForces");
    positions = OpenCLArray::create<mm_float4>(cl, numCopies*paddedParticles, "rpmdPositions");
    velocities = OpenCLArray::create<mm_float4>(cl, numCopies*paddedParticles, "rpmdVelocities");
65
66
67
68
69
70
71
    cl.getIntegrationUtilities().initRandomNumberGenerator((unsigned int) integrator.getRandomNumberSeed());
    
    // Fill in the posq and velm arrays with safe values to avoid a risk of nans.
    
    vector<mm_float4> temp(positions->getSize());
    for (int i = 0; i < positions->getSize(); i++)
        temp[i] = mm_float4(0, 0, 0, 0);
Peter Eastman's avatar
Peter Eastman committed
72
    positions->upload(temp);
73
74
75
76
77
78
79
    for (int i = 0; i < velocities->getSize(); i++)
        temp[i] = mm_float4(0, 0, 0, 1);
    velocities->upload(temp);

    // Create kernels.
    
    map<string, string> defines;
80
81
82
83
84
85
    defines["NUM_ATOMS"] = cl.intToString(cl.getNumAtoms());
    defines["PADDED_NUM_ATOMS"] = cl.intToString(cl.getPaddedNumAtoms());
    defines["NUM_COPIES"] = cl.intToString(numCopies);
    defines["HBAR"] = cl.doubleToString(1.054571628e-34*AVOGADRO/(1000*1e-12));
    defines["SCALE"] = cl.doubleToString(1.0/sqrt((double) numCopies));
    defines["M_PI"] = cl.doubleToString(M_PI);
86
87
88
89
90
91
92
93
94
    map<string, string> replacements;
    replacements["FFT_Q_FORWARD"] = createFFT(numCopies, "q", true);
    replacements["FFT_Q_BACKWARD"] = createFFT(numCopies, "q", false);
    replacements["FFT_V_FORWARD"] = createFFT(numCopies, "v", true);
    replacements["FFT_V_BACKWARD"] = createFFT(numCopies, "v", false);
    cl::Program program = cl.createProgram(cl.replaceStrings(OpenCLRpmdKernelSources::rpmd, replacements), defines, "");
    pileKernel = cl::Kernel(program, "applyPileThermostat");
    stepKernel = cl::Kernel(program, "integrateStep");
    velocitiesKernel = cl::Kernel(program, "advanceVelocities");
95
96
    copyToContextKernel = cl::Kernel(program, "copyToContext");
    copyFromContextKernel = cl::Kernel(program, "copyFromContext");
Peter Eastman's avatar
Peter Eastman committed
97
    translateKernel = cl::Kernel(program, "applyCellTranslations");
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
}

void OpenCLIntegrateRPMDStepKernel::execute(ContextImpl& context, const RPMDIntegrator& integrator, bool forcesAreValid) {
    OpenCLIntegrationUtilities& integration = cl.getIntegrationUtilities();
    if (!hasInitializedKernel) {
        hasInitializedKernel = true;
        pileKernel.setArg<cl::Buffer>(0, velocities->getDeviceBuffer());
        pileKernel.setArg(1, 2*workgroupSize*sizeof(mm_float4), NULL);
        pileKernel.setArg(2, 2*workgroupSize*sizeof(mm_float4), NULL);
        pileKernel.setArg(3, numCopies*sizeof(mm_float2), NULL);
        stepKernel.setArg<cl::Buffer>(0, positions->getDeviceBuffer());
        stepKernel.setArg<cl::Buffer>(1, velocities->getDeviceBuffer());
        stepKernel.setArg<cl::Buffer>(2, forces->getDeviceBuffer());
        stepKernel.setArg(3, 2*workgroupSize*sizeof(mm_float4), NULL);
        stepKernel.setArg(4, 2*workgroupSize*sizeof(mm_float4), NULL);
        stepKernel.setArg(5, 2*workgroupSize*sizeof(mm_float4), NULL);
        stepKernel.setArg(6, numCopies*sizeof(mm_float2), NULL);
        velocitiesKernel.setArg<cl::Buffer>(0, velocities->getDeviceBuffer());
        velocitiesKernel.setArg<cl::Buffer>(1, forces->getDeviceBuffer());
Peter Eastman's avatar
Peter Eastman committed
117
118
        translateKernel.setArg<cl::Buffer>(0, positions->getDeviceBuffer());
        translateKernel.setArg<cl::Buffer>(1, cl.getPosq().getDeviceBuffer());
119
        translateKernel.setArg<cl::Buffer>(2, cl.getAtomIndexArray().getDeviceBuffer());
120
121
122
123
    }
    
    // Loop over copies and compute the force on each one.
    
124
125
    copyToContextKernel.setArg<cl::Buffer>(0, positions->getDeviceBuffer());
    copyToContextKernel.setArg<cl::Buffer>(1, cl.getPosq().getDeviceBuffer());
126
    copyToContextKernel.setArg<cl::Buffer>(2, cl.getAtomIndexArray().getDeviceBuffer());
127
128
    copyFromContextKernel.setArg<cl::Buffer>(0, cl.getForce().getDeviceBuffer());
    copyFromContextKernel.setArg<cl::Buffer>(1, forces->getDeviceBuffer());
129
    copyFromContextKernel.setArg<cl::Buffer>(2, cl.getAtomIndexArray().getDeviceBuffer());
Peter Eastman's avatar
Peter Eastman committed
130
131
    if (!forcesAreValid)
        computeForces(context);
132
133
134
135
    
    // Apply the PILE-L thermostat.
    
    const double dt = integrator.getStepSize();
Peter Eastman's avatar
Peter Eastman committed
136
    pileKernel.setArg<cl_uint>(5, integration.prepareRandomNumbers(numParticles*numCopies));
Peter Eastman's avatar
Bug fix  
Peter Eastman committed
137
    pileKernel.setArg<cl::Buffer>(4, integration.getRandom().getDeviceBuffer()); // Do this *after* prepareRandomNumbers(), which might rebuild the array.
138
139
140
    pileKernel.setArg<cl_float>(6, (cl_float) dt);
    pileKernel.setArg<cl_float>(7, (cl_float) (integrator.getTemperature()*BOLTZ));
    pileKernel.setArg<cl_float>(8, (cl_float) integrator.getFriction());
141
142
143
144
    cl.executeKernel(pileKernel, numParticles*numCopies, workgroupSize);

    // Update positions and velocities.
    
145
146
    stepKernel.setArg<cl_float>(7, (cl_float) dt);
    stepKernel.setArg<cl_float>(8, (cl_float) (integrator.getTemperature()*BOLTZ));
147
148
149
150
    cl.executeKernel(stepKernel, numParticles*numCopies, workgroupSize);

    // Calculate forces based on the updated positions.
    
Peter Eastman's avatar
Peter Eastman committed
151
    computeForces(context);
152
153
    
    // Update velocities.
154
    velocitiesKernel.setArg<cl_float>(2, (cl_float) dt);
155
156
157
158
    cl.executeKernel(velocitiesKernel, numParticles*numCopies, workgroupSize);

    // Apply the PILE-L thermostat again.

Peter Eastman's avatar
Peter Eastman committed
159
    pileKernel.setArg<cl_uint>(5, integration.prepareRandomNumbers(numParticles*numCopies));
160
161
162
163
164
165
166
167
    cl.executeKernel(pileKernel, numParticles*numCopies, workgroupSize);

    // Update the time and step count.

    cl.setTime(cl.getTime()+dt);
    cl.setStepCount(cl.getStepCount()+1);
}

Peter Eastman's avatar
Peter Eastman committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
void OpenCLIntegrateRPMDStepKernel::computeForces(ContextImpl& context) {
    for (int i = 0; i < numCopies; i++) {
        copyToContextKernel.setArg<cl_int>(3, i);
        cl.executeKernel(copyToContextKernel, cl.getNumAtoms());
        context.calcForcesAndEnergy(true, false);
        copyFromContextKernel.setArg<cl_int>(3, i);
        cl.executeKernel(copyFromContextKernel, cl.getNumAtoms());
        if (cl.getAtomsWereReordered() && cl.getNonbondedUtilities().getUsePeriodic()) {
            // Atoms may have been translated into a different periodic box, so apply
            // the same translation to all the beads.
            
            translateKernel.setArg<cl_int>(3, i);
            cl.executeKernel(translateKernel, cl.getNumAtoms());
        }
    }
}

185
186
187
188
double OpenCLIntegrateRPMDStepKernel::computeKineticEnergy(ContextImpl& context, const RPMDIntegrator& integrator) {
    return cl.getIntegrationUtilities().computeKineticEnergy(0);
}

189
190
191
192
193
void OpenCLIntegrateRPMDStepKernel::setPositions(int copy, const vector<Vec3>& pos) {
    if (positions == NULL)
        throw OpenMMException("RPMDIntegrator: Cannot set positions before the integrator is added to a Context");
    if (pos.size() != numParticles)
        throw OpenMMException("RPMDIntegrator: wrong number of values passed to setPositions()");
194
195
    vector<mm_float4> posq(cl.getPaddedNumAtoms());
    cl.getPosq().download(posq);
196
    for (int i = 0; i < numParticles; i++)
197
        posq[i] = mm_float4((cl_float) pos[i][0], (cl_float) pos[i][1], (cl_float) pos[i][2], posq[i].w);
198
199
200
201
202
203
204
205
    cl.getQueue().enqueueWriteBuffer(positions->getDeviceBuffer(), CL_TRUE, copy*cl.getPaddedNumAtoms()*sizeof(mm_float4), numParticles*sizeof(mm_float4), &posq[0]);
}

void OpenCLIntegrateRPMDStepKernel::setVelocities(int copy, const vector<Vec3>& vel) {
    if (velocities == NULL)
        throw OpenMMException("RPMDIntegrator: Cannot set velocities before the integrator is added to a Context");
    if (vel.size() != numParticles)
        throw OpenMMException("RPMDIntegrator: wrong number of values passed to setVelocities()");
206
207
    vector<mm_float4> velm(cl.getPaddedNumAtoms());
    cl.getVelm().download(velm);
208
    for (int i = 0; i < numParticles; i++)
209
        velm[i] = mm_float4((cl_float) vel[i][0], (cl_float) vel[i][1], (cl_float) vel[i][2], velm[i].w);
210
211
212
    cl.getQueue().enqueueWriteBuffer(velocities->getDeviceBuffer(), CL_TRUE, copy*cl.getPaddedNumAtoms()*sizeof(mm_float4), numParticles*sizeof(mm_float4), &velm[0]);
}

213
214
215
void OpenCLIntegrateRPMDStepKernel::copyToContext(int copy, ContextImpl& context) {
    copyToContextKernel.setArg<cl::Buffer>(0, positions->getDeviceBuffer());
    copyToContextKernel.setArg<cl::Buffer>(1, cl.getPosq().getDeviceBuffer());
216
    copyToContextKernel.setArg<cl::Buffer>(2, cl.getAtomIndexArray().getDeviceBuffer());
217
218
219
220
221
    copyToContextKernel.setArg<cl_int>(3, copy);
    cl.executeKernel(copyToContextKernel, cl.getNumAtoms());
    copyToContextKernel.setArg<cl::Buffer>(0, velocities->getDeviceBuffer());
    copyToContextKernel.setArg<cl::Buffer>(1, cl.getVelm().getDeviceBuffer());
    cl.executeKernel(copyToContextKernel, cl.getNumAtoms());
222
223
224
225
226
227
228
229
230
}

string OpenCLIntegrateRPMDStepKernel::createFFT(int size, const string& variable, bool forward) {
    stringstream source;
    int unfactored = size;
    int stage = 0;
    int L = size;
    int m = 1;
    string sign = (forward ? "1.0f" : "-1.0f");
Peter Eastman's avatar
Peter Eastman committed
231
232
    string multReal = (forward ? "multiplyComplexRealPart" : "multiplyComplexRealPartConj");
    string multImag = (forward ? "multiplyComplexImagPart" : "multiplyComplexImagPartConj");
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

    source<<"{\n";
    source<<"__local float4* real0 = "<<variable<<"real;\n";
    source<<"__local float4* imag0 = "<<variable<<"imag;\n";
    source<<"__local float4* real1 = &temp[blockStart];\n";
    source<<"__local float4* imag1 = &temp[blockStart+get_local_size(0)];\n";

    // Factor size, generating an appropriate block of code for each factor.

    while (unfactored > 1) {
        int input = stage%2;
        int output = 1-input;
        source<<"{\n";
        if (unfactored%5 == 0) {
            L = L/5;
            source<<"// Pass "<<(stage+1)<<" (radix 5)\n";
            source<<"if (indexInBlock < "<<(L*m)<<") {\n";
            source<<"int i = indexInBlock;\n";
            source<<"int j = i/"<<m<<";\n";
            source<<"float4 c0r = real"<<input<<"[i];\n";
            source<<"float4 c0i = imag"<<input<<"[i];\n";
            source<<"float4 c1r = real"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c1i = imag"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c2r = real"<<input<<"[i+"<<(2*L*m)<<"];\n";
            source<<"float4 c2i = imag"<<input<<"[i+"<<(2*L*m)<<"];\n";
            source<<"float4 c3r = real"<<input<<"[i+"<<(3*L*m)<<"];\n";
            source<<"float4 c3i = imag"<<input<<"[i+"<<(3*L*m)<<"];\n";
            source<<"float4 c4r = real"<<input<<"[i+"<<(4*L*m)<<"];\n";
            source<<"float4 c4i = imag"<<input<<"[i+"<<(4*L*m)<<"];\n";
            source<<"float4 d0r = c1r+c4r;\n";
            source<<"float4 d0i = c1i+c4i;\n";
            source<<"float4 d1r = c2r+c3r;\n";
            source<<"float4 d1i = c2i+c3i;\n";
266
267
268
269
            source<<"float4 d2r = "<<cl.doubleToString(sin(0.4*M_PI))<<"*(c1r-c4r);\n";
            source<<"float4 d2i = "<<cl.doubleToString(sin(0.4*M_PI))<<"*(c1i-c4i);\n";
            source<<"float4 d3r = "<<cl.doubleToString(sin(0.4*M_PI))<<"*(c2r-c3r);\n";
            source<<"float4 d3i = "<<cl.doubleToString(sin(0.4*M_PI))<<"*(c2i-c3i);\n";
270
271
            source<<"float4 d4r = d0r+d1r;\n";
            source<<"float4 d4i = d0i+d1i;\n";
272
273
            source<<"float4 d5r = "<<cl.doubleToString(0.25*sqrt(5.0))<<"*(d0r-d1r);\n";
            source<<"float4 d5i = "<<cl.doubleToString(0.25*sqrt(5.0))<<"*(d0i-d1i);\n";
274
275
276
277
278
279
            source<<"float4 d6r = c0r-0.25f*d4r;\n";
            source<<"float4 d6i = c0i-0.25f*d4i;\n";
            source<<"float4 d7r = d6r+d5r;\n";
            source<<"float4 d7i = d6i+d5i;\n";
            source<<"float4 d8r = d6r-d5r;\n";
            source<<"float4 d8i = d6i-d5i;\n";
280
            string coeff = cl.doubleToString(sin(0.2*M_PI)/sin(0.4*M_PI));
281
282
283
284
285
286
            source<<"float4 d9r = "<<sign<<"*(d2i+"<<coeff<<"*d3i);\n";
            source<<"float4 d9i = "<<sign<<"*(-d2r-"<<coeff<<"*d3r);\n";
            source<<"float4 d10r = "<<sign<<"*("<<coeff<<"*d2i-d3i);\n";
            source<<"float4 d10i = "<<sign<<"*(d3r-"<<coeff<<"*d2r);\n";
            source<<"real"<<output<<"[i+4*j*"<<m<<"] = c0r+d4r;\n";
            source<<"imag"<<output<<"[i+4*j*"<<m<<"] = c0i+d4i;\n";
Peter Eastman's avatar
Peter Eastman committed
287
288
289
290
291
292
293
294
            source<<"real"<<output<<"[i+(4*j+1)*"<<m<<"] = "<<multReal<<"(w[j*"<<size<<"/"<<(5*L)<<"], d7r+d9r, d7i+d9i);\n";
            source<<"imag"<<output<<"[i+(4*j+1)*"<<m<<"] = "<<multImag<<"(w[j*"<<size<<"/"<<(5*L)<<"], d7r+d9r, d7i+d9i);\n";
            source<<"real"<<output<<"[i+(4*j+2)*"<<m<<"] = "<<multReal<<"(w[j*"<<(2*size)<<"/"<<(5*L)<<"], d8r+d10r, d8i+d10i);\n";
            source<<"imag"<<output<<"[i+(4*j+2)*"<<m<<"] = "<<multImag<<"(w[j*"<<(2*size)<<"/"<<(5*L)<<"], d8r+d10r, d8i+d10i);\n";
            source<<"real"<<output<<"[i+(4*j+3)*"<<m<<"] = "<<multReal<<"(w[j*"<<(3*size)<<"/"<<(5*L)<<"], d8r-d10r, d8i-d10i);\n";
            source<<"imag"<<output<<"[i+(4*j+3)*"<<m<<"] = "<<multImag<<"(w[j*"<<(3*size)<<"/"<<(5*L)<<"], d8r-d10r, d8i-d10i);\n";
            source<<"real"<<output<<"[i+(4*j+4)*"<<m<<"] = "<<multReal<<"(w[j*"<<(4*size)<<"/"<<(5*L)<<"], d7r-d9r, d7i-d9i);\n";
            source<<"imag"<<output<<"[i+(4*j+4)*"<<m<<"] = "<<multImag<<"(w[j*"<<(4*size)<<"/"<<(5*L)<<"], d7r-d9r, d7i-d9i);\n";
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
            source<<"}\n";
            m = m*5;
            unfactored /= 5;
        }
        else if (unfactored%4 == 0) {
            L = L/4;
            source<<"// Pass "<<(stage+1)<<" (radix 4)\n";
            source<<"if (indexInBlock < "<<(L*m)<<") {\n";
            source<<"int i = indexInBlock;\n";
            source<<"int j = i/"<<m<<";\n";
            source<<"float4 c0r = real"<<input<<"[i];\n";
            source<<"float4 c0i = imag"<<input<<"[i];\n";
            source<<"float4 c1r = real"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c1i = imag"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c2r = real"<<input<<"[i+"<<(2*L*m)<<"];\n";
            source<<"float4 c2i = imag"<<input<<"[i+"<<(2*L*m)<<"];\n";
            source<<"float4 c3r = real"<<input<<"[i+"<<(3*L*m)<<"];\n";
            source<<"float4 c3i = imag"<<input<<"[i+"<<(3*L*m)<<"];\n";
            source<<"float4 d0r = c0r+c2r;\n";
            source<<"float4 d0i = c0i+c2i;\n";
            source<<"float4 d1r = c0r-c2r;\n";
            source<<"float4 d1i = c0i-c2i;\n";
            source<<"float4 d2r = c1r+c3r;\n";
            source<<"float4 d2i = c1i+c3i;\n";
            source<<"float4 d3r = "<<sign<<"*(c1i-c3i);\n";
            source<<"float4 d3i = "<<sign<<"*(c3r-c1r);\n";
            source<<"real"<<output<<"[i+3*j*"<<m<<"] = d0r+d2r;\n";
            source<<"imag"<<output<<"[i+3*j*"<<m<<"] = d0i+d2i;\n";
Peter Eastman's avatar
Peter Eastman committed
323
324
325
326
327
328
            source<<"real"<<output<<"[i+(3*j+1)*"<<m<<"] = "<<multReal<<"(w[j*"<<size<<"/"<<(4*L)<<"], d1r+d3r, d1i+d3i);\n";
            source<<"imag"<<output<<"[i+(3*j+1)*"<<m<<"] = "<<multImag<<"(w[j*"<<size<<"/"<<(4*L)<<"], d1r+d3r, d1i+d3i);\n";
            source<<"real"<<output<<"[i+(3*j+2)*"<<m<<"] = "<<multReal<<"(w[j*"<<(2*size)<<"/"<<(4*L)<<"], d0r-d2r, d0i-d2i);\n";
            source<<"imag"<<output<<"[i+(3*j+2)*"<<m<<"] = "<<multImag<<"(w[j*"<<(2*size)<<"/"<<(4*L)<<"], d0r-d2r, d0i-d2i);\n";
            source<<"real"<<output<<"[i+(3*j+3)*"<<m<<"] = "<<multReal<<"(w[j*"<<(3*size)<<"/"<<(4*L)<<"], d1r-d3r, d1i-d3i);\n";
            source<<"imag"<<output<<"[i+(3*j+3)*"<<m<<"] = "<<multImag<<"(w[j*"<<(3*size)<<"/"<<(4*L)<<"], d1r-d3r, d1i-d3i);\n";
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
            source<<"}\n";
            m = m*4;
            unfactored /= 4;
        }
        else if (unfactored%3 == 0) {
            L = L/3;
            source<<"// Pass "<<(stage+1)<<" (radix 3)\n";
            source<<"if (indexInBlock < "<<(L*m)<<") {\n";
            source<<"int i = indexInBlock;\n";
            source<<"int j = i/"<<m<<";\n";
            source<<"float4 c0r = real"<<input<<"[i];\n";
            source<<"float4 c0i = imag"<<input<<"[i];\n";
            source<<"float4 c1r = real"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c1i = imag"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c2r = real"<<input<<"[i+"<<(2*L*m)<<"];\n";
            source<<"float4 c2i = imag"<<input<<"[i+"<<(2*L*m)<<"];\n";
            source<<"float4 d0r = c1r+c2r;\n";
            source<<"float4 d0i = c1i+c2i;\n";
            source<<"float4 d1r = c0r-0.5f*d0r;\n";
            source<<"float4 d1i = c0i-0.5f*d0i;\n";
349
350
            source<<"float4 d2r = "<<sign<<"*"<<cl.doubleToString(sin(M_PI/3.0))<<"*(c1i-c2i);\n";
            source<<"float4 d2i = "<<sign<<"*"<<cl.doubleToString(sin(M_PI/3.0))<<"*(c2r-c1r);\n";
351
352
            source<<"real"<<output<<"[i+2*j*"<<m<<"] = c0r+d0r;\n";
            source<<"imag"<<output<<"[i+2*j*"<<m<<"] = c0i+d0i;\n";
Peter Eastman's avatar
Peter Eastman committed
353
354
355
356
            source<<"real"<<output<<"[i+(2*j+1)*"<<m<<"] = "<<multReal<<"(w[j*"<<size<<"/"<<(3*L)<<"], d1r+d2r, d1i+d2i);\n";
            source<<"imag"<<output<<"[i+(2*j+1)*"<<m<<"] = "<<multImag<<"(w[j*"<<size<<"/"<<(3*L)<<"], d1r+d2r, d1i+d2i);\n";
            source<<"real"<<output<<"[i+(2*j+2)*"<<m<<"] = "<<multReal<<"(w[j*"<<(2*size)<<"/"<<(3*L)<<"], d1r-d2r, d1i-d2i);\n";
            source<<"imag"<<output<<"[i+(2*j+2)*"<<m<<"] = "<<multImag<<"(w[j*"<<(2*size)<<"/"<<(3*L)<<"], d1r-d2r, d1i-d2i);\n";
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
            source<<"}\n";
            m = m*3;
            unfactored /= 3;
        }
        else if (unfactored%2 == 0) {
            L = L/2;
            source<<"// Pass "<<(stage+1)<<" (radix 2)\n";
            source<<"if (indexInBlock < "<<(L*m)<<") {\n";
            source<<"int i = indexInBlock;\n";
            source<<"int j = i/"<<m<<";\n";
            source<<"float4 c0r = real"<<input<<"[i];\n";
            source<<"float4 c0i = imag"<<input<<"[i];\n";
            source<<"float4 c1r = real"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"float4 c1i = imag"<<input<<"[i+"<<(L*m)<<"];\n";
            source<<"real"<<output<<"[i+j*"<<m<<"] = c0r+c1r;\n";
            source<<"imag"<<output<<"[i+j*"<<m<<"] = c0i+c1i;\n";
Peter Eastman's avatar
Peter Eastman committed
373
374
            source<<"real"<<output<<"[i+(j+1)*"<<m<<"] = "<<multReal<<"(w[j*"<<size<<"/"<<(2*L)<<"], c0r-c1r, c0i-c1i);\n";
            source<<"imag"<<output<<"[i+(j+1)*"<<m<<"] = "<<multImag<<"(w[j*"<<size<<"/"<<(2*L)<<"], c0r-c1r, c0i-c1i);\n";
375
376
377
378
379
            source<<"}\n";
            m = m*2;
            unfactored /= 2;
        }
        else
380
            throw OpenMMException("Illegal size for FFT: "+cl.intToString(size));
381
382
383
384
385
386
387
388
        source<<"barrier(CLK_LOCAL_MEM_FENCE);\n";
        source<<"}\n";
        ++stage;
    }

    // Create the kernel.

    if (stage%2 == 1) {
Peter Eastman's avatar
Peter Eastman committed
389
390
        source<<"real0[indexInBlock] = real1[indexInBlock];\n";
        source<<"imag0[indexInBlock] = imag1[indexInBlock];\n";
391
392
393
394
    }
    source<<"}\n";
    return source.str();
}