8.1
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 extern int cs_mpi_device_support;
164 
165 #else
166 
167 #define cs_alloc_mode CS_ALLOC_HOST
168 
169 #define cs_mpi_device_support 0;
170 
171 #endif
172 
173 /*=============================================================================
174  * Public C function prototypes
175  *============================================================================*/
176 
177 /*----------------------------------------------------------------------------*/
183 /*----------------------------------------------------------------------------*/
184 
185 #if defined(HAVE_ACCEL)
186 
187 int
188 cs_get_device_id(void);
189 
190 #else
191 
192 static inline int
194 {
195  return -1;
196 }
197 
198 #endif
199 
200 /*----------------------------------------------------------------------------*/
219 /*----------------------------------------------------------------------------*/
220 
221 #if defined(HAVE_ACCEL)
222 
223 void *
225  size_t ni,
226  size_t size,
227  const char *var_name,
228  const char *file_name,
229  int line_num);
230 
231 #else
232 
233 inline static void *
235  size_t ni,
236  size_t size,
237  const char *var_name,
238  const char *file_name,
239  int line_num)
240 {
241  CS_UNUSED(mode);
242  return bft_mem_malloc(ni, size, var_name, file_name, line_num);
243 }
244 
245 #endif
246 
247 /*----------------------------------------------------------------------------*/
275 /*----------------------------------------------------------------------------*/
276 
277 #if defined(HAVE_ACCEL)
278 
279 void *
280 cs_realloc_hd(void *ptr,
281  cs_alloc_mode_t mode,
282  size_t ni,
283  size_t size,
284  const char *var_name,
285  const char *file_name,
286  int line_num);
287 
288 #else
289 
290 inline static void *
291 cs_realloc_hd(void *ptr,
292  cs_alloc_mode_t mode,
293  size_t ni,
294  size_t size,
295  const char *var_name,
296  const char *file_name,
297  int line_num)
298 {
299  CS_UNUSED(mode);
300  return bft_mem_realloc(ptr, ni, size, var_name, file_name, line_num);
301 }
302 
303 #endif
304 
305 /*----------------------------------------------------------------------------*/
317 /*----------------------------------------------------------------------------*/
318 
319 #if defined(HAVE_ACCEL)
320 
321 void
322 cs_free_hd(void *ptr,
323  const char *var_name,
324  const char *file_name,
325  int line_num);
326 
327 #else
328 
329 inline static void
330 cs_free_hd(void *ptr,
331  const char *var_name,
332  const char *file_name,
333  int line_num)
334 {
335  bft_mem_free(ptr, var_name, file_name, line_num);
336 }
337 
338 #endif
339 
340 /*----------------------------------------------------------------------------*/
352 /*----------------------------------------------------------------------------*/
353 
354 #if defined(HAVE_ACCEL)
355 
356 void
357 cs_free(void *ptr,
358  const char *var_name,
359  const char *file_name,
360  int line_num);
361 
362 #else
363 
364 inline static void
365 cs_free(void *ptr,
366  const char *var_name,
367  const char *file_name,
368  int line_num)
369 {
370  bft_mem_free(ptr, var_name, file_name, line_num);
371 }
372 
373 #endif
374 
375 /*----------------------------------------------------------------------------*/
389 /*----------------------------------------------------------------------------*/
390 
391 #if defined(HAVE_ACCEL)
392 
393 void *
394 cs_get_device_ptr(void *ptr);
395 
396 #else
397 
398 inline static void *
400 {
401  return ptr;
402 }
403 
404 #endif
405 
406 /*----------------------------------------------------------------------------*/
420 /*----------------------------------------------------------------------------*/
421 
422 #if defined(HAVE_ACCEL)
423 
424 const void *
425 cs_get_device_ptr_const(const void *ptr);
426 
427 #else
428 
429 inline static const void *
430 cs_get_device_ptr_const(const void *ptr)
431 {
432  return ptr;
433 }
434 
435 #endif
436 
437 /*----------------------------------------------------------------------------*/
452 /*----------------------------------------------------------------------------*/
453 
454 #if defined(HAVE_ACCEL)
455 
456 const void *
457 cs_get_device_ptr_const_pf(const void *ptr);
458 
459 #else
460 
461 inline static const void *
463 {
464  return ptr;
465 }
466 
467 #endif
468 
469 /*----------------------------------------------------------------------------*/
478 /*----------------------------------------------------------------------------*/
479 
480 #if defined(HAVE_ACCEL)
481 
483 cs_check_device_ptr(const void *ptr);
484 
485 #else
486 
487 inline static cs_alloc_mode_t
488 cs_check_device_ptr(const void *ptr)
489 {
490  CS_UNUSED(ptr);
491  return CS_ALLOC_HOST;
492 }
493 
494 #endif
495 
496 /*----------------------------------------------------------------------------*/
510 /*----------------------------------------------------------------------------*/
511 
512 #if defined(HAVE_ACCEL)
513 
514 void *
515 cs_associate_device_ptr(void *host_ptr,
516  size_t ni,
517  size_t size);
518 
519 #else
520 
521 #define cs_associate_device_ptr(_host_ptr, _ni, _size);
522 
523 #endif
524 
525 /*----------------------------------------------------------------------------*/
534 /*----------------------------------------------------------------------------*/
535 
536 #if defined(HAVE_ACCEL)
537 
538 void
539 cs_disassociate_device_ptr(void *host_ptr);
540 
541 #else
542 
543 #define cs_disassociate_device_ptr(_host_ptr);
544 
545 #endif
546 
547 /*----------------------------------------------------------------------------*/
558 /*----------------------------------------------------------------------------*/
559 
560 #if defined(HAVE_ACCEL)
561 
562 void
563 cs_set_alloc_mode(void **host_ptr,
564  cs_alloc_mode_t mode);
565 
566 #else
567 
568 #define cs_set_alloc_mode(_host_ptr, mode);
569 
570 #endif
571 
572 /*----------------------------------------------------------------------------*/
588 /*----------------------------------------------------------------------------*/
589 
590 #if defined(HAVE_ACCEL)
591 
592 void
593 cs_sync_h2d(const void *ptr);
594 
595 #else
596 
597 static inline void
598 cs_sync_h2d(const void *ptr)
599 {
600  CS_UNUSED(ptr);
601 }
602 
603 #endif
604 
605 /*----------------------------------------------------------------------------*/
627 /*----------------------------------------------------------------------------*/
628 
629 #if defined(HAVE_ACCEL)
630 
631 void
632 cs_sync_h2d_future(const void *ptr);
633 
634 #else
635 
636 static inline void
637 cs_sync_h2d_future(const void *ptr)
638 {
639  CS_UNUSED(ptr);
640 }
641 
642 #endif
643 
644 /*----------------------------------------------------------------------------*/
661 /*----------------------------------------------------------------------------*/
662 
663 #if defined(HAVE_ACCEL)
664 
665 void
666 cs_sync_d2h(void *ptr);
667 
668 #else
669 
670 static inline void
671 cs_sync_d2h(void *ptr)
672 {
673  CS_UNUSED(ptr);
674 }
675 
676 #endif
677 
678 /*----------------------------------------------------------------------------*/
689 /*----------------------------------------------------------------------------*/
690 
691 #if defined(HAVE_ACCEL)
692 
693 void
694 cs_prefetch_h2d(void *ptr,
695  size_t size);
696 
697 #else
698 
699 static inline void
700 cs_prefetch_h2d(void *ptr,
701  size_t size)
702 {
703  CS_UNUSED(ptr);
704  CS_UNUSED(size);
705 }
706 
707 #endif
708 
709 /*----------------------------------------------------------------------------*/
720 /*----------------------------------------------------------------------------*/
721 
722 #if defined(HAVE_ACCEL)
723 
724 void
725 cs_prefetch_d2h(void *ptr,
726  size_t size);
727 
728 #else
729 
730 static inline void
731 cs_prefetch_d2h(void *ptr,
732  size_t size)
733 {
734  CS_UNUSED(ptr);
735  CS_UNUSED(size);
736 }
737 
738 #endif
739 
740 #if defined(HAVE_ACCEL)
741 
742 /*----------------------------------------------------------------------------*/
753 /*----------------------------------------------------------------------------*/
754 
755 void
756 cs_copy_h2d(void *dest,
757  const void *src,
758  size_t size);
759 
760 /*----------------------------------------------------------------------------*/
771 /*----------------------------------------------------------------------------*/
772 
773 void
774 cs_copy_d2h(void *dest,
775  const void *src,
776  size_t size);
777 
778 /*----------------------------------------------------------------------------*/
789 /*----------------------------------------------------------------------------*/
790 
791 void
792 cs_copy_d2d(void *dest,
793  const void *src,
794  size_t size);
795 
796 #endif /* defined(HAVE_ACCEL) */
797 
798 /*----------------------------------------------------------------------------*/
804 /*----------------------------------------------------------------------------*/
805 
806 #if defined(HAVE_ACCEL)
807 
808 int
810 
811 #else
812 
813 static inline int
815 {
816  return 0;
817 }
818 
819 #endif
820 
821 /*----------------------------------------------------------------------------*/
829 /*----------------------------------------------------------------------------*/
830 
831 #if defined(HAVE_ACCEL)
832 
833 size_t
834 cs_get_allocation_hd_size(void *host_ptr);
835 
836 #else
837 
838 static inline size_t
840 {
841  CS_UNUSED(host_ptr);
842  return 0;
843 }
844 
845 #endif
846 
847 #if defined(HAVE_OPENMP_TARGET)
848 
849 /*----------------------------------------------------------------------------*/
858 /*----------------------------------------------------------------------------*/
859 
860 int
861 cs_omp_target_select_default_device(void);
862 
863 #endif /* defined(HAVE_OPENMP_TARGET) */
864 
865 /*----------------------------------------------------------------------------*/
866 
868 
869 /*=============================================================================
870  * Public C++ function prototypes and definitions.
871  *============================================================================*/
872 
873 #ifdef __cplusplus
874 
875 /*----------------------------------------------------------------------------*/
886 /*----------------------------------------------------------------------------*/
887 
888 #if defined(HAVE_ACCEL)
889 
890 template<typename T>
891 static inline void
892 cs_set_alloc_mode_r(T* &host_ptr,
893  cs_alloc_mode_t mode)
894 {
895  void *p = host_ptr;
896  cs_set_alloc_mode(&p, mode);
897  host_ptr = (T *)p;
898 }
899 
900 #else
901 
902 #define cs_set_alloc_mode_r(_host_ptr, mode);
903 
904 #endif
905 
906 /*----------------------------------------------------------------------------*/
907 
908 #endif /* defined(__cplusplus) */
909 
910 #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:1428
void cs_copy_d2h(void *dest, const void *src, size_t size)
Copy data from device to host.
Definition: cs_base_accel.cxx:1458
void cs_copy_d2d(void *dest, const void *src, size_t size)
Copy data from device to device.
Definition: cs_base_accel.cxx:1488
static void * cs_get_device_ptr(void *ptr)
Return matching device pointer for a given pointer.
Definition: cs_base_accel.h:399
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:234
static void cs_prefetch_d2h(void *ptr, size_t size)
Prefetch data from device to host.
Definition: cs_base_accel.h:731
static int cs_get_device_id(void)
Return currently associated device id.
Definition: cs_base_accel.h:193
#define cs_associate_device_ptr(_host_ptr, _ni, _size)
Associate device memory with a given host memory pointer.
Definition: cs_base_accel.h:521
#define cs_set_alloc_mode(_host_ptr, mode)
Set allocation mode for an already allocated pointer.
Definition: cs_base_accel.h:568
static void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_base_accel.h:671
#define cs_disassociate_device_ptr(_host_ptr)
Detach device memory from a given host memory pointer.
Definition: cs_base_accel.h:543
#define cs_alloc_mode
Definition: cs_base_accel.h:167
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:839
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:637
#define cs_mpi_device_support
Definition: cs_base_accel.h:169
static void cs_prefetch_h2d(void *ptr, size_t size)
Prefetch data from host to device.
Definition: cs_base_accel.h:700
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:291
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:488
static int cs_get_n_allocations_hd(void)
Return number of host-device allocations.
Definition: cs_base_accel.h:814
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:330
static void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_base_accel.h:598
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:365
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:462
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:430
#define BEGIN_C_DECLS
Definition: cs_defs.h:514
#define CS_UNUSED(x)
Definition: cs_defs.h:500
#define END_C_DECLS
Definition: cs_defs.h:515
@ p
Definition: cs_field_pointer.h:67