Stopped using images in biophysics Docker repository
[gromacs.git] / src / gromacs / ewald / pme_gpu_3dfft_ocl.cpp
blob4ba6649497494df884b6d86868834edb6b527a37
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019,2020, 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.
36 /*! \internal \file
37 * \brief Implements OpenCL 3D FFT routines for PME GPU.
39 * \author Aleksei Iupinov <a.yupinov@gmail.com>
40 * \ingroup module_ewald
43 #include "gmxpre.h"
45 #include <array>
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, const int gridIndex)
68 // Extracting all the data from PME GPU
69 std::array<size_t, DIM> realGridSize, realGridSizePadded, complexGridSizePadded;
71 GMX_RELEASE_ASSERT(!pme_gpu_settings(pmeGpu).useDecomposition,
72 "FFT decomposition not implemented");
73 PmeGpuKernelParamsBase* kernelParamsPtr = pmeGpu->kernelParams.get();
74 for (int i = 0; i < DIM; i++)
76 realGridSize[i] = kernelParamsPtr->grid.realGridSize[i];
77 realGridSizePadded[i] = kernelParamsPtr->grid.realGridSizePadded[i];
78 complexGridSizePadded[i] = kernelParamsPtr->grid.complexGridSizePadded[i];
79 GMX_ASSERT(kernelParamsPtr->grid.complexGridSizePadded[i]
80 == kernelParamsPtr->grid.complexGridSize[i],
81 "Complex padding not implemented");
83 cl_context context = pmeGpu->archSpecific->deviceContext_.context();
84 deviceStreams_.push_back(pmeGpu->archSpecific->pmeStream_.stream());
85 realGrid_ = kernelParamsPtr->grid.d_realGrid[gridIndex];
86 complexGrid_ = kernelParamsPtr->grid.d_fourierGrid[gridIndex];
87 const bool performOutOfPlaceFFT = pmeGpu->archSpecific->performOutOfPlaceFFT;
90 // clFFT expects row-major, so dimensions/strides are reversed (ZYX instead of XYZ)
91 std::array<size_t, DIM> realGridDimensions = { realGridSize[ZZ], realGridSize[YY], realGridSize[XX] };
92 std::array<size_t, DIM> realGridStrides = { 1, realGridSizePadded[ZZ],
93 realGridSizePadded[YY] * realGridSizePadded[ZZ] };
94 std::array<size_t, DIM> complexGridStrides = { 1, complexGridSizePadded[ZZ],
95 complexGridSizePadded[YY] * complexGridSizePadded[ZZ] };
97 constexpr clfftDim dims = CLFFT_3D;
98 handleClfftError(clfftCreateDefaultPlan(&planR2C_, context, dims, realGridDimensions.data()),
99 "clFFT planning failure");
100 handleClfftError(clfftSetResultLocation(planR2C_, performOutOfPlaceFFT ? CLFFT_OUTOFPLACE : CLFFT_INPLACE),
101 "clFFT planning failure");
102 handleClfftError(clfftSetPlanPrecision(planR2C_, CLFFT_SINGLE), "clFFT planning failure");
103 constexpr cl_float scale = 1.0;
104 handleClfftError(clfftSetPlanScale(planR2C_, CLFFT_FORWARD, scale),
105 "clFFT coefficient setup failure");
106 handleClfftError(clfftSetPlanScale(planR2C_, CLFFT_BACKWARD, scale),
107 "clFFT coefficient setup failure");
109 // The only difference between 2 plans is direction
110 handleClfftError(clfftCopyPlan(&planC2R_, context, planR2C_), "clFFT plan copying failure");
112 handleClfftError(clfftSetLayout(planR2C_, CLFFT_REAL, CLFFT_HERMITIAN_INTERLEAVED),
113 "clFFT R2C layout failure");
114 handleClfftError(clfftSetLayout(planC2R_, CLFFT_HERMITIAN_INTERLEAVED, CLFFT_REAL),
115 "clFFT C2R layout failure");
117 handleClfftError(clfftSetPlanInStride(planR2C_, dims, realGridStrides.data()),
118 "clFFT stride setting failure");
119 handleClfftError(clfftSetPlanOutStride(planR2C_, dims, complexGridStrides.data()),
120 "clFFT stride setting failure");
122 handleClfftError(clfftSetPlanInStride(planC2R_, dims, complexGridStrides.data()),
123 "clFFT stride setting failure");
124 handleClfftError(clfftSetPlanOutStride(planC2R_, dims, realGridStrides.data()),
125 "clFFT stride setting failure");
127 handleClfftError(clfftBakePlan(planR2C_, deviceStreams_.size(), deviceStreams_.data(), nullptr, nullptr),
128 "clFFT precompiling failure");
129 handleClfftError(clfftBakePlan(planC2R_, deviceStreams_.size(), deviceStreams_.data(), nullptr, nullptr),
130 "clFFT precompiling failure");
132 // TODO: implement solve kernel as R2C FFT callback
133 // TODO: disable last transpose (clfftSetPlanTransposeResult)
136 GpuParallel3dFft::~GpuParallel3dFft()
138 clfftDestroyPlan(&planR2C_);
139 clfftDestroyPlan(&planC2R_);
142 void GpuParallel3dFft::perform3dFft(gmx_fft_direction dir, CommandEvent* timingEvent)
144 cl_mem tempBuffer = nullptr;
145 constexpr std::array<cl_event, 0> waitEvents{ {} };
147 clfftPlanHandle plan;
148 clfftDirection direction;
149 cl_mem * inputGrids, *outputGrids;
151 switch (dir)
153 case GMX_FFT_REAL_TO_COMPLEX:
154 plan = planR2C_;
155 direction = CLFFT_FORWARD;
156 inputGrids = &realGrid_;
157 outputGrids = &complexGrid_;
158 break;
159 case GMX_FFT_COMPLEX_TO_REAL:
160 plan = planC2R_;
161 direction = CLFFT_BACKWARD;
162 inputGrids = &complexGrid_;
163 outputGrids = &realGrid_;
164 break;
165 default:
166 GMX_THROW(
167 gmx::NotImplementedError("The chosen 3D-FFT case is not implemented on GPUs"));
169 handleClfftError(clfftEnqueueTransform(plan, direction, deviceStreams_.size(),
170 deviceStreams_.data(), waitEvents.size(), waitEvents.data(),
171 timingEvent, inputGrids, outputGrids, tempBuffer),
172 "clFFT execution failure");