Skip to content

Commit 6c24870

Browse files
authoredApr 13, 2023
ggml : introduce GGML_ALIGNED_MALLOC/GGML_ALIGNED_FREE macros (#884)
which allows us to use aligned_alloc or _aligned_malloc functions
1 parent 8cda5c9 commit 6c24870

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed
 

‎ggml.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ typedef void* thread_ret_t;
114114
#define GGML_MEM_ALIGN 16
115115
#endif
116116

117+
#if defined(_MSC_VER) || defined(__MINGW32__)
118+
#define GGML_ALIGNED_MALLOC(size) _aligned_malloc(size, GGML_MEM_ALIGN)
119+
#define GGML_ALIGNED_FREE(ptr) _aligned_free(ptr)
120+
#else
121+
#define GGML_ALIGNED_MALLOC(size) aligned_alloc(GGML_MEM_ALIGN, size)
122+
#define GGML_ALIGNED_FREE(ptr) free(ptr)
123+
#endif
124+
117125
#define UNUSED(x) (void)(x)
118126
#define SWAP(x, y, T) do { T SWAP = x; x = y; y = SWAP; } while (0)
119127

@@ -2966,7 +2974,7 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
29662974

29672975
*ctx = (struct ggml_context) {
29682976
/*.mem_size =*/ params.mem_size,
2969-
/*.mem_buffer =*/ params.mem_buffer ? params.mem_buffer : malloc(params.mem_size),
2977+
/*.mem_buffer =*/ params.mem_buffer ? params.mem_buffer : GGML_ALIGNED_MALLOC(params.mem_size),
29702978
/*.mem_buffer_owned =*/ params.mem_buffer ? false : true,
29712979
/*.no_alloc =*/ params.no_alloc,
29722980
/*.n_objects =*/ 0,
@@ -3001,7 +3009,7 @@ void ggml_free(struct ggml_context * ctx) {
30013009
__func__, i, ctx->n_objects, ctx->objects_end->offs + ctx->objects_end->size);
30023010

30033011
if (ctx->mem_buffer_owned) {
3004-
free(ctx->mem_buffer);
3012+
GGML_ALIGNED_FREE(ctx->mem_buffer);
30053013
}
30063014

30073015
found = true;

1 commit comments

Comments
 (1)

nnnn20430 commented on Apr 15, 2023

@nnnn20430

This seems to have broken build in termux, because termux min sdk is 24 and bionic doesn't support aligned_alloc in that version

Please sign in to comment.