8.1
general documentation
cs_base_cuda.h
Go to the documentation of this file.
1 #ifndef __CS_BASE_CUDA_H__
2 #define __CS_BASE_CUDA_H__
3 
4 /*============================================================================
5  * Definitions, global variables, and base functions for CUDA
6  *============================================================================*/
7 
8 /*
9  This file is part of code_saturne, a general-purpose CFD tool.
10 
11  Copyright (C) 1998-2023 EDF S.A.
12 
13  This program is free software; you can redistribute it and/or modify it under
14  the terms of the GNU General Public License as published by the Free Software
15  Foundation; either version 2 of the License, or (at your option) any later
16  version.
17 
18  This program is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  details.
22 
23  You should have received a copy of the GNU General Public License along with
24  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25  Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27 
28 /*----------------------------------------------------------------------------*/
29 
30 #include "cs_defs.h"
31 
32 /*----------------------------------------------------------------------------
33  * Standard C library headers
34  *----------------------------------------------------------------------------*/
35 
36 #include <stdio.h>
37 
38 /*----------------------------------------------------------------------------
39  * Local headers
40  *----------------------------------------------------------------------------*/
41 
42 #include "cs_log.h"
43 
44 /*=============================================================================
45  * Macro definitions
46  *============================================================================*/
47 
48 #define CS_CUDA_CHECK(a) { \
49  cudaError_t ret_code = a; \
50  if (cudaSuccess != ret_code) { \
51  bft_error(__FILE__, __LINE__, 0, "[CUDA error] %d: %s\n running: %s", \
52  ret_code, ::cudaGetErrorString(ret_code), #a); \
53  } \
54  }
55 
56 #define CS_CUDA_CHECK_CALL(a, file_name, line_num) { \
57  cudaError_t ret_code = a; \
58  if (cudaSuccess != ret_code) { \
59  bft_error(file_name, line_num, 0, "[CUDA error] %d: %s\n running: %s", \
60  ret_code, ::cudaGetErrorString(ret_code), #a); \
61  } \
62  }
63 
64 /* For all current compute capabilities, the warp size is 32; If it ever
65  changes, it can be obtained through cudaDeviceProp, so we could then
66  replace this macro with a global variable */
67 
68 #define CS_CUDA_WARP_SIZE 32
69 
70 /*----------------------------------------------------------------------------*/
71 
73 
74 /*============================================================================
75  * Type definitions
76  *============================================================================*/
77 
78 /*=============================================================================
79  * Global variable definitions
80  *============================================================================*/
81 
82 extern int cs_glob_cuda_device_id;
83 
84 /* Other device parameters */
85 
88 extern int cs_glob_cuda_max_blocks;
89 extern int cs_glob_cuda_n_mp; /* Number of multiprocessors */
90 
91 /*============================================================================
92  * Semi-private function prototypes
93  *
94  * The following functions are intended to be used by the common
95  * host-device memory management functions from cs_base_accel.c, and
96  * not directly by the user.
97  *============================================================================*/
98 
99 #if defined(HAVE_CUDA)
100 
101 /*----------------------------------------------------------------------------*/
119 /*----------------------------------------------------------------------------*/
120 
121 void *
122 cs_cuda_mem_malloc_device(size_t n,
123  const char *var_name,
124  const char *file_name,
125  int line_num);
126 
127 /*----------------------------------------------------------------------------*/
145 /*----------------------------------------------------------------------------*/
146 
147 void *
148 cs_cuda_mem_malloc_host(size_t n,
149  const char *var_name,
150  const char *file_name,
151  int line_num);
152 
153 /*----------------------------------------------------------------------------*/
171 /*----------------------------------------------------------------------------*/
172 
173 void *
174 cs_cuda_mem_malloc_managed(size_t n,
175  const char *var_name,
176  const char *file_name,
177  int line_num);
178 
179 /*----------------------------------------------------------------------------*/
197 /*----------------------------------------------------------------------------*/
198 
199 void
200 cs_cuda_mem_free(void *p,
201  const char *var_name,
202  const char *file_name,
203  int line_num);
204 
205 /*----------------------------------------------------------------------------*/
223 /*----------------------------------------------------------------------------*/
224 
225 void
226 cs_cuda_mem_free_host(void *p,
227  const char *var_name,
228  const char *file_name,
229  int line_num);
230 
231 /*----------------------------------------------------------------------------*/
243 /*----------------------------------------------------------------------------*/
244 
245 void
246 cs_cuda_copy_h2d(void *dst,
247  const void *src,
248  size_t size);
249 
250 /*----------------------------------------------------------------------------*/
265 /*----------------------------------------------------------------------------*/
266 
267 void
268 cs_cuda_copy_h2d_async(void *dst,
269  const void *src,
270  size_t size);
271 
272 /*----------------------------------------------------------------------------*/
286 /*----------------------------------------------------------------------------*/
287 
288 void
289 cs_cuda_copy_d2h(void *dst,
290  const void *src,
291  size_t size);
292 
293 /*----------------------------------------------------------------------------*/
307 /*----------------------------------------------------------------------------*/
308 
309 void
310 cs_cuda_copy_d2h_async(void *dst,
311  const void *src,
312  size_t size);
313 
314 /*----------------------------------------------------------------------------*/
328 /*----------------------------------------------------------------------------*/
329 
330 void
331 cs_cuda_prefetch_h2d(void *dst,
332  size_t size);
333 
334 /*----------------------------------------------------------------------------*/
348 /*----------------------------------------------------------------------------*/
349 
350 void
351 cs_cuda_prefetch_d2h(void *dst,
352  size_t size);
353 
354 /*----------------------------------------------------------------------------*/
366 /*----------------------------------------------------------------------------*/
367 
368 void
369 cs_cuda_copy_d2d(void *dst,
370  const void *src,
371  size_t size);
372 
373 /*----------------------------------------------------------------------------*/
389 /*----------------------------------------------------------------------------*/
390 
391 void *
392 cs_cuda_get_host_ptr(const void *ptr);
393 
394 #endif
395 
396 /*=============================================================================
397  * Inline static function prototypes
398  *============================================================================*/
399 
400 /*----------------------------------------------------------------------------*/
413 /*----------------------------------------------------------------------------*/
414 
415 static inline unsigned int
417  unsigned int block_size)
418 {
419  return (n % block_size) ? n/block_size + 1 : n/block_size;
420 }
421 
422 /*=============================================================================
423  * Public function prototypes
424  *============================================================================*/
425 
426 #if defined(HAVE_CUDA)
427 
428 /*----------------------------------------------------------------------------*/
434 /*----------------------------------------------------------------------------*/
435 
436 void
437 cs_base_cuda_device_info(cs_log_t log_id);
438 
439 /*----------------------------------------------------------------------------*/
445 /*----------------------------------------------------------------------------*/
446 
447 void
448 cs_base_cuda_version_info(cs_log_t log_id);
449 
450 /*----------------------------------------------------------------------------*/
456 /*----------------------------------------------------------------------------*/
457 
458 void
459 cs_base_cuda_compiler_info(cs_log_t log_id);
460 
461 /*----------------------------------------------------------------------------*/
470 /*----------------------------------------------------------------------------*/
471 
472 int
473 cs_base_cuda_select_default_device(void);
474 
475 /*----------------------------------------------------------------------------*/
481 /*----------------------------------------------------------------------------*/
482 
483 int
484 cs_base_cuda_get_device(void);
485 
486 #endif
487 
488 /*----------------------------------------------------------------------------*/
489 
491 
492 #endif /* __CS_BASE_CUDA_H__ */
static unsigned int cs_cuda_grid_size(cs_lnum_t n, unsigned int block_size)
Compute grid size for given array and block sizes.
Definition: cs_base_cuda.h:416
int cs_glob_cuda_n_mp
int cs_glob_cuda_max_threads_per_block
int cs_glob_cuda_max_blocks
int cs_glob_cuda_device_id
int cs_glob_cuda_max_block_size
#define BEGIN_C_DECLS
Definition: cs_defs.h:514
#define END_C_DECLS
Definition: cs_defs.h:515
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:313
@ p
Definition: cs_field_pointer.h:67
cs_log_t
Definition: cs_log.h:48