6

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.

wh1sp3r
  • 1,640
  • 2
  • 17
  • 30
  • I don't know anything about .NET's use of SIMD, but for SIMD instructions to be really useful there needs to be an opportunity for the same operations to be carried out on multiple sets of data in parallel. I don't see such opportunities in your code. In particular all inputs and outputs are created within the loop body. – 500 - Internal Server Error Aug 09 '15 at 16:33
  • This isn't a typical use case. It may be that the act of creating the matrix is not built to use SIMD yet. Typically one is applying a single matrix transform to many vectors at once. – Cory Nelson Aug 09 '15 at 16:37
  • Well, same here. I did the same code in C++ and the result is very very different so using SSE2 is much faster than calculate it in C code myself. – wh1sp3r Aug 09 '15 at 16:38
  • Cory Nelsen: I am also multiplying the matrix and making inversion of the matrix, not only creating. – wh1sp3r Aug 09 '15 at 16:41
  • 5
    You are not using the .NET 4.6 version, Version.IsHardwareAccelerated is no longer accessible. Makes no real difference, SIMD support was not substantially changed from the CTP version. Only Vector2/3/4 get the [JitIntrinsic] love. Not Matrix4x4. Maybe next version. [Backgrounder post](http://stackoverflow.com/a/26905565/17034) is still accurate. – Hans Passant Aug 09 '15 at 17:16
  • "Only Vector2/3/4 get the [JitIntrinsic] love. Not Matrix4x4", yes, that's the reason then, lol. Is there any reference to that Matrix4x4 did not get love with JitIntrinsic ? :) – wh1sp3r Aug 09 '15 at 17:32
  • 1) you should use .NET 4.6, not nuget. 2) you can check you're running hardware accelerated with this code `typeof(Vector2).Assembly.GetType("System.Numerics.Vector").GetProperty("IsHardwareAccelerated", BindingFlags.Public | BindingFlags.Static).GetValue(null)`, and maybe it's in fact false. – Simon Mourier Aug 09 '15 at 17:39
  • Ok, uninstalled nuget version, unfortunately, "typeof(Vector2).Assembly.GetType("System.Numerics.Vector").GetProperty("IsHardw‌​areAccelerated", BindingFlags.Public | BindingFlags.Static)" this returns NULL. – wh1sp3r Aug 09 '15 at 17:54

1 Answers1

2

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.

wh1sp3r
  • 1,640
  • 2
  • 17
  • 30