Skip to main content

A python library to run metal compute kernels on macOS

Project description

metalcompute for Python

Build status

A python library to run metal compute kernels on macOS >= 14

Installations

Install latest stable release from PyPI:

> python3 -m pip install metalcompute

Install latest unstable version from Github:

> python3 -m pip install git+https://github.com/baldand/py-metal-compute.git

Install locally from source:

> python3 -m pip install .

Basic test

Example execution from M1-based Mac running macOS 12:

> python3 tests/basic.py
Calculating sin of 1234567 values
Expected value: 0.9805107116699219 Received value: 0.9807852506637573
Metal compute took: 0.0040209293365478516 s
Reference compute took: 0.1068720817565918 s

Interface

import metalcompute as mc

devices = mc.get_devices()
# Get list of available Metal devices

dev = mc.Device() 
# Call before use. Will open default Metal device
# or to pick a specific device:
# mc.Device(device_index)

program = """
#include <metal_stdlib>
using namespace metal;

kernel void test(const device float *in [[ buffer(0) ]],
                device float  *out [[ buffer(1) ]],
                uint id [[ thread_position_in_grid ]]) {
    out[id] = sin(in[id]);
}
"""
function_name = "test"

kernel_fn = dev.kernel(program).function(function_name)
# Will raise exception with details if metal kernel has errors

buf_0 = array('f',[1.0,3.14159]) # Any python buffer object
buf_n = dev.buffer(out_size) 
# Allocate metal buffers for input and output (must be compatible with kernel)
# Input buffers can be dev.buffer or python buffers (will be copied)
# Output buffers must be dev.buffer
# Buffer objects support python buffer protocol
# Can be modified or read using e.g. memoryview, numpy.frombuffer

kernel_fn(kernel_call_count, buf_0, ..., buf_n)
# Run the kernel once with supplied input data, 
# filling supplied output data
# Specify number of kernel calls
# Will block until data available

handle = kernel_fn(kernel_call_count, buf_0, ..., buf_n)
# Run the kernel once, 
# Specify number of kernel calls
# Supply all needed buffers
# Will return immediately, before kernel runs, 
# allowing additional kernels to be queued
# Do not modify or read buffers until kernel completed!

del handle
# Block until previously queued kernel has completed

Examples

Measure TFLOPS of GPU

> metalcompute-measure
Using device: Apple M1 (unified memory=True)
Running compute intensive Metal kernel to measure TFLOPS...
Estimated GPU TFLOPS: 2.53236
Running compute intensive Metal kernel to measure data transfer rate...
Data transfer rate: 58.7291 GB/s

Render a 3D image with raymarching

# Usage: metalcompute-raymarch [-width <width>] [-height <height>] [-outname <output image file: PNG, JPG>]

> metalcompute-raymarch.py -width 1024 -height 1024 -outname raymarch.jpg
Render took 0.0119569s

Raymarched spheres scene

Mandelbrot set

# Usage: metalcompute-mandelbrot [-width <width>] [-height <height>] [-outname <output image file: PNG, JPG>]

> metalcompute-mandelbrot
Rendering mandelbrot set using Metal compute, res:4096x4096, iters:8192
Render took 0.401446s
Writing image to mandelbrot.png
Image encoding took 1.35182s

Mandelbrot set

Livecoding visual kernels in VSCode

There is an example script to allow livecoding of visual metal kernels entirely within VSCode using a localhost http server to render frames.

It also includes syntax error highlighting in the editor.

See livemetal.py

Status

This is a preview version.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metalcompute-0.2.9.tar.gz (16.2 kB view details)

Uploaded Source

Built Distributions

metalcompute-0.2.9-pp310-pypy310_pp73-macosx_14_0_arm64.whl (89.9 kB view details)

Uploaded PyPymacOS 14.0+ ARM64

metalcompute-0.2.9-pp39-pypy39_pp73-macosx_14_0_arm64.whl (90.0 kB view details)

Uploaded PyPymacOS 14.0+ ARM64

metalcompute-0.2.9-cp313-cp313-macosx_14_0_arm64.whl (91.5 kB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

metalcompute-0.2.9-cp312-cp312-macosx_14_0_arm64.whl (91.5 kB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

metalcompute-0.2.9-cp311-cp311-macosx_14_0_arm64.whl (91.3 kB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

metalcompute-0.2.9-cp310-cp310-macosx_14_0_arm64.whl (91.3 kB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

metalcompute-0.2.9-cp39-cp39-macosx_14_0_arm64.whl (91.3 kB view details)

Uploaded CPython 3.9macOS 14.0+ ARM64

File details

Details for the file metalcompute-0.2.9.tar.gz.

File metadata

  • Download URL: metalcompute-0.2.9.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for metalcompute-0.2.9.tar.gz
Algorithm Hash digest
SHA256 8e06ee4616cf3aa26384d8ff10e910c19ae4d8bc85a9ae2f202f5134729c46c6
MD5 23acf50fad0a09974a06e909ca8738ae
BLAKE2b-256 62d3b6b2535c0986145220500f94176a4671fb08cb0ee05ff1f59f3d234a2347

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-pp310-pypy310_pp73-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-pp310-pypy310_pp73-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 2deac1bd59a0e5d6d9df40d2a66cd5a574ce4d5ceb14c7ccaa01bfb7c78dee6d
MD5 753d947706f590e8674cd48be415fc55
BLAKE2b-256 30e7095e2c054851d05235c63b434b4dfde37a7cb1075893f36998e9ce75f870

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-pp39-pypy39_pp73-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-pp39-pypy39_pp73-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 43a546922787a374ebfdcff5bcfe488f52aa4a4d7681164d05402a53f62cd275
MD5 c6b2b7bff5940106c8c3e79852b2822b
BLAKE2b-256 af3f969677f05ed7211504a1c15d367ab8625736a1f11badf9d5af8db89c7a0e

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 2b2aa3d11b85a0379bd4c6b7339e68c4cb9bbb21731caf9493c2d128dcfe726c
MD5 2b3ee447e22f4b51a1b8fcb1372b04a3
BLAKE2b-256 86022ef422d069a5084a778604e601753dd48507393360e7451147e8f6a7c842

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 cc6efde5c5368871fbb2dad08ca7c7a57a7ad831c364630beed1ac5632ae332a
MD5 b0f53488c6eb09ae0f195778ce4f7c6f
BLAKE2b-256 b3e33de38fe057a7363453fc22cf23c53aa33ddcf28298fb5245297fdf357b5f

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 5a01d071e7e3e1c7e34a43271386d34365bc7dc871fbf388b400184f40f49272
MD5 50c1381d0d5d9a1aaaa915f5b9a25662
BLAKE2b-256 30ae3780fdd20265222349cba0fe4ac748e4608b431b54b643674d5a6bf8fa8e

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 649b5fe0ca064f31318a16f1a303ec93f9a260786a7f852e16f16626e59ebcbb
MD5 4172fb9ffcb1d6d3e5315eb1e29a5877
BLAKE2b-256 3074de2850964a062e608e7822487eee487396f3c3da89e47001c9524755846e

See more details on using hashes here.

File details

Details for the file metalcompute-0.2.9-cp39-cp39-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for metalcompute-0.2.9-cp39-cp39-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 8d12b27fc59fd0c943bb733e7ec689a274e8be4eff787433709fc9211b27db2a
MD5 d7099642de938ef3bbfa439f3276f6b9
BLAKE2b-256 4ae7626e0b3451e6ac2cf4bcbb7472b9dd085965e0f38213bdaa12d520d2200d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page