Prune change-management.rst and update links.
[alexxy/gromacs.git] / src / gromacs / pbcutil / pbc_aiuc_cuda.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \libinternal \file
36  *
37  * \brief Basic routines to handle periodic boundary conditions with CUDA.
38  *
39  * This file contains GPU implementation of the PBC-aware vector evaluation.
40  *
41  * \todo CPU, GPU and SIMD routines essentially do the same operations on
42  *       different data-types. Currently this leads to code duplication,
43  *       which has to be resolved. For details, see Issue #2863
44  *       https://gitlab.com/gromacs/gromacs/-/issues/2863
45  *
46  * \author Mark Abraham <mark.j.abraham@gmail.com>
47  * \author Berk Hess <hess@kth.se>
48  * \author Artem Zhmurov <zhmurov@gmail.com>
49  *
50  * \inlibraryapi
51  * \ingroup module_pbcutil
52  */
53 #ifndef GMX_PBCUTIL_PBC_AIUC_CUDA_CUH
54 #define GMX_PBCUTIL_PBC_AIUC_CUDA_CUH
55
56 #include "gromacs/gpu_utils/vectype_ops.cuh"
57 #include "gromacs/pbcutil/pbc_aiuc.h"
58
59 /*! \brief Computes the vector between two points taking PBC into account.
60  *
61  * Computes the vector dr between points r2 and r1, taking into account the
62  * periodic boundary conditions, described in pbcAiuc object. Note that this
63  * routine always does the PBC arithmetic for all directions, multiplying the
64  * displacements by zeroes if the corresponding direction is not periodic.
65  * For triclinic boxes only distances up to half the smallest box diagonal
66  * element are guaranteed to be the shortest. This means that distances from
67  * 0.5/sqrt(2) times a box vector length (e.g. for a rhombic dodecahedron)
68  * can use a more distant periodic image.
69  *
70  * \todo This routine uses CUDA float4 types for input coordinates and
71  *       returns in rvec data-type. Other than that, it does essentially
72  *       the same thing as the version below, as well as SIMD and CPU
73  *       versions. This routine is used in gpubonded module.
74  *       To avoid code duplication, these implementations should be
75  *       unified. See Issue #2863:
76  *       https://gitlab.com/gromacs/gromacs/-/issues/2863
77  *
78  * \param[in]  pbcAiuc  PBC object.
79  * \param[in]  r1       Coordinates of the first point.
80  * \param[in]  r2       Coordinates of the second point.
81  * \param[out] dr       Resulting distance.
82  */
83 template<bool returnShift>
84 static __forceinline__ __device__ int
85                        pbcDxAiuc(const PbcAiuc& pbcAiuc, const float4 r1, const float4 r2, float3& dr)
86 {
87     dr.x = r1.x - r2.x;
88     dr.y = r1.y - r2.y;
89     dr.z = r1.z - r2.z;
90
91     float shz = rintf(dr.z * pbcAiuc.invBoxDiagZ);
92     dr.x -= shz * pbcAiuc.boxZX;
93     dr.y -= shz * pbcAiuc.boxZY;
94     dr.z -= shz * pbcAiuc.boxZZ;
95
96     float shy = rintf(dr.y * pbcAiuc.invBoxDiagY);
97     dr.x -= shy * pbcAiuc.boxYX;
98     dr.y -= shy * pbcAiuc.boxYY;
99
100     float shx = rintf(dr.x * pbcAiuc.invBoxDiagX);
101     dr.x -= shx * pbcAiuc.boxXX;
102
103     if (returnShift)
104     {
105         ivec ishift;
106
107         ishift[XX] = -__float2int_rn(shx);
108         ishift[YY] = -__float2int_rn(shy);
109         ishift[ZZ] = -__float2int_rn(shz);
110
111         return IVEC2IS(ishift);
112     }
113     else
114     {
115         return 0;
116     }
117 }
118
119 /*! \brief Computes the vector between two points taking PBC into account.
120  *
121  * Computes the vector dr between points r2 and r1, taking into account the
122  * periodic boundary conditions, described in pbcAiuc object. Same as above,
123  * only takes and returns data in float3 format. Does not return shifts.
124  *
125  * \todo This routine uses CUDA float3 types for both input and returns
126  *       values. Other than that, it does essentially the same thing as the
127  *       version above, as well as SIMD and CPU versions. This routine is
128  *       used in GPU-based constraints.
129  *       To avoid code duplication, these implementations should be
130  *       unified. See Issue #2863:
131  *       https://gitlab.com/gromacs/gromacs/-/issues/2863
132  *
133  * \param[in]  pbcAiuc  PBC object.
134  * \param[in]  r1       Coordinates of the first point.
135  * \param[in]  r2       Coordinates of the second point.
136  * \returns    dr       Resulting distance.
137  */
138 static __forceinline__ __host__ __device__ float3 pbcDxAiuc(const PbcAiuc& pbcAiuc,
139                                                             const float3&  r1,
140                                                             const float3&  r2)
141 {
142     float3 dr = r1 - r2;
143
144     float shz = rintf(dr.z * pbcAiuc.invBoxDiagZ);
145     dr.x -= shz * pbcAiuc.boxZX;
146     dr.y -= shz * pbcAiuc.boxZY;
147     dr.z -= shz * pbcAiuc.boxZZ;
148
149     float shy = rintf(dr.y * pbcAiuc.invBoxDiagY);
150     dr.x -= shy * pbcAiuc.boxYX;
151     dr.y -= shy * pbcAiuc.boxYY;
152
153     float shx = rintf(dr.x * pbcAiuc.invBoxDiagX);
154     dr.x -= shx * pbcAiuc.boxXX;
155
156     return dr;
157 }
158
159 #endif // GMX_PBCUTIL_PBC_AIUC_CUDA_CUH