added Verlet scheme and NxN non-bonded functionality
[alexxy/gromacs.git] / src / gmxlib / cuda_tools / cudautils.cuh
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  *
4  *                This source code is part of
5  *
6  *                 G   R   O   M   A   C   S
7  *
8  *          GROningen MAchine for Chemical Simulations
9  *
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2012, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
34  */
35
36 #ifndef CUDAUTILS_CUH
37 #define CUDAUTILS_CUH
38
39 #include <stdio.h>
40
41 #include "gmx_fatal.h"
42
43 /* CUDA library and hardware related defines */
44 /* TODO list some constants instead that can be used for consistency checks to
45    detect future devices with features that make the currect code incompatible
46    with them (e.g. expected warp size = 32, check against the dev_info->props.warpsize). */
47 #define WARP_SIZE           32
48
49 /* TODO error checking needs to be rewritten. We have 2 types of error checks needed 
50    based on where they occur in the code: 
51    - non performance-critical: these errors are unsafe to be ignored and must be 
52      _always_ checked for, e.g. initializations
53    - performance critical: handling errors might hurt performance so care need to be taken
54      when/if we should check for them at all, e.g. in cu_upload_X. However, we should be 
55      able to turn the check for these errors on!
56
57   Probably we'll need two sets of the macros below... 
58  
59  */
60 #define CHECK_CUDA_ERRORS
61
62 #ifdef CHECK_CUDA_ERRORS
63
64 /*! Check for CUDA error on the return status of a CUDA RT API call. */
65 #define CU_RET_ERR(status, msg) \
66     do { \
67         if (status != cudaSuccess) \
68         { \
69             gmx_fatal(FARGS, "%s: %s\n", msg, cudaGetErrorString(status)); \
70         } \
71     } while (0)
72
73 /*! Check for any previously occurred uncaught CUDA error. */
74 #define CU_CHECK_PREV_ERR() \
75     do { \
76         cudaError_t _CU_CHECK_PREV_ERR_status = cudaGetLastError(); \
77         if (_CU_CHECK_PREV_ERR_status != cudaSuccess) { \
78             gmx_warning("Just caught a previously occurred CUDA error (%s), will try to continue.", cudaGetErrorString(_CU_CHECK_PREV_ERR_status)); \
79         } \
80     } while (0)
81
82 /*! Check for any previously occurred uncaught CUDA error 
83   -- aimed at use after kernel calls. */
84 #define CU_LAUNCH_ERR(msg) \
85     do { \
86         cudaError_t _CU_LAUNCH_ERR_status = cudaGetLastError(); \
87         if (_CU_LAUNCH_ERR_status != cudaSuccess) { \
88             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_LAUNCH_ERR_status)); \
89         } \
90     } while (0)
91
92 /*! Synchronize with GPU and check for any previously occurred uncaught CUDA error 
93   -- aimed at use after kernel calls. */
94 #define CU_LAUNCH_ERR_SYNC(msg) \
95     do { \
96         cudaError_t _CU_SYNC_LAUNCH_ERR_status = cudaThreadSynchronize(); \
97         if (_CU_SYNC_LAUNCH_ERR_status != cudaSuccess) { \
98             gmx_fatal(FARGS, "Error while launching kernel %s: %s\n", msg, cudaGetErrorString(_CU_SYNC_LAUNCH_ERR_status)); \
99         } \
100     } while (0)
101
102 #else
103
104 #define CU_RET_ERR(status, msg) do { } while (0)
105 #define CU_CHECK_PREV_ERR()     do { } while (0)
106 #define CU_LAUNCH_ERR(msg)      do { } while (0)
107 #define CU_LAUNCH_ERR_SYNC(msg) do { } while (0)
108
109 #endif /* CHECK_CUDA_ERRORS */ 
110
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114
115 /*! CUDA device information. */
116 typedef struct cuda_dev_info cuda_dev_info_t;
117 struct cuda_dev_info
118 {
119     int             id;      /* id of the CUDA device */
120     cudaDeviceProp  prop;    /* CUDA device properties */
121     int             stat;    /* result of the device check */
122 };
123
124
125 /*! Launches asynchronous host to device memory copy in stream 0. */
126 int cu_copy_D2H(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
127
128 /*! Launches asynchronous host to device memory copy in stream s. */
129 int cu_copy_D2H_async(void * /*h_dest*/, void * /*d_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
130
131 /*! Allocates host memory and launches synchronous host to device memory copy. */
132 int cu_copy_D2H_alloc(void ** /*h_dest*/, void * /*d_src*/, size_t /*bytes*/);
133
134
135 /*! Launches synchronous host to device memory copy. */
136 int cu_copy_H2D(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
137
138 /*! Launches asynchronous host to device memory copy in stream s. */
139 int cu_copy_H2D_async(void * /*d_dest*/, void * /*h_src*/, size_t /*bytes*/, cudaStream_t /*s = 0*/);
140
141 /*! Allocates device memory and launches synchronous host to device memory copy. */
142 int cu_copy_H2D_alloc(void ** /*d_dest*/, void * /*h_src*/, size_t /*bytes*/);
143
144 /*! Frees device memory and resets the size and allocation size to -1. */
145 void cu_free_buffered(void *d_ptr, int *n = NULL, int *nalloc = NULL);
146
147 /*! Reallocates the device memory and copies data from the host. */
148 void cu_realloc_buffered(void **d_dest, void *h_src,
149                          size_t type_size,
150                          int *curr_size, int *curr_alloc_size,
151                          int req_size,
152                          cudaStream_t s,
153                          bool bAsync);
154
155 /*! Waits for event e to complete, */
156 int cu_wait_event(cudaEvent_t /*e*/);
157
158 /*! Calculates and returns the time elapsed between event start and end. */
159 float cu_event_elapsed(cudaEvent_t /*start*/, cudaEvent_t /*end*/);
160
161 /*! Waits for event end to complete and calculates the time between start and end. */
162 int cu_wait_event_time(cudaEvent_t /*end*/, cudaEvent_t /*begin*/, float * /*time*/);
163
164 #ifdef __cplusplus
165 }
166 #endif
167
168 #endif /* CUDAUTILS_CUH */