MLX C operates using several opaque C struct objects. Most objects follow a strict lifecycle: they must be created with a constructor function (e.g., mlx_array_new()) and must be manually released with a corresponding free function (e.g., mlx_array_free()). There must always be exactly one free call for every new call.
Key compute-related objects include:
mlx_array: The primary object holding data for computations.mlx_device: Defines the compute unit (e.g., CPU or GPU).mlx_stream: Ingests and executes array operations on a specific device.
Other data-holding objects include mlx_string, mlx_vector, mlx_map, mlx_closure, and mlx_distributed_group.
mlx_stream stream = mlx_default_gpu_stream_new();
mlx_array a = mlx_array_new_float(1.0);
mlx_array b = mlx_array_new_float(1.0);
mlx_array_add(&b, a, b, stream); // b now holds a+b=2
mlx_array_set(&a, b); // a now holds 3
mlx_array_free(a);
mlx_array_free(b);