8.0
general documentation
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-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 "bft_mem.h"
43 
44 /*=============================================================================
45  * Macro definitions
46  *============================================================================*/
47 
48 /*
49  * Allocate memory for _ni items of type _type.
50  *
51  * This macro calls cs_malloc_hd(), automatically setting the
52  * allocated variable name and source file name and line arguments.
53  *
54  * If separate allocations are used on the host and device
55  * (mode == CS_ALLOC_HOST_DEVICE), the host pointer is returned.
56  *
57  * parameters:
58  * _ptr --> pointer to allocated memory.
59  * _ni <-- number of items.
60  * _type <-- element type.
61  * _mode <-- allocation mode.
62  */
63 
64 #define CS_MALLOC_HD(_ptr, _ni, _type, _mode) \
65 _ptr = (_type *) cs_malloc_hd(_mode, _ni, sizeof(_type), \
66  #_ptr, __FILE__, __LINE__)
67 
68 /*
69  * Reallocate memory for _ni items of type _type.
70  *
71  * This macro calls cs_realloc_hd(), automatically setting the
72  * allocated variable name and source file name and line arguments.
73  *
74  * If the allocation parameters are unchanged, no actual reallocation
75  * occurs.
76  *
77  * parameters:
78  * _ptr <-> pointer to allocated memory.
79  * _ni <-- number of items.
80  * _type <-- element type.
81  * _mode <-- allocation mode.
82  */
83 
84 #define CS_REALLOC_HD(_ptr, _ni, _type, _mode) \
85 _ptr = (_type *) cs_realloc_hd(_ptr, _mode, _ni, sizeof(_type), \
86  #_ptr, __FILE__, __LINE__)
87 
88 /*
89  * Free allocated memory.
90  *
91  * This macro calls cs_mem_free_any(), automatically setting the
92  * allocated variable name and source file name and line arguments.
93  *
94  * The freed pointer is set to NULL to avoid accidental reuse.
95  *
96  * If separate allocations are used on the host and device
97  * (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be used with this
98  * function.
99  *
100  * parameters:
101  * _ptr <-> pointer to allocated memory.
102  */
103 
104 #define CS_FREE_HD(_ptr) \
105 cs_free_hd(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
106 
107 /*
108  * Free allocated memory.
109  *
110  * This macro calls cs_mem_free(), automatically setting the
111  * allocated variable name and source file name and line arguments.
112  *
113  * The freed pointer is set to NULL to avoid accidental reuse.
114  *
115  * If separate allocations are used on the host and device
116  * (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be used with this
117  * function.
118  *
119  * parameters:
120  * _ptr <-> pointer to allocated memory.
121  */
122 
123 #define CS_FREE(_ptr) \
124 cs_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
125 
126 /*----------------------------------------------------------------------------*/
127 
129 
130 /*============================================================================
131  * Type definitions
132  *============================================================================*/
133 
134 /*----------------------------------------------------------------------------
135  * Variable value type.
136  *----------------------------------------------------------------------------*/
137 
142 typedef enum {
143 
154 
155 /*=============================================================================
156  * Global variable definitions
157  *============================================================================*/
158 
159 #if defined(HAVE_ACCEL)
160 
162 
163 #else
164 
165 #define cs_alloc_mode CS_ALLOC_HOST
166 
167 #endif
168 
169 /*=============================================================================
170  * Public C function prototypes
171  *============================================================================*/
172 
173 /*----------------------------------------------------------------------------*/
179 /*----------------------------------------------------------------------------*/
180 
181 #if defined(HAVE_ACCEL)
182 
183 int
184 cs_get_device_id(void);
185 
186 #else
187 
188 static inline int
190 {
191  return -1;
192 }
193 
194 #endif
195 
196 /*----------------------------------------------------------------------------*/
215 /*----------------------------------------------------------------------------*/
216 
217 #if defined(HAVE_ACCEL)
218 
219 void *
221  size_t ni,
222  size_t size,
223  const char *var_name,
224  const char *file_name,
225  int line_num);
226 
227 #else
228 
229 inline static void *
231  size_t ni,
232  size_t size,
233  const char *var_name,
234  const char *file_name,
235  int line_num)
236 {
237  CS_UNUSED(mode);
238  return bft_mem_malloc(ni, size, var_name, file_name, line_num);
239 }
240 
241 #endif
242 
243 /*----------------------------------------------------------------------------*/
271 /*----------------------------------------------------------------------------*/
272 
273 #if defined(HAVE_ACCEL)
274 
275 void *
276 cs_realloc_hd(void *ptr,
277  cs_alloc_mode_t mode,
278  size_t ni,
279  size_t size,
280  const char *var_name,
281  const char *file_name,
282  int line_num);
283 
284 #else
285 
286 inline static void *
287 cs_realloc_hd(void *ptr,
288  cs_alloc_mode_t mode,
289  size_t ni,
290  size_t size,
291  const char *var_name,
292  const char *file_name,
293  int line_num)
294 {
295  CS_UNUSED(mode);
296  return bft_mem_realloc(ptr, ni, size, var_name, file_name, line_num);
297 }
298 
299 #endif
300 
301 /*----------------------------------------------------------------------------*/
313 /*----------------------------------------------------------------------------*/
314 
315 #if defined(HAVE_ACCEL)
316 
317 void
318 cs_free_hd(void *ptr,
319  const char *var_name,
320  const char *file_name,
321  int line_num);
322 
323 #else
324 
325 inline static void
326 cs_free_hd(void *ptr,
327  const char *var_name,
328  const char *file_name,
329  int line_num)
330 {
331  bft_mem_free(ptr, var_name, file_name, line_num);
332 }
333 
334 #endif
335 
336 /*----------------------------------------------------------------------------*/
348 /*----------------------------------------------------------------------------*/
349 
350 #if defined(HAVE_ACCEL)
351 
352 void
353 cs_free(void *ptr,
354  const char *var_name,
355  const char *file_name,
356  int line_num);
357 
358 #else
359 
360 inline static void
361 cs_free(void *ptr,
362  const char *var_name,
363  const char *file_name,
364  int line_num)
365 {
366  bft_mem_free(ptr, var_name, file_name, line_num);
367 }
368 
369 #endif
370 
371 /*----------------------------------------------------------------------------*/
385 /*----------------------------------------------------------------------------*/
386 
387 #if defined(HAVE_ACCEL)
388 
389 void *
390 cs_get_device_ptr(void *ptr);
391 
392 #else
393 
394 inline static void *
396 {
397  return ptr;
398 }
399 
400 #endif
401 
402 /*----------------------------------------------------------------------------*/
416 /*----------------------------------------------------------------------------*/
417 
418 #if defined(HAVE_ACCEL)
419 
420 const void *
421 cs_get_device_ptr_const(const void *ptr);
422 
423 #else
424 
425 inline static const void *
426 cs_get_device_ptr_const(const void *ptr)
427 {
428  return ptr;
429 }
430 
431 #endif
432 
433 /*----------------------------------------------------------------------------*/
448 /*----------------------------------------------------------------------------*/
449 
450 #if defined(HAVE_ACCEL)
451 
452 const void *
453 cs_get_device_ptr_const_pf(const void *ptr);
454 
455 #else
456 
457 inline static const void *
459 {
460  return ptr;
461 }
462 
463 #endif
464 
465 /*----------------------------------------------------------------------------*/
474 /*----------------------------------------------------------------------------*/
475 
476 #if defined(HAVE_ACCEL)
477 
479 cs_check_device_ptr(const void *ptr);
480 
481 #else
482 
483 inline static cs_alloc_mode_t
484 cs_check_device_ptr(const void *ptr)
485 {
486  CS_UNUSED(ptr);
487  return CS_ALLOC_HOST;
488 }
489 
490 #endif
491 
492 /*----------------------------------------------------------------------------*/
506 /*----------------------------------------------------------------------------*/
507 
508 #if defined(HAVE_ACCEL)
509 
510 void *
511 cs_associate_device_ptr(void *host_ptr,
512  size_t ni,
513  size_t size);
514 
515 #else
516 
517 #define cs_associate_device_ptr(_host_ptr, _ni, _size);
518 
519 #endif
520 
521 /*----------------------------------------------------------------------------*/
530 /*----------------------------------------------------------------------------*/
531 
532 #if defined(HAVE_ACCEL)
533 
534 void
535 cs_disassociate_device_ptr(void *host_ptr);
536 
537 #else
538 
539 #define cs_disassociate_device_ptr(_host_ptr);
540 
541 #endif
542 
543 /*----------------------------------------------------------------------------*/
554 /*----------------------------------------------------------------------------*/
555 
556 #if defined(HAVE_ACCEL)
557 
558 void
559 cs_set_alloc_mode(void **host_ptr,
560  cs_alloc_mode_t mode);
561 
562 #else
563 
564 #define cs_set_alloc_mode(_host_ptr, mode);
565 
566 #endif
567 
568 /*----------------------------------------------------------------------------*/
584 /*----------------------------------------------------------------------------*/
585 
586 #if defined(HAVE_ACCEL)
587 
588 void
589 cs_sync_h2d(const void *ptr);
590 
591 #else
592 
593 static inline void
594 cs_sync_h2d(const void *ptr)
595 {
596  CS_UNUSED(ptr);
597 }
598 
599 #endif
600 
601 /*----------------------------------------------------------------------------*/
623 /*----------------------------------------------------------------------------*/
624 
625 #if defined(HAVE_ACCEL)
626 
627 void
628 cs_sync_h2d_future(const void *ptr);
629 
630 #else
631 
632 static inline void
633 cs_sync_h2d_future(const void *ptr)
634 {
635  CS_UNUSED(ptr);
636 }
637 
638 #endif
639 
640 /*----------------------------------------------------------------------------*/
657 /*----------------------------------------------------------------------------*/
658 
659 #if defined(HAVE_ACCEL)
660 
661 void
662 cs_sync_d2h(void *ptr);
663 
664 #else
665 
666 static inline void
667 cs_sync_d2h(void *ptr)
668 {
669  CS_UNUSED(ptr);
670 }
671 
672 #endif
673 
674 /*----------------------------------------------------------------------------*/
685 /*----------------------------------------------------------------------------*/
686 
687 #if defined(HAVE_ACCEL)
688 
689 void
690 cs_prefetch_h2d(void *ptr,
691  size_t size);
692 
693 #else
694 
695 static inline void
696 cs_prefetch_h2d(void *ptr,
697  size_t size)
698 {
699  CS_UNUSED(ptr);
700  CS_UNUSED(size);
701 }
702 
703 #endif
704 
705 /*----------------------------------------------------------------------------*/
716 /*----------------------------------------------------------------------------*/
717 
718 #if defined(HAVE_ACCEL)
719 
720 void
721 cs_prefetch_d2h(void *ptr,
722  size_t size);
723 
724 #else
725 
726 static inline void
727 cs_prefetch_d2h(void *ptr,
728  size_t size)
729 {
730  CS_UNUSED(ptr);
731  CS_UNUSED(size);
732 }
733 
734 #endif
735 
736 #if defined(HAVE_ACCEL)
737 
738 /*----------------------------------------------------------------------------*/
749 /*----------------------------------------------------------------------------*/
750 
751 void
752 cs_copy_h2d(void *dest,
753  const void *src,
754  size_t size);
755 
756 /*----------------------------------------------------------------------------*/
767 /*----------------------------------------------------------------------------*/
768 
769 void
770 cs_copy_d2h(void *dest,
771  const void *src,
772  size_t size);
773 
774 /*----------------------------------------------------------------------------*/
785 /*----------------------------------------------------------------------------*/
786 
787 void
788 cs_copy_d2d(void *dest,
789  const void *src,
790  size_t size);
791 
792 #endif /* defined(HAVE_ACCEL) */
793 
794 /*----------------------------------------------------------------------------*/
800 /*----------------------------------------------------------------------------*/
801 
802 #if defined(HAVE_ACCEL)
803 
804 int
806 
807 #else
808 
809 static inline int
811 {
812  return 0;
813 }
814 
815 #endif
816 
817 /*----------------------------------------------------------------------------*/
825 /*----------------------------------------------------------------------------*/
826 
827 #if defined(HAVE_ACCEL)
828 
829 size_t
830 cs_get_allocation_hd_size(void *host_ptr);
831 
832 #else
833 
834 static inline size_t
836 {
837  CS_UNUSED(host_ptr);
838  return 0;
839 }
840 
841 #endif
842 
843 #if defined(HAVE_OPENMP_TARGET)
844 
845 /*----------------------------------------------------------------------------*/
854 /*----------------------------------------------------------------------------*/
855 
856 int
857 cs_omp_target_select_default_device(void);
858 
859 #endif /* defined(HAVE_OPENMP_TARGET) */
860 
861 /*----------------------------------------------------------------------------*/
862 
864 
865 /*=============================================================================
866  * Public C++ function prototypes and definitions.
867  *============================================================================*/
868 
869 #ifdef __cplusplus
870 
871 /*----------------------------------------------------------------------------*/
882 /*----------------------------------------------------------------------------*/
883 
884 #if defined(HAVE_ACCEL)
885 
886 template<typename T>
887 static inline void
888 cs_set_alloc_mode_r(T* &host_ptr,
889  cs_alloc_mode_t mode)
890 {
891  void *p = host_ptr;
892  cs_set_alloc_mode(&p, mode);
893  host_ptr = (T *)p;
894 }
895 
896 #else
897 
898 #define cs_set_alloc_mode_r(_host_ptr, mode);
899 
900 #endif
901 
902 /*----------------------------------------------------------------------------*/
903 
904 #endif /* defined(__cplusplus) */
905 
906 #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.c:815
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.c:899
void * bft_mem_free(void *ptr, const char *var_name, const char *file_name, int line_num)
Free allocated memory.
Definition: bft_mem.c:1056
void cs_copy_h2d(void *dest, const void *src, size_t size)
Copy data from host to device.
Definition: cs_base_accel.cxx:1406
void cs_copy_d2h(void *dest, const void *src, size_t size)
Copy data from device to host.
Definition: cs_base_accel.cxx:1436
void cs_copy_d2d(void *dest, const void *src, size_t size)
Copy data from device to device.
Definition: cs_base_accel.cxx:1466
static void * cs_get_device_ptr(void *ptr)
Return matching device pointer for a given pointer.
Definition: cs_base_accel.h:395
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:230
static void cs_prefetch_d2h(void *ptr, size_t size)
Prefetch data from device to host.
Definition: cs_base_accel.h:727
static int cs_get_device_id(void)
Return currently associated device id.
Definition: cs_base_accel.h:189
#define cs_associate_device_ptr(_host_ptr, _ni, _size)
Associate device memory with a given host memory pointer.
Definition: cs_base_accel.h:517
#define cs_set_alloc_mode(_host_ptr, mode)
Set allocation mode for an already allocated pointer.
Definition: cs_base_accel.h:564
static void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_base_accel.h:667
#define cs_disassociate_device_ptr(_host_ptr)
Detach device memory from a given host memory pointer.
Definition: cs_base_accel.h:539
#define cs_alloc_mode
Definition: cs_base_accel.h:165
static size_t cs_get_allocation_hd_size(void *host_ptr)
Check if a given host pointer is allocated with associated with cs_alloc_hd or cs_realloc_hd.
Definition: cs_base_accel.h:835
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:633
static void cs_prefetch_h2d(void *ptr, size_t size)
Prefetch data from host to device.
Definition: cs_base_accel.h:696
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:287
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:484
static int cs_get_n_allocations_hd(void)
Return number of host-device allocations.
Definition: cs_base_accel.h:810
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:326
static void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_base_accel.h:594
static void cs_free(void *ptr, const char *var_name, const char *file_name, int line_num)
Free memory on host and device for a given pointer.
Definition: cs_base_accel.h:361
cs_alloc_mode_t
Definition: cs_base_accel.h:142
@ CS_ALLOC_HOST
Definition: cs_base_accel.h:144
@ CS_ALLOC_HOST_DEVICE_PINNED
Definition: cs_base_accel.h:146
@ CS_ALLOC_HOST_DEVICE_SHARED
Definition: cs_base_accel.h:149
@ CS_ALLOC_HOST_DEVICE
Definition: cs_base_accel.h:145
@ CS_ALLOC_DEVICE
Definition: cs_base_accel.h:151
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:458
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:426
#define BEGIN_C_DECLS
Definition: cs_defs.h:509
#define CS_UNUSED(x)
Definition: cs_defs.h:495
#define END_C_DECLS
Definition: cs_defs.h:510
@ p
Definition: cs_field_pointer.h:67