Run C# code on GPU

jojovilco picture jojovilco · Nov 7, 2010 · Viewed 19.8k times · Source

I have no knowledge of GPU programming concepts and APIs. I have a few questions:

  1. Is it possible to write a piece of managed C# code and compile/translate it to some kind of module, which can be executed on the GPU? Or am I doomed to have two implementations, one for managed on the CPU and one for the GPU (I understand that there will be restrictions on what can be executed on the GPU)?
  2. Does there exist a decent and mature API to program independently against various GPU hardware vendors (i.e. a common API)?
  3. Are there any best practices if one wants to develop applications that run on a CPU, written in managed language, and also provide speed optimizations if suitable GPU hardware is present?

I would also be glad for links to any kind of documentation with appropriate learning resources.

Best, Jozef

Answer

James Gaunt picture James Gaunt · Nov 7, 2010

1) No - not for the general case of C# - obviously anything can be created for some subset of the language

2) Yes - HLSL using Direct X or Open GL

3) Not generally possible - CPU and GPU coding are fundamentally different

Basically you can't think of CPU and GPU coding as being comparable. A GPU is a highly specialised parallel processing tool - for lots of parallel simple calculations.

Trying to write a general progam in a GPU with lots of branches etc just won't be efficient - maybe not even possible.

Their memory access architectures are totally different.

You should write for the CPU but farm out appropriate parallel computations to the GPU.