Skip to content

Commit 626fa1d

Browse files
committed
metal : use virtual GPU address for private buffers
ggml-ci
1 parent a14bd35 commit 626fa1d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ggml/src/ggml-metal/ggml-metal.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#import <Metal/Metal.h>
1111

12+
#include <stdatomic.h>
13+
1214
#undef MIN
1315
#undef MAX
1416
#define MIN(a, b) ((a) < (b) ? (a) : (b))
@@ -6327,8 +6329,10 @@ static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_ba
63276329
ctx->all_data = ggml_metal_host_malloc(size_aligned);
63286330
ctx->is_shared = true;
63296331
} else {
6330-
// dummy, non-NULL value - we'll populate this after creating the Metal buffer below
6331-
ctx->all_data = (void *) 0x000000400ULL;
6332+
// virtual address for GPU memory allocations
6333+
static atomic_uintptr_t addr_device = 0x000000400ULL;
6334+
6335+
ctx->all_data = (void *) atomic_fetch_add_explicit(&addr_device, size_aligned, memory_order_relaxed);
63326336
ctx->is_shared = false;
63336337
}
63346338
ctx->all_size = size_aligned;
@@ -6351,7 +6355,10 @@ static ggml_backend_buffer_t ggml_backend_metal_buffer_type_alloc_buffer(ggml_ba
63516355
} else {
63526356
ctx->buffers[0].metal = [device newBufferWithLength:size_aligned options:MTLResourceStorageModePrivate];
63536357

6354-
ctx->all_data = (void *) (ctx->buffers[0].metal.gpuAddress);
6358+
// MTLBuffer.gpuAddress is not available on early OSes, so we use a virtual address instead
6359+
//if (@available(macOS 10.13, iOS 16.0, *)) {
6360+
// ctx->all_data = (void *) (ctx->buffers[0].metal.gpuAddress);
6361+
//}
63556362
}
63566363
}
63576364

0 commit comments

Comments
 (0)