Make DeviceStream into a class
[alexxy/gromacs.git] / src / gromacs / ewald / pme_gpu_3dfft.cu
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \internal \file
37  *  \brief Implements CUDA FFT routines for PME GPU.
38  *
39  *  \author Aleksei Iupinov <a.yupinov@gmail.com>
40  *  \ingroup module_ewald
41  */
42
43 #include "gmxpre.h"
44
45 #include "pme_gpu_3dfft.h"
46
47 #include "gromacs/utility/fatalerror.h"
48 #include "gromacs/utility/gmxassert.h"
49
50 #include "pme.cuh"
51 #include "pme_gpu_types.h"
52 #include "pme_gpu_types_host.h"
53 #include "pme_gpu_types_host_impl.h"
54
55 static void handleCufftError(cufftResult_t status, const char* msg)
56 {
57     if (status != CUFFT_SUCCESS)
58     {
59         gmx_fatal(FARGS, "%s (error code %d)\n", msg, status);
60     }
61 }
62
63 GpuParallel3dFft::GpuParallel3dFft(const PmeGpu* pmeGpu)
64 {
65     const PmeGpuCudaKernelParams* kernelParamsPtr = pmeGpu->kernelParams.get();
66     ivec                          realGridSize, realGridSizePadded, complexGridSizePadded;
67     for (int i = 0; i < DIM; i++)
68     {
69         realGridSize[i]          = kernelParamsPtr->grid.realGridSize[i];
70         realGridSizePadded[i]    = kernelParamsPtr->grid.realGridSizePadded[i];
71         complexGridSizePadded[i] = kernelParamsPtr->grid.complexGridSizePadded[i];
72     }
73
74     GMX_RELEASE_ASSERT(!pme_gpu_settings(pmeGpu).useDecomposition,
75                        "FFT decomposition not implemented");
76
77     const int complexGridSizePaddedTotal =
78             complexGridSizePadded[XX] * complexGridSizePadded[YY] * complexGridSizePadded[ZZ];
79     const int realGridSizePaddedTotal =
80             realGridSizePadded[XX] * realGridSizePadded[YY] * realGridSizePadded[ZZ];
81
82     realGrid_ = (cufftReal*)kernelParamsPtr->grid.d_realGrid;
83     GMX_RELEASE_ASSERT(realGrid_, "Bad (null) input real-space grid");
84     complexGrid_ = (cufftComplex*)kernelParamsPtr->grid.d_fourierGrid;
85     GMX_RELEASE_ASSERT(complexGrid_, "Bad (null) input complex grid");
86
87     cufftResult_t result;
88     /* Commented code for a simple 3D grid with no padding */
89     /*
90        result = cufftPlan3d(&planR2C_, realGridSize[XX], realGridSize[YY], realGridSize[ZZ],
91        CUFFT_R2C); handleCufftError(result, "cufftPlan3d R2C plan failure");
92
93        result = cufftPlan3d(&planC2R_, realGridSize[XX], realGridSize[YY], realGridSize[ZZ],
94        CUFFT_C2R); handleCufftError(result, "cufftPlan3d C2R plan failure");
95      */
96
97     const int rank = 3, batch = 1;
98     result = cufftPlanMany(&planR2C_, rank, realGridSize, realGridSizePadded, 1, realGridSizePaddedTotal,
99                            complexGridSizePadded, 1, complexGridSizePaddedTotal, CUFFT_R2C, batch);
100     handleCufftError(result, "cufftPlanMany R2C plan failure");
101
102     result = cufftPlanMany(&planC2R_, rank, realGridSize, complexGridSizePadded, 1,
103                            complexGridSizePaddedTotal, realGridSizePadded, 1,
104                            realGridSizePaddedTotal, CUFFT_C2R, batch);
105     handleCufftError(result, "cufftPlanMany C2R plan failure");
106
107     cudaStream_t stream = pmeGpu->archSpecific->pmeStream_.stream();
108     GMX_RELEASE_ASSERT(stream, "Using the default CUDA stream for PME cuFFT");
109
110     result = cufftSetStream(planR2C_, stream);
111     handleCufftError(result, "cufftSetStream R2C failure");
112
113     result = cufftSetStream(planC2R_, stream);
114     handleCufftError(result, "cufftSetStream C2R failure");
115 }
116
117 GpuParallel3dFft::~GpuParallel3dFft()
118 {
119     cufftResult_t result;
120     result = cufftDestroy(planR2C_);
121     handleCufftError(result, "cufftDestroy R2C failure");
122     result = cufftDestroy(planC2R_);
123     handleCufftError(result, "cufftDestroy C2R failure");
124 }
125
126 void GpuParallel3dFft::perform3dFft(gmx_fft_direction dir, CommandEvent* /*timingEvent*/)
127 {
128     cufftResult_t result;
129     if (dir == GMX_FFT_REAL_TO_COMPLEX)
130     {
131         result = cufftExecR2C(planR2C_, realGrid_, complexGrid_);
132         handleCufftError(result, "cuFFT R2C execution failure");
133     }
134     else
135     {
136         result = cufftExecC2R(planC2R_, complexGrid_, realGrid_);
137         handleCufftError(result, "cuFFT C2R execution failure");
138     }
139 }