2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief Implements OpenCL 3D FFT routines for PME GPU.
39 * \author Aleksei Iupinov <a.yupinov@gmail.com>
40 * \ingroup module_ewald
47 #include "gromacs/utility/exceptions.h"
48 #include "gromacs/utility/gmxassert.h"
49 #include "gromacs/utility/stringutil.h"
51 #include "pme_gpu_3dfft.h"
52 #include "pme_gpu_internal.h"
53 #include "pme_gpu_types.h"
54 #include "pme_gpu_types_host_impl.h"
56 //! Throws the exception on clFFT error
57 static void handleClfftError(clfftStatus status, const char* msg)
59 // Supposedly it's just a superset of standard OpenCL errors
60 if (status != CLFFT_SUCCESS)
62 GMX_THROW(gmx::InternalError(gmx::formatString("%s: %d", msg, status)));
66 GpuParallel3dFft::GpuParallel3dFft(const PmeGpu* pmeGpu)
68 // Extracting all the data from PME GPU
69 std::array<size_t, DIM> realGridSize, realGridSizePadded, complexGridSizePadded;
71 GMX_RELEASE_ASSERT(!pme_gpu_uses_dd(pmeGpu), "FFT decomposition not implemented");
72 PmeGpuKernelParamsBase* kernelParamsPtr = pmeGpu->kernelParams.get();
73 for (int i = 0; i < DIM; i++)
75 realGridSize[i] = kernelParamsPtr->grid.realGridSize[i];
76 realGridSizePadded[i] = kernelParamsPtr->grid.realGridSizePadded[i];
77 complexGridSizePadded[i] = kernelParamsPtr->grid.complexGridSizePadded[i];
78 GMX_ASSERT(kernelParamsPtr->grid.complexGridSizePadded[i]
79 == kernelParamsPtr->grid.complexGridSize[i],
80 "Complex padding not implemented");
82 cl_context context = pmeGpu->archSpecific->context;
83 commandStreams_.push_back(pmeGpu->archSpecific->pmeStream);
84 realGrid_ = kernelParamsPtr->grid.d_realGrid;
85 complexGrid_ = kernelParamsPtr->grid.d_fourierGrid;
86 const bool performOutOfPlaceFFT = pmeGpu->archSpecific->performOutOfPlaceFFT;
89 // clFFT expects row-major, so dimensions/strides are reversed (ZYX instead of XYZ)
90 std::array<size_t, DIM> realGridDimensions = { realGridSize[ZZ], realGridSize[YY], realGridSize[XX] };
91 std::array<size_t, DIM> realGridStrides = { 1, realGridSizePadded[ZZ],
92 realGridSizePadded[YY] * realGridSizePadded[ZZ] };
93 std::array<size_t, DIM> complexGridStrides = { 1, complexGridSizePadded[ZZ],
94 complexGridSizePadded[YY] * complexGridSizePadded[ZZ] };
96 constexpr clfftDim dims = CLFFT_3D;
97 handleClfftError(clfftCreateDefaultPlan(&planR2C_, context, dims, realGridDimensions.data()),
98 "clFFT planning failure");
99 handleClfftError(clfftSetResultLocation(planR2C_, performOutOfPlaceFFT ? CLFFT_OUTOFPLACE : CLFFT_INPLACE),
100 "clFFT planning failure");
101 handleClfftError(clfftSetPlanPrecision(planR2C_, CLFFT_SINGLE), "clFFT planning failure");
102 constexpr cl_float scale = 1.0;
103 handleClfftError(clfftSetPlanScale(planR2C_, CLFFT_FORWARD, scale),
104 "clFFT coefficient setup failure");
105 handleClfftError(clfftSetPlanScale(planR2C_, CLFFT_BACKWARD, scale),
106 "clFFT coefficient setup failure");
108 // The only difference between 2 plans is direction
109 handleClfftError(clfftCopyPlan(&planC2R_, context, planR2C_), "clFFT plan copying failure");
111 handleClfftError(clfftSetLayout(planR2C_, CLFFT_REAL, CLFFT_HERMITIAN_INTERLEAVED),
112 "clFFT R2C layout failure");
113 handleClfftError(clfftSetLayout(planC2R_, CLFFT_HERMITIAN_INTERLEAVED, CLFFT_REAL),
114 "clFFT C2R layout failure");
116 handleClfftError(clfftSetPlanInStride(planR2C_, dims, realGridStrides.data()),
117 "clFFT stride setting failure");
118 handleClfftError(clfftSetPlanOutStride(planR2C_, dims, complexGridStrides.data()),
119 "clFFT stride setting failure");
121 handleClfftError(clfftSetPlanInStride(planC2R_, dims, complexGridStrides.data()),
122 "clFFT stride setting failure");
123 handleClfftError(clfftSetPlanOutStride(planC2R_, dims, realGridStrides.data()),
124 "clFFT stride setting failure");
126 handleClfftError(clfftBakePlan(planR2C_, commandStreams_.size(), commandStreams_.data(), nullptr, nullptr),
127 "clFFT precompiling failure");
128 handleClfftError(clfftBakePlan(planC2R_, commandStreams_.size(), commandStreams_.data(), nullptr, nullptr),
129 "clFFT precompiling failure");
131 // TODO: implement solve kernel as R2C FFT callback
132 // TODO: disable last transpose (clfftSetPlanTransposeResult)
135 GpuParallel3dFft::~GpuParallel3dFft()
137 clfftDestroyPlan(&planR2C_);
138 clfftDestroyPlan(&planC2R_);
141 void GpuParallel3dFft::perform3dFft(gmx_fft_direction dir, CommandEvent* timingEvent)
143 cl_mem tempBuffer = nullptr;
144 constexpr std::array<cl_event, 0> waitEvents{ {} };
146 clfftPlanHandle plan;
147 clfftDirection direction;
148 cl_mem * inputGrids, *outputGrids;
152 case GMX_FFT_REAL_TO_COMPLEX:
154 direction = CLFFT_FORWARD;
155 inputGrids = &realGrid_;
156 outputGrids = &complexGrid_;
158 case GMX_FFT_COMPLEX_TO_REAL:
160 direction = CLFFT_BACKWARD;
161 inputGrids = &complexGrid_;
162 outputGrids = &realGrid_;
166 gmx::NotImplementedError("The chosen 3D-FFT case is not implemented on GPUs"));
168 handleClfftError(clfftEnqueueTransform(plan, direction, commandStreams_.size(),
169 commandStreams_.data(), waitEvents.size(), waitEvents.data(),
170 timingEvent, inputGrids, outputGrids, tempBuffer),
171 "clFFT execution failure");