Run C# code on GPU

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 21:52:13

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.

1) No, not for the general case of C#, but a small subset, yes. Either through a runtime (check Tidepowerd GPU.NET) or via language support (LINQ or Code Quotations).

2) Yes, DirectCompute (DX11 Compute Shaders) and OpenCL are both vendor independent, mature APIs and you can find .NET binding for them.

3) No, as James said, they are different beast. GPU are high latency processors optimized for high throughput data parallel applications whereas CPU are low latency processors optimized for sequential general purpose applications.

The only research project I know that tries to address this issue is the SPAP language.

My advice, don't try to find the perfect universal API/runtime because there's none. Pick an existing technology (DirectCompute or OpenCL) and see how you can leverage it for your business.

Useful links for starting:

1) Not that I know of, but there might be a library for C# that can help you.

2) OpenCL. It's GPU-independent and can even run on CPUs.

3) OpenCL will help you with that, you can compile for CPU too with OpenCL, though I'm not sure how great of code it makes for the CPU. I've really fallen in love with OpenCL lately, it works really really well.

There's also brahma. It supposedly captures expressions and compiles them for the GPU. I haven't tried myself.

And, Microsoft has a research prototype called accelerator, which is similar in goal but syntactically different.

Have you looked at Alea GPU? There libraries, while not completely free, have a fair license. There is great documentation and an impressive looking tool-chain.

For Java, see the Aparapi project (https://github.com/aparapi/aparapi). This allows a subset of Java to be run on any GPU which supports OpenCL. The bytecode of Kernel classes is cross-compiled at runtime to OpenCL code. There are severe restrictions on the java code which can be cross-compiled - basically no Objects can be used as fields, locals or method args.

However a hefty advantage is that the kernels can be executed in either Java or OpenCL (with automatic fallback to Java ThreadPool execution in the event of unavailability of an appropriate GPU/APU device). This sounds like the closest thing to what you are seeking in part 3 of your question (though of course the managed language is not C#).

I'm not aware of anything similar in C#.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!