CUDA實現平衡樹求字首和

2021-05-26 06:16:28 字數 1014 閱讀 1878

應實驗要求,使用cuda實現平衡樹的字首求和演算法,很可惜只是做了乙個簡單的版本,沒有時間做更多的優化,在此記錄下來以後在進行深入優化。

平衡樹求字首和的思想是使用邏輯上的樹形結構,先一次遍歷所有葉子節點實現相鄰的兩個節點相加,層層上播直到根節點。然後從根節點在執行一次向下的播送過程,最後所有的字首和都是存放在葉子節點中。

為了簡單起見使用一維陣列儲存所有的葉子節點和相應的中間節點,然後使用一些小標小技術讓其滿足邏輯上的樹形結構。

插入序列**:

int getjump(int n,int step)

void prefixst(int *input,int n)

step *= 2;

}/*down broadcast

*/ // printf("step : %d",step);

step /= 2;

for(h = 1;h < n;h *= 2)

else

}step /= 2;

}}

然後依據這個序列**做一些小小的改進就能得到cuda的**,其中把迴圈遍歷改為執行緒的tid就行。這裡使用最簡單的1 blocks,n 列threads的方式,為了與一維陣列進行很好的關聯,便於操作執行緒。

插入並行**:

使用cuda的自帶計時器:發現時間效果很差,並行的算出來還沒得序列的好,看來還有很大的優化空間...

float time;

cudaevent_t start,stop;

cudaeventcreate(&start);

cudaeventcreate(&stop);

cudaeventrecord(start,0);

/*function runs*/

cudaeventrecord(stop,0);

cudaeventsynchronize(stop);

cudaeventelapsedtime(&time,start,stop);

CUDA 求平方和

include include include define data size 1048576 int data data size void generatenumbers int number,int size printf sum cpu d n sum system pause 要怎麼把計...

java 實現平衡樹

package 平衡樹 public class tree 計算高度 public int hi node t public void insert int element public node insert int data,node rot if data rot.data 用當前值與根節點的...

字首樹 java實現)

package class 07 字首樹 例子 乙個字串型別的陣列arr1,另乙個字串型別的陣列arr2。arr2中有哪些字元,是arr1中出現的?請列印 arr2中有哪些字元,是作為arr1中某個字串字首出現的?請列印 arr2中有哪些字元,是作為arr1中某個字串字首出現的?請列印arr2中出現...