C# 向量化数组加法
是否有办法以 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要查看 Mono.Simd:
http://tirania.org/blog /archive/2008/Nov-03.html
它支持C#中的SIMD
You will want to look at Mono.Simd:
http://tirania.org/blog/archive/2008/Nov-03.html
It supports SIMD in C#
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.
我想这一切都取决于你在做什么,但如果你担心向量化向量和,你可能想看看 Math.NET 提供优化的数值计算。
从他们的网站:
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: