久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

融合三角形循環進行并行化,計算子索引

Fusing a triangle loop for parallelization, calculating sub-indices(融合三角形循環進行并行化,計算子索引)
本文介紹了融合三角形循環進行并行化,計算子索引的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

并行化的一個常用技術是像這樣融合嵌套的 for 循環

A common technique in parallelization is to fuse nested for loops like this

for(int i=0; i<n; i++) {
    for(int j=0; j<n; j++) {

for(int x=0; x<n*n; x++) {
    int i = x/n; int j = x%n;

我想知道如何才能融合這樣的三角形循環

I'm wondering how I can do this to fuse a triangle loop like this

for(int i=0; i<n; i++) {
   for(int j=0; j<i+1; j++) {

這有 n*(n+1)/2 次迭代.我們將融合迭代稱為 x.使用二次公式我想出了這個:

This has n*(n+1)/2 iterations. Let's call the fused iteration x. Using the quadratic formula I have come up with this:

for(int x=0; x<(n*(n+1)/2); x++) {      
    int i  = (-1 + sqrt(1.0+8.0*x))/2;
    int j = x - i*(i+1)/2;

與融合方形循環不同,這需要使用 sqrt 函數以及從 int 到 float 以及從 float 到 int 的轉換.

Unlike fusing the square loop this requires using the sqrt function and conversions from int to float and from float to int.

我想知道是否有更簡單或更有效的方法來做到這一點?例如,不需要 sqrt 函數或從 int 到 float 或 float 到 int 的轉換的解決方案.

I'm wondering if there is a simpler or more efficient way of doing this? For example a solution which does not require the sqrt function or conversions from int to float or float to int.

我不想要依賴于上一次或下一次迭代的解決方案.我只想要像 int i = funci(x) 和 int j = funcj(x,i)

以下是一些代碼,表明這是有效的:

Here is some code showing that this works:

#include <stdio.h>
#include <math.h>
int main() {
    int n = 5;
    int cnt = 0;
    for(int i=0; i<n; i++) {
        for(int j=0; j<i+1; j++) {
            printf("%d: %d %d
", cnt++, i,j);      
        }
    } printf("
");

    int nmax = n*(n+1)/2;
    for(int x=0; x<nmax; x++) {     
        int i  = (-1 + sqrt(1.0+8.0*x))/2;
        int j = x - i*(i+1)/2;
        printf("%d: %d %d
", x,i,j);
    }       
}

推薦答案

考慮到您試圖融合一個三角形以實現并行化,非顯而易見的解決方案是選擇 x 到 (i,j):

Considering that you're trying to fuse a triangle with the intent of parallelizing, the non-obvious solution is to choose a non-trivial mapping of x to (i,j):

j | i ->
  |              ____
| |      =>    |\   |
V |___         |_\__|

畢竟,您不會以任何特殊順序處理它們,因此無需關心確切的映射.

After all, you're not processing them in any special order, so the exact mapping is a don't care.

所以像計算矩形一樣計算 x->i,j,但是如果 i >j then { i=N-i, j = N-j }(鏡像Y軸,然后鏡像X軸).

So calculate x->i,j as you'd do for a rectangle, but if i > j then { i=N-i, j = N-j } (mirror Y axis, then mirror X axis).

   ____
 |\   |      |           |
 |_\__|  ==> |_  __  =>  | 
                  / |      |  
                 /__|      |___

這篇關于融合三角形循環進行并行化,計算子索引的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 成年人黄色小视频 | 亚洲欧美激情国产综合久久久 | 狠狠操狠狠干 | 日韩欧美精品 | 男人av的天堂 | 亚洲国产看片 | 亚洲精品一区二区三区中文字幕 | 九九精品久久久 | 极情综合网 | 中文字幕亚洲一区 | 亚洲精品一区二区冲田杏梨 | 亚洲综合大片69999 | 丝袜 亚洲 另类 欧美 综合 | 自拍视频网站 | 日韩精品1区2区3区 国产精品国产成人国产三级 | 99在线精品视频 | se婷婷| 密桃av| 99久久久99久久国产片鸭王 | 欧美成人一区二区三区片免费 | 天天色图 | 一区二区三区网站 | 毛片入口 | 国产一区免费视频 | 国产亚韩| 中文字幕亚洲一区二区va在线 | 欧美日韩在线免费 | 自拍视频网站 | 99热在线播放 | 亚洲看片网站 | 亚洲精品第一国产综合野 | 一级黄色淫片 | 国产一区二区三区不卡av | 91视频网址 | 成人欧美一区二区三区黑人孕妇 | 日干夜操| 国产精品久久久久久久久久久久午夜片 | www.亚洲精品 | 一区二区三区精品在线视频 | 色性av | 亚洲高清成人在线 |