MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / ggml_aligned_malloc

Function ggml_aligned_malloc

subprojects/llama.cpp/ggml/src/ggml.c:316–370  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

314
315
316void * ggml_aligned_malloc(size_t size) {
317#if defined(__s390x__)
318 const int alignment = 256;
319#else
320 const int alignment = 64;
321#endif
322
323#if defined(_MSC_VER) || defined(__MINGW32__)
324 return _aligned_malloc(size, alignment);
325#else
326 if (size == 0) {
327 GGML_LOG_WARN("Behavior may be unexpected when allocating 0 bytes for ggml_aligned_malloc!\n");
328 return NULL;
329 }
330 void * aligned_memory = NULL;
331 #ifdef GGML_USE_CPU_HBM
332 int result = hbw_posix_memalign(&aligned_memory, alignment, size);
333 #elif TARGET_OS_OSX
334 GGML_UNUSED(alignment);
335 kern_return_t alloc_status = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t *) &aligned_memory, size, VM_FLAGS_ANYWHERE);
336 int result = EFAULT;
337 switch (alloc_status) {
338 case KERN_SUCCESS:
339 result = 0;
340 break;
341 case KERN_INVALID_ADDRESS:
342 result = EINVAL;
343 break;
344 case KERN_NO_SPACE:
345 result = ENOMEM;
346 break;
347 default:
348 result = EFAULT;
349 break;
350 }
351 #else
352 int result = posix_memalign(&aligned_memory, alignment, size);
353 #endif
354 if (result != 0) {
355 // Handle allocation failure
356 const char *error_desc = "unknown allocation error";
357 switch (result) {
358 case EINVAL:
359 error_desc = "invalid alignment value";
360 break;
361 case ENOMEM:
362 error_desc = "insufficient memory";
363 break;
364 }
365 GGML_LOG_ERROR("%s: %s (attempted to allocate %6.2f MB)\n", __func__, error_desc, size/(1024.0*1024.0));
366 return NULL;
367 }
368 return aligned_memory;
369#endif
370}
371
372void ggml_aligned_free(void * ptr, size_t size) {
373 GGML_UNUSED(size);

Callers 11

ggml_initFunction · 0.85
repack_q4_0_q4x4x2Function · 0.85
repack_q4x4x2_q4_0Function · 0.85
repack_q8_0_q8x4x2Function · 0.85
repack_q8x4x2_q8_0Function · 0.85
repack_mxfp4_mxfp4x4x2Function · 0.85
repack_mxfp4x4x2_mxfp4Function · 0.85
ggml_threadpool_new_implFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected