• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
            通常我們大量使用array of structure來開發(fā)程序,因?yàn)閍rray of structure 具有面向?qū)ο蟮奶卣鳎子诿枋隹陀^世界,代碼也容易理解。但是 array of structure 卻常常會(huì)阻礙程序的并行化。
            structure of array 與之相反,它易于并行化,但拙于描述客觀世界,代碼也變得難以理解。 
            要想代碼性能好就要使用structure of array , 要想開發(fā)效率高就要使用array of structure, 設(shè)計(jì)之初就要做出選擇,開發(fā)后期如果想轉(zhuǎn)換到另一種方案將會(huì)大費(fèi)周章。 
             Intel 的 Array building block 提供了一套編程接口 讓我們可以從array of structure 的視角編寫基于 structure of array的程序。這話說起來有點(diǎn)繞,可以這樣理解,在邏輯層是array of structure , 在物理層是structrue of array. 
            在C++中我們?nèi)绾螌?shí)現(xiàn)這種邏輯層(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);
            }

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

            然后我們有一個(gè)模板類用于定義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);
            }
            編譯器會(huì)自動(dòng)向量化test(soa,SIZE);
            test(rgb, SIZE);中的第二個(gè)for循環(huán)生成的代碼如下:
            .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);中的第二個(gè)for循環(huán)生成的代碼如下:
            .L16:
                movdqa  (
            %rsi,%rax), %xmm0
                paddd   (
            %rcx,%rax), %xmm0
                movdqa  
            %xmm0, (%rdx,%rax)
                addq    $
            16%rax
                cmpq    $
            262144%rax
                jne .L16

            要將AOS轉(zhuǎn)換為SOA,分如下三步
            1。 定義一個(gè)影子結(jié)構(gòu)
            2。 利用SOA<shadow,...>模板定義相應(yīng)的SOA結(jié)構(gòu)
            3。 修改業(yè)務(wù)代碼,SOA<shadow,...> 與AOS有相同的操作方式,因而可以盡量少的修改代碼。


            Feedback

            # re: 并行化你的程序--Array of structure 與 structure of Array  回復(fù)  更多評(píng)論   

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

            # re: 并行化你的程序--Array of structure 與 structure of Array  回復(fù)  更多評(píng)論   

            2012-08-06 22:16 by djx_zh
            @ningle
            開發(fā)UEFI application的主流還是C。 如果application規(guī)模十分龐大,用C++開發(fā)效率會(huì)高些。

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲级αV无码毛片久久精品| 国内精品久久久久久99| 久久精品免费大片国产大片| 国产精品美女久久久久av爽 | 九九99精品久久久久久| 一本大道久久a久久精品综合| 精品乱码久久久久久夜夜嗨| 日韩欧美亚洲综合久久| 久久香蕉一级毛片| 久久久亚洲欧洲日产国码是AV| 精品国产VA久久久久久久冰 | 国产亚州精品女人久久久久久 | 亚洲αv久久久噜噜噜噜噜| 亚洲综合久久综合激情久久| 久久伊人色| 久久精品中文字幕久久| 中文字幕人妻色偷偷久久 | 亚洲欧美另类日本久久国产真实乱对白 | 久久久久亚洲精品中文字幕 | 精品国产一区二区三区久久| 久久精品一区二区三区AV| 国产精品丝袜久久久久久不卡| 久久综合九色综合网站| 亚洲欧美成人久久综合中文网 | 久久精品国产精品亚洲人人| 国产产无码乱码精品久久鸭| 日韩人妻无码精品久久久不卡| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久国产精品二国产精品| 国产精品久久久久影院嫩草| 欧美亚洲色综久久精品国产| 久久天天躁狠狠躁夜夜不卡| 久久久久久国产精品无码下载| 香蕉久久夜色精品国产小说| 久久久久久久亚洲Av无码| 蜜臀久久99精品久久久久久小说| 久久精品成人欧美大片| 7777久久久国产精品消防器材 | 97r久久精品国产99国产精| 国产精品免费看久久久| 久久亚洲春色中文字幕久久久|