zoukankan      html  css  js  c++  java
  • .net core SIMD范例分析

    单指令多数据流(SIMD)是CPU基本运算之外为了提高并行处理多条数据效率的技术,常用于多媒体处理如视频,3D模拟的计算。实现方式不同品牌的CPU各有自己的指令集,如SSE MMX 3DNOW等。

    C#开发.net core软件的过程中也可以让编译器自动采用这些SIMD指令集进行代码优化,测试了一下在我的AMD 锐龙7 2700X上对于整数加法处理可以提高10倍的效率。

    下面是我自己写的例子:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Numerics;
    using System.Diagnostics;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MySIMDTest
    {
        class Program
        {
            static Random rand = new Random();
            static Vector<int> getVec32(int count)
            {
                var lst = new List<int>(count);
                for (int i = 0; i < count; ++i) lst.Add(rand.Next(100));
                return new Vector<int>(lst.ToArray());
            }
    
            static Vector<short> getVec16(int count)
            {
                var lst = new List<int>(count);
                for (int i = 0; i < count; ++i) lst.Add(rand.Next(100));
                return new Vector<short>(lst.Select(i => (short)i).ToArray());
            }
    
            static void Main(string[] args)
            {
                var sw = new Stopwatch();
                var testTimes = (int)(Math.Pow(10, 5));
                var vecSize = (int)(Math.Pow(10, 2));
    
                Action testNormal = () =>
                {
                    Console.Write("normal test ");
                    var lstVecN1 = new List<Vector<int>>();
                    var lstVecN2 = new List<Vector<int>>();
                    for (int i = 0; i < testTimes; ++i)
                    {
                        lstVecN1.Add(getVec32(vecSize));
                        lstVecN2.Add(getVec32(vecSize));
                    }
    
                    sw.Restart();
                    for (int i = 0; i < testTimes; ++i)
                    {
                        for(int j = 0; j < vecSize; ++j)
                        {
                            var r = lstVecN1[i] + lstVecN2[i];
                        }
                    }
                    sw.Stop();
                    Console.WriteLine(sw.Elapsed);
                };
    
                Action test16 = () =>
                {
                    Console.Write("16 test");
                    var lstVecA1 = new List<Vector<short>>();
                    var lstVecA2 = new List<Vector<short>>();
                    for (int i = 0; i < testTimes; ++i)
                    {
                        lstVecA1.Add(getVec16(vecSize));
                        lstVecA2.Add(getVec16(vecSize));
                    }
    
                    sw.Restart();
                    for (int i = 0; i < testTimes; ++i)
                    {
                        var result1 = lstVecA1[i] + lstVecA2[i];
                    }
                    sw.Stop();
                    Console.WriteLine(sw.Elapsed);
                };
    
    
                Action test32 = () =>
                {
                    Console.Write("32 test");
                    var lstVecB1 = new List<Vector<int>>();
                    var lstVecB2 = new List<Vector<int>>();
                    for (int i = 0; i < testTimes; ++i)
                    {
                        lstVecB1.Add(getVec32(vecSize));
                        lstVecB2.Add(getVec32(vecSize));
                    }
    
                    sw.Restart();
                    for (int i = 0; i < testTimes; ++i)
                    {
                        var result1 = lstVecB1[i] + lstVecB2[i];
                    }
                    sw.Stop();
                    Console.WriteLine(sw.Elapsed);
                };
    
                for (int i = 0; i < 8; ++i) testNormal();
                for (int i = 0; i < 8; ++i) test32();
                for (int i = 0; i < 8; ++i) test16();
                Console.ReadKey();
            }
        }
    }

    运行结果:

    只要用Vector<T>支持的重载运算符来代替数组或者列表之类进行计算,即可获得编译器SIMD自动优化指令的效果。不过目前文档里说只支持x86系列的CPU ARM的CPU相关支持还在研发中

  • 相关阅读:
    Atitti 图像处理 图像混合 图像叠加 blend 原理与实现
    Atitit Gaussian Blur 高斯模糊 的原理and实现and 用途
    Atitit 图像处理 灰度图片 灰度化的原理与实现
    Atitit (Sketch Filter)素描滤镜的实现  图像处理  attilax总结
    Atitit 实现java的linq 以及与stream api的比较
    Atitit attilax在自然语言处理领域的成果
    Atitit 图像处理 常用8大滤镜效果 Jhlabs 图像处理类库 java常用图像处理类库
    Atitit 图像处理--图像分类 模式识别 肤色检测识别原理 与attilax的实践总结
    Atitit apache 和guava的反射工具
    atitit。企业的价值观 员工第一 vs 客户第一.docx
  • 原文地址:https://www.cnblogs.com/fancybit/p/11412198.html
Copyright © 2011-2022 走看看