Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable SIMD in a new .NET Framework 4.6?

Tags:

c#

.net

I wrote a code to test, which way is faster. I run the code in Release x64. I have VS 2015 Pro with .NET Framework 4.6.

#if SYSTEMMATHLIB
using System.Numerics;
#else
using Engine.Mathematic;
#endif

namespace Engine.Editor
{
    public static class Program
    {
        public static void Main()
        {
            #if SYSTEMMATHLIB
            Console.WriteLine("Hardware Acceleration: " + Vector.IsHardwareAccelerated.ToString());
            #endif

            Stopwatch sw = new Stopwatch();
            sw.Start();

#if SYSTEMMATHLIB
            for (int i = 0; i < 1000000; i++)
            {
                Matrix4x4 m = Matrix4x4.CreateRotationZ(0.50f);
                m.Translation = new Vector3(5, 10, 15);

                Matrix4x4 m2 = Matrix4x4.CreateRotationX(0.50f);
                m2.Translation = new Vector3(-5, 10, -15);

                Matrix4x4 m3 = m * m2;
                Matrix4x4 inv; Matrix4x4.Invert(m3, out inv);
            }
#else
            for (int i = 0; i < 1000000; i++)
            {
                Matrix m = Matrix.RotationZ(0.50f);
                m.TranslationVector = new Vector3(5, 10, 15);

                Matrix m2 = Matrix.RotationX(0.50f);
                m2.TranslationVector = new Vector3(-5, 10, -15);

                Matrix m3 = m * m2;
                Matrix inv; Matrix.Invert(ref m3, out inv);
            }
#endif
            long mili = sw.ElapsedMilliseconds;
            sw.Stop();        

            Console.WriteLine("Total mili: " + mili.ToString());
            Console.ReadLine();
        }
    }
}

Well, if i run it using System.Numerics from Framework 4.6 ( Nuget version ), it takes 212ms to calculate. If i switch it to my library, which is just simple c# code to calculate it, it also takes around 210 ms. That's a bit strange right ? I though SIMD should be must faster !

By the way, IsHardwareAccelerated returns "True".

So what I am doing wrong ???

For info only: C++ without SIMD runs that for 390ms and 77ms with SIMD.

like image 877
wh1sp3r Avatar asked Oct 31 '22 21:10

wh1sp3r


1 Answers

I decompiled System.Numerics.Vector and I realized, SIMD is implemented only for Vectors and not for Matrix4x4. So Hans Passant was right.

I hope they will also add support for SIMD Matrix soon.

like image 172
wh1sp3r Avatar answered Nov 15 '22 03:11

wh1sp3r