简体   繁体   English

在GPU上运行C#代码

[英]Run C# code on GPU

I have no knowledge of GPU programming concepts and APIs. 我不了解GPU编程概念和API。 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? 是否可以编写一段托管的C#代码并将其编译/转换为某种模块,可以在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)? 或者我注定要有两个实现,一个用于CPU管理,一个用于GPU(我知道GPU上可以执行的操作会有限制)?
  2. Does there exist a decent and mature API to program independently against various GPU hardware vendors (ie a common API)? 是否存在适当且成熟的API以针对各种GPU硬件供应商(即通用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? 如果想要开发在CPU上运行的应用程序(使用托管语言编写),并且如果存在合适的GPU硬件,还提供速度优化,是否有任何最佳实践?

I would also be glad for links to any kind of documentation with appropriate learning resources. 我也很高兴能够通过适当的学习资源链接到任何类型的文档。

Best, Jozef 最好的,约瑟夫

1) No - not for the general case of C# - obviously anything can be created for some subset of the language 1)否 - 不是针对C#的一般情况 - 显然可以为该语言的某个子集创建任何内容

2) Yes - HLSL using Direct X or Open GL 2)是 - 使用Direct X或Open GL的HLSL

3) Not generally possible - CPU and GPU coding are fundamentally different 3)通常不可能 - CPU和GPU编码根本不同

Basically you can't think of CPU and GPU coding as being comparable. 基本上你不能认为CPU和GPU编码是可比的。 A GPU is a highly specialised parallel processing tool - for lots of parallel simple calculations. GPU是一种高度专业化的并行处理工具 - 用于大量并行简单计算。

Trying to write a general progam in a GPU with lots of branches etc just won't be efficient - maybe not even possible. 尝试在具有大量分支等的GPU中编写一般程序只是效率不高 - 甚至可能无法实现。

Their memory access architectures are totally different. 他们的内存访问架构完全不同。

You should write for the CPU but farm out appropriate parallel computations to the GPU. 您应该为CPU编写代码,但要对GPU进行适当的并行计算。

1) No, not for the general case of C#, but a small subset, yes. 1)不,不是针对C#的一般情况,而是针对一小部分,是的。 Either through a runtime (check Tidepowerd GPU.NET) or via language support (LINQ or Code Quotations). 通过运行时(检查Tidepowerd GPU.NET)或通过语言支持(LINQ或代码引用)。

2) Yes, DirectCompute (DX11 Compute Shaders) and OpenCL are both vendor independent, mature APIs and you can find .NET binding for them. 2)是的,DirectCompute(DX11计算着色器)和OpenCL都是独立于供应商的成熟API,您可以找到它们的.NET绑定。

3) No, as James said, they are different beast. 3)不,正如詹姆斯所说,他们是不同的野兽。 GPU are high latency processors optimized for high throughput data parallel applications whereas CPU are low latency processors optimized for sequential general purpose applications. GPU是针对高吞吐量数据并行应用而优化的高延迟处理器,而CPU是针对顺序通用应用而优化的低延迟处理器。

The only research project I know that tries to address this issue is the SPAP language. 我所知道的唯一试图解决这个问题的研究项目是SPAP语言。

My advice, don't try to find the perfect universal API/runtime because there's none. 我的建议是,不要试图找到完美的通用API /运行时,因为没有。 Pick an existing technology (DirectCompute or OpenCL) and see how you can leverage it for your business. 选择现有技术(DirectCompute或OpenCL),了解如何将其用于您的业务。

Useful links for starting: 有用的开始链接:

1) Not that I know of, but there might be a library for C# that can help you. 1)不是我所知道的,但可能有一个C#库可以帮助你。

2) OpenCL. 2)OpenCL。 It's GPU-independent and can even run on CPUs. 它独立于GPU,甚至可以在CPU上运行。

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. 3)OpenCL将为您提供帮助,您也可以使用OpenCL编译CPU,但我不确定它为CPU带来了多少代码。 I've really fallen in love with OpenCL lately, it works really really well. 我最近真的爱上了OpenCL,它确实非常好用。

There's also brahma . 还有梵天 It supposedly captures expressions and compiles them for the GPU. 它应该捕获表达式并为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 ? 你看过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 ). 对于Java,请参阅Aparapi项目( https://github.com/aparapi/aparapi )。 This allows a subset of Java to be run on any GPU which supports OpenCL. 这允许Java的子集在支持OpenCL的任何GPU上运行。 The bytecode of Kernel classes is cross-compiled at runtime to OpenCL code. 内核类的字节码在运行时交叉编译为OpenCL代码。 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. java代码有严格的限制,可以交叉编译 - 基本上没有对象可以用作字段,本地或方法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). 然而,一个巨大的优势是内核可以在Java或OpenCL中执行(在没有适当的GPU / APU设备的情况下自动回退到Java ThreadPool执行)。 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#). 这听起来像是你在问题的第3部分中最接近的东西(当然托管语言不是C#)。

I'm not aware of anything similar in C#. 我不知道C#中有类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM