Page-align buffers used by Metal
This commit is contained in:
parent
bc04508666
commit
a9d0bea047
2 changed files with 21 additions and 0 deletions
5
ggml.c
5
ggml.c
|
@ -20,6 +20,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
// if C99 - static_assert is noop
|
// if C99 - static_assert is noop
|
||||||
// ref: https://stackoverflow.com/a/53923785/4039976
|
// ref: https://stackoverflow.com/a/53923785/4039976
|
||||||
|
@ -121,7 +122,11 @@ typedef void* thread_ret_t;
|
||||||
#else
|
#else
|
||||||
inline static void* ggml_aligned_malloc(size_t size) {
|
inline static void* ggml_aligned_malloc(size_t size) {
|
||||||
void* aligned_memory = NULL;
|
void* aligned_memory = NULL;
|
||||||
|
#ifdef GGML_USE_METAL
|
||||||
|
int result = posix_memalign(&aligned_memory, getpagesize(), size);
|
||||||
|
#else
|
||||||
int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size);
|
int result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size);
|
||||||
|
#endif
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
// Handle allocation failure
|
// Handle allocation failure
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
16
llama-util.h
16
llama-util.h
|
@ -405,13 +405,29 @@ struct llama_buffer {
|
||||||
llama_buffer() = default;
|
llama_buffer() = default;
|
||||||
|
|
||||||
void resize(size_t len) {
|
void resize(size_t len) {
|
||||||
|
#ifdef GGML_USE_METAL
|
||||||
|
free(addr);
|
||||||
|
int result = posix_memalign((void **) &addr, getpagesize(), len);
|
||||||
|
if (result == 0) {
|
||||||
|
memset(addr, 0, len);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
addr = NULL;
|
||||||
|
}
|
||||||
|
#else
|
||||||
delete[] addr;
|
delete[] addr;
|
||||||
addr = new uint8_t[len];
|
addr = new uint8_t[len];
|
||||||
|
#endif
|
||||||
size = len;
|
size = len;
|
||||||
}
|
}
|
||||||
|
|
||||||
~llama_buffer() {
|
~llama_buffer() {
|
||||||
|
#ifdef GGML_USE_METAL
|
||||||
|
free(addr);
|
||||||
|
#else
|
||||||
delete[] addr;
|
delete[] addr;
|
||||||
|
#endif
|
||||||
|
addr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// disable copy and move
|
// disable copy and move
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue