Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .github/scripts/setup+build-linux-container-cmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
set -ex

# [Setup] Install dependencies inside the container.
# Note: mlx-c requires a C++20 toolchain with <format> support:
# - libstdc++ >= 13 (Ubuntu noble, Fedora 39+)
# - or libc++ >= 17
# Debian bookworm (libstdc++ 12) is *not* sufficient.
if command -v apt-get >/dev/null 2>&1; then
apt-get update -y
apt-get install -y \
Expand All @@ -12,6 +16,9 @@ if command -v apt-get >/dev/null 2>&1; then
liblapacke-dev \
libopenblas-dev

export CC=/usr/bin/gcc
export CXX=/usr/bin/g++

elif command -v dnf >/dev/null 2>&1; then
dnf update -y
dnf install -y \
Expand All @@ -23,6 +30,11 @@ elif command -v dnf >/dev/null 2>&1; then
clang \
ninja-build

# Fedora's default gcc (13+) has <format>; keep it as the compiler so this
# build keeps matching what has been passing in CI.
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++

else
echo "No supported package manager found (apt-get, dnf)"
exit 1
Expand All @@ -31,8 +43,8 @@ fi
# [CMake] CI Build Sanity Check: Verifies code compilation, not for release.
export CMAKE_ARGS="-DCMAKE_COMPILE_WARNING_AS_ERROR=ON"
export DEBUG=1
export CMAKE_C_COMPILER=/usr/bin/clang
export CMAKE_CXX_COMPILER=/usr/bin/clang++

"$CXX" --version

rm -rf build
mkdir -p build
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,20 @@ jobs:
matrix:
include:
# See https://github.com/swiftlang/swift-docker/tree/main/
#
# Note: mlx-c uses std::format, which requires libstdc++ >= 13 (or
# libc++ >= 17). Debian bookworm only ships gcc/libstdc++ 12 (and
# gcc-13 is not available in bookworm-backports), so use Ubuntu
# noble (24.04, gcc/libstdc++ 13) for the Debian-family build.
- host: ubuntu-22.04
arch: x86_64
container: swift:bookworm
container: swift:noble
- host: ubuntu-22.04
arch: x86_64
container: swift:fedora39
- host: ubuntu-22.04-arm
arch: aarch64
container: swift:bookworm
container: swift:noble
- host: ubuntu-22.04-arm
arch: aarch64
container: swift:fedora39
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ endif()
FetchContent_Declare(
mlx-c
GIT_REPOSITORY "https://github.com/ml-explore/mlx-c.git"
GIT_TAG "c74db5307cc8ce122f48d97ef951b30578674e7f")
GIT_TAG "ebc88f10caa1b625e6b581437a8dea6df8a70085")
FetchContent_MakeAvailable(mlx-c)

# swift-numerics
Expand Down
2 changes: 1 addition & 1 deletion Source/Cmlx/include-framework/Metal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Metal.hpp.in
//
// Autogenerated from commit 68ad1c14e2bae5b8cca5aefe3a5eb91650c0147a.
// Autogenerated from commit 8f784840de21cacd0c5c237ea860179e47dcec91.
//
// Copyright 2020-2024 Apple Inc.
//
Expand Down
7 changes: 6 additions & 1 deletion Source/Cmlx/include-framework/mlx-c-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stddef.h>

#include <Cmlx/mlx-c-string.h>
#include <Cmlx/mlx-c-vector.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -26,6 +25,12 @@ typedef struct mlx_device_ {
void* ctx;
} mlx_device;

/**
* A MLX vector of strings.
* Forward declaration to avoid circular references.
*/
typedef struct mlx_vector_string_ mlx_vector_string;

/**
* Device type.
*/
Expand Down
8 changes: 4 additions & 4 deletions Source/Cmlx/include-framework/mlx-c-io_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ typedef struct mlx_io_vtable_ {
bool (*is_open)(void*);
bool (*good)(void*);
size_t (*tell)(void*);
void (*seek)(void*, int64_t off, int whence);
void (*read)(void*, char* data, size_t n);
void (*read_at_offset)(void*, char* data, size_t n, size_t off);
void (*write)(void*, const char* data, size_t n);
int (*seek)(void*, int64_t off, int whence);
size_t (*read)(void*, char* data, size_t n);
size_t (*read_at_offset)(void*, char* data, size_t n, size_t off);
size_t (*write)(void*, const char* data, size_t n);
const char* (*label)(void*);
void (*free)(void*);
} mlx_io_vtable;
Expand Down
59 changes: 59 additions & 0 deletions Source/Cmlx/include-framework/mlx-c-stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define MLX_STREAM_H

#include <stdbool.h>
#include <stddef.h>

#include <Cmlx/mlx-c-device.h>

Expand All @@ -24,6 +25,22 @@ typedef struct mlx_stream_ {
void* ctx;
} mlx_stream;

/**
* A per-thread stream object.
*
* The same thread-local stream can be used from any number of threads; the
* stream's GPU command encoder is registered once per thread.
*/
typedef struct mlx_stream_thread_local_ {
void* ctx;
} mlx_stream_thread_local;

/**
* A vector of mlx_stream.
* Forward declaration to avoid circular references.
*/
typedef struct mlx_vector_stream_ mlx_vector_stream;

/**
* Returns a new empty stream.
*/
Expand All @@ -45,6 +62,36 @@ mlx_stream mlx_stream_new_device(mlx_device dev);
* responsibility to ensure there are no data races on them.
*/
mlx_stream mlx_stream_new_thread_unsafe(mlx_device dev);

/**
* Returns a new per-thread stream on a device.
*
* Streams created this way are unique per thread: evaluating the same
* `mlx_stream_thread_local` from different threads yields distinct streams.
*/
mlx_stream_thread_local mlx_stream_thread_local_new(mlx_device dev);
/**
* Set a per-thread stream to the provided src stream.
*/
int mlx_stream_thread_local_set(
mlx_stream_thread_local* tls,
const mlx_stream_thread_local src);
/**
* Free a per-thread stream.
*/
int mlx_stream_thread_local_free(mlx_stream_thread_local tls);
/**
* Returns the concrete stream backing a per-thread stream on the current
* thread.
*/
int mlx_stream_from_thread_local(
mlx_stream* res,
const mlx_stream_thread_local tls);

/**
* Get the vector of available streams.
*/
int mlx_get_streams(mlx_vector_stream* res);
/**
* Set stream to provided src stream.
*/
Expand Down Expand Up @@ -73,6 +120,18 @@ int mlx_stream_get_index(int* index, mlx_stream stream);
* Synchronize with the provided stream.
*/
int mlx_synchronize(mlx_stream stream);
/**
* Synchronize with the default stream.
*/
int mlx_synchronize_default(void);
/**
* Synchronize with the stream corresponding to the current thread.
*/
int mlx_synchronize_thread_local(mlx_stream_thread_local tls);
/**
* Destroy all streams created in the current thread.
*/
int mlx_clear_streams(void);
/**
* Returns the default stream on the given device.
*/
Expand Down
30 changes: 30 additions & 0 deletions Source/Cmlx/include-framework/mlx-c-vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define MLX_VECTOR_H

#include <Cmlx/mlx-c-array.h>
#include <Cmlx/mlx-c-stream.h>
#include <Cmlx/mlx-c-string.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -124,6 +125,35 @@ int mlx_vector_string_append_value(mlx_vector_string vec, const char* val);
size_t mlx_vector_string_size(mlx_vector_string vec);
int mlx_vector_string_get(char** res, const mlx_vector_string vec, size_t idx);

/**
* A vector of stream.
*/
typedef struct mlx_vector_stream_ {
void* ctx;
} mlx_vector_stream;
mlx_vector_stream mlx_vector_stream_new(void);
int mlx_vector_stream_set(mlx_vector_stream* vec, const mlx_vector_stream src);
int mlx_vector_stream_free(mlx_vector_stream vec);
mlx_vector_stream mlx_vector_stream_new_data(
const mlx_stream* data,
size_t size);
mlx_vector_stream mlx_vector_stream_new_value(const mlx_stream val);
int mlx_vector_stream_set_data(
mlx_vector_stream* vec,
const mlx_stream* data,
size_t size);
int mlx_vector_stream_set_value(mlx_vector_stream* vec, const mlx_stream val);
int mlx_vector_stream_append_data(
mlx_vector_stream vec,
const mlx_stream* data,
size_t size);
int mlx_vector_stream_append_value(mlx_vector_stream vec, const mlx_stream val);
size_t mlx_vector_stream_size(mlx_vector_stream vec);
int mlx_vector_stream_get(
mlx_stream* res,
const mlx_vector_stream vec,
size_t idx);

/**@}*/

#ifdef __cplusplus
Expand Down
7 changes: 6 additions & 1 deletion Source/Cmlx/include/mlx/c/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stddef.h>

#include "mlx/c/string.h"
#include "mlx/c/vector.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -26,6 +25,12 @@ typedef struct mlx_device_ {
void* ctx;
} mlx_device;

/**
* A MLX vector of strings.
* Forward declaration to avoid circular references.
*/
typedef struct mlx_vector_string_ mlx_vector_string;

/**
* Device type.
*/
Expand Down
8 changes: 4 additions & 4 deletions Source/Cmlx/include/mlx/c/io_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ typedef struct mlx_io_vtable_ {
bool (*is_open)(void*);
bool (*good)(void*);
size_t (*tell)(void*);
void (*seek)(void*, int64_t off, int whence);
void (*read)(void*, char* data, size_t n);
void (*read_at_offset)(void*, char* data, size_t n, size_t off);
void (*write)(void*, const char* data, size_t n);
int (*seek)(void*, int64_t off, int whence);
size_t (*read)(void*, char* data, size_t n);
size_t (*read_at_offset)(void*, char* data, size_t n, size_t off);
size_t (*write)(void*, const char* data, size_t n);
const char* (*label)(void*);
void (*free)(void*);
} mlx_io_vtable;
Expand Down
59 changes: 59 additions & 0 deletions Source/Cmlx/include/mlx/c/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define MLX_STREAM_H

#include <stdbool.h>
#include <stddef.h>

#include "mlx/c/device.h"

Expand All @@ -24,6 +25,22 @@ typedef struct mlx_stream_ {
void* ctx;
} mlx_stream;

/**
* A per-thread stream object.
*
* The same thread-local stream can be used from any number of threads; the
* stream's GPU command encoder is registered once per thread.
*/
typedef struct mlx_stream_thread_local_ {
void* ctx;
} mlx_stream_thread_local;

/**
* A vector of mlx_stream.
* Forward declaration to avoid circular references.
*/
typedef struct mlx_vector_stream_ mlx_vector_stream;

/**
* Returns a new empty stream.
*/
Expand All @@ -45,6 +62,36 @@ mlx_stream mlx_stream_new_device(mlx_device dev);
* responsibility to ensure there are no data races on them.
*/
mlx_stream mlx_stream_new_thread_unsafe(mlx_device dev);

/**
* Returns a new per-thread stream on a device.
*
* Streams created this way are unique per thread: evaluating the same
* `mlx_stream_thread_local` from different threads yields distinct streams.
*/
mlx_stream_thread_local mlx_stream_thread_local_new(mlx_device dev);
/**
* Set a per-thread stream to the provided src stream.
*/
int mlx_stream_thread_local_set(
mlx_stream_thread_local* tls,
const mlx_stream_thread_local src);
/**
* Free a per-thread stream.
*/
int mlx_stream_thread_local_free(mlx_stream_thread_local tls);
/**
* Returns the concrete stream backing a per-thread stream on the current
* thread.
*/
int mlx_stream_from_thread_local(
mlx_stream* res,
const mlx_stream_thread_local tls);

/**
* Get the vector of available streams.
*/
int mlx_get_streams(mlx_vector_stream* res);
/**
* Set stream to provided src stream.
*/
Expand Down Expand Up @@ -73,6 +120,18 @@ int mlx_stream_get_index(int* index, mlx_stream stream);
* Synchronize with the provided stream.
*/
int mlx_synchronize(mlx_stream stream);
/**
* Synchronize with the default stream.
*/
int mlx_synchronize_default(void);
/**
* Synchronize with the stream corresponding to the current thread.
*/
int mlx_synchronize_thread_local(mlx_stream_thread_local tls);
/**
* Destroy all streams created in the current thread.
*/
int mlx_clear_streams(void);
/**
* Returns the default stream on the given device.
*/
Expand Down
Loading
Loading