Just a coder
Just a coder

Reputation: 16690

Running functions on the GPU with swift

I have a function that can run a long time processing a job (has several inner-loops). I assume that running this on the GPU instead of the CPU will be faster?

Is there a way to run arbitrary functions on the GPU?

Apple has stated that Metal:

Metal 2 provides near-direct access to the graphics processing unit (GPU), enabling you to maximize the... compute potential of your apps

I see examples using Metal for gaming and graphics rendering, but are there any examples or links to help me with just running regular functions?

Upvotes: 3

Views: 2089

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

The GPU can be useful for certain kinds of pure computation.

The task must be highly parallelizable.

It must not require any APIs from the system libraries or frameworks. Metal has its own library that only has computation features. No disk access. No data structures beyond arrays/buffers.

There's an upper limit on how long the system will allow a compute shader to run (because it could be an infinite loop with no other way to stop it). So, depending on what you mean by "long time processing", it might not be suitable, or would only be suitable if broken up into chunks of work.

If a task is suitable for the GPU, it has to be explicitly programmed for the GPU. You can't just migrate normal code to the GPU transparently or automatically. It will have to be rewritten.

Upvotes: 8

Related Questions