Using SSE in c# is it possible?

后端 未结 10 1911
借酒劲吻你
借酒劲吻你 2020-11-29 11:02

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?

相关标签:
10条回答
  • 2020-11-29 11:42

    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.

    0 讨论(0)
  • 2020-11-29 11:45

    If you have a 'chunk' of work you want to do, the best bet is to write it in C++ using the MMX/SSE intrinsics and then make a very simple /clr managed C++ class that wraps your functionality and exposes it out as a .net class. Then your code can just use that assembly as if it were a normal class.

    For more about the VC intrinsics you can look at this little ditty I wrote many years ago.

    http://msdn.microsoft.com/en-us/library/0aws1s9k.aspx

    Oh - I'm assuming you are actually wanting to use the parallel functions to speed something up. As others have pointed out - if you just want to move data in larger chunks and the like, the JIT already knows how to use SSE for those basics.

    0 讨论(0)
  • 2020-11-29 11:49

    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

    0 讨论(0)
  • 2020-11-29 11:50

    Can C# explicitly make an SSE call?

    No. C# cannot produce inline IL much less inline x86/amd64 assembly.

    The CLR, and more specifically the JIT, will use SSE if it's available removing the need to force it in most circumstances. I say most because I'm not an SSE expert and I'm sure that there are cases where it could be beneficial and the JIT does not make the optimization.

    0 讨论(0)
  • 2020-11-29 11:51

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

    0 讨论(0)
  • 2020-11-29 11:51

    It is finally possible. Here the post http://blogs.msdn.com/b/dotnet/archive/2014/04/07/the-jit-finally-proposed-jit-and-simd-are-getting-married.aspx

    0 讨论(0)
提交回复
热议问题