7.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-2021 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 
138 typedef enum {
139 
150 
151 /*=============================================================================
152  * Global variable definitions
153  *============================================================================*/
154 
155 #if defined(HAVE_ACCEL)
156 
158 
159 #else
160 
161 #define cs_alloc_mode CS_ALLOC_HOST
162 
163 #endif
164 
165 /*=============================================================================
166  * Public function prototypes
167  *============================================================================*/
168 
169 /*----------------------------------------------------------------------------*/
175 /*----------------------------------------------------------------------------*/
176 
177 #if defined(HAVE_ACCEL)
178 
179 int
180 cs_get_device_id(void);
181 
182 #else
183 
184 static inline int
185 cs_get_device_id(void)
186 {
187  return -1;
188 }
189 
190 #endif
191 
192 /*----------------------------------------------------------------------------*/
211 /*----------------------------------------------------------------------------*/
212 
213 #if defined(HAVE_ACCEL)
214 
215 void *
217  size_t ni,
218  size_t size,
219  const char *var_name,
220  const char *file_name,
221  int line_num);
222 
223 #else
224 
225 inline static void *
227  size_t ni,
228  size_t size,
229  const char *var_name,
230  const char *file_name,
231  int line_num)
232 {
233  CS_UNUSED(mode);
234  return bft_mem_malloc(ni, size, var_name, file_name, line_num);
235 }
236 
237 #endif
238 
239 /*----------------------------------------------------------------------------*/
262 /*----------------------------------------------------------------------------*/
263 
264 #if defined(HAVE_ACCEL)
265 
266 void *
267 cs_realloc_hd(void *ptr,
268  cs_alloc_mode_t mode,
269  size_t ni,
270  size_t size,
271  const char *var_name,
272  const char *file_name,
273  int line_num);
274 
275 #else
276 
277 inline static void *
278 cs_realloc_hd(void *ptr,
279  cs_alloc_mode_t mode,
280  size_t ni,
281  size_t size,
282  const char *var_name,
283  const char *file_name,
284  int line_num)
285 {
286  CS_UNUSED(mode);
287  return bft_mem_realloc(ptr, ni, size, var_name, file_name, line_num);
288 }
289 
290 #endif
291 
292 /*----------------------------------------------------------------------------*/
304 /*----------------------------------------------------------------------------*/
305 
306 #if defined(HAVE_ACCEL)
307 
308 void
309 cs_free_hd(void *ptr,
310  const char *var_name,
311  const char *file_name,
312  int line_num);
313 
314 #else
315 
316 inline static void
317 cs_free_hd(void *ptr,
318  const char *var_name,
319  const char *file_name,
320  int line_num)
321 {
322  bft_mem_free(ptr, var_name, file_name, line_num);
323 }
324 
325 #endif
326 
327 /*----------------------------------------------------------------------------*/
339 /*----------------------------------------------------------------------------*/
340 
341 #if defined(HAVE_ACCEL)
342 
343 void
344 cs_free(void *ptr,
345  const char *var_name,
346  const char *file_name,
347  int line_num);
348 
349 #else
350 
351 inline static void
352 cs_free(void *ptr,
353  const char *var_name,
354  const char *file_name,
355  int line_num)
356 {
357  bft_mem_free(ptr, var_name, file_name, line_num);
358 }
359 
360 #endif
361 
362 /*----------------------------------------------------------------------------*/
376 /*----------------------------------------------------------------------------*/
377 
378 #if defined(HAVE_ACCEL)
379 
380 void *
381 cs_get_device_ptr(void *ptr);
382 
383 #else
384 
385 inline static void *
386 cs_get_device_ptr(void *ptr)
387 {
388  return ptr;
389 }
390 
391 #endif
392 
393 /*----------------------------------------------------------------------------*/
407 /*----------------------------------------------------------------------------*/
408 
409 #if defined(HAVE_ACCEL)
410 
411 const void *
412 cs_get_device_ptr_const(const void *ptr);
413 
414 #else
415 
416 inline static const void *
417 cs_get_device_ptr_const(const void *ptr)
418 {
419  return ptr;
420 }
421 
422 #endif
423 
424 /*----------------------------------------------------------------------------*/
439 /*----------------------------------------------------------------------------*/
440 
441 #if defined(HAVE_ACCEL)
442 
443 const void *
444 cs_get_device_ptr_const_pf(const void *ptr);
445 
446 #else
447 
448 inline static const void *
449 cs_get_device_ptr_const_pf(const void *ptr)
450 {
451  return ptr;
452 }
453 
454 #endif
455 
456 /*----------------------------------------------------------------------------*/
465 /*----------------------------------------------------------------------------*/
466 
467 #if defined(HAVE_ACCEL)
468 
470 cs_check_device_ptr(const void *ptr);
471 
472 #else
473 
474 inline static cs_alloc_mode_t
475 cs_check_device_ptr(const void *ptr)
476 {
477  CS_UNUSED(ptr);
478  return CS_ALLOC_HOST;
479 }
480 
481 #endif
482 
483 /*----------------------------------------------------------------------------*/
497 /*----------------------------------------------------------------------------*/
498 
499 #if defined(HAVE_ACCEL)
500 
501 void *
502 cs_associate_device_ptr(void *host_ptr,
503  size_t ni,
504  size_t size);
505 
506 #else
507 
508 #define cs_associate_device_ptr(_host_ptr, _ni, _size);
509 
510 #endif
511 
512 /*----------------------------------------------------------------------------*/
521 /*----------------------------------------------------------------------------*/
522 
523 #if defined(HAVE_ACCEL)
524 
525 void
526 cs_dissassociate_device_ptr(void *host_ptr);
527 
528 #else
529 
530 #define cs_dissassociate_device_ptr(_host_ptr);
531 
532 #endif
533 
534 /*----------------------------------------------------------------------------*/
545 /*----------------------------------------------------------------------------*/
546 
547 #if defined(HAVE_ACCEL)
548 
549 void
550 cs_set_alloc_mode(void **host_ptr,
551  cs_alloc_mode_t mode);
552 
553 #else
554 
555 #define cs_set_alloc_mode(_host_ptr, mode);
556 
557 #endif
558 
559 /*----------------------------------------------------------------------------*/
575 /*----------------------------------------------------------------------------*/
576 
577 #if defined(HAVE_ACCEL)
578 
579 void
580 cs_sync_h2d(const void *ptr);
581 
582 #else
583 
584 static inline void
585 cs_sync_h2d(const void *ptr)
586 {
587  CS_UNUSED(ptr);
588 }
589 
590 #endif
591 
592 /*----------------------------------------------------------------------------*/
614 /*----------------------------------------------------------------------------*/
615 
616 #if defined(HAVE_ACCEL)
617 
618 void
619 cs_sync_h2d_future(const void *ptr);
620 
621 #else
622 
623 static inline void
624 cs_sync_h2d_future(const void *ptr)
625 {
626  CS_UNUSED(ptr);
627 }
628 
629 #endif
630 
631 /*----------------------------------------------------------------------------*/
648 /*----------------------------------------------------------------------------*/
649 
650 #if defined(HAVE_ACCEL)
651 
652 void
653 cs_sync_d2h(void *ptr);
654 
655 #else
656 
657 static inline void
658 cs_sync_d2h(void *ptr)
659 {
660  CS_UNUSED(ptr);
661 }
662 
663 #endif
664 
665 /*----------------------------------------------------------------------------*/
676 /*----------------------------------------------------------------------------*/
677 
678 #if defined(HAVE_ACCEL)
679 
680 void
681 cs_prefetch_h2d(void *ptr,
682  size_t size);
683 
684 #else
685 
686 static inline void
687 cs_prefetch_h2d(void *ptr,
688  size_t size)
689 {
690  CS_UNUSED(ptr);
691  CS_UNUSED(size);
692 }
693 
694 #endif
695 
696 /*----------------------------------------------------------------------------*/
707 /*----------------------------------------------------------------------------*/
708 
709 #if defined(HAVE_ACCEL)
710 
711 void
712 cs_prefetch_d2h(void *ptr,
713  size_t size);
714 
715 #else
716 
717 static inline void
718 cs_prefetch_d2h(void *ptr,
719  size_t size)
720 {
721  CS_UNUSED(ptr);
722  CS_UNUSED(size);
723 }
724 
725 #endif
726 
727 #if defined(HAVE_ACCEL)
728 
729 /*----------------------------------------------------------------------------*/
740 /*----------------------------------------------------------------------------*/
741 
742 void
743 cs_copy_h2d(void *dest,
744  const void *src,
745  size_t size);
746 
747 /*----------------------------------------------------------------------------*/
758 /*----------------------------------------------------------------------------*/
759 
760 void
761 cs_copy_d2h(void *dest,
762  const void *src,
763  size_t size);
764 
765 /*----------------------------------------------------------------------------*/
776 /*----------------------------------------------------------------------------*/
777 
778 void
779 cs_copy_d2d(void *dest,
780  const void *src,
781  size_t size);
782 
783 #endif /* defined(HAVE_ACCEL) */
784 
785 /*----------------------------------------------------------------------------*/
791 /*----------------------------------------------------------------------------*/
792 
793 #if defined(HAVE_ACCEL)
794 
795 int
797 
798 #else
799 
800 static inline int
802 {
803  return 0;
804 }
805 
806 #endif
807 
808 /*----------------------------------------------------------------------------*/
816 /*----------------------------------------------------------------------------*/
817 
818 #if defined(HAVE_ACCEL)
819 
820 size_t
821 cs_get_allocation_hd_size(void *host_ptr);
822 
823 #else
824 
825 static inline size_t
826 cs_get_allocation_hd_size(void *host_ptr)
827 {
828  CS_UNUSED(host_ptr);
829  return 0;
830 }
831 
832 #endif
833 
834 #if defined(HAVE_OPENMP_TARGET)
835 
836 /*----------------------------------------------------------------------------*/
845 /*----------------------------------------------------------------------------*/
846 
847 int
848 cs_omp_target_select_default_device(void);
849 
850 #endif /* defined(HAVE_OPENMP_TARGET) */
851 
852 /*----------------------------------------------------------------------------*/
853 
855 
856 #endif /* __CS_BASE_ACCEL_H__ */
void * cs_get_device_ptr(void *ptr)
Return matching device pointer for a given pointer.
Definition: cs_base_accel.cxx:639
const void * cs_get_device_ptr_const(const void *ptr)
Return matching device pointer for a given constant pointer.
Definition: cs_base_accel.cxx:704
void cs_sync_h2d_future(const void *ptr)
Initiate synchronization of data from host to device for future access.
Definition: cs_base_accel.cxx:1089
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.cxx:606
void cs_copy_h2d(void *dest, const void *src, size_t size)
Copy data from host to device.
Definition: cs_base_accel.cxx:1292
void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_base_accel.cxx:993
#define BEGIN_C_DECLS
Definition: cs_defs.h:510
cs_alloc_mode_t cs_check_device_ptr(const void *ptr)
Check if a pointer is associated with a device.
Definition: cs_base_accel.cxx:825
#define CS_UNUSED(x)
Definition: cs_defs.h:496
void cs_prefetch_h2d(void *ptr, size_t size)
Prefetch data from host to device.
Definition: cs_base_accel.cxx:1232
Definition: cs_base_accel.h:140
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.cxx:336
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.cxx:1393
Definition: cs_base_accel.h:142
void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_base_accel.cxx:1150
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
Definition: cs_base_accel.h:141
#define cs_associate_device_ptr(_host_ptr, _ni, _size)
Associate device memory with a given host memory pointer.
Definition: cs_base_accel.h:508
#define cs_alloc_mode
Definition: cs_base_accel.h:161
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.cxx:526
cs_alloc_mode_t
Definition: cs_base_accel.h:138
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.cxx:449
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.cxx:765
int cs_get_device_id(void)
Return currently associated device id.
Definition: cs_base_accel.cxx:297
#define cs_set_alloc_mode(_host_ptr, mode)
Set allocation mode for an already allocated pointer.
Definition: cs_base_accel.h:555
int cs_get_n_allocations_hd(void)
Return number of host-device allocations.
Definition: cs_base_accel.cxx:1377
Definition: cs_base_accel.h:145
#define END_C_DECLS
Definition: cs_defs.h:511
#define cs_dissassociate_device_ptr(_host_ptr)
Detach device memory from a given host memory pointer.
Definition: cs_base_accel.h:530
void cs_copy_d2h(void *dest, const void *src, size_t size)
Copy data from device to host.
Definition: cs_base_accel.cxx:1322
void cs_copy_d2d(void *dest, const void *src, size_t size)
Copy data from device to device.
Definition: cs_base_accel.cxx:1352
Definition: cs_base_accel.h:147
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 * 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 cs_prefetch_d2h(void *ptr, size_t size)
Prefetch data from device to host.
Definition: cs_base_accel.cxx:1262