Merge branch release-5-1 into release-2016
[alexxy/gromacs.git] / src / gromacs / gpu_utils / cudautils.cuh
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2014,2015,2016, 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 #ifndef GMX_GPU_UTILS_CUDAUTILS_CUH
36 #define GMX_GPU_UTILS_CUDAUTILS_CUH
37
38 #include "config.h"
39
40 #include <stdio.h>
41 #if HAVE_NVML
42 #include <nvml.h>
43 #endif /* HAVE_NVML */
44
45 #include "gromacs/utility/fatalerror.h"
46
47 /* TODO error checking needs to be rewritten. We have 2 types of error checks needed
48    based on where they occur in the code:
49    - non performance-critical: these errors are unsafe to be ignored and must be
50      _always_ checked for, e.g. initializations
51    - performance critical: handling errors might hurt performance so care need to be taken
52      when/if we should check for them at all, e.g. in cu_upload_X. However, we should be
53      able to turn the check for these errors on!
54
55    Probably we'll need two sets of the macros below...
56
57  */
58 #define CHECK_CUDA_ERRORS
59
60 #ifdef CHECK_CUDA_ERRORS
61
62 /*! Check for CUDA error on the return status of a CUDA RT API call. */
63 #define CU_RET_ERR(status, msg) \
64     do { \
65         if (status != cudaSuccess) \
66         { \
67             gmx_fatal(FARGS, "%s: %s\n", msg, cudaGetErrorString(status)); \
68         } \
69     } while (0)
70
71 /*! Check for any previously occurred uncaught CUDA error. */
72 #define CU_CHECK_PREV_ERR() \
73     do { \
74         cudaError_t _CU_CHECK_PREV_ERR_status = cudaGetLastError(); \
75         if (_CU_CHECK_PREV_ERR_status != cudaSuccess) { \
76             gmx_warning("Just caught a previously occurred CUDA error (%s), will try to continue.", cudaGetErrorString(_CU_CHECK_PREV_ERR_status)); \
77         } \
78     } while (0)
79
80 /*! Check for any previously occurred uncaught CUDA error
81    -- aimed at use after kernel calls. */
82 #define CU_LAUNCH_ERR(msg) \
83     do { \
84         cudaError_t _CU_LAUNCH_ERR_status = cudaGetLastError(); \
85         if (_CU_LAUNCH_ERR_status != cudaSuccess) { \
86             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_LAUNCH_ERR_status)); \
87         } \
88     } while (0)
89
90 /*! Synchronize with GPU and check for any previously occurred uncaught CUDA error
91    -- aimed at use after kernel calls. */
92 #define CU_LAUNCH_ERR_SYNC(msg) \
93     do { \
94         cudaError_t _CU_SYNC_LAUNCH_ERR_status = cudaThreadSynchronize(); \
95         if (_CU_SYNC_LAUNCH_ERR_status != cudaSuccess) { \
96             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_SYNC_LAUNCH_ERR_status)); \
97         } \
98     } while (0)
99
100 #else /* CHECK_CUDA_ERRORS */
101
102 #define CU_RET_ERR(status, msg) do { } while (0)
103 #define CU_CHECK_PREV_ERR()     do { } while (0)
104 #define CU_LAUNCH_ERR(msg)      do { } while (0)
105 #define CU_LAUNCH_ERR_SYNC(msg) do { } while (0)
106 #define HANDLE_NVML_RET_ERR(status, msg) do { } while (0)
107
108 #endif /* CHECK_CUDA_ERRORS */
109
110 /*! \brief CUDA device information.
111  *
112  * The CUDA device information is queried and set at detection and contains
113  * both information about the device/hardware returned by the runtime as well
114  * as additional data like support status.
115  */
116 struct gmx_device_info_t
117 {
118     int                 id;                      /* id of the CUDA device */
119     cudaDeviceProp      prop;                    /* CUDA device properties */
120     int                 stat;                    /* result of the device check */
121     gmx_bool            nvml_initialized;        /* If NVML was initialized */
122     unsigned int        nvml_orig_app_sm_clock;  /* The original SM clock before we changed it */
123     unsigned int        nvml_orig_app_mem_clock; /* The original memory clock before we changed it */
124     gmx_bool            nvml_app_clocks_changed; /* If application clocks have been changed */
125     unsigned int        nvml_set_app_sm_clock;   /* The SM clock we set */
126     unsigned int        nvml_set_app_mem_clock;  /* The memory clock we set */
127 #if HAVE_NVML
128     nvmlDevice_t        nvml_device_id;          /* NVML device id */
129     nvmlEnableState_t   nvml_is_restricted;      /* Status of application clocks permission */
130 #endif                                           /* HAVE_NVML */
131 };
132
133
134 /*! Launches asynchronous host to device memory copy in stream 0. */
135 int cu_copy_D2H(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
136
137 /*! Launches asynchronous host to device memory copy in stream s. */
138 int cu_copy_D2H_async(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
139
140 /*! Allocates host memory and launches synchronous host to device memory copy. */
141 int cu_copy_D2H_alloc(void ** /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
142
143
144 /*! Launches synchronous host to device memory copy. */
145 int cu_copy_H2D(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
146
147 /*! Launches asynchronous host to device memory copy in stream s. */
148 int cu_copy_H2D_async(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
149
150 /*! Allocates device memory and launches synchronous host to device memory copy. */
151 int cu_copy_H2D_alloc(void ** /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
152
153 /*! Frees device memory and resets the size and allocation size to -1. */
154 void cu_free_buffered(void *d_ptr, int *n = NULL, int *nalloc = NULL);
155
156 /*! Reallocates the device memory and copies data from the host. */
157 void cu_realloc_buffered(void **d_dest, void *h_src,
158                          size_t type_size,
159                          int *curr_size, int *curr_alloc_size,
160                          int req_size,
161                          cudaStream_t s,
162                          bool bAsync);
163
164 /*! Waits for event e to complete, */
165 int cu_wait_event(cudaEvent_t /*e*/);
166
167 /*! Calculates and returns the time elapsed between event start and end. */
168 float cu_event_elapsed(cudaEvent_t /*start*/, cudaEvent_t /*end*/);
169
170 /*! Waits for event end to complete and calculates the time between start and end. */
171 int cu_wait_event_time(cudaEvent_t /*end*/, cudaEvent_t /*begin*/, float * /*time*/);
172
173 #endif