Esempio n. 1
0
        public void Add()
        {
            var a = VecFloat.Allocate(1024);

            a.SetAll(2);

            var b = VecFloat.Allocate(1024);

            for (var i = 0; i < b.Count; ++i)
            {
                b[i] = i;
            }

            var r = VecFloat.Allocate(1024);

            VecFloat.Add(a, b, r);

            for (var i = 0; i < r.Count; ++i)
            {
                if (r[i] != 2 + i)
                {
                    throw new Exception();
                }
            }
            VecFloat.Release(ref a);
            VecFloat.Release(ref b);
            VecFloat.Release(ref r);
        }
Esempio n. 2
0
        public void Max()
        {
            var a = VecFloat.Allocate(1024);

            a.SetAll(2);

            var b = VecFloat.Allocate(1024);

            b.SetAll(5);

            var r = VecFloat.Allocate(1024);

            VecFloat.Max(a, b, r);

            for (var i = 0; i < r.Count; ++i)
            {
                if (r.Get(i) != 5)
                {
                    throw new Exception();
                }
            }
            VecFloat.Release(ref a);
            VecFloat.Release(ref b);
            VecFloat.Release(ref r);
        }
Esempio n. 3
0
        public void Divide()
        {
            var a = VecFloat.Allocate(1024);

            a.SetAll(2);

            var b = VecFloat.Allocate(1024);

            for (var i = 0; i < b.Count; ++i)
            {
                b[i] = i;
            }

            var r = VecFloat.Allocate(1024);

            VecFloat.Divide(a, b, r);

            for (var i = 0; i < r.Count; ++i)
            {
                var av = 2.0f;
                var bv = (float)(i);
                var rv = av / bv;
                var d  = r[i] - rv;
                if (Math.Abs(d) > 0.000001f)
                {
                    throw new Exception("Index: " + i + " r:" + r[i] + " a:" + a[i] + " b:" + b[i] + " s:" + av + ":" + bv + ":" + rv + " d:" + d);
                }
            }
            VecFloat.Release(ref a);
            VecFloat.Release(ref b);
            VecFloat.Release(ref r);
        }
Esempio n. 4
0
        static void PreLoad(int allocSize)
        {
            var a = VecFloat.Allocate(allocSize);

            a.SetAll(2);

            var b = VecFloat.Allocate(allocSize);

            b.SetAll(5);

            var r = VecFloat.Allocate(allocSize);

            VecFloat.Add(a, b, r);

            VecFloat.Release(ref a);
            VecFloat.Release(ref b);
            VecFloat.Release(ref r);
        }
Esempio n. 5
0
        private static float Simd(int allocSize)
        {
            var a = VecFloat.Allocate(allocSize);

            a.SetAll(2);

            var b = VecFloat.Allocate(allocSize);

            b.SetAll(5);

            var r = VecFloat.Allocate(allocSize);

            VecFloat.Add(a, b, r);

            var result = VecFloat.Sum(r);

            VecFloat.Release(ref a);
            VecFloat.Release(ref b);
            VecFloat.Release(ref r);
            return(result);
        }
Esempio n. 6
0
        public void SetAndRead()
        {
            var vector = VecFloat.Allocate(1024);

            for (var i = 0; i < vector.Count; ++i)
            {
                float f = i;
                vector.Set(i, f);
            }

            for (var i = 0; i < vector.Count; ++i)
            {
                float f = i;
                var   b = vector.Get(i);
                if (f != b)
                {
                    throw new Exception();
                }
            }

            VecFloat.Release(ref vector);
        }
Esempio n. 7
0
        public void AllocateAndDeallocate()
        {
            var vector = VecFloat.Allocate(1024);

            VecFloat.Release(ref vector);
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            Stopwatch watch     = new Stopwatch();
            int       allocSize = 10000000;
            Random    rnd       = new Random();

            float[] setA = new float[allocSize];
            float[] setB = new float[allocSize];

            var a = VecFloat.Allocate(allocSize);
            var b = VecFloat.Allocate(allocSize);

            for (int i = 0; i < allocSize; i++)
            {
                setA[i] = rnd.Next(0, 10);
                setB[i] = rnd.Next(0, 10);
            }

            a.SetRange(0, setA, 0, allocSize);
            b.SetRange(0, setB, 0, allocSize);

            //a.SetAll(2);


            // b.SetAll(5);

            var r = VecFloat.Allocate(allocSize);

            watch.Start();
            VecFloat.Add(a, b, r);
            watch.Stop();

            VecFloat.Release(ref a);
            VecFloat.Release(ref b);
            VecFloat.Release(ref r);


            Console.WriteLine("SimdSharp: " + watch.ElapsedMilliseconds);



            //watch.Restart();


            float[] qa = new float[allocSize];
            float[] qb = new float[allocSize];
            float[] qr = new float[allocSize];

            for (int i = 0; i < allocSize; i++)
            {
                qa[i] = rnd.Next(0, 10);
                qb[i] = rnd.Next(0, 10);
            }

            watch.Reset();
            watch.Start();
            for (int i = 0; i < allocSize; i++)
            {
                qr[i] = qa[i] + qb[i];
            }
            watch.Stop();

            Console.WriteLine("Loop: " + watch.ElapsedMilliseconds);


            //not a fair test, its only using half its contents, wasting 50% cache and its not sped up in .net framework with intrinsics anyways
            Vector2[] va = new Vector2[allocSize];
            Vector2[] vb = new Vector2[allocSize];
            Vector2[] vr = new Vector2[allocSize];

            for (int i = 0; i < allocSize; i++)
            {
                va[i] = new Vector2(rnd.Next(0, 10), rnd.Next(0, 10));
                vb[i] = new Vector2(rnd.Next(0, 10), rnd.Next(0, 10));
            }

            watch.Reset();
            watch.Start();
            for (int i = 0; i < allocSize; i++)
            {
                vr[i].X = va[i].X + vb[i].X;
            }

            watch.Stop();

            Console.WriteLine("Numerics: " + watch.ElapsedMilliseconds);

            Console.ReadLine();
        }