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.3.tar.gz
(16.3 kB
view hashes)
Built Distributions
Close
Hashes for metalcompute-0.2.3-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1f7c6da52c3dbe8323d361a394e948c9f18f31efecddd9d64d2fc27d3a53a2b |
|
MD5 | 99c0ab6aab906fdcca013bdbc5c5cc7e |
|
BLAKE2b-256 | 537545a78a5769b8a0af5393b3d4869521a6e0cf589dbf0795e146a07cc8b527 |
Close
Hashes for metalcompute-0.2.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af8519c0da20e076650a1ba0347a623133bfc8e4e59f655d5e8230b1997dca2b |
|
MD5 | 3598ac7537016c54c9d57a47d20a08e2 |
|
BLAKE2b-256 | e52a65d49145e7eb779169adfbf918db1c77782631af6676c35a53a2790ce458 |
Close
Hashes for metalcompute-0.2.3-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad69a8abb1cfc5ca697fc095fcf49525c09b6260bfe7cdc8a10d70f1885f0c8f |
|
MD5 | 56261d7b6fd66b3c2254127ec3d1a941 |
|
BLAKE2b-256 | 64d0ac41076841da26ba88bddaca9b8f7d51bdaaf05f28efb644160235e67f28 |
Close
Hashes for metalcompute-0.2.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ce73c57536cf865148369a6c3faae7106ec6e460f033fc07362c50749dbcdee |
|
MD5 | f43c1e36b39cca040230cf240ab6ec67 |
|
BLAKE2b-256 | 79dd499e7a7994483b54e8e668ef389cc9118726c7862ed0426c2f41f722f8fa |
Close
Hashes for metalcompute-0.2.3-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24fcfbb587f8d31f6a1837cf7802340025ee41699f88a8629e505b9343b49c79 |
|
MD5 | 58c2b7891749b62086f8916e9a55d47f |
|
BLAKE2b-256 | 4945d5cfdfcf1675a53504e167de734dcff676d8265e0834d0accf93625a2163 |
Close
Hashes for metalcompute-0.2.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cf1b43728c4bf01683d125aecf0fed8d7c5a78a8a49ae76753e6f052fe9bc05 |
|
MD5 | 11d69e6c3123c8297060da0819126b53 |
|
BLAKE2b-256 | 9396f66bed837c9ac0452f82a3adafed971425dd375f64397b85852def7871da |