9.2
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-2026 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 * Public types
40 *============================================================================*/
41
46typedef enum {
47
58
59/*============================================================================
60 * Public macros
61 *============================================================================*/
62
63/*
64 * Allocate memory for _ni items of type _type.
65 *
66 * This macro calls cs_mem_malloc(), automatically setting the
67 * allocated variable name and source file name and line arguments.
68 *
69 * parameters:
70 * _ptr --> pointer to allocated memory.
71 * _ni <-- number of items.
72 * _type <-- element type.
73 */
74
75#define CS_MALLOC(_ptr, _ni, _type) \
76_ptr = (_type *) cs_mem_malloc(_ni, sizeof(_type), \
77 #_ptr, __FILE__, __LINE__)
78
79/*
80 * Allocate memory for _ni items of type _type.
81 *
82 * This macro calls cs_mem_malloc_hd(), automatically setting the
83 * allocated variable name and source file name and line arguments.
84 *
85 * If separate allocations are used on the host and device
86 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer is returned.
87 *
88 * parameters:
89 * _ptr --> pointer to allocated memory.
90 * _ni <-- number of items.
91 * _type <-- element type.
92 * _mode <-- allocation mode.
93 */
94
95#define CS_MALLOC_HD(_ptr, _ni, _type, _mode) \
96_ptr = (_type *) cs_mem_malloc_hd(_mode, _ni, sizeof(_type), \
97 #_ptr, __FILE__, __LINE__)
98
99/*
100 * Reallocate memory for _ni items of type _type.
101 *
102 * This macro calls cs_mem_realloc(), automatically setting the
103 * allocated variable name and source file name and line arguments.
104 *
105 * parameters:
106 * _ptr <-> pointer to allocated memory.
107 * _ni <-- number of items.
108 * _type <-- element type.
109 */
110
111#define CS_REALLOC(_ptr, _ni, _type) \
112_ptr = (_type *) cs_mem_realloc(_ptr, _ni, sizeof(_type), \
113 #_ptr, __FILE__, __LINE__)
114
115/*
116 * Reallocate memory for _ni items of type _type.
117 *
118 * This macro calls cs_mem_realloc_hd(), automatically setting the
119 * allocated variable name and source file name and line arguments.
120 *
121 * If the allocation parameters are unchanged, no actual reallocation
122 * occurs.
123 *
124 * parameters:
125 * _ptr <-> pointer to allocated memory.
126 * _ni <-- number of items.
127 * _type <-- element type.
128 * _mode <-- allocation mode.
129 */
130
131#define CS_REALLOC_HD(_ptr, _ni, _type, _mode) \
132_ptr = (_type *) cs_mem_realloc_hd(_ptr, _mode, _ni, sizeof(_type), \
133 #_ptr, __FILE__, __LINE__)
134
135/*
136 * Free allocated memory.
137 *
138 * This macro calls cs_mem_free(), automatically setting the
139 * allocated variable name and source file name and line arguments.
140 *
141 * The freed pointer is set to NULL to avoid accidental reuse.
142 *
143 * If separate allocations are used on the host and device
144 * (mode == CS_ALLOC_HOST_DEVICE), the host pointer should be used with this
145 * function.
146 *
147 * parameters:
148 * _ptr <-> pointer to allocated memory.
149 */
150
151#define CS_FREE(_ptr) \
152cs_mem_free(_ptr, #_ptr, __FILE__, __LINE__), _ptr = NULL
153
154/*
155 * Allocate aligned memory for _ni items of type _type.
156 *
157 * This macro calls cs_mem_memalign(), automatically setting the
158 * allocated variable name and source file name and line arguments.
159 *
160 * parameters:
161 * _ptr --> pointer to allocated memory.
162 * _align <-- alignment.
163 * _ni <-- number of items.
164 * _type <-- element type.
165 */
166
167#define CS_MEMALIGN(_ptr, _align, _ni, _type) \
168_ptr = (_type *) cs_mem_memalign(_align, _ni, sizeof(_type), \
169 #_ptr, __FILE__, __LINE__)
170
171/*=============================================================================
172 * Global variable definitions
173 *============================================================================*/
174
175#if defined(HAVE_ACCEL)
176
180
181#else
182
183#define cs_alloc_mode CS_ALLOC_HOST
184#define cs_alloc_mode_read_mostly CS_ALLOC_HOST
185#define cs_alloc_mode_device CS_ALLOC_HOST
186
187#endif
188
189/*============================================================================
190 * Semi-private function prototypes
191 *============================================================================*/
192
195/*----------------------------------------------------------------------------
196 * Initialize memory handling.
197 *
198 * This function should be called before any other cs_mem_...()
199 * function. To activate memory allocation logging, a logfile
200 * name should be given as an argument. The resulting file will
201 * be a regular, local file. If this file cannot be opened for
202 * some reason, logging is silently de-activated.
203 *
204 * If the log file name argument is non-null but is an empty string,
205 * memory management be tracked, but not logged in detail, so only
206 * statistics will be available.
207 *
208 * parameter:
209 * log_file_name <-- name of optional log_file (if NULL, no log).
210 *----------------------------------------------------------------------------*/
211
212void
213cs_mem_init(const char *log_file_name);
214
215/*----------------------------------------------------------------------------
216 * End memory handling.
217 *
218 * This function should be called after all other cs_mem_...()
219 * functions. In case of memory allocation logging, it
220 * writes final information to the log file and closes is.
221 *----------------------------------------------------------------------------*/
222
223extern "C" void
224cs_mem_end(void);
225
226/*----------------------------------------------------------------------------
227 * Indicates if cs_mem_...() functions are initialized.
228 *
229 * returns:
230 * 1 if cs_mem_init has been called, 0 otherwise.
231 *----------------------------------------------------------------------------*/
232
233extern "C" int
235
236/*----------------------------------------------------------------------------
237 * Allocate memory for ni items of size bytes.
238 *
239 * This function calls malloc(), but adds tracing capabilities, and
240 * automatically calls the cs_error() errorhandler if it fails to
241 * allocate the required memory.
242 *
243 * Allocation couting and logging to trace file will be done if
244 * both required by the cs_mem_init options and if file_name != nullptr.
245 * If required but file_name == nullptr, it must be handled by the caller,
246 * using `cs_mem_log_mem_op`.
247 *
248 * parameters:
249 * ni <-- number of items.
250 * size <-- element size.
251 * var_name <-- allocated variable name string.
252 * file_name <-- name of calling source file.
253 * line_num <-- line number in calling source file.
254 *
255 * returns:
256 * pointer to allocated memory.
257 *----------------------------------------------------------------------------*/
258
259extern "C" void *
260cs_mem_malloc(size_t ni,
261 size_t size,
262 const char *var_name,
263 const char *file_name,
264 int line_num);
265
266#if defined(HAVE_ACCEL)
267
268/*----------------------------------------------------------------------------*/
269/*
270 * \brief Allocate memory on host and device for ni elements of size bytes.
271 *
272 * This function calls the appropriate allocation function based on
273 * the requested mode, and allows introspection of the allocated memory.
274 *
275 * If separate pointers are used on the host and device,
276 * the host pointer is returned.
277 *
278 * \param [in] mode allocation mode
279 * \param [in] ni number of elements
280 * \param [in] size element size
281 * \param [in] var_name allocated variable name string
282 * \param [in] file_name name of calling source file
283 * \param [in] line_num line number in calling source file
284 *
285 * \returns pointer to allocated memory.
286 */
287/*----------------------------------------------------------------------------*/
288
289extern "C" void *
290cs_mem_malloc_hd(cs_alloc_mode_t mode,
291 size_t ni,
292 size_t size,
293 const char *var_name,
294 const char *file_name,
295 int line_num);
296
297#else
298
299extern "C" inline void *
300cs_mem_malloc_hd([[maybe_unused]] cs_alloc_mode_t mode,
301 size_t ni,
302 size_t size,
303 const char *var_name,
304 const char *file_name,
305 int line_num)
306{
307 return cs_mem_malloc(ni, size, var_name, file_name, line_num);
308}
309
310#endif
311
312/*----------------------------------------------------------------------------
313 * Reallocate memory for ni items of size bytes.
314 *
315 * This function calls realloc(), but adds tracing capabilities, and
316 * automatically calls the cs_error() errorhandler if it fails to
317 * allocate the required memory.
318 *
319 * parameters:
320 * ptr <-> pointer to previous memory location
321 * (if NULL, cs_alloc() called).
322 * ni <-- number of items.
323 * size <-- element size.
324 * var_name <-- allocated variable name string.
325 * file_name <-- name of calling source file.
326 * line_num -> line number in calling source file
327 *
328 * returns:
329 * pointer to allocated memory.
330 *----------------------------------------------------------------------------*/
331
332extern "C" void *
333cs_mem_realloc(void *ptr,
334 size_t ni,
335 size_t size,
336 const char *var_name,
337 const char *file_name,
338 int line_num);
339
340/*----------------------------------------------------------------------------*/
341/*
342 * \brief Reallocate memory on host and device for ni elements of size bytes.
343 *
344 * This function calls the appropriate reallocation function based on
345 * the requested mode, and allows introspection of the allocated memory.
346 *
347 * If separate pointers are used on the host and device,
348 * the host pointer should be used with this function.
349 *
350 * If the allocation parameters are unchanged, no actual reallocation
351 * occurs on the host.
352 *
353 * If the device uses a separate allocation, it is freed, and a new
354 * allocation is delayed (as per initial allocation) so as to invalidate copies
355 * which will not be up to date anymore after the associated values
356 * modification.
357 *
358 * \param [in] ptr pointer to previously allocated memory
359 * \param [in] mode allocation mode
360 * \param [in] ni number of elements
361 * \param [in] size element size
362 * \param [in] var_name allocated variable name string
363 * \param [in] file_name name of calling source file
364 * \param [in] line_num line number in calling source file
365 *
366 * \returns pointer to allocated memory.
367 */
368/*----------------------------------------------------------------------------*/
369
370#if defined(HAVE_ACCEL)
371
372extern "C" void *
373cs_mem_realloc_hd(void *ptr,
374 cs_alloc_mode_t mode,
375 size_t ni,
376 size_t size,
377 const char *var_name,
378 const char *file_name,
379 int line_num);
380
381#else
382
383extern "C" inline void *
384cs_mem_realloc_hd(void *ptr,
385 [[maybe_unused]] cs_alloc_mode_t mode,
386 size_t ni,
387 size_t size,
388 const char *var_name,
389 const char *file_name,
390 int line_num)
391{
392 return cs_mem_realloc(ptr, ni, size, var_name, file_name, line_num);
393}
394
395#endif
396
397/*----------------------------------------------------------------------------
398 * Free allocated memory.
399 *
400 * This function calls free(), but adds tracing capabilities, and
401 * automatically calls the cs_error() errorhandler if it fails to
402 * free the corresponding memory. In case of a null pointer argument,
403 * the function simply returns.
404 *
405 * parameters:
406 * ptr <-> pointer to previous memory location
407 * (if NULL, cs_alloc() called).
408 * var_name <-- allocated variable name string.
409 * file_name <-- name of calling source file.
410 * line_num <-- line number in calling source file.
411 *
412 * returns:
413 * null pointer.
414 *----------------------------------------------------------------------------*/
415
416extern "C" void *
417cs_mem_free(void *ptr,
418 const char *var_name,
419 const char *file_name,
420 int line_num);
421
422/*----------------------------------------------------------------------------
423 * Allocate aligned memory for ni elements of size bytes.
424 *
425 * This function calls posix_memalign() if available, but adds tracing
426 * capabilities, and automatically calls the cs_error() errorhandler if
427 * it fails to allocate the required memory.
428 *
429 * The associated function cs_mem_have_memalign() indicates if this
430 * type of allocation may be used on this system.
431 *
432 * parameters:
433 * alignment <-- alignent.
434 * ni <-- number of items.
435 * size <-- element size.
436 * var_name <-- allocated variable name string.
437 * file_name <-- name of calling source file.
438 * line_num <-- line number in calling source file.
439 *
440 * returns:
441 * pointer to allocated memory.
442 *----------------------------------------------------------------------------*/
443
444extern "C" void *
445cs_mem_memalign(size_t alignment,
446 size_t ni,
447 size_t size,
448 const char *var_name,
449 const char *file_name,
450 int line_num);
451
452/*----------------------------------------------------------------------------*/
453/*
454 * \brief Return current theoretical dynamic memory allocated.
455 *
456 * \return current memory handled through cs_mem_...() (in kB).
457 *----------------------------------------------------------------------------*/
458
459extern "C" size_t
461
462/*----------------------------------------------------------------------------*/
463/*
464 * \brief Return maximum theoretical dynamic memory allocated.
465 *
466 * \return maximum memory handled through cs_mem_...() (in kB).
467 *----------------------------------------------------------------------------*/
468
469extern "C" size_t
470cs_mem_size_max(void);
471
472/*----------------------------------------------------------------------------
473 * Indicate if a memory aligned allocation variant is available.
474 *
475 * If no such function is available, cs_mem_memalign() will always fail.
476 *
477 * returns:
478 * 1 if memory aligned allocation is possible, 0 otherwise.
479 *----------------------------------------------------------------------------*/
480
481extern "C" int
483
484/*----------------------------------------------------------------------------*/
485/* Returns the error handler associated with the cs_mem_...() functions.
486 *
487 * returns:
488 * pointer to the error handler function.
489 *----------------------------------------------------------------------------*/
490
491extern "C" bft_error_handler_t *
493
494/*----------------------------------------------------------------------------
495 * Associates an error handler with the cs_mem_...() functions.
496 *
497 * With the default error handler, an error message is output to stderr,
498 * (after cs_print_flush() is called), and the general error handler used
499 * by cs_error() is then called (which results in the termination of the
500 * current process or process group).
501 *
502 * parameter:
503 * handler <-- pointer to the error handler function.
504 *----------------------------------------------------------------------------*/
505
506extern "C" void
508
509/*============================================================================
510 * Semi-private function definitions
511 *============================================================================*/
512
513#if defined(HAVE_OPENMP_TARGET)
514
515/*----------------------------------------------------------------------------*/
516/*
517 * \brief Set OpenMp target device id
518 *
519 * \param [in] device_id device id to use for OpenMP device memory handling.
520 */
521/*----------------------------------------------------------------------------*/
522
523extern "C" void
524cs_mem_set_omp_target_device_id(int device_id);
525
526#endif
527
530/*============================================================================
531 * Public function prototypes
532 *============================================================================*/
533
534/*----------------------------------------------------------------------------*/
550extern "C" int
551cs_mem_stats(uint64_t *alloc_cur,
552 uint64_t *alloc_max,
553 uint64_t *n_allocs,
554 uint64_t *n_reallocs,
555 uint64_t *n_frees,
556 uint64_t *n_current);
557
558/*----------------------------------------------------------------------------*/
559
560#if defined(HAVE_ACCEL)
561
562/*----------------------------------------------------------------------------*/
573/*----------------------------------------------------------------------------*/
574
575extern "C" void
576cs_copy_h2d(void *dest,
577 const void *src,
578 size_t size);
579
580/*----------------------------------------------------------------------------*/
591/*----------------------------------------------------------------------------*/
592
593extern "C" void
594cs_copy_d2h(void *dest,
595 const void *src,
596 size_t size);
597
598/*----------------------------------------------------------------------------*/
609/*----------------------------------------------------------------------------*/
610
611extern "C" void
612cs_copy_d2d(void *dest,
613 const void *src,
614 size_t size);
615
616#endif // defined(HAVE_ACCEL)
617
618/*----------------------------------------------------------------------------*/
632/*----------------------------------------------------------------------------*/
633
634#if defined(HAVE_ACCEL)
635
636extern "C" void *
637cs_get_device_ptr(void *ptr);
638
639#else
640
641extern "C" inline void *
643{
644 return ptr;
645}
646
647#endif
648
649#if defined(HAVE_ACCEL)
650
651template <class T>
652inline T *
653cs_get_device_ptr(T *ptr)
654{
655 void *ptr_v
656 = cs_get_device_ptr(reinterpret_cast<void *>(ptr));
657
658 return (T *)ptr_v;
659}
660
661#endif // HAVE_ACCEL
662
663/*----------------------------------------------------------------------------*/
677/*----------------------------------------------------------------------------*/
678
679#if defined(HAVE_ACCEL)
680
681const void *
682cs_get_device_ptr_const(const void *ptr);
683
684#else
685
686inline const void *
688{
689 return ptr;
690}
691
692#endif
693
694#if defined(HAVE_ACCEL)
695
696template <class T>
697inline const T *
698cs_get_device_ptr_const(const T *ptr)
699{
700 const void *ptr_v
701 = cs_get_device_ptr_const(reinterpret_cast<const void *>(ptr));
702
703 return (const T *)ptr_v;
704}
705
706#endif // HAVE_ACCEL
707
708/*----------------------------------------------------------------------------*/
717/*----------------------------------------------------------------------------*/
718
719#if defined(HAVE_ACCEL)
720
722cs_check_device_ptr(const void *ptr);
723
724#else
725
726inline cs_alloc_mode_t
727cs_check_device_ptr([[maybe_unused]] const void *ptr)
728{
729 return CS_ALLOC_HOST;
730}
731
732#endif
733
734/*----------------------------------------------------------------------------*/
743/*----------------------------------------------------------------------------*/
744
745#if defined(HAVE_ACCEL)
746
747extern "C" bool
748cs_mem_is_device_ptr(const void *ptr);
749
750#else
751
752extern "C" inline bool
753cs_mem_is_device_ptr(const void *ptr)
754{
755 CS_UNUSED(ptr);
756 return false;
757}
758
759#endif
760
761/*----------------------------------------------------------------------------*/
775/*----------------------------------------------------------------------------*/
776
777#if defined(HAVE_ACCEL)
778
779extern "C" void *
780cs_associate_device_ptr(void *host_ptr,
781 size_t ni,
782 size_t size);
783
784#else
785
786#define cs_associate_device_ptr(_host_ptr, _ni, _size);
787
788#endif
789
790/*----------------------------------------------------------------------------*/
799/*----------------------------------------------------------------------------*/
800
801#if defined(HAVE_ACCEL)
802
803extern "C" void
804cs_disassociate_device_ptr(void *host_ptr);
805
806#else
807
808#define cs_disassociate_device_ptr(_host_ptr);
809
810#endif
811
812/*----------------------------------------------------------------------------*/
823/*----------------------------------------------------------------------------*/
824
825#if defined(HAVE_ACCEL)
826
827extern "C" void
828cs_set_alloc_mode(void **host_ptr,
829 cs_alloc_mode_t mode);
830
831#else
832
833#define cs_set_alloc_mode(_host_ptr, mode);
834
835#endif
836
837/*----------------------------------------------------------------------------*/
848/*----------------------------------------------------------------------------*/
849
850#if defined(HAVE_ACCEL)
851
852template<typename T>
853inline void
854cs_set_alloc_mode_r(T* &host_ptr,
855 cs_alloc_mode_t mode)
856{
857 void *p = host_ptr;
858 cs_set_alloc_mode(&p, mode);
859 host_ptr = (T *)p;
860}
861
862#else
863
864#define cs_set_alloc_mode_r(_host_ptr, mode);
865
866#endif // HAVE_ACCEL
867
868/*----------------------------------------------------------------------------*/
874/*----------------------------------------------------------------------------*/
875
876#if defined(HAVE_ACCEL)
877
878extern "C" void
880
881#else
882
883#define cs_mem_advise_set_read_mostly(ptr);
884
885#endif
886
887/*----------------------------------------------------------------------------*/
893/*----------------------------------------------------------------------------*/
894
895#if defined(HAVE_ACCEL)
896
897extern "C" void
899
900#else
901
902inline void
903cs_mem_advise_unset_read_mostly([[maybe_unused]] void *ptr)
904{
905}
906
907#endif
908
909/*----------------------------------------------------------------------------*/
925/*----------------------------------------------------------------------------*/
926
927#if defined(HAVE_ACCEL)
928
929extern "C" void
930cs_sync_h2d(const void *ptr);
931
932#else
933
934extern "C" inline void
935cs_sync_h2d([[maybe_unused]] const void *ptr)
936{
937}
938
939#endif
940
941/*----------------------------------------------------------------------------*/
957/*----------------------------------------------------------------------------*/
958
959#if defined(HAVE_ACCEL)
960
961extern "C" void
962cs_sync_h2d_start(const void *ptr);
963
964#else
965
966extern "C" inline void
967cs_sync_h2d_start([[maybe_unused]] const void *ptr)
968{
969}
970
971#endif
972
973/*----------------------------------------------------------------------------*/
990/*----------------------------------------------------------------------------*/
991
992#if defined(HAVE_ACCEL)
993
994extern "C" void
995cs_sync_d2h(void *ptr);
996
997#else
998
999extern "C" inline void
1000cs_sync_d2h([[maybe_unused]] void *ptr)
1001{
1002}
1003
1004#endif
1005
1006/*----------------------------------------------------------------------------*/
1023/*----------------------------------------------------------------------------*/
1024
1025#if defined(HAVE_ACCEL)
1026
1027extern "C" void
1028cs_sync_d2h_start(void *ptr);
1029
1030#else
1031
1032extern "C" inline void
1033cs_sync_d2h_start([[maybe_unused]] void *ptr)
1034{
1035}
1036
1037#endif
1038
1039/*----------------------------------------------------------------------------*/
1056/*----------------------------------------------------------------------------*/
1057
1058#if defined(HAVE_ACCEL)
1059
1060extern "C" void
1061cs_sync_d2h_if_needed(void *ptr);
1062
1063#else
1064
1065extern "C" inline void
1066cs_sync_d2h_if_needed([[maybe_unused]] void *ptr)
1067{
1068}
1069
1070#endif
1071
1072/*----------------------------------------------------------------------------*/
1089/*----------------------------------------------------------------------------*/
1090
1091#if defined(HAVE_ACCEL)
1092
1093extern "C" void
1095
1096#else
1097
1098extern "C" inline void
1099cs_sync_d2h_if_needed_start([[maybe_unused]] void *ptr)
1100{
1101}
1102
1103#endif
1104
1105/*----------------------------------------------------------------------------*/
1116/*----------------------------------------------------------------------------*/
1117
1118#if defined(HAVE_ACCEL)
1119
1120extern "C" void
1121cs_prefetch_h2d(const void *ptr,
1122 size_t size);
1123
1124#else
1125
1126extern "C" inline void
1127cs_prefetch_h2d([[maybe_unused]] const void *ptr,
1128 [[maybe_unused]] size_t size)
1129{
1130}
1131
1132#endif
1133
1134/*----------------------------------------------------------------------------*/
1145/*----------------------------------------------------------------------------*/
1146
1147#if defined(HAVE_ACCEL)
1148
1149extern "C" void
1150cs_prefetch_d2h(void *ptr,
1151 size_t size);
1152
1153#else
1154
1155extern "C" inline void
1156cs_prefetch_d2h([[maybe_unused]] void *ptr,
1157 [[maybe_unused]] size_t size)
1158{
1159}
1160
1161#endif
1162
1163/*----------------------------------------------------------------------------*/
1168/*----------------------------------------------------------------------------*/
1169
1170#if defined(HAVE_ACCEL)
1171
1172extern "C" void
1174
1175#else
1176
1177extern "C" inline void
1179{
1180}
1181
1182#endif
1183
1184#if defined(HAVE_ACCEL)
1185
1186/*----------------------------------------------------------------------------*/
1187/*
1188 * \brief Activate device memory pool
1189 *
1190 * \param [in] status true to activate, false to deactivate
1191 */
1192/*----------------------------------------------------------------------------*/
1193
1194extern "C" void
1195cs_mem_device_pool_set_active(bool status);
1196
1197/*----------------------------------------------------------------------------*/
1198/*
1199 * \brief Set maximum allocation size for free memory pool.
1200 *
1201 * When the memory pool is active, memory that should be freed is transferred
1202 * to the memory pool instead, so it can be reused.
1203 * If > 0, this size corresponds to the maximum total size the pool will
1204 * manage before evicting blocks.
1205 *
1206 * \param [in] size maximum total allocation size in pool, or 0
1207 */
1208/*----------------------------------------------------------------------------*/
1209
1210extern "C" void
1211cs_mem_device_pool_set_max_capacity(size_t size);
1212
1213/*----------------------------------------------------------------------------*/
1214/*
1215 * \brief Set maximum number of tries before a block in memory pool is evicted
1216 *
1217 * \param [in] n_tries number of tries
1218 */
1219/*----------------------------------------------------------------------------*/
1220
1221extern "C" void
1222cs_mem_device_pool_set_max_tries(short int n_tries);
1223
1224/*----------------------------------------------------------------------------*/
1225/*
1226 * \brief Clear device memory pool if present
1227 */
1228/*----------------------------------------------------------------------------*/
1229
1230extern "C" void
1231cs_mem_device_pool_clear(void);
1232
1233#endif // defined(HAVE_ACCEL)
1234
1235/*----------------------------------------------------------------------------*/
1236
1237#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 CS_UNUSED(x)
Definition: cs_defs.h:518
@ p
Definition: cs_field_pointer.h:63
int cs_mem_have_memalign(void)
Indicate if a memory aligned allocation variant is available.
Definition: cs_mem.cpp:2339
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:2176
size_t cs_mem_size_max(void)
Return maximum theoretical dynamic memory allocated.
Definition: cs_mem.cpp:2261
int cs_mem_initialized(void)
Indicates if cs_mem_...() functions are initialized.
Definition: cs_mem.cpp:1928
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:1957
bft_error_handler_t * cs_mem_error_handler_get(void)
Returns the error handler associated with the cs_mem_...() functions.
Definition: cs_mem.cpp:2357
void cs_mem_init(const char *log_file_name)
Initialize memory handling.
Definition: cs_mem.cpp:1757
size_t cs_mem_size_current(void)
Return current theoretical dynamic memory allocated.
Definition: cs_mem.cpp:2247
void cs_mem_end(void)
End memory handling.
Definition: cs_mem.cpp:1851
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:2016
void cs_mem_error_handler_set(bft_error_handler_t *handler)
Definition: cs_mem.cpp:2376
void * cs_mem_free(void *ptr, const char *var_name, const char *file_name, int line_num)
Free allocated memory.
Definition: cs_mem.cpp:2109
#define cs_mem_advise_set_read_mostly(ptr)
Advise memory system that a given allocation will be mostly read.
Definition: cs_mem.h:883
#define cs_alloc_mode_device
Definition: cs_mem.h:185
#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:864
void cs_sync_d2h_if_needed_start(void *ptr)
Start synchronization of data from device to host, only if needed.
Definition: cs_mem.h:1099
void cs_prefetch_h2d(const void *ptr, size_t size)
Prefetch data from host to device.
Definition: cs_mem.h:1127
void cs_prefetch_d2h(void *ptr, size_t size)
Prefetch data from device to host.
Definition: cs_mem.h:1156
void cs_mem_advise_unset_read_mostly(void *ptr)
Advise memory system that a given allocation will be mostly read.
Definition: cs_mem.h:903
#define cs_associate_device_ptr(_host_ptr, _ni, _size)
Associate device memory with a given host memory pointer.
Definition: cs_mem.h:786
void * cs_get_device_ptr(void *ptr)
Return matching device pointer for a given pointer.
Definition: cs_mem.h:642
#define cs_set_alloc_mode(_host_ptr, mode)
Set allocation mode for an already allocated pointer.
Definition: cs_mem.h:833
#define cs_disassociate_device_ptr(_host_ptr)
Detach device memory from a given host memory pointer.
Definition: cs_mem.h:808
void cs_sync_h2d(const void *ptr)
Synchronize data from host to device.
Definition: cs_mem.h:935
#define cs_alloc_mode
Definition: cs_mem.h:183
bool cs_mem_is_device_ptr(const void *ptr)
Check if a pointer is a device (or shared) pointer.
Definition: cs_mem.h:753
#define cs_alloc_mode_read_mostly
Definition: cs_mem.h:184
void cs_sync_h2d_start(const void *ptr)
Start synchronization of data from host to device.
Definition: cs_mem.h:967
void cs_sync_d2h(void *ptr)
Synchronize data from device to host.
Definition: cs_mem.h:1000
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:2290
void cs_sync_d2h_start(void *ptr)
Start synchronization of data from device to host.
Definition: cs_mem.h:1033
void cs_sync_d2h_if_needed(void *ptr)
Synchronize data from device to host, only if needed.
Definition: cs_mem.h:1066
cs_alloc_mode_t cs_check_device_ptr(const void *ptr)
Check if a pointer is associated with a device.
Definition: cs_mem.h:727
const void * cs_get_device_ptr_const(const void *ptr)
Return matching device pointer for a given constant pointer.
Definition: cs_mem.h:687
cs_alloc_mode_t
Definition: cs_mem.h:46
@ CS_ALLOC_HOST
Definition: cs_mem.h:48
@ CS_ALLOC_HOST_DEVICE_PINNED
Definition: cs_mem.h:50
@ CS_ALLOC_HOST_DEVICE_SHARED
Definition: cs_mem.h:53
@ CS_ALLOC_HOST_DEVICE
Definition: cs_mem.h:49
@ CS_ALLOC_DEVICE
Definition: cs_mem.h:55
void cs_mem_hd_async_wait(void)
Wait for asynchronous memory operations between device and pinned host memory to finish.
Definition: cs_mem.h:1178