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
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.1.tar.gz
(16.2 kB
view hashes)
Built Distributions
Close
Hashes for metalcompute-0.2.1-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e52c9fdc05b906fd037d8ec9efc4659824ab10d1b7833c01f5309776e7023287 |
|
MD5 | 6609dd151955421fd687831952c30442 |
|
BLAKE2b-256 | e5b8fc11cade4e4aa6d9de66889ad67994bfdf83963f5f085f183c0b634edf0b |
Close
Hashes for metalcompute-0.2.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1df0d28b39b8651938d5adcdcb49c7e701f42caabdd274ea92f1d0ebe10ce5e5 |
|
MD5 | 684885e90b79e5740d4c882b9f967fa3 |
|
BLAKE2b-256 | 562f075eb4e80550a99c64dc8b223ce06f12828e169cabdd20c4a408e7ca8d6b |
Close
Hashes for metalcompute-0.2.1-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6f06f396da598a3bc39b30970fc31d9cbb7aeaedb72ade62285b30ecc4ba1d9 |
|
MD5 | e18dbd40fffc1270ea791e353fdd0862 |
|
BLAKE2b-256 | fb34a9ce2be35eca11c9d37fdede011c6978fcc5f13cd72f6f73202fe28ad1b2 |
Close
Hashes for metalcompute-0.2.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 771f199e3a112123126883252f83e60340b9ef8380ae0ba8a7200f27bec5aef1 |
|
MD5 | ca6c6e6fba22f485355816d10d3cf1b2 |
|
BLAKE2b-256 | 33c7c6dd1712caa6ca871b5c3164c1a0e9ae697722027063c1e9b458e1ab1931 |
Close
Hashes for metalcompute-0.2.1-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c4e80318fec0876127ec7f4d50a506bff2cb61cbf5d7879ffb830934492d6f6 |
|
MD5 | e09f74314e706864e3cfc414349ed1c7 |
|
BLAKE2b-256 | d4974413540a72afd356df613b6f202b6b56347a430c4824349b55c0abaa0a9f |
Close
Hashes for metalcompute-0.2.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbbbac189f952ab6da99405394f3237a4768ae52c392ad8ca3f07767cbfe7267 |
|
MD5 | d6eed6a4f621dc556ecbb3f3cc599cb9 |
|
BLAKE2b-256 | 05782a77f8b380ae55ac674fc0a4e70c2254db7178b8791e99b0cbc544fd7b98 |