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.2.tar.gz
(16.2 kB
view hashes)
Built Distributions
Close
Hashes for metalcompute-0.2.2-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b65405c23646af759e169cfb8ec7633f5d41886a70e45bccc2711ab9fd8dce1d |
|
MD5 | 1cb7d98c60ad76fc19865fb27aa17612 |
|
BLAKE2b-256 | b0c537f368c27a055625a5f706d879e5bde012fc87a87a20d1df68f0c5c65744 |
Close
Hashes for metalcompute-0.2.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f488515482ac465d07fa9a4007d1f98ad031c3a91c0dbe11f7edc4d9e7184520 |
|
MD5 | b46cbd1bc81509b4ec5b19512ae48447 |
|
BLAKE2b-256 | d585b4200bef33200133decbad43093fc5290d03ba4ae5d3f83d1be2b0585012 |
Close
Hashes for metalcompute-0.2.2-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62ca7f0ea68b2c8e5722dc2f7b7d7df883ca03e578fdf97cf61b7cd159889617 |
|
MD5 | 491b265bc0d583b9a5bf7b15589f7d73 |
|
BLAKE2b-256 | 21e20878cd7a3860daf415da0728ac73dd7b3c153f45c21632c25f8a10c5a38b |
Close
Hashes for metalcompute-0.2.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8173c01033f7bfbd82262c0b4842ab8f9c3c519c1e484d6df3364e7c72c80bd9 |
|
MD5 | b1bb94370373576a409c25f6afe16c80 |
|
BLAKE2b-256 | 5129643a98480eeae5d4926e06f630c45ebef99d0ced89c5420c17c5abe0f8cb |
Close
Hashes for metalcompute-0.2.2-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 699f773c4256610faf1d725f1e0ded67bc6f6a4ef4bed9644dc065f68e43d00a |
|
MD5 | da2ffc715366d29f8b0db8d8b86f3f2f |
|
BLAKE2b-256 | ce6ddc4a03a939cb7c63db02313d1d4c07eed367a536d2cf5d47844fd55dd5fe |
Close
Hashes for metalcompute-0.2.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51f3f654558f4ad478760bc4ef954f416f14edfec67ce9517b71902deb5aa292 |
|
MD5 | 1191f18d9d3af12e653c492961d0b9fe |
|
BLAKE2b-256 | 60d934debe7421c8885945c37bec5408d790e7befdbc1062947787067f2abac5 |