/* -------------------------------------------------------------------------- * * 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 . * * -------------------------------------------------------------------------- */ #include "OpenCLFFT3D.h" #include "OpenCLExpressionUtilities.h" #include "OpenCLKernelSources.h" #include "../src/SimTKUtilities/SimTKOpenMMRealType.h" #include #include #include using namespace OpenMM; using namespace std; OpenCLFFT3D::OpenCLFFT3D(OpenCLContext& context, int xsize, int ysize, int zsize) : context(context), xsize(xsize), ysize(ysize), zsize(zsize) { xkernel = createKernel(xsize, ysize, zsize, ysize*zsize, zsize, 1); ykernel = createKernel(ysize, zsize, xsize, zsize, 1, ysize*zsize); zkernel = createKernel(zsize, xsize, ysize, 1, ysize*zsize, zsize); } void OpenCLFFT3D::execFFT(OpenCLArray& data, bool forward) { int maxSize = xkernel.getWorkGroupInfo(context.getDevice()); if (context.getDevice().getInfo() == CL_DEVICE_TYPE_CPU) maxSize = 1; xkernel.setArg(0, data.getDeviceBuffer()); xkernel.setArg(1, forward ? 1.0f : -1.0f); context.executeKernel(xkernel, xsize*ysize*zsize, min(xsize, (int) maxSize)); ykernel.setArg(0, data.getDeviceBuffer()); ykernel.setArg(1, forward ? 1.0f : -1.0f); context.executeKernel(ykernel, xsize*ysize*zsize, min(ysize, (int) maxSize)); zkernel.setArg(0, data.getDeviceBuffer()); zkernel.setArg(1, forward ? 1.0f : -1.0f); context.executeKernel(zkernel, xsize*ysize*zsize, min(zsize, (int) maxSize)); } int OpenCLFFT3D::findLegalDimension(int minimum) { if (minimum < 1) return 1; while (true) { // Attempt to factor the current value. int unfactored = minimum; for (int factor = 2; factor < 6; factor++) { while (unfactored > 1 && unfactored%factor == 0) unfactored /= factor; } if (unfactored == 1) return minimum; minimum++; } } cl::Kernel OpenCLFFT3D::createKernel(int xsize, int ysize, int zsize, int xmult, int ymult, int zmult) { stringstream source; int unfactored = xsize; int stage = 0; int L = xsize; int m = 1; // Factor xsize, 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<<"for (int i = get_local_id(0); i < "<<(L*m)<<"; i += get_local_size(0)) {\n"; source<<"int j = i/"< replacements; replacements["XSIZE"] = OpenCLExpressionUtilities::intToString(xsize); replacements["YSIZE"] = OpenCLExpressionUtilities::intToString(ysize); replacements["ZSIZE"] = OpenCLExpressionUtilities::intToString(zsize); replacements["XMULT"] = OpenCLExpressionUtilities::intToString(xmult); replacements["YMULT"] = OpenCLExpressionUtilities::intToString(ymult); replacements["ZMULT"] = OpenCLExpressionUtilities::intToString(zmult); replacements["M_PI"] = OpenCLExpressionUtilities::doubleToString(M_PI); replacements["COMPUTE_FFT"] = source.str(); cl::Program program = context.createProgram(context.replaceStrings(OpenCLKernelSources::fft, replacements)); cl::Kernel kernel(program, "execFFT"); kernel.setArg(2, xsize*sizeof(mm_float2), NULL); kernel.setArg(3, xsize*sizeof(mm_float2), NULL); kernel.setArg(4, xsize*sizeof(mm_float2), NULL); return kernel; }