Install baseversion.h
[gromacs.git] / src / gromacs / gmxlib / cuda_tools / cudautils.cuh
blob3ab9588e381916bac0669112134ade339ded7899
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2014,2015, 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  */
36 #ifndef CUDAUTILS_CUH
37 #define CUDAUTILS_CUH
39 #include "config.h"
41 #include <stdio.h>
42 #ifdef HAVE_NVML
43 #include <nvml.h>
44 #endif /* HAVE_NVML */
46 #include "gromacs/utility/fatalerror.h"
48 /* CUDA library and hardware related defines */
49 /* TODO list some constants instead that can be used for consistency checks to
50    detect future devices with features that make the currect code incompatible
51    with them (e.g. expected warp size = 32, check against the dev_info->props.warpsize). */
52 #define WARP_SIZE           32
54 /* TODO error checking needs to be rewritten. We have 2 types of error checks needed
55    based on where they occur in the code:
56    - non performance-critical: these errors are unsafe to be ignored and must be
57      _always_ checked for, e.g. initializations
58    - performance critical: handling errors might hurt performance so care need to be taken
59      when/if we should check for them at all, e.g. in cu_upload_X. However, we should be
60      able to turn the check for these errors on!
62    Probably we'll need two sets of the macros below...
64  */
65 #define CHECK_CUDA_ERRORS
67 #ifdef CHECK_CUDA_ERRORS
69 /*! Check for CUDA error on the return status of a CUDA RT API call. */
70 #define CU_RET_ERR(status, msg) \
71     do { \
72         if (status != cudaSuccess) \
73         { \
74             gmx_fatal(FARGS, "%s: %s\n", msg, cudaGetErrorString(status)); \
75         } \
76     } while (0)
78 /*! Check for any previously occurred uncaught CUDA error. */
79 #define CU_CHECK_PREV_ERR() \
80     do { \
81         cudaError_t _CU_CHECK_PREV_ERR_status = cudaGetLastError(); \
82         if (_CU_CHECK_PREV_ERR_status != cudaSuccess) { \
83             gmx_warning("Just caught a previously occurred CUDA error (%s), will try to continue.", cudaGetErrorString(_CU_CHECK_PREV_ERR_status)); \
84         } \
85     } while (0)
87 /*! Check for any previously occurred uncaught CUDA error
88    -- aimed at use after kernel calls. */
89 #define CU_LAUNCH_ERR(msg) \
90     do { \
91         cudaError_t _CU_LAUNCH_ERR_status = cudaGetLastError(); \
92         if (_CU_LAUNCH_ERR_status != cudaSuccess) { \
93             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_LAUNCH_ERR_status)); \
94         } \
95     } while (0)
97 /*! Synchronize with GPU and check for any previously occurred uncaught CUDA error
98    -- aimed at use after kernel calls. */
99 #define CU_LAUNCH_ERR_SYNC(msg) \
100     do { \
101         cudaError_t _CU_SYNC_LAUNCH_ERR_status = cudaThreadSynchronize(); \
102         if (_CU_SYNC_LAUNCH_ERR_status != cudaSuccess) { \
103             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_SYNC_LAUNCH_ERR_status)); \
104         } \
105     } while (0)
107 #else /* CHECK_CUDA_ERRORS */
109 #define CU_RET_ERR(status, msg) do { } while (0)
110 #define CU_CHECK_PREV_ERR()     do { } while (0)
111 #define CU_LAUNCH_ERR(msg)      do { } while (0)
112 #define CU_LAUNCH_ERR_SYNC(msg) do { } while (0)
113 #define HANDLE_NVML_RET_ERR(status, msg) do { } while (0)
115 #endif /* CHECK_CUDA_ERRORS */
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
121 /*! CUDA device information. */
122 struct gmx_device_info_t
124     int                 id;                     /* id of the CUDA device */
125     cudaDeviceProp      prop;                   /* CUDA device properties */
126     int                 stat;                   /* result of the device check */
127     gmx_bool            nvml_initialized;       /* If NVML was initialized */
128     gmx_bool            nvml_ap_clocks_changed; /* If application clocks have been changed */
129 #ifdef HAVE_NVML
130     nvmlDevice_t        nvml_device_id;         /* NVML device id */
131     nvmlEnableState_t   nvml_is_restricted;     /* Status of application clocks permission */
132 #endif                                          /* HAVE_NVML */
136 /*! Launches asynchronous host to device memory copy in stream 0. */
137 int cu_copy_D2H(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
139 /*! Launches asynchronous host to device memory copy in stream s. */
140 int cu_copy_D2H_async(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
142 /*! Allocates host memory and launches synchronous host to device memory copy. */
143 int cu_copy_D2H_alloc(void ** /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
146 /*! Launches synchronous host to device memory copy. */
147 int cu_copy_H2D(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
149 /*! Launches asynchronous host to device memory copy in stream s. */
150 int cu_copy_H2D_async(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
152 /*! Allocates device memory and launches synchronous host to device memory copy. */
153 int cu_copy_H2D_alloc(void ** /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
155 /*! Frees device memory and resets the size and allocation size to -1. */
156 void cu_free_buffered(void *d_ptr, int *n = NULL, int *nalloc = NULL);
158 /*! Reallocates the device memory and copies data from the host. */
159 void cu_realloc_buffered(void **d_dest, void *h_src,
160                          size_t type_size,
161                          int *curr_size, int *curr_alloc_size,
162                          int req_size,
163                          cudaStream_t s,
164                          bool bAsync);
166 /*! Waits for event e to complete, */
167 int cu_wait_event(cudaEvent_t /*e*/);
169 /*! Calculates and returns the time elapsed between event start and end. */
170 float cu_event_elapsed(cudaEvent_t /*start*/, cudaEvent_t /*end*/);
172 /*! Waits for event end to complete and calculates the time between start and end. */
173 int cu_wait_event_time(cudaEvent_t /*end*/, cudaEvent_t /*begin*/, float * /*time*/);
175 #ifdef __cplusplus
177 #endif
179 #endif /* CUDAUTILS_CUH */