An open API service indexing awesome lists of open source software.

https://github.com/yhirose/mtlcpp

Utilities for Metal-cpp
https://github.com/yhirose/mtlcpp

apple-silicon cpp cpp20 header-only m1-mac metal metal-cpp

Last synced: 6 months ago
JSON representation

Utilities for Metal-cpp

Awesome Lists containing this project

README

          

mtlcpp
======

A header-only C++20 linear algebra library for Metal on MacOS

* This project is still in development and is far from reaching the first alpha version :)
* Data types supported in this library are `int` and `float` only, since Metal doesn't support `double`
* This library uses GPU cores in Apple M1 chip with [Metal-cpp](https://developer.apple.com/metal/cpp/)

Build and run unit tests and benchmark
--------------------------------------

* Install Xcode Command Line Tools
* Run the following commands in Terminal

```bash
cd test
make
```

Benchmark as of 3/2/2024 on M1 MacBook Pro 14
---------------------------------------------

| ns/op | op/s | benchmark
|--------------------:|--------------------:|:----------
| 150,856,709.00 | 6.63 | CPU: `a + b`
| 2,262,442.07 | 442.00 | GPU: `a + b`
| 1,351,401.59 | 739.97 | Eigen: `a + b`
| 964,220,500.00 | 1.04 | CPU: `a.dot(b)`
| 1,094,602.35 | 913.57 | GPU: `a.dot(b)`
| 3,002,299.36 | 333.08 | Eigen: `a * b`

```cpp
// test/bench.cpp

// `add` benchmark
const size_t n = 10'000'000;

auto a = mtl::ones({n});
auto b = mtl::ones({n});
auto c = mtl::array();

mtl::use_cpu();
Bench().run("CPU: a + b", [&] {
c = a + b;
});

mtl::use_gpu();
Bench().run("GPU: a + b", [&] {
c = a + b;
});

auto aa = Eigen::Vector::Ones(n);
auto bb = Eigen::Vector::Ones(n);
auto cc = Eigen::Vector(n);

Bench().run("Eigen: a + b", [&] {
cc = aa + bb;
});

// `dot` benchmark
auto a = mtl::ones({1000, 1000});
auto b = mtl::ones({1000, 100});
auto c = mtl::array();

mtl::use_cpu();
Bench().run("CPU: a.dot(b)", [&] {
c = a.dot(b);
});

mtl::use_gpu();
Bench().run("GPU: a.dot(b)", [&] {
c = a.dot(b);
});

auto aa = Eigen::Matrix::Ones(1000, 1000);
auto bb = Eigen::Matrix::Ones(1000, 100);
auto cc = Eigen::Matrix();

Bench().run("Eigen: a * b", [&] {
cc = aa * bb;
});
```

Operations
----------

### GPU and CPU

* `+` (add)
* `-` (sub)
* `*` (mul)
* `/` (div)
* `dot` (dot product)

### CPU only

* `==`
* `clone`
* `constants`
* `empty`
* `zeros`
* `ones`
* `random`
* `transpose`
* `sigmoid`
* `sum`
* `mean`
* `min`
* `max`
* `count`
* `all`
* `softmax`
* `argmax`
* `array_equal`
* `allclose`

License
-------

MIT license (© 2024 Yuji Hirose)