/* -------------------------------------------------------------------------- * * 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-2012 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) { zkernel = createKernel(xsize, ysize, zsize); xkernel = createKernel(ysize, zsize, xsize); ykernel = createKernel(zsize, xsize, ysize); } void OpenCLFFT3D::execFFT(OpenCLArray& in, OpenCLArray& out, bool forward) { int maxSize = xkernel.getWorkGroupInfo(context.getDevice()); if (context.getDevice().getInfo() == CL_DEVICE_TYPE_CPU) maxSize = 1; zkernel.setArg(0, in.getDeviceBuffer()); zkernel.setArg(1, out.getDeviceBuffer()); zkernel.setArg(2, forward ? 1 : -1); context.executeKernel(zkernel, xsize*ysize*zsize, min(zsize, (int) maxSize)); xkernel.setArg(0, out.getDeviceBuffer()); xkernel.setArg(1, in.getDeviceBuffer()); xkernel.setArg(2, forward ? 1 : -1); context.executeKernel(xkernel, xsize*ysize*zsize, min(xsize, (int) maxSize)); ykernel.setArg(0, in.getDeviceBuffer()); ykernel.setArg(1, out.getDeviceBuffer()); ykernel.setArg(2, forward ? 1 : -1); context.executeKernel(ykernel, xsize*ysize*zsize, min(ysize, (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) { bool loopRequired = (context.getDevice().getInfo() == CL_DEVICE_TYPE_CPU); stringstream source; int stage = 0; int L = zsize; int m = 1; // Factor zsize, generating an appropriate block of code for each factor. while (L > 1) { int input = stage%2; int output = 1-input; source<<"{\n"; if (L%5 == 0) { L = L/5; source<<"// Pass "<<(stage+1)<<" (radix 5)\n"; if (loopRequired) source<<"for (int i = get_local_id(0); i < "<<(L*m)<<"; i += get_local_size(0)) {\n"; else { source<<"if (get_local_id(0) < "<<(L*m)<<") {\n"; source<<"int i = get_local_id(0);\n"; } source<<"int j = i/"< replacements; replacements["XSIZE"] = context.intToString(xsize); replacements["YSIZE"] = context.intToString(ysize); replacements["ZSIZE"] = context.intToString(zsize); replacements["M_PI"] = context.doubleToString(M_PI); replacements["COMPUTE_FFT"] = source.str(); replacements["LOOP_REQUIRED"] = (loopRequired ? "1" : "0"); cl::Program program = context.createProgram(context.replaceStrings(OpenCLKernelSources::fft, replacements)); cl::Kernel kernel(program, "execFFT"); int bufferSize = zsize*(context.getUseDoublePrecision() ? sizeof(mm_double2) : sizeof(mm_float2)); kernel.setArg(3, bufferSize, NULL); kernel.setArg(4, bufferSize, NULL); kernel.setArg(5, bufferSize, NULL); return kernel; }