Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run an algorithm on the GPU using Metal?

Tags:

ios

swift

metal

I'm using the Minimum Edit Distance algorithm to determine how closely two strings are related. I've implemented it to run on the CPU and it works great when you have hundreds of strings, but it is a source of slow down when you are comparing thousands of strings, multiple times. So I thought it might be useful to off load on to the GPU since it could perform multiple comparisons at once.

Is this possible? The Metal resources I've come across are mainly for graphics which aren't helpful. Or maybe they are?

like image 850
DookieMan Avatar asked Jan 18 '17 17:01

DookieMan


People also ask

What is metal in GPU?

Metal is Apple's API for programming the GPU (graphics processor unit). While often ignored by non-game developers, Metal can be a computational powerhouse with little effort.

Is metal a game engine?

Build your own low-level game engine in Metal! Metal is a unified application programming interface (API) for the graphics processing unit, or GPU. It's unified because it applies to both 3D graphics and data-parallel computation paradigms.

What is a metal shader?

Optimize graphics and compute performance with kernels that are fine-tuned for the unique characteristics of each Metal GPU family.

What is metal framework?

The Metal framework gives your app direct access to a device's graphics processing unit (GPU). With Metal, apps can leverage a GPU to quickly render complex scenes and run computational tasks in parallel.


2 Answers

What you want to do is possible, at least for certain problem sizes, but it's not particularly straightforward. What you'll need to do is express the algorithm in a way that can be run on the GPU, and on iOS, that probably means using Metal. Specifically, you'll need to write one or more compute kernels in the Metal shading language that implement the minimum edit distance algorithm, then dispatch them using a Metal compute command encoder. There are several resources on compute programming with Metal around the Web.

I'm not aware of an existing Metal implementation of MED, but there is at least one CUDA implementation, and you can read a longer explanation of MED on the GPU here.

like image 109
warrenm Avatar answered Sep 23 '22 03:09

warrenm


In addition to @warrenm answer, you need to write kernel function or couple of them. there is cool metal tutorial website: http://metalbyexample.com, also Apple documentation has something about it: https://developer.apple.com/library/content/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Compute-Ctx/Compute-Ctx.html

and also there are couple of tutorials on https://www.raywenderlich.com but they are mostly graphic-oriented. You can also check GPUImage library for iOS, that is a cool wrapper around OpenGL with nice interface. Maybe there is also an option to write custom functions that will be executed using OpenGL?

like image 45
Maciej Chrzastek Avatar answered Sep 22 '22 03:09

Maciej Chrzastek