Apply clang-tidy-11 fixes to CUDA files
[alexxy/gromacs.git] / src / gromacs / gpu_utils / tests / typecasts_runner.cu
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,2021, 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 /*! \internal \file
36  * \brief
37  * Runners for tests of CUDA types compatibility.
38  *
39  * \author Artem Zhmurov <zhmurov@gmail.com>
40  */
41 #include "gmxpre.h"
42
43 #include "typecasts_runner.h"
44
45 #include <vector>
46
47 #include "gromacs/gpu_utils/cudautils.cuh"
48 #include "gromacs/gpu_utils/devicebuffer.h"
49 #include "gromacs/gpu_utils/typecasts.cuh"
50 #include "gromacs/hardware/device_information.h"
51 #include "gromacs/utility/arrayref.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/stringutil.h"
54
55 namespace gmx
56 {
57
58 namespace test
59 {
60
61 /* \brief Perform a component-wise conversion of the float3 vector back to RVec format.
62  *
63  * This is needed to pass the data back to the CPU testing code for comparison with the initial input.
64  *
65  * \param[out] rVecOutput    Output data in RVec format for the output.
66  * \param[in]  float3Output  Output data in float3 format.
67  * \param[in]  numElements   Size of the data buffers.
68  */
69 void inline saveFloat3InRVecFormat(ArrayRef<gmx::RVec> rVecOutput, const float3* float3Output, int numElements)
70 {
71     for (int i = 0; i < numElements; i++)
72     {
73         rVecOutput[i][XX] = float3Output[i].x;
74         rVecOutput[i][YY] = float3Output[i].y;
75         rVecOutput[i][ZZ] = float3Output[i].z;
76     }
77 }
78
79 void convertRVecToFloat3OnHost(ArrayRef<gmx::RVec> rVecOutput, ArrayRef<const gmx::RVec> rVecInput)
80 {
81     const int numElements = rVecInput.size();
82
83     float3* dataFloat3 = asFloat3(const_cast<RVec*>(rVecInput.data()));
84
85     saveFloat3InRVecFormat(rVecOutput, dataFloat3, numElements);
86 }
87
88 //! Number of CUDA threads in a block.
89 constexpr static int c_threadsPerBlock = 256;
90
91 /*! \brief GPU kernel to perform type conversion on the device.
92  *
93  * \param[out] gm_float3Output Buffer to write the output into.
94  * \param[in]  gm_rVecInput    Input data in RVec format.
95  * \param[in]  size            Size of the data buffers.
96  *
97  */
98 static __global__ void convertRVecToFloat3OnDevice_kernel(DeviceBuffer<float3> gm_float3Output,
99                                                           DeviceBuffer<RVec>   gm_rVecInput,
100                                                           const int            size)
101 {
102     int threadIndex = blockIdx.x * blockDim.x + threadIdx.x;
103     if (threadIndex < size)
104     {
105         gm_float3Output[threadIndex] = asFloat3(gm_rVecInput)[threadIndex];
106     }
107 }
108
109 void convertRVecToFloat3OnDevice(ArrayRef<gmx::RVec>       h_rVecOutput,
110                                  ArrayRef<const gmx::RVec> h_rVecInput,
111                                  const TestDevice*         testDevice)
112 {
113     const DeviceContext& deviceContext = testDevice->deviceContext();
114     const DeviceStream&  deviceStream  = testDevice->deviceStream();
115
116     setActiveDevice(testDevice->deviceInfo());
117
118     const int numElements = h_rVecInput.size();
119
120     DeviceBuffer<RVec> d_rVecInput;
121     allocateDeviceBuffer(&d_rVecInput, numElements, deviceContext);
122     copyToDeviceBuffer(
123             &d_rVecInput, h_rVecInput.data(), 0, numElements, deviceStream, GpuApiCallBehavior::Sync, nullptr);
124
125     DeviceBuffer<float3> d_float3Output;
126     allocateDeviceBuffer(&d_float3Output, numElements * DIM, deviceContext);
127
128     std::vector<float3> h_float3Output(numElements);
129
130     KernelLaunchConfig kernelLaunchConfig;
131     kernelLaunchConfig.gridSize[0]      = (numElements + c_threadsPerBlock - 1) / c_threadsPerBlock;
132     kernelLaunchConfig.blockSize[0]     = c_threadsPerBlock;
133     kernelLaunchConfig.blockSize[1]     = 1;
134     kernelLaunchConfig.blockSize[2]     = 1;
135     kernelLaunchConfig.sharedMemorySize = 0;
136
137     auto       kernelPtr  = convertRVecToFloat3OnDevice_kernel;
138     const auto kernelArgs = prepareGpuKernelArguments(
139             kernelPtr, kernelLaunchConfig, &d_float3Output, &d_rVecInput, &numElements);
140     launchGpuKernel(kernelPtr,
141                     kernelLaunchConfig,
142                     deviceStream,
143                     nullptr,
144                     "convertRVecToFloat3OnDevice_kernel",
145                     kernelArgs);
146
147     copyFromDeviceBuffer(
148             h_float3Output.data(), &d_float3Output, 0, numElements, deviceStream, GpuApiCallBehavior::Sync, nullptr);
149
150     saveFloat3InRVecFormat(h_rVecOutput, h_float3Output.data(), numElements);
151
152     freeDeviceBuffer(&d_rVecInput);
153     freeDeviceBuffer(&d_float3Output);
154 }
155
156 } // namespace test
157 } // namespace gmx