Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Imagegen MLX Dynamic

From Leeroopedia
Knowledge Sources
Domains Image Generation, Native Bindings
Last Updated 2025-02-15 00:00 GMT

Overview

Implements runtime dynamic loading of the MLX-C shared library via dlopen/dlsym, supporting macOS, Linux, and Windows.

Description

The mlx_dynamic.c file provides the dynamic library loading infrastructure for MLX-C. The mlx_dynamic_init function searches for the MLX-C shared library (libmlxc.dylib on macOS, libmlxc.so on Linux, libmlxc.dll on Windows) in multiple locations: executable directory, build directory, and system paths. On macOS, it uses _NSGetExecutablePath for relative path resolution. The library handle is stored statically and exposed via mlx_get_handle for the generated wrapper code to resolve function pointers via dlsym/GetProcAddress. Error handling preserves all tried paths for diagnostic messages. The file provides mlx_dynamic_cleanup for optional teardown at program exit.

Usage

Called once at program startup by the Go MLX initialization code (InitMLX) to load the MLX-C shared library before any MLX operations can be performed.

Code Reference

Source Location

  • Repository: Ollama
  • File: x/imagegen/mlx/mlx_dynamic.c
  • Lines: 1-144

Signature

int mlx_dynamic_init(void);
const char* mlx_dynamic_error(void);
int mlx_dynamic_is_initialized(void);
void* mlx_get_handle(void);
void mlx_dynamic_cleanup(void);

Import

#include "mlx_dynamic.h"

I/O Contract

Inputs

Name Type Required Description
(none) - - Searches system paths for MLX-C library automatically

Outputs

Name Type Description
return int 0 on success, -1 on failure (call mlx_dynamic_error() for details)

Usage Examples

if (mlx_dynamic_init() != 0) {
    fprintf(stderr, "MLX init failed: %s\n", mlx_dynamic_error());
    return -1;
}

// Library handle available for symbol resolution
void* handle = mlx_get_handle();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment