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.0.tar.gz
(16.2 kB
view hashes)
Built Distributions
Close
Hashes for metalcompute-0.2.0-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa145feb5e79432cebfa5c98dafdee0f9e58d7a34af0797b66d70851ac314515 |
|
MD5 | bcf37cb38028edbe59107331b9ffdce9 |
|
BLAKE2b-256 | ce8178b88bc9dff22cf0ff481e3885f31a52cb2b612927a71aa900d2fca07371 |
Close
Hashes for metalcompute-0.2.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24e2a98e4779ade823b33f7deecd656eac303af9ec391c0ed35062fb6ebae1c9 |
|
MD5 | 57ac8ae0ac73612a42a5b6d32f34f594 |
|
BLAKE2b-256 | 097cca266538980ac276bb9a464795b36266184322f612db2c24357d6917a590 |
Close
Hashes for metalcompute-0.2.0-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9e1a5e81f86ccd6c8393bd17946b40d8f7d51eebca9e97706c5b564f52e0001 |
|
MD5 | 361827a55a71942479433fc24ec3ddbc |
|
BLAKE2b-256 | 0e5ccf68aa392aa5dad9a4a777d5983c04ddce5f1b47a59827545f86fe8c17d3 |
Close
Hashes for metalcompute-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2554a8da837f25610828025dc36f4d82144308b87ec1535dce998b0c4e3c125 |
|
MD5 | 1abd770b3c7c6230e66f65a38797805f |
|
BLAKE2b-256 | 25c156ec38db3e741768fcbaff343aadfef858670a451dbf1efc3ad766b9236f |
Close
Hashes for metalcompute-0.2.0-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b14c25820834472c3dd0b4e4480d1c70d04f5eb72c825202c93bca26b06f8f6f |
|
MD5 | b8d6820ca08f901f9ac30f1b4fc9ea74 |
|
BLAKE2b-256 | 49d9cd9e875b42b2bd9090c777ec208ac805f3ff4475d01973e959bf064b00af |
Close
Hashes for metalcompute-0.2.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbd9b379fb06fc0d53adb299cbf6c496f0de579977d5d3ee09938425c08fa6cc |
|
MD5 | 4fb00fcd9824fb9a0c242f2d2ebaf3a5 |
|
BLAKE2b-256 | 932d0ac4ba66f1cf6a3a5e79b11de524dc45190e8e4e47b239741304655f0368 |