Using SSE in c# is it possible?

2020-01-26 10:32发布

I was reading a question about c# code optimization and one solution was to use c++ with SSE. Is it possible to do SSE directly from a c# program?

标签: c# sse
10条回答
三岁会撩人
2楼-- · 2020-01-26 11:08

Based on this forum posting, the MS JIT compiler automatically uses SSE if SSE is available on the target machine.

查看更多
叛逆
3楼-- · 2020-01-26 11:11

The upcoming Mono 2.2 release will have SIMD support. Miguel de Icaza blogged about the upcoming feature here, and the API is here.

Although there will be a library that will support development under Microsoft's .NET Windows runtime, it will not have the performance benefits that you are looking for unless you run the code under the Mono runtime. Which might be doable depending on your circumstances.

Update: Mono 2.2 is released

查看更多
We Are One
4楼-- · 2020-01-26 11:12

Modern C# Supports SIMD/SSE instructions well and makes them fairly simple to use. Not all instructions are yet supported.

Here is an example of an SSE .Sum() of an array of uint[]:

    using System.Numerics;

    private static ulong SumSseInner(this uint[] arrayToSum, int l, int r)
    {
        var sumVectorLower = new Vector<ulong>();
        var sumVectorUpper = new Vector<ulong>();
        var longLower      = new Vector<ulong>();
        var longUpper      = new Vector<ulong>();
        int sseIndexEnd = l + ((r - l + 1) / Vector<uint>.Count) * Vector<uint>.Count;
        int i;
        for (i = l; i < sseIndexEnd; i += Vector<int>.Count)
        {
            var inVector = new Vector<uint>(arrayToSum, i);
            Vector.Widen(inVector, out longLower, out longUpper);
            sumVectorLower += longLower;
            sumVectorUpper += longUpper;
        }
        ulong overallSum = 0;
        for (; i <= r; i++)
            overallSum += arrayToSum[i];
        sumVectorLower += sumVectorUpper;
        for (i = 0; i < Vector<long>.Count; i++)
            overallSum += sumVectorLower[i];
        return overallSum;
    }

This particular function is part of an open source and free nuget package, HPCsharp, available on nuget.org, which I maintain.

查看更多
别忘想泡老子
5楼-- · 2020-01-26 11:15

Filip is correct. I have another, older post showing a similar, but more detailed example. I have actually run this code, and modified it myself to prove to myself that it works. I am contemplating using this technique in a project I am working and is why I am out looking to see what may be new since this is a bit old. As the author implies, you can write any function you wish in C++, compile it, then copy the bytes into your C#.

http://blogs.msdn.com/b/devinj/archive/2005/07/12/438323.aspx

I would add that Joe's CLI C++ class is a good idea as well, however, I don't think the sse compiler flag and the /clr flag are compatible on the same project. I just verified that: have to write your high perf code in a separate project to use the SSE (/arch:sse or /arch:sse2) compiler flag as /clr is incomatible. To do anything much more complex than do simple arithmetic on a few inputs, I think this is the best approach.

查看更多
登录 后发表回答