9.1
general documentation
cs_mem.h
Go to the documentation of this file.
1#ifndef CS_MEM_H
2#define CS_MEM_H
3
4/*============================================================================
5 * Base memory allocation wrappers with optional tracing
6 *============================================================================*/
7
8/*
9 This file is part of code_saturne, a general-purpose CFD tool.
10
11 Copyright (C) 1998-2025 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 "base/cs_defs.h"
31
32/*----------------------------------------------------------------------------*/
33
34/* BFT headers */
35
36#include "bft/bft_error.h"
37
38/*-----------------------------------------------------------------------------*/
39
41
42/*============================================================================
43 * Public types
44 *============================================================================*/
45
50typedef enum {
51
62
63/*============================================================================
64 * Public macros
65 *============================================================================*/
66
67/*
68 * Allocate memory for _ni items of type _type.
69 *
70 * This macro calls cs_mem_malloc(), automatically setting the
71 * allocated variable name and source file name and line arguments.
72 *
73 * parameters:
74 * _ptr --> pointer to allocated memory.
75 * _ni <-- number of items.
76 * _type <-- element type.
77 */
78
79#define CS_MALLOC(_ptr, _ni, _type) \
80_ptr = (_type *) cs_mem_malloc(_ni, sizeof(_type), \
81 #_ptr, __FILE__, __LINE__)
82
83/*
84 * Allocate memory for _ni items of type _type.
85 *
86 * This macro calls cs_mem_malloc_hd(), automatically setting the
87 * allocated variable name and source file name and line arguments.
88 *
89 * If separate allocations are used on the host and device
90 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer is returned.
91 *
92 * parameters:
93 * _ptr --> pointer to allocated memory.
94 * _ni <-- number of items.
95 * _type <-- element type.
96 * _mode <-- allocation mode.
97 */
98
99#define CS_MALLOC_HD(_ptr, _ni, _type, _mode) \
100_ptr = (_type *) cs_mem_malloc_hd(_mode, _ni, sizeof(_type), \
101 #_ptr, __FILE__, __LINE__)
102
103/*
104 * Reallocate memory for _ni items of type _type.
105 *
106 * This macro calls cs_mem_realloc(), automatically setting the
107 * allocated variable name and source file name and line arguments.
108 *
109 * parameters:
110 * _ptr <-> pointer to allocated memory.
111 * _ni <-- number of items.
112 * _type <-- element type.
113 */
114
115#define CS_REALLOC(_ptr, _ni, _type) \
116_ptr = (_type *) cs_mem_realloc(_ptr, _ni, sizeof(_type), \
117 #_ptr, __FILE__, __LINE__)
118
119/*
120 * Reallocate memory for _ni items of type _type.
121 *
122 * This macro calls cs_mem_realloc_hd(), automatically setting the
123 * allocated variable name and source file name and line arguments.
124 *
125 * If the allocation parameters are unchanged, no actual reallocation
126 * occurs.
127 *
128 * parameters:
129 * _ptr <-> pointer to allocated memory.
130 * _ni <-- number of items.
131 * _type <-- element type.
132 * _mode <-- allocation mode.
133 */
134
135#define CS_REALLOC_HD(_ptr, _ni, _type, _mode) \
136_ptr = (_type *) cs_mem_realloc_hd(_ptr, _mode, _ni, sizeof(_type), \
137 #_ptr, __FILE__, __LINE__)
138
139/*
140 * Free allocated memory.
141 *
142 * This macro calls cs_mem_free(), automatically setting the
143 * allocated variable name and source file name and line arguments.
144 *
145 * The freed pointer is set to NULL to avoid accidental reuse.
146 *
147 * If separate allocations are used on the host and device
148 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be used with this
149 * function.
150 *
151 * parameters:
152 * _ptr <-> pointer to allocated memory.
153 */
154
155#define CS_FREE(_ptr) \
156cs_mem_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
157
158/*
159 * Allocate aligned memory for _ni items of type _type.
160 *
161 * This macro calls cs_mem_memalign(), automatically setting the
162 * allocated variable name and source file name and line arguments.
163 *
164 * parameters:
165 * _ptr --> pointer to allocated memory.
166 * _align <-- alignment.
167 * _ni <-- number of items.
168 * _type <-- element type.
169 */
170
171#define CS_MEMALIGN(_ptr, _align, _ni, _type) \
172_ptr = (_type *) cs_mem_memalign(_align, _ni, sizeof(_type), \
173 #_ptr, __FILE__, __LINE__)
174
175/*=============================================================================
176 * Global variable definitions
177 *============================================================================*/
178
179#if defined(HAVE_ACCEL)
180
184
185#else
186
187#define cs_alloc_mode CS_ALLOC_HOST
188#define cs_alloc_mode_read_mostly CS_ALLOC_HOST
189#define cs_alloc_mode_device CS_ALLOC_HOST
190
191#endif
192
193/*============================================================================
194 * Semi-private function prototypes
195 *============================================================================*/
196
199/*----------------------------------------------------------------------------
200 * Initialize memory handling.
201 *
202 * This function should be called before any other cs_mem_...()
203 * function. To activate memory allocation logging, a logfile
204 * name should be given as an argument. The resulting file will
205 * be a regular, local file. If this file cannot be opened for
206 * some reason, logging is silently de-activated.
207 *
208 * If the log file name argument is non-null but is an empty string,
209 * memory management be tracked, but not logged in detail, so only
210 * statistics will be available.
211 *
212 * parameter:
213 * log_file_name <-- name of optional log_file (if NULL, no log).
214 *----------------------------------------------------------------------------*/
215
216void
217cs_mem_init(const char *log_file_name);
218
219/*----------------------------------------------------------------------------
220 * End memory handling.
221 *
222 * This function should be called after all other cs_mem_...()
223 * functions. In case of memory allocation logging, it
224 * writes final information to the log file and closes is.
225 *----------------------------------------------------------------------------*/
226
227void
228cs_mem_end(void);
229
230/*----------------------------------------------------------------------------
231 * Indicates if cs_mem_...() functions are initialized.
232 *
233 * returns:
234 * 1 if cs_mem_init has been called, 0 otherwise.
235 *----------------------------------------------------------------------------*/
236
237int
239
240/*----------------------------------------------------------------------------
241 * Allocate memory for ni items of size bytes.
242 *
243 * This function calls malloc(), but adds tracing capabilities, and
244 * automatically calls the cs_error() errorhandler if it fails to
245 * allocate the required memory.
246 *
247 * Allocation couting and logging to trace file will be done if
248 * both required by the cs_mem_init options and if file_name != nullptr.
249 * If required but file_name == nullptr, it must be handled by the caller,
250 * using `cs_mem_log_mem_op`.
251 *
252 * parameters:
253 * ni <-- number of items.
254 * size <-- element size.
255 * var_name <-- allocated variable name string.
256 * file_name <-- name of calling source file.
257 * line_num <-- line number in calling source file.
258 *
259 * returns:
260 * pointer to allocated memory.
261 *----------------------------------------------------------------------------*/
262
263void *
264cs_mem_malloc(size_t ni,
265 size_t size,
266 const char *var_name,
267 const char *file_name,
268 int line_num);
269
270#if defined(HAVE_ACCEL)
271
272/*----------------------------------------------------------------------------*/
273/*
274 * \brief Allocate memory on host and device for ni elements of size bytes.
275 *
276 * This function calls the appropriate allocation function based on
277 * the requested mode, and allows introspection of the allocated memory.
278 *
279 * If separate pointers are used on the host and device,
280 * the host pointer is returned.
281 *
282 * \param [in] mode allocation mode
283 * \param [in] ni number of elements
284 * \param [in] size element size
285 * \param [in] var_name allocated variable name string
286 * \param [in] file_name name of calling source file
287 * \param [in] line_num line number in calling source file
288 *
289 * \returns pointer to allocated memory.
290 */
291/*----------------------------------------------------------------------------*/
292
293void *
294cs_mem_malloc_hd(cs_alloc_mode_t mode,
295 size_t ni,
296 size_t size,
297 const char *var_name,
298 const char *file_name,
299 int line_num);
300
301#else
302
303inline static void *
304cs_mem_malloc_hd(cs_alloc_mode_t mode,
305 size_t ni,
306 size_t size,
307 const char *var_name,
308 const char *file_name,
309 int line_num)
310{
311 CS_UNUSED(mode);
312 return cs_mem_malloc(ni, size, var_name, file_name, line_num);
313}
314
315#endif
316
317/*----------------------------------------------------------------------------
318 * Reallocate memory for ni items of size bytes.
319 *
320 * This function calls realloc(), but adds tracing capabilities, and
321 * automatically calls the cs_error() errorhandler if it fails to
322 * allocate the required memory.
323 *
324 * parameters:
325 * ptr <-> pointer to previous memory location
326 * (if NULL, cs_alloc() called).
327 * ni <-- number of items.
328 * size <-- element size.
329 * var_name <-- allocated variable name string.
330 * file_name <-- name of calling source file.
331 * line_num -> line number in calling source file
332 *
333 * returns:
334 * pointer to allocated memory.
335 *----------------------------------------------------------------------------*/
336
337void *
338cs_mem_realloc(void *ptr,
339 size_t ni,
340 size_t size,
341 const char *var_name,
342 const char *file_name,
343 int line_num);
344
345/*----------------------------------------------------------------------------*/
346/*
347 * \brief Reallocate memory on host and device for ni elements of size bytes.
348 *
349 * This function calls the appropriate reallocation function based on
350 * the requested mode, and allows introspection of the allocated memory.
351 *
352 * If separate pointers are used on the host and device,
353 * the host pointer should be used with this function.
354 *
355 * If the allocation parameters are unchanged, no actual reallocation
356 * occurs on the host.
357 *
358 * If the device uses a separate allocation, it is freed, and a new
359 * allocation is delayed (as per initial allocation) so as to invalidate copies
360 * which will not be up to date anymore after the associated values
361 * modification.
362 *
363 * \param [in] ptr pointer to previously allocated memory
364 * \param [in] mode allocation mode
365 * \param [in] ni number of elements
366 * \param [in] size element size
367 * \param [in] var_name allocated variable name string
368 * \param [in] file_name name of calling source file
369 * \param [in] line_num line number in calling source file
370 *
371 * \returns pointer to allocated memory.
372 */
373/*----------------------------------------------------------------------------*/
374
375#if defined(HAVE_ACCEL)
376
377void *
378cs_mem_realloc_hd(void *ptr,
379 cs_alloc_mode_t mode,
380 size_t ni,
381 size_t size,
382 const char *var_name,
383 const char *file_name,
384 int line_num);
385
386#else
387
388inline static void *
389cs_mem_realloc_hd(void *ptr,
390 cs_alloc_mode_t mode,
391 size_t ni,
392 size_t size,
393 const char *var_name,
394 const char *file_name,
395 int line_num)
396{
397 CS_UNUSED(mode);
398 return cs_mem_realloc(ptr, ni, size, var_name, file_name, line_num);
399}
400
401#endif
402
403/*----------------------------------------------------------------------------
404 * Free allocated memory.
405 *
406 * This function calls free(), but adds tracing capabilities, and
407 * automatically calls the cs_error() errorhandler if it fails to
408 * free the corresponding memory. In case of a null pointer argument,
409 * the function simply returns.
410 *
411 * parameters:
412 * ptr <-> pointer to previous memory location
413 * (if NULL, cs_alloc() called).
414 * var_name <-- allocated variable name string.
415 * file_name <-- name of calling source file.
416 * line_num <-- line number in calling source file.
417 *
418 * returns:
419 * null pointer.
420 *----------------------------------------------------------------------------*/
421
422void *
423cs_mem_free(void *ptr,
424 const char *var_name,
425 const char *file_name,
426 int line_num);
427
428/*----------------------------------------------------------------------------
429 * Allocate aligned memory for ni elements of size bytes.
430 *
431 * This function calls posix_memalign() if available, but adds tracing
432 * capabilities, and automatically calls the cs_error() errorhandler if
433 * it fails to allocate the required memory.
434 *
435 * The associated function cs_mem_have_memalign() indicates if this
436 * type of allocation may be used on this system.
437 *
438 * parameters:
439 * alignment <-- alignent.
440 * ni <-- number of items.
441 * size <-- element size.
442 * var_name <-- allocated variable name string.
443 * file_name <-- name of calling source file.
444 * line_num <-- line number in calling source file.
445 *
446 * returns:
447 * pointer to allocated memory.
448 *----------------------------------------------------------------------------*/
449
450void *
451cs_mem_memalign(size_t alignment,
452 size_t ni,
453 size_t size,
454 const char *var_name,
455 const char *file_name,
456 int line_num);
457
458/*----------------------------------------------------------------------------*/
459/*
460 * \brief Return current theoretical dynamic memory allocated.
461 *
462 * \return current memory handled through cs_mem_...() (in kB).
463 *----------------------------------------------------------------------------*/
464
465size_t
467
468/*----------------------------------------------------------------------------*/
469/*
470 * \brief Return maximum theoretical dynamic memory allocated.
471 *
472 * \return maximum memory handled through cs_mem_...() (in kB).
473 *----------------------------------------------------------------------------*/
474
475size_t
476cs_mem_size_max(void);
477
478/*----------------------------------------------------------------------------
479 * Indicate if a memory aligned allocation variant is available.
480 *
481 * If no such function is available, cs_mem_memalign() will always fail.
482 *
483 * returns:
484 * 1 if memory aligned allocation is possible, 0 otherwise.
485 *----------------------------------------------------------------------------*/
486
487int
489
490/*----------------------------------------------------------------------------*/
491/* Returns the error handler associated with the cs_mem_...() functions.
492 *
493 * returns:
494 * pointer to the error handler function.
495 *----------------------------------------------------------------------------*/
496
499
500/*----------------------------------------------------------------------------
501 * Associates an error handler with the cs_mem_...() functions.
502 *
503 * With the default error handler, an error message is output to stderr,
504 * (after cs_print_flush() is called), and the general error handler used
505 * by cs_error() is then called (which results in the termination of the
506 * current process or process group).
507 *
508 * parameter:
509 * handler <-- pointer to the error handler function.
510 *----------------------------------------------------------------------------*/
511
512void
514
515/*============================================================================
516 * Semi-private function definitions
517 *============================================================================*/
518
519#if defined(HAVE_OPENMP_TARGET)
520
521/*----------------------------------------------------------------------------*/
522/*
523 * \brief Set OpenMp target device id
524 *
525 * \param [in] device_id device id to use for OpenMP device memory handling.
526 */
527/*----------------------------------------------------------------------------*/
528
529void
530cs_mem_set_omp_target_device_id(int device_id);
531
532#endif
533
536/*============================================================================
537 * Public function prototypes
538 *============================================================================*/
539
540/*----------------------------------------------------------------------------*/
556int
557cs_mem_stats(uint64_t *alloc_cur,
558 uint64_t *alloc_max,
559 uint64_t *n_allocs,
560 uint64_t *n_reallocs,
561 uint64_t *n_frees,
562 uint64_t *n_current);
563
564/*----------------------------------------------------------------------------*/
565
567
568#if defined(__cplusplus)
569
570#if defined(HAVE_ACCEL)
571
572/*----------------------------------------------------------------------------*/
583/*----------------------------------------------------------------------------*/
584
585void
586cs_copy_h2d(void *dest,
587 const void *src,
588 size_t size);
589
590/*----------------------------------------------------------------------------*/
601/*----------------------------------------------------------------------------*/
602
603void
604cs_copy_d2h(void *dest,
605 const void *src,
606 size_t size);
607
608/*----------------------------------------------------------------------------*/
619/*----------------------------------------------------------------------------*/
620
621void
622cs_copy_d2d(void *dest,
623 const void *src,
624 size_t size);
625
626#endif // defined(HAVE_ACCEL)
627
628/*----------------------------------------------------------------------------*/
642/*----------------------------------------------------------------------------*/
643
644#if defined(HAVE_ACCEL)
645
646void *
647cs_get_device_ptr(void *ptr);
648
649#else
650
651inline static void *
653{
654 return ptr;
655}
656
657#endif
658
659#if defined(HAVE_ACCEL)
660
661template <class T>
662inline T *
663cs_get_device_ptr(T *ptr)
664{
665 void *ptr_v
666 = cs_get_device_ptr(reinterpret_cast<void *>(ptr));
667
668 return (T *)ptr_v;
669}
670
671#endif // HAVE_ACCEL
672
673/*----------------------------------------------------------------------------*/
687/*----------------------------------------------------------------------------*/
688
689#if defined(HAVE_ACCEL)
690
691const void *
692cs_get_device_ptr_const(const void *ptr);
693
694#else
695
696inline static const void *
698{
699 return ptr;
700}
701
702#endif
703
704#if defined(HAVE_ACCEL)
705
706template <class T>
707inline const T *
708cs_get_device_ptr_const(const T *ptr)
709{
710 const void *ptr_v
711 = cs_get_device_ptr_const(reinterpret_cast<const void *>(ptr));
712
713 return (const T *)ptr_v;
714}
715
716#endif // HAVE_ACCEL
717
718/*----------------------------------------------------------------------------*/
727/*----------------------------------------------------------------------------*/
728
729#if defined(HAVE_ACCEL)
730
732cs_check_device_ptr(const void *ptr);
733
734#else
735
736inline static cs_alloc_mode_t
737cs_check_device_ptr(const void *ptr)
738{
739 CS_UNUSED(ptr);
740 return CS_ALLOC_HOST;
741}
742
743#endif
744
745/*----------------------------------------------------------------------------*/
754/*----------------------------------------------------------------------------*/
755
756#if defined(HAVE_ACCEL)
757
758bool
759cs_mem_is_device_ptr(const void *ptr);
760
761#else
762
763inline static bool
764cs_mem_is_device_ptr(const void *ptr)
765{
766 CS_UNUSED(ptr);
767 return false;
768}
769
770#endif
771
772/*----------------------------------------------------------------------------*/
786/*----------------------------------------------------------------------------*/
787
788#if defined(HAVE_ACCEL)
789
790void *
791cs_associate_device_ptr(void *host_ptr,
792 size_t ni,
793 size_t size);
794
795#else
796
797#define cs_associate_device_ptr(_host_ptr, _ni, _size);
798
799#endif
800
801/*----------------------------------------------------------------------------*/
810/*----------------------------------------------------------------------------*/
811
812#if defined(HAVE_ACCEL)
813
814void
815cs_disassociate_device_ptr(void *host_ptr);
816
817#else
818
819#define cs_disassociate_device_ptr(_host_ptr);
820
821#endif
822
823/*----------------------------------------------------------------------------*/
834/*----------------------------------------------------------------------------*/
835
836#if defined(HAVE_ACCEL)
837
838void
839cs_set_alloc_mode(void **host_ptr,
840 cs_alloc_mode_t mode);
841
842#else
843
844#define cs_set_alloc_mode(_host_ptr, mode);
845
846#endif
847
848/*----------------------------------------------------------------------------*/
859/*----------------------------------------------------------------------------*/
860
861#if defined(HAVE_ACCEL)
862
863template<typename T>
864static inline void
865cs_set_alloc_mode_r(T* &host_ptr,
866 cs_alloc_mode_t mode)
867{
868 void *p = host_ptr;
869 cs_set_alloc_mode(&p, mode);
870 host_ptr = (T *)p;
871}
872
873#else
874
875#define cs_set_alloc_mode_r(_host_ptr, mode);
876
877#endif // HAVE_ACCEL
878
879/*----------------------------------------------------------------------------*/
885/*----------------------------------------------------------------------------*/
886
887#if defined(HAVE_ACCEL)
888
889void
891
892#else
893
894#define cs_mem_advise_set_read_mostly(ptr);
895
896#endif
897
898/*----------------------------------------------------------------------------*/
904/*----------------------------------------------------------------------------*/
905
906#if defined(HAVE_ACCEL)
907
908void
910
911#else
912
913static inline void
915{
916 CS_UNUSED(ptr);
917}
918
919#endif
920
921/*----------------------------------------------------------------------------*/
937/*----------------------------------------------------------------------------*/
938
939#if defined(HAVE_ACCEL)
940
941void
942cs_sync_h2d(const void *ptr);
943
944#else
945
946static inline void
947cs_sync_h2d(const void *ptr)
948{
949 CS_UNUSED(ptr);
950}
951
952#endif
953
954/*----------------------------------------------------------------------------*/
970/*----------------------------------------------------------------------------*/
971
972#if defined(HAVE_ACCEL)
973
974void
975cs_sync_h2d_start(const void *ptr);
976
977#else
978
979static inline void
980cs_sync_h2d_start(const void *ptr)
981{
982 CS_UNUSED(ptr);
983}
984
985#endif
986
987/*----------------------------------------------------------------------------*/
1004/*----------------------------------------------------------------------------*/
1005
1006#if defined(HAVE_ACCEL)
1007
1008void
1009cs_sync_d2h(void *ptr);
1010
1011#else
1012
1013static inline void
1014cs_sync_d2h(void *ptr)
1015{
1016 CS_UNUSED(ptr);
1017}
1018
1019#endif
1020
1021/*----------------------------------------------------------------------------*/
1038/*----------------------------------------------------------------------------*/
1039
1040#if defined(HAVE_ACCEL)
1041
1042void
1043cs_sync_d2h_start(void *ptr);
1044
1045#else
1046
1047static inline void
1049{
1050 CS_UNUSED(ptr);
1051}
1052
1053#endif
1054
1055/*----------------------------------------------------------------------------*/
1072/*----------------------------------------------------------------------------*/
1073
1074#if defined(HAVE_ACCEL)
1075
1076void
1077cs_sync_d2h_if_needed(void *ptr);
1078
1079#else
1080
1081static inline void
1083{
1084 CS_UNUSED(ptr);
1085}
1086
1087#endif
1088
1089/*----------------------------------------------------------------------------*/
1106/*----------------------------------------------------------------------------*/
1107
1108#if defined(HAVE_ACCEL)
1109
1110void
1112
1113#else
1114
1115static inline void
1117{
1118 CS_UNUSED(ptr);
1119}
1120
1121#endif
1122
1123/*----------------------------------------------------------------------------*/
1134/*----------------------------------------------------------------------------*/
1135
1136#if defined(HAVE_ACCEL)
1137
1138void
1139cs_prefetch_h2d(const void *ptr,
1140 size_t size);
1141
1142#else
1143
1144static inline void
1145cs_prefetch_h2d([[maybe_unused]] const void *ptr,
1146 [[maybe_unused]] size_t size)
1147{
1148}
1149
1150#endif
1151
1152/*----------------------------------------------------------------------------*/
1163/*----------------------------------------------------------------------------*/
1164
1165#if defined(HAVE_ACCEL)
1166
1167void
1168cs_prefetch_d2h(void *ptr,
1169 size_t size);
1170
1171#else
1172
1173static inline void
1175 size_t size)
1176{
1177 CS_UNUSED(ptr);
1178 CS_UNUSED(size);
1179}
1180
1181#endif
1182
1183/*----------------------------------------------------------------------------*/
1188/*----------------------------------------------------------------------------*/
1189
1190#if defined(HAVE_ACCEL)
1191
1192void
1194
1195#else
1196
1197static inline void
1199{
1200}
1201
1202#endif
1203
1204#if defined(HAVE_ACCEL)
1205
1206/*----------------------------------------------------------------------------*/
1207/*
1208 * \brief Activate device memory pool
1209 *
1210 * \param [in] status true to activate, false to deactivate
1211 */
1212/*----------------------------------------------------------------------------*/
1213
1214void
1215cs_mem_device_pool_set_active(bool status);
1216
1217/*----------------------------------------------------------------------------*/
1218/*
1219 * \brief Set maximum allocation size for free memory pool.
1220 *
1221 * When the memory pool is active, memory that should be freed is transferred
1222 * to the memory pool instead, so it can be reused.
1223 * If > 0, this size corresponds to the maximum total size the pool will
1224 * manage before evicting blocks.
1225 *
1226 * \param [in] size maximum total allocation size in pool, or 0
1227 */
1228/*----------------------------------------------------------------------------*/
1229
1230void
1231cs_mem_device_pool_set_max_capacity(size_t size);
1232
1233/*----------------------------------------------------------------------------*/
1234/*
1235 * \brief Set maximum number of tries before a block in memory pool is evicted
1236 *
1237 * \param [in] n_tries number of tries
1238 */
1239/*----------------------------------------------------------------------------*/
1240
1241void
1242cs_mem_device_pool_set_max_tries(short int n_tries);
1243
1244/*----------------------------------------------------------------------------*/
1245/*
1246 * \brief Clear device memory pool if present
1247 */
1248/*----------------------------------------------------------------------------*/
1249
1250void
1251cs_mem_device_pool_clear(void);
1252
1253#endif // defined(HAVE_ACCEL)
1254
1255/*----------------------------------------------------------------------------*/
1256
1257#endif /* defined(__cplusplus) */
1258
1259#endif /* CS_MEM_H */
void() bft_error_handler_t(const char *const file_name, const int line_num, const int sys_error_code, const char *const format, va_list arg_ptr)
Function pointer to opaque error handler.
Definition: bft_error.h:52
#define BEGIN_C_DECLS
Definition: cs_defs.h:554
#define CS_UNUSED(x)
Definition: cs_defs.h:543
#define END_C_DECLS
Definition: cs_defs.h:555
@ p
Definition: cs_field_pointer.h:67
int cs_mem_have_memalign(void)
Indicate if a memory aligned allocation variant is available.
Definition: cs_mem.cpp:2321
void * cs_mem_memalign(size_t alignment, size_t ni, size_t size, const char *var_name, const char *file_name, int line_num)
Allocate aligned memory for ni elements of size bytes.
Definition: cs_mem.cpp:2133
size_t cs_mem_size_max(void)
Return maximum theoretical dynamic memory allocated.
Definition: cs_mem.cpp:2218
int cs_mem_initialized(void)
Indicates if cs_mem_...() functions are initialized.
Definition: cs_mem.cpp:1885
void * cs_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: cs_mem.cpp:1914
bft_error_handler_t * cs_mem_error_handler_get(void)
Returns the error handler associated with the cs_mem_...() functions.
Definition: cs_mem.cpp:2339
void cs_mem_init(const char *log_file_name)
Initialize memory handling.
Definition: cs_mem.cpp:1714
size_t cs_mem_size_current(void)
Return current theoretical dynamic memory allocated.
Definition: cs_mem.cpp:2204
void cs_mem_end(void)
End memory handling.
Definition: cs_mem.cpp:1808
void * cs_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: cs_mem.cpp:1973
void cs_mem_error_handler_set(bft_error_handler_t *handler)
Definition: cs_mem.cpp:2358
void * cs_mem_free(void *ptr, const char *var_name, const char *file_name, int line_num)
Free allocated memory.
Definition: cs_mem.cpp:2066
static void cs_sync_d2h_start(void *ptr)
Start synchronization of data from device to host.
Definition: cs_mem.h:1048
static void cs_sync_h2d_start(const void *ptr)
Start synchronization of data from host to device.
Definition: cs_mem.h:980
#define cs_mem_advise_set_read_mostly(ptr)
Advise memory system that a given allocation will be mostly read.
Definition: cs_mem.h:894
#define cs_alloc_mode_device
Definition: cs_mem.h:189
#define cs_set_alloc_mode_r(_host_ptr, mode)
Set allocation mode for an already allocated pointer using pass by reference semantics for the pointe...
Definition: cs_mem.h:875
static bool cs_mem_is_device_ptr(const void *ptr)
Check if a pointer is a device (or shared) pointer.
Definition: cs_mem.h:764
static void cs_prefetch_d2h(void *ptr, size_t size)
Prefetch data from device to host.
Definition: cs_mem.h:1174
static void cs_mem_advise_unset_read_mostly(void *ptr)
Advise memory system that a given allocation will be mostly read.
Definition: cs_mem.h:914
static const void * cs_get_device_ptr_const(const void *ptr)
Return matching device pointer for a given constant pointer.
Definition: cs_mem.h:697
#define cs_associate_device_ptr(_host_ptr, _ni, _size)
Associate device memory with a given host memory pointer.
Definition: cs_mem.h:797
static void cs_mem_hd_async_wait(void)
Wait for asynchronous memory operations between device and pinned host memory to finish.
Definition: cs_mem.h:1198
#define cs_set_alloc_mode(_host_ptr, mode)
Set allocation mode for an already allocated pointer.
Definition: cs_mem.h:844
static void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_mem.h:1014
#define cs_disassociate_device_ptr(_host_ptr)
Detach device memory from a given host memory pointer.
Definition: cs_mem.h:819
#define cs_alloc_mode
Definition: cs_mem.h:187
#define cs_alloc_mode_read_mostly
Definition: cs_mem.h:188
static void cs_prefetch_h2d(const void *ptr, size_t size)
Prefetch data from host to device.
Definition: cs_mem.h:1145
int cs_mem_stats(uint64_t *alloc_cur, uint64_t *alloc_max, uint64_t *n_allocs, uint64_t *n_reallocs, uint64_t *n_frees, uint64_t *n_current)
Return memory allocation stats, if available.
Definition: cs_mem.cpp:2272
static cs_alloc_mode_t cs_check_device_ptr(const void *ptr)
Check if a pointer is associated with a device.
Definition: cs_mem.h:737
static void cs_sync_d2h_if_needed_start(void *ptr)
Start synchronization of data from device to host, only if needed.
Definition: cs_mem.h:1116
static void cs_sync_d2h_if_needed(void *ptr)
Synchronize data from device to host, only if needed.
Definition: cs_mem.h:1082
static void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_mem.h:947
static void * cs_get_device_ptr(void *ptr)
Return matching device pointer for a given pointer.
Definition: cs_mem.h:652
cs_alloc_mode_t
Definition: cs_mem.h:50
@ CS_ALLOC_HOST
Definition: cs_mem.h:52
@ CS_ALLOC_HOST_DEVICE_PINNED
Definition: cs_mem.h:54
@ CS_ALLOC_HOST_DEVICE_SHARED
Definition: cs_mem.h:57
@ CS_ALLOC_HOST_DEVICE
Definition: cs_mem.h:53
@ CS_ALLOC_DEVICE
Definition: cs_mem.h:59