Update copyright statements and change license to LGPL
[alexxy/gromacs.git] / src / gmxlib / cuda_tools / cudautils.cuh
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-2012, The GROMACS development team,
6  * check out http://www.gromacs.org for more information.
7  * Copyright (c) 2012, by the GROMACS development team, led by
8  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9  * others, as listed in the AUTHORS file in the top-level source
10  * 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
39 #ifndef CUDAUTILS_CUH
40 #define CUDAUTILS_CUH
41
42 #include <stdio.h>
43
44 #include "gmx_fatal.h"
45
46 /* CUDA library and hardware related defines */
47 /* TODO list some constants instead that can be used for consistency checks to
48    detect future devices with features that make the currect code incompatible
49    with them (e.g. expected warp size = 32, check against the dev_info->props.warpsize). */
50 #define WARP_SIZE           32
51
52 /* TODO error checking needs to be rewritten. We have 2 types of error checks needed 
53    based on where they occur in the code: 
54    - non performance-critical: these errors are unsafe to be ignored and must be 
55      _always_ checked for, e.g. initializations
56    - performance critical: handling errors might hurt performance so care need to be taken
57      when/if we should check for them at all, e.g. in cu_upload_X. However, we should be 
58      able to turn the check for these errors on!
59
60   Probably we'll need two sets of the macros below... 
61  
62  */
63 #define CHECK_CUDA_ERRORS
64
65 #ifdef CHECK_CUDA_ERRORS
66
67 /*! Check for CUDA error on the return status of a CUDA RT API call. */
68 #define CU_RET_ERR(status, msg) \
69     do { \
70         if (status != cudaSuccess) \
71         { \
72             gmx_fatal(FARGS, "%s: %s\n", msg, cudaGetErrorString(status)); \
73         } \
74     } while (0)
75
76 /*! Check for any previously occurred uncaught CUDA error. */
77 #define CU_CHECK_PREV_ERR() \
78     do { \
79         cudaError_t _CU_CHECK_PREV_ERR_status = cudaGetLastError(); \
80         if (_CU_CHECK_PREV_ERR_status != cudaSuccess) { \
81             gmx_warning("Just caught a previously occurred CUDA error (%s), will try to continue.", cudaGetErrorString(_CU_CHECK_PREV_ERR_status)); \
82         } \
83     } while (0)
84
85 /*! Check for any previously occurred uncaught CUDA error 
86   -- aimed at use after kernel calls. */
87 #define CU_LAUNCH_ERR(msg) \
88     do { \
89         cudaError_t _CU_LAUNCH_ERR_status = cudaGetLastError(); \
90         if (_CU_LAUNCH_ERR_status != cudaSuccess) { \
91             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_LAUNCH_ERR_status)); \
92         } \
93     } while (0)
94
95 /*! Synchronize with GPU and check for any previously occurred uncaught CUDA error 
96   -- aimed at use after kernel calls. */
97 #define CU_LAUNCH_ERR_SYNC(msg) \
98     do { \
99         cudaError_t _CU_SYNC_LAUNCH_ERR_status = cudaThreadSynchronize(); \
100         if (_CU_SYNC_LAUNCH_ERR_status != cudaSuccess) { \
101             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_SYNC_LAUNCH_ERR_status)); \
102         } \
103     } while (0)
104
105 #else
106
107 #define CU_RET_ERR(status, msg) do { } while (0)
108 #define CU_CHECK_PREV_ERR()     do { } while (0)
109 #define CU_LAUNCH_ERR(msg)      do { } while (0)
110 #define CU_LAUNCH_ERR_SYNC(msg) do { } while (0)
111
112 #endif /* CHECK_CUDA_ERRORS */ 
113
114 #ifdef __cplusplus
115 extern "C" {
116 #endif
117
118 /*! CUDA device information. */
119 typedef struct cuda_dev_info cuda_dev_info_t;
120 struct cuda_dev_info
121 {
122     int             id;      /* id of the CUDA device */
123     cudaDeviceProp  prop;    /* CUDA device properties */
124     int             stat;    /* result of the device check */
125 };
126
127
128 /*! Launches asynchronous host to device memory copy in stream 0. */
129 int cu_copy_D2H(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
130
131 /*! Launches asynchronous host to device memory copy in stream s. */
132 int cu_copy_D2H_async(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
133
134 /*! Allocates host memory and launches synchronous host to device memory copy. */
135 int cu_copy_D2H_alloc(void ** /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
136
137
138 /*! Launches synchronous host to device memory copy. */
139 int cu_copy_H2D(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
140
141 /*! Launches asynchronous host to device memory copy in stream s. */
142 int cu_copy_H2D_async(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
143
144 /*! Allocates device memory and launches synchronous host to device memory copy. */
145 int cu_copy_H2D_alloc(void ** /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
146
147 /*! Frees device memory and resets the size and allocation size to -1. */
148 void cu_free_buffered(void *d_ptr, int *n = NULL, int *nalloc = NULL);
149
150 /*! Reallocates the device memory and copies data from the host. */
151 void cu_realloc_buffered(void **d_dest, void *h_src,
152                          size_t type_size,
153                          int *curr_size, int *curr_alloc_size,
154                          int req_size,
155                          cudaStream_t s,
156                          bool bAsync);
157
158 /*! Waits for event e to complete, */
159 int cu_wait_event(cudaEvent_t /*e*/);
160
161 /*! Calculates and returns the time elapsed between event start and end. */
162 float cu_event_elapsed(cudaEvent_t /*start*/, cudaEvent_t /*end*/);
163
164 /*! Waits for event end to complete and calculates the time between start and end. */
165 int cu_wait_event_time(cudaEvent_t /*end*/, cudaEvent_t /*begin*/, float * /*time*/);
166
167 #ifdef __cplusplus
168 }
169 #endif
170
171 #endif /* CUDAUTILS_CUH */