8.2
general documentation
Loading...
Searching...
No Matches
cs_base_accel.h
Go to the documentation of this file.
1#ifndef __CS_BASE_ACCEL_H__
2#define __CS_BASE_ACCEL_H__
3
4/*============================================================================
5 * Definitions, global variables, and base functions for accelerators.
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2024 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 and C++ library headers
34 *----------------------------------------------------------------------------*/
35
36#include <stdio.h>
37
38#if defined(SYCL_LANGUAGE_VERSION)
39#include <sycl/sycl.hpp>
40#endif
41
42/*----------------------------------------------------------------------------
43 * Local headers
44 *----------------------------------------------------------------------------*/
45
46#include "bft_mem.h"
47
48/*=============================================================================
49 * Macro definitions
50 *============================================================================*/
51
52/*
53 * Allocate memory for _ni items of type _type.
54 *
55 * This macro calls cs_malloc_hd(), automatically setting the
56 * allocated variable name and source file name and line arguments.
57 *
58 * If separate allocations are used on the host and device
59 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer is returned.
60 *
61 * parameters:
62 * _ptr --> pointer to allocated memory.
63 * _ni <-- number of items.
64 * _type <-- element type.
65 * _mode <-- allocation mode.
66 */
67
68#define CS_MALLOC_HD(_ptr, _ni, _type, _mode) \
69_ptr = (_type *) cs_malloc_hd(_mode, _ni, sizeof(_type), \
70 #_ptr, __FILE__, __LINE__)
71
72/*
73 * Reallocate memory for _ni items of type _type.
74 *
75 * This macro calls cs_realloc_hd(), automatically setting the
76 * allocated variable name and source file name and line arguments.
77 *
78 * If the allocation parameters are unchanged, no actual reallocation
79 * occurs.
80 *
81 * parameters:
82 * _ptr <-> pointer to allocated memory.
83 * _ni <-- number of items.
84 * _type <-- element type.
85 * _mode <-- allocation mode.
86 */
87
88#define CS_REALLOC_HD(_ptr, _ni, _type, _mode) \
89_ptr = (_type *) cs_realloc_hd(_ptr, _mode, _ni, sizeof(_type), \
90 #_ptr, __FILE__, __LINE__)
91
92/*
93 * Free allocated memory.
94 *
95 * This macro calls cs_mem_free_any(), automatically setting the
96 * allocated variable name and source file name and line arguments.
97 *
98 * The freed pointer is set to NULL to avoid accidental reuse.
99 *
100 * If separate allocations are used on the host and device
101 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be used with this
102 * function.
103 *
104 * parameters:
105 * _ptr <-> pointer to allocated memory.
106 */
107
108#define CS_FREE_HD(_ptr) \
109cs_free_hd(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
110
111/*
112 * Free allocated memory.
113 *
114 * This macro calls cs_mem_free(), automatically setting the
115 * allocated variable name and source file name and line arguments.
116 *
117 * The freed pointer is set to NULL to avoid accidental reuse.
118 *
119 * If separate allocations are used on the host and device
120 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be used with this
121 * function.
122 *
123 * parameters:
124 * _ptr <-> pointer to allocated memory.
125 */
126
127#define CS_FREE(_ptr) \
128cs_free_hd(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
129
130/*----------------------------------------------------------------------------*/
131
133
134/*============================================================================
135 * Type definitions
136 *============================================================================*/
137
138/*=============================================================================
139 * Global variable definitions
140 *============================================================================*/
141
142#if defined(HAVE_ACCEL)
143
146
147extern int cs_mpi_device_support;
148
149#else
150
151#define cs_alloc_mode CS_ALLOC_HOST
152#define cs_alloc_mode_read_mostly CS_ALLOC_HOST
153
154#define cs_mpi_device_support 0;
155
156#endif
157
159
160#if defined(SYCL_LANGUAGE_VERSION)
161extern sycl::queue cs_glob_sycl_queue;
162#endif
163
164/*=============================================================================
165 * Public C function prototypes
166 *============================================================================*/
167
168/*----------------------------------------------------------------------------*/
174/*----------------------------------------------------------------------------*/
175
176#if defined(HAVE_ACCEL)
177
178int
179cs_get_device_id(void);
180
181#else
182
183static inline int
185{
186 return -1;
187}
188
189#endif
190
191/*----------------------------------------------------------------------------*/
210/*----------------------------------------------------------------------------*/
211
212#if defined(HAVE_ACCEL)
213
214void *
216 size_t ni,
217 size_t size,
218 const char *var_name,
219 const char *file_name,
220 int line_num);
221
222#else
223
224inline static void *
226 size_t ni,
227 size_t size,
228 const char *var_name,
229 const char *file_name,
230 int line_num)
231{
232 CS_UNUSED(mode);
233 return bft_mem_malloc(ni, size, var_name, file_name, line_num);
234}
235
236#endif
237
238/*----------------------------------------------------------------------------*/
266/*----------------------------------------------------------------------------*/
267
268#if defined(HAVE_ACCEL)
269
270void *
271cs_realloc_hd(void *ptr,
272 cs_alloc_mode_t mode,
273 size_t ni,
274 size_t size,
275 const char *var_name,
276 const char *file_name,
277 int line_num);
278
279#else
280
281inline static void *
283 cs_alloc_mode_t mode,
284 size_t ni,
285 size_t size,
286 const char *var_name,
287 const char *file_name,
288 int line_num)
289{
290 CS_UNUSED(mode);
291 return bft_mem_realloc(ptr, ni, size, var_name, file_name, line_num);
292}
293
294#endif
295
296/*----------------------------------------------------------------------------*/
308/*----------------------------------------------------------------------------*/
309
310#if defined(HAVE_ACCEL)
311
312void
313cs_free_hd(void *ptr,
314 const char *var_name,
315 const char *file_name,
316 int line_num);
317
318#else
319
320inline static void
321cs_free_hd(void *ptr,
322 const char *var_name,
323 const char *file_name,
324 int line_num)
325{
326 bft_mem_free(ptr, var_name, file_name, line_num);
327}
328
329#endif
330
331/*----------------------------------------------------------------------------*/
345/*----------------------------------------------------------------------------*/
346
347#if defined(HAVE_ACCEL)
348
349void *
350cs_get_device_ptr(void *ptr);
351
352#else
353
354inline static void *
356{
357 return ptr;
358}
359
360#endif
361
363
364#if defined(__cplusplus) && defined(HAVE_ACCEL)
365
366template <class T>
367inline const T *restrict
369{
370 const void *ptr_v
371 = cs_get_device_ptr(reinterpret_cast<void *>(ptr));
372
373 return (const T *restrict)ptr_v;
374}
375
376#endif // __cplusplus && HAVE_ACCEL
377
379
380/*----------------------------------------------------------------------------*/
394/*----------------------------------------------------------------------------*/
395
396#if defined(HAVE_ACCEL)
397
398const void *
399cs_get_device_ptr_const(const void *ptr);
400
401#else
402
403inline static const void *
405{
406 return ptr;
407}
408
409#endif
410
412
413#if defined(__cplusplus) && defined(HAVE_ACCEL)
414
415template <class T>
416inline const T *restrict
417cs_get_device_ptr_const(const T *ptr)
418{
419 const void *ptr_v
420 = cs_get_device_ptr_const(reinterpret_cast<const void *>(ptr));
421
422 return (const T *restrict)ptr_v;
423}
424
425#endif // __cplusplus && HAVE_ACCEL
426
428
429/*----------------------------------------------------------------------------*/
444/*----------------------------------------------------------------------------*/
445
446#if defined(HAVE_ACCEL)
447
448const void *
449cs_get_device_ptr_const_pf(const void *ptr);
450
451#else
452
453inline static const void *
455{
456 return ptr;
457}
458
459#endif
460
462
463#if defined(__cplusplus) && defined(HAVE_ACCEL)
464
465template <class T>
466inline const T *restrict
467cs_get_device_ptr_const_pf(const T *ptr)
468{
469 const void *ptr_v
470 = cs_get_device_ptr_const_pf(reinterpret_cast<const void *>(ptr));
471
472 return (const T *restrict)ptr_v;
473}
474
475#endif // __cplusplus && HAVE_ACCEL
476
478
479/*----------------------------------------------------------------------------*/
488/*----------------------------------------------------------------------------*/
489
490#if defined(HAVE_ACCEL)
491
493cs_check_device_ptr(const void *ptr);
494
495#else
496
497inline static cs_alloc_mode_t
498cs_check_device_ptr(const void *ptr)
499{
500 CS_UNUSED(ptr);
501 return CS_ALLOC_HOST;
502}
503
504#endif
505
506/*----------------------------------------------------------------------------*/
520/*----------------------------------------------------------------------------*/
521
522#if defined(HAVE_ACCEL)
523
524void *
525cs_associate_device_ptr(void *host_ptr,
526 size_t ni,
527 size_t size);
528
529#else
530
531#define cs_associate_device_ptr(_host_ptr, _ni, _size);
532
533#endif
534
535/*----------------------------------------------------------------------------*/
544/*----------------------------------------------------------------------------*/
545
546#if defined(HAVE_ACCEL)
547
548void
549cs_disassociate_device_ptr(void *host_ptr);
550
551#else
552
553#define cs_disassociate_device_ptr(_host_ptr);
554
555#endif
556
557/*----------------------------------------------------------------------------*/
568/*----------------------------------------------------------------------------*/
569
570#if defined(HAVE_ACCEL)
571
572void
573cs_set_alloc_mode(void **host_ptr,
574 cs_alloc_mode_t mode);
575
576#else
577
578#define cs_set_alloc_mode(_host_ptr, mode);
579
580#endif
581
582/*----------------------------------------------------------------------------*/
588/*----------------------------------------------------------------------------*/
589
590#if defined(HAVE_ACCEL)
591
592void
594
595#else
596
597#define cs_mem_advise_set_read_mostly(ptr);
598
599#endif
600
601/*----------------------------------------------------------------------------*/
607/*----------------------------------------------------------------------------*/
608
609#if defined(HAVE_ACCEL)
610
611void
613
614#else
615
616#define cs_mem_advise_unset_read_mostly(ptr);
617
618#endif
619
620/*----------------------------------------------------------------------------*/
636/*----------------------------------------------------------------------------*/
637
638#if defined(HAVE_ACCEL)
639
640void
641cs_sync_h2d(const void *ptr);
642
643#else
644
645static inline void
646cs_sync_h2d(const void *ptr)
647{
648 CS_UNUSED(ptr);
649}
650
651#endif
652
653/*----------------------------------------------------------------------------*/
675/*----------------------------------------------------------------------------*/
676
677#if defined(HAVE_ACCEL)
678
679void
680cs_sync_h2d_future(const void *ptr);
681
682#else
683
684static inline void
685cs_sync_h2d_future(const void *ptr)
686{
687 CS_UNUSED(ptr);
688}
689
690#endif
691
692/*----------------------------------------------------------------------------*/
709/*----------------------------------------------------------------------------*/
710
711#if defined(HAVE_ACCEL)
712
713void
714cs_sync_d2h(void *ptr);
715
716#else
717
718static inline void
719cs_sync_d2h(void *ptr)
720{
721 CS_UNUSED(ptr);
722}
723
724#endif
725
726/*----------------------------------------------------------------------------*/
743/*----------------------------------------------------------------------------*/
744
745#if defined(HAVE_ACCEL)
746
747void
748cs_sync_d2h_if_needed(void *ptr);
749
750#else
751
752static inline void
754{
755 CS_UNUSED(ptr);
756}
757
758#endif
759
760
761/*----------------------------------------------------------------------------*/
772/*----------------------------------------------------------------------------*/
773
774#if defined(HAVE_ACCEL)
775
776void
777cs_prefetch_h2d(void *ptr,
778 size_t size);
779
780#else
781
782static inline void
784 size_t size)
785{
786 CS_UNUSED(ptr);
787 CS_UNUSED(size);
788}
789
790#endif
791
792/*----------------------------------------------------------------------------*/
803/*----------------------------------------------------------------------------*/
804
805#if defined(HAVE_ACCEL)
806
807void
808cs_prefetch_d2h(void *ptr,
809 size_t size);
810
811#else
812
813static inline void
815 size_t size)
816{
817 CS_UNUSED(ptr);
818 CS_UNUSED(size);
819}
820
821#endif
822
823#if defined(HAVE_ACCEL)
824
825/*----------------------------------------------------------------------------*/
836/*----------------------------------------------------------------------------*/
837
838void
839cs_copy_h2d(void *dest,
840 const void *src,
841 size_t size);
842
843/*----------------------------------------------------------------------------*/
854/*----------------------------------------------------------------------------*/
855
856void
857cs_copy_d2h(void *dest,
858 const void *src,
859 size_t size);
860
861/*----------------------------------------------------------------------------*/
872/*----------------------------------------------------------------------------*/
873
874void
875cs_copy_d2d(void *dest,
876 const void *src,
877 size_t size);
878
879#endif /* defined(HAVE_ACCEL) */
880
881#if defined(HAVE_OPENMP_TARGET)
882
883/*----------------------------------------------------------------------------*/
892/*----------------------------------------------------------------------------*/
893
894int
895cs_omp_target_select_default_device(void);
896
897#endif /* defined(HAVE_OPENMP_TARGET) */
898
899#if defined(HAVE_SYCL)
900
901/*----------------------------------------------------------------------------*/
912/*----------------------------------------------------------------------------*/
913
914int
915cs_sycl_select_default_device(void);
916
917#endif
918
919/*----------------------------------------------------------------------------*/
920
922
923/*=============================================================================
924 * Public C++ function prototypes and definitions.
925 *============================================================================*/
926
927#ifdef __cplusplus
928
929/*----------------------------------------------------------------------------*/
940/*----------------------------------------------------------------------------*/
941
942#if defined(HAVE_ACCEL)
943
944template<typename T>
945static inline void
946cs_set_alloc_mode_r(T* &host_ptr,
947 cs_alloc_mode_t mode)
948{
949 void *p = host_ptr;
950 cs_set_alloc_mode(&p, mode);
951 host_ptr = (T *)p;
952}
953
954#else
955
956#define cs_set_alloc_mode_r(_host_ptr, mode);
957
958#endif
959
960/*----------------------------------------------------------------------------*/
961
962#endif /* defined(__cplusplus) */
963
964#endif /* __CS_BASE_ACCEL_H__ */
void * bft_mem_malloc(size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Allocate memory for ni elements of size bytes.
Definition bft_mem.cpp:829
void * bft_mem_realloc(void *ptr, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Reallocate memory for ni elements of size bytes.
Definition bft_mem.cpp:886
void * bft_mem_free(void *ptr, const char *var_name, const char *file_name, int line_num)
Free allocated memory.
Definition bft_mem.cpp:977
cs_alloc_mode_t
Definition bft_mem.h:50
@ CS_ALLOC_HOST
Definition bft_mem.h:52
void cs_copy_h2d(void *dest, const void *src, size_t size)
Copy data from host to device.
Definition cs_base_accel.cxx:1672
void cs_copy_d2h(void *dest, const void *src, size_t size)
Copy data from device to host.
Definition cs_base_accel.cxx:1709
void cs_copy_d2d(void *dest, const void *src, size_t size)
Copy data from device to device.
Definition cs_base_accel.cxx:1746
static const void * cs_get_device_ptr_const_pf(const void *ptr)
Return matching device pointer for a given constant pointer, prefetching if applicable.
Definition cs_base_accel.h:454
#define cs_mem_advise_set_read_mostly(ptr)
Advise memory system that a given allocation will be mostly read.
Definition cs_base_accel.h:597
#define cs_mem_advise_unset_read_mostly(ptr)
Advise memory system that a given allocation will be mostly read.
Definition cs_base_accel.h:616
static void * cs_malloc_hd(cs_alloc_mode_t mode, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Allocate memory on host and device for ni elements of size bytes.
Definition cs_base_accel.h:225
static void cs_prefetch_d2h(void *ptr, size_t size)
Prefetch data from device to host.
Definition cs_base_accel.h:814
static int cs_get_device_id(void)
Return currently associated device id.
Definition cs_base_accel.h:184
static const void * cs_get_device_ptr_const(const void *ptr)
Return matching device pointer for a given constant pointer.
Definition cs_base_accel.h:404
#define cs_associate_device_ptr(_host_ptr, _ni, _size)
Associate device memory with a given host memory pointer.
Definition cs_base_accel.h:531
#define cs_set_alloc_mode(_host_ptr, mode)
Set allocation mode for an already allocated pointer.
Definition cs_base_accel.h:578
static void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition cs_base_accel.h:719
#define cs_disassociate_device_ptr(_host_ptr)
Detach device memory from a given host memory pointer.
Definition cs_base_accel.h:553
#define cs_alloc_mode
Definition cs_base_accel.h:151
static void cs_sync_h2d_future(const void *ptr)
Initiate synchronization of data from host to device for future access.
Definition cs_base_accel.h:685
#define cs_alloc_mode_read_mostly
Definition cs_base_accel.h:152
#define cs_mpi_device_support
Definition cs_base_accel.h:154
static void cs_prefetch_h2d(void *ptr, size_t size)
Prefetch data from host to device.
Definition cs_base_accel.h:783
static cs_alloc_mode_t cs_check_device_ptr(const void *ptr)
Check if a pointer is associated with a device.
Definition cs_base_accel.h:498
static void * cs_realloc_hd(void *ptr, cs_alloc_mode_t mode, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Reallocate memory on host and device for ni elements of size bytes.
Definition cs_base_accel.h:282
static void cs_free_hd(void *ptr, const char *var_name, const char *file_name, int line_num)
Free memory on host and device for a given host pointer.
Definition cs_base_accel.h:321
static void cs_sync_d2h_if_needed(void *ptr)
Synchronize data from device to host, only if needed.
Definition cs_base_accel.h:753
static void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition cs_base_accel.h:646
static void * cs_get_device_ptr(void *ptr)
Return matching device pointer for a given pointer.
Definition cs_base_accel.h:355
#define restrict
Definition cs_defs.h:141
#define BEGIN_C_DECLS
Definition cs_defs.h:528
#define CS_UNUSED(x)
Definition cs_defs.h:514
#define END_C_DECLS
Definition cs_defs.h:529
@ p
Definition cs_field_pointer.h:67