CK
← Back to C-Kernel-Engine Docs
Doxygen Source Documentation
Loading...
Searching...
No Matches
ckernel_alloc.h
Go to the documentation of this file.
1
#ifndef CKERNEL_ALLOC_H
2
#define CKERNEL_ALLOC_H
3
4
#include <stddef.h>
5
#include <stdint.h>
6
7
/**
8
* Allocate a large, contiguous memory region for model weights/activations.
9
*
10
* Implementation strategy:
11
* - Try to allocate 2MB-aligned memory backed by huge pages where possible.
12
* - Fall back to aligned_alloc + madvise(MADV_HUGEPAGE) when explicit
13
* huge pages are not available.
14
*
15
* Returns NULL on failure.
16
*/
17
void
*
ck_huge_alloc
(
size_t
bytes);
18
19
/**
20
* Free memory allocated by ck_huge_alloc.
21
*
22
* The bytes parameter should be the same size passed to ck_huge_alloc.
23
*/
24
void
ck_huge_free
(
void
*ptr,
size_t
bytes);
25
26
typedef
enum
{
27
CK_BUMP_MODE_UNINITIALIZED
= 0,
28
CK_BUMP_MODE_ANON
= 1,
29
CK_BUMP_MODE_MIXED_FILE_BACKED
= 2,
30
}
ck_bump_mode_t
;
31
32
typedef
struct
{
33
uint8_t *
base
;
34
size_t
total_size
;
35
size_t
weights_base
;
36
size_t
activations_base
;
37
size_t
mapped_len
;
38
size_t
weights_file_size
;
39
ck_bump_mode_t
mode
;
40
}
ck_bump_alloc_t
;
41
42
/*
43
* Initialize a logical bump arena.
44
*
45
* The allocator first tries the existing anonymous DRAM path. On Linux, if that
46
* fails, it can fall back to a mixed-backed arena where the weights region is
47
* file-backed from weights.bump and the runtime region is anonymous memory,
48
* while preserving one contiguous virtual address space.
49
*/
50
int
ck_bump_alloc_init
(
ck_bump_alloc_t
*alloc,
51
const
char
*weights_path,
52
size_t
total_size,
53
size_t
weights_base,
54
size_t
activations_base);
55
56
/*
57
* Release a logical bump arena previously initialized by ck_bump_alloc_init().
58
*/
59
void
ck_bump_alloc_free
(
ck_bump_alloc_t
*alloc);
60
61
/*
62
* Return non-zero when weights still need to be copied into RAM.
63
*/
64
int
ck_bump_alloc_needs_weight_materialization
(
const
ck_bump_alloc_t
*alloc);
65
66
#endif
/* CKERNEL_ALLOC_H */
ck_bump_alloc_free
void ck_bump_alloc_free(ck_bump_alloc_t *alloc)
Definition
ckernel_alloc.c:313
ck_bump_alloc_init
int ck_bump_alloc_init(ck_bump_alloc_t *alloc, const char *weights_path, size_t total_size, size_t weights_base, size_t activations_base)
Definition
ckernel_alloc.c:257
ck_bump_mode_t
ck_bump_mode_t
Definition
ckernel_alloc.h:26
CK_BUMP_MODE_ANON
@ CK_BUMP_MODE_ANON
Definition
ckernel_alloc.h:28
CK_BUMP_MODE_MIXED_FILE_BACKED
@ CK_BUMP_MODE_MIXED_FILE_BACKED
Definition
ckernel_alloc.h:29
CK_BUMP_MODE_UNINITIALIZED
@ CK_BUMP_MODE_UNINITIALIZED
Definition
ckernel_alloc.h:27
ck_huge_alloc
void * ck_huge_alloc(size_t bytes)
Definition
ckernel_alloc.c:70
ck_huge_free
void ck_huge_free(void *ptr, size_t bytes)
Definition
ckernel_alloc.c:104
ck_bump_alloc_needs_weight_materialization
int ck_bump_alloc_needs_weight_materialization(const ck_bump_alloc_t *alloc)
Definition
ckernel_alloc.c:331
ck_bump_alloc_t
Definition
ckernel_alloc.h:32
ck_bump_alloc_t::weights_base
size_t weights_base
Definition
ckernel_alloc.h:35
ck_bump_alloc_t::activations_base
size_t activations_base
Definition
ckernel_alloc.h:36
ck_bump_alloc_t::mapped_len
size_t mapped_len
Definition
ckernel_alloc.h:37
ck_bump_alloc_t::base
uint8_t * base
Definition
ckernel_alloc.h:33
ck_bump_alloc_t::weights_file_size
size_t weights_file_size
Definition
ckernel_alloc.h:38
ck_bump_alloc_t::total_size
size_t total_size
Definition
ckernel_alloc.h:34
ck_bump_alloc_t::mode
ck_bump_mode_t mode
Definition
ckernel_alloc.h:39
include
ckernel_alloc.h
Generated by
1.9.8