b35fcabd4ab2d357378c33a65eb79a97e2692cca
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpu_utils.cu
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2010,2011,2012,2013,2014,2015,2016, The GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \file
37  *  \brief Define functions for detection and initialization for CUDA devices.
38  *
39  *  \author Szilard Pall <pall.szilard@gmail.com>
40  */
41
42 #include "gmxpre.h"
43
44 #include "gpu_utils.h"
45
46 #include <assert.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49
50 #include <cuda_profiler_api.h>
51
52 #include "gromacs/gpu_utils/cudautils.cuh"
53 #include "gromacs/gpu_utils/device_context.h"
54 #include "gromacs/gpu_utils/device_stream.h"
55 #include "gromacs/hardware/device_information.h"
56 #include "gromacs/hardware/device_management.h"
57 #include "gromacs/utility/basedefinitions.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/exceptions.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/logger.h"
63 #include "gromacs/utility/programcontext.h"
64 #include "gromacs/utility/smalloc.h"
65 #include "gromacs/utility/snprintf.h"
66 #include "gromacs/utility/stringutil.h"
67
68 static bool cudaProfilerRun = ((getenv("NVPROF_ID") != nullptr));
69
70 bool isHostMemoryPinned(const void* h_ptr)
71 {
72     cudaPointerAttributes memoryAttributes;
73     cudaError_t           stat = cudaPointerGetAttributes(&memoryAttributes, h_ptr);
74
75     bool isPinned = false;
76     switch (stat)
77     {
78         case cudaSuccess:
79             // In CUDA 11.0, the field called memoryType in
80             // cudaPointerAttributes was replaced by a field called
81             // type, along with a documented change of behavior when the
82             // pointer passed to cudaPointerGetAttributes is to
83             // non-registered host memory. That change means that this
84             // code needs conditional compilation and different
85             // execution paths to function with all supported versions.
86 #if CUDART_VERSION < 11 * 1000
87             isPinned = true;
88 #else
89             isPinned = (memoryAttributes.type == cudaMemoryTypeHost);
90 #endif
91             break;
92
93         case cudaErrorInvalidValue:
94             // If the buffer was not pinned, then it will not be recognized by CUDA at all
95             isPinned = false;
96             // Reset the last error status
97             cudaGetLastError();
98             break;
99
100         default: CU_RET_ERR(stat, "Unexpected CUDA error");
101     }
102     return isPinned;
103 }
104
105 void startGpuProfiler(void)
106 {
107     /* The NVPROF_ID environment variable is set by nvprof and indicates that
108        mdrun is executed in the CUDA profiler.
109        If nvprof was run is with "--profile-from-start off", the profiler will
110        be started here. This way we can avoid tracing the CUDA events from the
111        first part of the run. Starting the profiler again does nothing.
112      */
113     if (cudaProfilerRun)
114     {
115         cudaError_t stat;
116         stat = cudaProfilerStart();
117         CU_RET_ERR(stat, "cudaProfilerStart failed");
118     }
119 }
120
121 void stopGpuProfiler(void)
122 {
123     /* Stopping the nvidia here allows us to eliminate the subsequent
124        API calls from the trace, e.g. uninitialization and cleanup. */
125     if (cudaProfilerRun)
126     {
127         cudaError_t stat;
128         stat = cudaProfilerStop();
129         CU_RET_ERR(stat, "cudaProfilerStop failed");
130     }
131 }
132
133 void resetGpuProfiler(void)
134 {
135     /* With CUDA <=7.5 the profiler can't be properly reset; we can only start
136      *  the profiling here (can't stop it) which will achieve the desired effect if
137      *  the run was started with the profiling disabled.
138      *
139      * TODO: add a stop (or replace it with reset) when this will work correctly in CUDA.
140      * stopGpuProfiler();
141      */
142     if (cudaProfilerRun)
143     {
144         startGpuProfiler();
145     }
146 }
147
148 /*! \brief Check and act on status returned from peer access CUDA call
149  *
150  * If status is "cudaSuccess", we continue. If
151  * "cudaErrorPeerAccessAlreadyEnabled", then peer access has already
152  * been enabled so we ignore. If "cudaErrorInvalidDevice" then the
153  * run is trying to access an invalid GPU, so we throw an error. If
154  * "cudaErrorInvalidValue" then there is a problem with the arguments
155  * to the CUDA call, and we throw an error. These cover all expected
156  * statuses, but if any other is returned we issue a warning and
157  * continue.
158  *
159  * \param[in] stat           CUDA call return status
160  * \param[in] gpuA           ID for GPU initiating peer access call
161  * \param[in] gpuB           ID for remote GPU
162  * \param[in] mdlog          Logger object
163  * \param[in] cudaCallName   name of CUDA peer access call
164  */
165 static void peerAccessCheckStat(const cudaError_t    stat,
166                                 const int            gpuA,
167                                 const int            gpuB,
168                                 const gmx::MDLogger& mdlog,
169                                 const char*          cudaCallName)
170 {
171
172     if (stat == cudaErrorPeerAccessAlreadyEnabled)
173     {
174         // Since peer access has already been enabled, this error can safely be ignored.
175         // Now clear the error internally within CUDA:
176         cudaGetLastError();
177         return;
178     }
179     if ((stat == cudaErrorInvalidDevice) || (stat == cudaErrorInvalidValue))
180     {
181         std::string errorString =
182                 gmx::formatString("%s from GPU %d to GPU %d failed", cudaCallName, gpuA, gpuB);
183         CU_RET_ERR(stat, errorString.c_str());
184     }
185     if (stat != cudaSuccess)
186     {
187         GMX_LOG(mdlog.warning)
188                 .asParagraph()
189                 .appendTextFormatted(
190                         "GPU peer access not enabled between GPUs %d and %d due to unexpected "
191                         "return value from %s. %s",
192                         gpuA,
193                         gpuB,
194                         cudaCallName,
195                         gmx::getDeviceErrorString(stat).c_str());
196         // Clear the error internally within CUDA
197         cudaGetLastError();
198     }
199 }
200
201 void setupGpuDevicePeerAccess(const std::vector<int>& gpuIdsToUse, const gmx::MDLogger& mdlog)
202 {
203     cudaError_t stat;
204
205     // take a note of currently-set GPU
206     int currentGpu;
207     stat = cudaGetDevice(&currentGpu);
208     CU_RET_ERR(stat, "cudaGetDevice in setupGpuDevicePeerAccess failed");
209
210     std::string message = gmx::formatString(
211             "Note: Peer access enabled between the following GPU pairs in the node:\n ");
212     bool peerAccessEnabled = false;
213
214     for (unsigned int i = 0; i < gpuIdsToUse.size(); i++)
215     {
216         int gpuA = gpuIdsToUse[i];
217         stat     = cudaSetDevice(gpuA);
218         if (stat != cudaSuccess)
219         {
220             GMX_LOG(mdlog.warning)
221                     .asParagraph()
222                     .appendTextFormatted(
223                             "GPU peer access not enabled due to unexpected return value from "
224                             "cudaSetDevice(%d). %s",
225                             gpuA,
226                             gmx::getDeviceErrorString(stat).c_str());
227             return;
228         }
229         for (unsigned int j = 0; j < gpuIdsToUse.size(); j++)
230         {
231             if (j != i)
232             {
233                 int gpuB          = gpuIdsToUse[j];
234                 int canAccessPeer = 0;
235                 stat              = cudaDeviceCanAccessPeer(&canAccessPeer, gpuA, gpuB);
236                 peerAccessCheckStat(stat, gpuA, gpuB, mdlog, "cudaDeviceCanAccessPeer");
237
238                 if (canAccessPeer)
239                 {
240                     stat = cudaDeviceEnablePeerAccess(gpuB, 0);
241                     peerAccessCheckStat(stat, gpuA, gpuB, mdlog, "cudaDeviceEnablePeerAccess");
242
243                     message           = gmx::formatString("%s%d->%d ", message.c_str(), gpuA, gpuB);
244                     peerAccessEnabled = true;
245                 }
246             }
247         }
248     }
249
250     // re-set GPU to that originally set
251     stat = cudaSetDevice(currentGpu);
252     if (stat != cudaSuccess)
253     {
254         CU_RET_ERR(stat, "cudaSetDevice in setupGpuDevicePeerAccess failed");
255         return;
256     }
257
258     if (peerAccessEnabled)
259     {
260         GMX_LOG(mdlog.info).asParagraph().appendTextFormatted("%s", message.c_str());
261     }
262 }