c5651b6b602432baeb1f31e798842e50c59bec29
[alexxy/gromacs.git] / src / gromacs / gpu_utils / gpu_utils.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2010, The GROMACS development team.
6  * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
7  * Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /*! \libinternal \file
39  *  \brief Declare functions for detection and initialization for GPU devices.
40  *
41  *  \author Szilard Pall <pall.szilard@gmail.com>
42  *  \author Mark Abraham <mark.j.abraham@gmail.com>
43  *
44  *  \inlibraryapi
45  */
46 #ifndef GMX_GPU_UTILS_GPU_UTILS_H
47 #define GMX_GPU_UTILS_GPU_UTILS_H
48
49 #include <cstdio>
50
51 #include <string>
52 #include <vector>
53
54 #include "gromacs/gpu_utils/gpu_macros.h"
55 #include "gromacs/utility/basedefinitions.h"
56
57 namespace gmx
58 {
59 class MDLogger;
60 }
61
62 //! Enum which is only used to describe transfer calls at the moment
63 enum class GpuApiCallBehavior : int
64 {
65     //! Synchronous
66     Sync,
67     //! Asynchronous
68     Async,
69     //! Size of the enumeration
70     Count
71 };
72
73 //! String corresponding to GPU API call behavior
74 const char* enumValueToString(GpuApiCallBehavior enumValue);
75
76 //! Types of actions associated to waiting or checking the completion of GPU tasks
77 enum class GpuTaskCompletion
78 {
79     Wait, /*<< Issue a blocking wait for the task */
80     Check /*<< Only check whether the task has completed */
81 };
82
83 /*! \brief Starts the GPU profiler if mdrun is being profiled.
84  *
85  *  When a profiler run is in progress (based on the presence of the NVPROF_ID
86  *  env. var.), the profiler is started to begin collecting data during the
87  *  rest of the run (or until stopGpuProfiler is called).
88  *
89  *  Note that this is implemented only for the CUDA API.
90  */
91 CUDA_FUNC_QUALIFIER
92 void startGpuProfiler() CUDA_FUNC_TERM;
93
94
95 /*! \brief Resets the GPU profiler if mdrun is being profiled.
96  *
97  * When a profiler run is in progress (based on the presence of the NVPROF_ID
98  * env. var.), the profiler data is restet in order to eliminate the data collected
99  * from the preceding part fo the run.
100  *
101  * This function should typically be called at the mdrun counter reset time.
102  *
103  * Note that this is implemented only for the CUDA API.
104  */
105 CUDA_FUNC_QUALIFIER
106 void resetGpuProfiler() CUDA_FUNC_TERM;
107
108
109 /*! \brief Stops the CUDA profiler if mdrun is being profiled.
110  *
111  *  This function can be called at cleanup when skipping recording
112  *  recording subsequent API calls from being traces/profiled is desired,
113  *  e.g. before uninitialization.
114  *
115  *  Note that this is implemented only for the CUDA API.
116  */
117 CUDA_FUNC_QUALIFIER
118 void stopGpuProfiler() CUDA_FUNC_TERM;
119
120 //! Tells whether the host buffer was pinned for non-blocking transfers. Only implemented for CUDA.
121 CUDA_FUNC_QUALIFIER
122 bool isHostMemoryPinned(const void* CUDA_FUNC_ARGUMENT(h_ptr)) CUDA_FUNC_TERM_WITH_RETURN(false);
123
124 /*! \brief Enable peer access between GPUs where supported
125  * \param[in] gpuIdsToUse   List of GPU IDs in use
126  * \param[in] mdlog         Logger object
127  */
128 CUDA_FUNC_QUALIFIER
129 void setupGpuDevicePeerAccess(const std::vector<int>& CUDA_FUNC_ARGUMENT(gpuIdsToUse),
130                               const gmx::MDLogger&    CUDA_FUNC_ARGUMENT(mdlog)) CUDA_FUNC_TERM;
131
132 /*! \brief Check the platform-defaults and environment variable to decide whether GPU timings
133  * should be enabled.
134  *
135  * Currently, timings are enabled for OpenCL, but disabled for CUDA and SYCL. This can be overridden
136  * by \c GMX_ENABLE_GPU_TIMING and \c GMX_DISABLE_GPU_TIMING environment variables.
137  */
138 bool decideGpuTimingsUsage();
139
140 #endif