A python library to run metal compute kernels on macOS
Project description
metalcompute for Python
A python library to run metal compute kernels on macOS >= 11
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
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
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
Release history Release notifications | RSS feed
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.5.tar.gz
(16.5 kB
view hashes)
Built Distributions
Close
Hashes for metalcompute-0.2.5-pp310-pypy310_pp73-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e66c821d9cae4f0002645caa97c112e8c6f9fc24f2084ac9eb6183f36603c79b |
|
MD5 | 5c4760cce5a2b8590184bc7b76c4cf83 |
|
BLAKE2b-256 | 977418e84f374002aa68f877ec9e955cdae25f65c5ecee9b5411f4eb0996d1cf |
Close
Hashes for metalcompute-0.2.5-pp39-pypy39_pp73-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c929f942e71ec72bfce5e59c82a289876e0216dc654e2750858f55521e6dfd99 |
|
MD5 | 42830fae5541937d65ff9c99bd293c34 |
|
BLAKE2b-256 | 2cd16cef53e29925f62c21b4026c0809efe7781acf27276ce80112f4d58bf8ba |
Close
Hashes for metalcompute-0.2.5-pp38-pypy38_pp73-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 429814e55bf84276aa6432963bbddb7122f3809c2bc2f73abec214723b88a830 |
|
MD5 | e71d8a6afe5d5ee07eb61e879d518ade |
|
BLAKE2b-256 | 542f0354fd80500d7d2ac97d53c3218d4a1173a9a9b9b112c180e26298d3191d |
Close
Hashes for metalcompute-0.2.5-cp312-cp312-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a86f85326671f3ea463ce4f818c328963a3b6bacec627292391718016636600c |
|
MD5 | 99477ec9e42d03814493cc4f3b92f925 |
|
BLAKE2b-256 | c479568b6503eb22bce7d17b5810a7cb4f8ae71a1d11beb1930e91237d4f9d87 |
Close
Hashes for metalcompute-0.2.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a57cd4d4032807670ee83dc71583b6ee24f1bb825484b5e00c235488b84d6ad |
|
MD5 | 2b5f11b2e9617b437e01b9f651422bc4 |
|
BLAKE2b-256 | b03e4e2b1422c35e14ba20adf896401e3f4bd7a71c879a21a535f0ff15bcc62b |
Close
Hashes for metalcompute-0.2.5-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e82a4c6bf1776ff3882ac5a68e9e930be6750a49c18ca1b11d7bb696a375d53 |
|
MD5 | baeb68da71f5c3b6ffef5aa421c9ed06 |
|
BLAKE2b-256 | c848784873b634655fef9d10a6f4452fe405a927691b50cfe9f8f616db315af4 |
Close
Hashes for metalcompute-0.2.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c3c42f0429c6fb08ff53415d1243d4e130b39f71220076eeaded8326ad933fb |
|
MD5 | 1719170d9477064982438b4682aed22d |
|
BLAKE2b-256 | 1f74a96131bdd41ce05778da8eadfdec2c7480fc876e298da08caa25d2754d0e |
Close
Hashes for metalcompute-0.2.5-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c24cb4a51426d8523766ca642bdfc9b041103b2fe3a91e0e4f33999f2e8646e |
|
MD5 | 3fc4dcdcd32d65d59e061376a367f5b0 |
|
BLAKE2b-256 | 39b470a2e6edc9f8cfd7fc427facb29736b03348a1b7ee330a7a1530df8e15dc |
Close
Hashes for metalcompute-0.2.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e21f42b4318b498830c3acea74d9e9d0fea2cff492dcb4f713827f5fb06b4bd8 |
|
MD5 | 4cb547d38b05437e894dbc3a61a194e9 |
|
BLAKE2b-256 | 9c3e3dacc051e8c3d60413a69d9601316399d87c2a395934c9f23c949bec438e |
Close
Hashes for metalcompute-0.2.5-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30e0dd22269c285f2e1d6d73dbc9d56cb3cf956134d3d5abbdf10075b6892943 |
|
MD5 | ebbc95a2b40e29aa5bd261b5ee76e071 |
|
BLAKE2b-256 | 070aa37f8c3108dea4f460739f6ec0e86cb487ce324aa88139410b3cfd8fc640 |
Close
Hashes for metalcompute-0.2.5-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9402c3e30e73ede84dd3137d9b89ed1fbc80741c6fd8bff4ea3a209fb50f6260 |
|
MD5 | c8e8241849d6a9ab219cbf68b5907560 |
|
BLAKE2b-256 | 4a22be6387bcb7c5c7975197244c357d2788ac0396af40f26a82e866c9ec9678 |
Close
Hashes for metalcompute-0.2.5-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36ebc6861487e6570b08866bc8178ab2e5ceb72981fbdbe6d0ec8753b6bbc654 |
|
MD5 | 9b71dc6eab0ca7e0e022dca892c6d1a9 |
|
BLAKE2b-256 | b80cbc1da4f9a031e21e5107a1e9fb52c419bf8b97c3a09b2f4ff592759decdc |
Close
Hashes for metalcompute-0.2.5-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 642d4d9aa1c9eceedc31d4c30eb328bd6eb57ce91ff951b66a96c8e310f5077e |
|
MD5 | e4bb5b546f40dc400f524385acc13530 |
|
BLAKE2b-256 | 405c5710da700245aede33129116ece456975507703abdc459b4e4c4fdc0da0d |