• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            string

            string
            posts - 27, comments - 177, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
            通常我們大量使用array of structure來開發程序,因為array of structure 具有面向對象的特征,易于描述客觀世界,代碼也容易理解。但是 array of structure 卻常常會阻礙程序的并行化。
            structure of array 與之相反,它易于并行化,但拙于描述客觀世界,代碼也變得難以理解。 
            要想代碼性能好就要使用structure of array , 要想開發效率高就要使用array of structure, 設計之初就要做出選擇,開發后期如果想轉換到另一種方案將會大費周章。 
             Intel 的 Array building block 提供了一套編程接口 讓我們可以從array of structure 的視角編寫基于 structure of array的程序。這話說起來有點繞,可以這樣理解,在邏輯層是array of structure , 在物理層是structrue of array. 
            在C++中我們如何實現這種邏輯層(array of structure )/物理層(structrue of array )的分離與映射呢?
            這是我們基于array of structure 的程序
            struct RGB
            {
                    int r;
                    int g;
                    int b;
            };
            template<class T>
            void test(T& rgb, size_t n)
            {
                int i =0;
                for(i=0;i<SIZE;i++){
                    rgb[i].r = 3*i;
                    rgb[i].g = 3*i + 1;
                    rgb[i].b = 3*i + 2;
                }
                for(i=0;i<SIZE;i++){
                    rgb[i].b=rgb[i].r + rgb[i].g;
                }

            #define SIZE 65536
            int main()
            {
              RGB* rgb = new RGB[SIZE];
              test(rgb, SIZE);
            }

            要將上面的程序轉換為SOA,我們首先為RGB定義一個影子
            struct RGBshadow
            {
                    RGBshadow(int& r, int& g, int& b):r(r),g(g),b(b){}
                    int& r;
                    int& g;
                    int& b;
            };

            然后我們有一個模板類用于定義SOA類,此類為通用類
            template<class Shadow, typename T1, typename T2, typename T3>
            class SOA
            {
                public:
                    typedef T1 aligned_t1 __attribute__((aligned(16)));
                    typedef T2 aligned_t2 __attribute__((aligned(16)));
                    typedef T3 aligned_t3 __attribute__((aligned(16)));
                public:
                    SOA(int n){
                        r = (aligned_t1*)_mm_malloc(n*sizeof(T1), 64);
                        g = (aligned_t2*)_mm_malloc(n*sizeof(T2), 64);
                        b = (aligned_t3*)_mm_malloc(n*sizeof(T3), 64);
                    }
                    ~SOA(){
                        if(r) _mm_free(r);
                        if(g) _mm_free(g);
                        if(b) _mm_free(b);
                    }
                    Shadow operator [] ( size_t i){
                        return Shadow(r[i],g[i],b[i]);
                    }
                private:
                    aligned_t1* r ;
                    aligned_t2* g ;
                    aligned_t3* b ;
            };

            #define SIZE 65536
            int main()
            {
              RGB* rgb = new RGB[SIZE];
              test(rgb, SIZE);
              SOA<RGBshadow, intint,int> soa(SIZE);
              test(soa, SIZE);
            }
            編譯器會自動向量化test(soa,SIZE);
            test(rgb, SIZE);中的第二個for循環生成的代碼如下:
            .L14:
                movl    (
            %rbx,%rax), %edx
                addl    
            4(%rbx,%rax), %edx
                movl    
            %edx, 8(%rbx,%rax)
                addq    $
            12%rax
                cmpq    $
            786432%rax
                jne .L14

            test(soa, SIZE);中的第二個for循環生成的代碼如下:
            .L16:
                movdqa  (
            %rsi,%rax), %xmm0
                paddd   (
            %rcx,%rax), %xmm0
                movdqa  
            %xmm0, (%rdx,%rax)
                addq    $
            16%rax
                cmpq    $
            262144%rax
                jne .L16

            要將AOS轉換為SOA,分如下三步
            1。 定義一個影子結構
            2。 利用SOA<shadow,...>模板定義相應的SOA結構
            3。 修改業務代碼,SOA<shadow,...> 與AOS有相同的操作方式,因而可以盡量少的修改代碼。


            Feedback

            # re: 并行化你的程序--Array of structure 與 structure of Array  回復  更多評論   

            2012-08-06 14:14 by ningle
            看你的博客,uefi的application基本都是用C++寫成,難道這是現在uefi application開發的主流嘛?

            # re: 并行化你的程序--Array of structure 與 structure of Array  回復  更多評論   

            2012-08-06 22:16 by djx_zh
            @ningle
            開發UEFI application的主流還是C。 如果application規模十分龐大,用C++開發效率會高些。
            国产精品久久久久9999| 亚洲欧美日韩中文久久| 久久精品九九亚洲精品| 久久精品亚洲中文字幕无码麻豆 | 亚洲va久久久噜噜噜久久狠狠 | 国产午夜免费高清久久影院| 久久人人爽人人爽人人AV| 久久久无码精品亚洲日韩按摩| 俺来也俺去啦久久综合网| 久久国产香蕉一区精品| 无码人妻精品一区二区三区久久久 | 日韩久久久久久中文人妻| 久久线看观看精品香蕉国产| 亚洲精品无码久久久| 国内精品久久久久久99| 日韩中文久久| 亚洲国产二区三区久久| 亚洲中文久久精品无码| 精品久久久久久国产免费了| 蜜臀久久99精品久久久久久小说| 99久久精品无码一区二区毛片 | 久久ww精品w免费人成| 亚洲国产精品无码久久九九| 久久99国产精一区二区三区 | 久久国产亚洲精品无码| 女人高潮久久久叫人喷水| 亚洲国产精品久久久久婷婷软件 | 色综合久久久久综合体桃花网 | 久久久久久精品成人免费图片| 品成人欧美大片久久国产欧美| 久久超乳爆乳中文字幕| 2020国产成人久久精品| 欧美久久一级内射wwwwww.| 51久久夜色精品国产| 久久精品www| 99久久国产综合精品麻豆| 97精品依人久久久大香线蕉97| 亚洲国产精品无码久久青草| 久久婷婷色综合一区二区| 久久伊人色| 一97日本道伊人久久综合影院|