C# 向量化数组加法

发布于 2024-12-17 06:54:31 字数 469 浏览 0 评论 0原文

是否有办法以 SIMD 方式“矢量化”数组中元素的添加?

例如,我想将: 转换

var a = new[] { 1, 2, 3, 4 };
var b = new[] { 1, 2, 3, 4 };
var c = new[] { 1, 2, 3, 4 };
var d = new[] { 1, 2, 3, 4 };

var e = new int[4];

for (int i = 0; i < a.Length; i++)
{
    e[i] = a[i] + b[i] + c[i] + d[i];
}

// e should equal { 4, 8, 12, 16 }

为:

var e = VectorAdd(a,b,c,d);

我知道 C++ / XNA 库中可能存在某些内容,但我不知道标准 .Net 库中是否有它。

谢谢!

Is there anyway to "vectorize" the addition of elements across arrays in a SIMD fashion?

For example, I would like to turn:

var a = new[] { 1, 2, 3, 4 };
var b = new[] { 1, 2, 3, 4 };
var c = new[] { 1, 2, 3, 4 };
var d = new[] { 1, 2, 3, 4 };

var e = new int[4];

for (int i = 0; i < a.Length; i++)
{
    e[i] = a[i] + b[i] + c[i] + d[i];
}

// e should equal { 4, 8, 12, 16 }

Into something like:

var e = VectorAdd(a,b,c,d);

I know something may exist in the C++ / XNA libraries, but I didn't know if we have it in the standard .Net libraries.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-12-24 06:54:31

您将需要查看 Mono.Simd:

http://tirania.org/blog /archive/2008/Nov-03.html

它支持C#中的SIMD

using Mono.Simd;


//...
var a = new Vector4f( 1, 2, 3, 4 );
var b = new Vector4f( 1, 2, 3, 4 );
var c = new Vector4f( 1, 2, 3, 4 );
var d = new Vector4f( 1, 2, 3, 4 );

var e = a+b+c+d;

You will want to look at Mono.Simd:

http://tirania.org/blog/archive/2008/Nov-03.html

It supports SIMD in C#

using Mono.Simd;


//...
var a = new Vector4f( 1, 2, 3, 4 );
var b = new Vector4f( 1, 2, 3, 4 );
var c = new Vector4f( 1, 2, 3, 4 );
var d = new Vector4f( 1, 2, 3, 4 );

var e = a+b+c+d;
空气里的味道 2024-12-24 06:54:31

Mono 提供了一个相对不错的 SIMD API(如 sehe 提到的),但如果 Mono 不是一个选项,我可能会编写一个 C++/CLI 接口库来完成繁重的工作。 C# 对于大多数问题集都可以很好地工作,但如果您开始编写高性能代码,最好使用一种可以让您真正掌握性能的语言。

在工作中,我们使用 P/Invoke 从 C# 调用用 C++ 编写的图像处理例程。 P/Invoke 有一些开销,但如果您很少进行调用并在本机端进行大量处理,那么这是值得的。

Mono provides a relatively decent SIMD API (as sehe mentions) but if Mono isn't an option I would probably write a C++/CLI interface library to do the heavy lifting. C# works pretty well for most problem sets but if you start getting into high performance code it's best to go to a language that gives you the control to really get dirty with performance.

Here at work we use P/Invoke to call image processing routines written in C++ from C#. P/Invoke has some overhead but if you make very few calls and do a lot of processing on the native side it can be worth it.

思念绕指尖 2024-12-24 06:54:31

我想这一切都取决于你在做什么,但如果你担心向量化向量和,你可能想看看 Math.NET 提供优化的数值计算。

从他们的网站:

它以 Microsoft .Net 4.0、Mono 和 Silverlight 4 为目标,除了纯粹的托管实现之外,还将支持本机硬件优化(MKL、ATLAS)。

I guess it all depends on what you are doing, but if you are worried about vectorizing vector sums, you might want to take a look at a library such as Math.NET which provide optimized numerical computations.

From their website:

It targets Microsoft .Net 4.0, Mono and Silverlight 4, and in addition to a purely managed implementation will also support native hardware optimization (MKL, ATLAS).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文