亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

與OpenMP并行填充直方圖(減少數組),而無需使用關鍵部分

與OpenMP并行填充直方圖(減少數組),而無需使用關鍵部分

牛魔王的故事 2019-11-19 11:06:07
我想使用OpenMP并行填充直方圖。我想出了兩種使用C / C ++中的OpenMP進行此操作的方法。第一種方法為每個線程proccess_data_v1創建一個私有直方圖變量hist_private,將其填充成小節,然后將私有直方圖求和成hist一個critical部分中的共享直方圖。第二種方法proccess_data_v2制作一個共享直方圖數組,其數組大小等于線程數,并行填充此數組,然后并行求和該共享直方圖hist。第二種方法對我來說似乎更好,因為它避免了關鍵部分,并且并行地對直方圖求和。但是,它需要知道線程數并調用omp_get_thread_num()。我通常會嘗試避免這種情況。有沒有更好的方法來執行第二種方法而不引用線程號并使用大小等于線程數的共享數組?void proccess_data_v1(float *data, int *hist, const int n, const int nbins, float max) {    #pragma omp parallel     {        int *hist_private = new int[nbins];        for(int i=0; i<nbins; i++) hist_private[i] = 0;        #pragma omp for nowait        for(int i=0; i<n; i++) {            float x = reconstruct_data(data[i]);            fill_hist(hist_private, nbins, max, x);        }        #pragma omp critical         {            for(int i=0; i<nbins; i++) {                hist[i] += hist_private[i];            }        }        delete[] hist_private;    }}void proccess_data_v2(float *data, int *hist, const int n, const int nbins, float max) {    const int nthreads = 8;    omp_set_num_threads(nthreads);    int *hista = new int[nbins*nthreads];    #pragma omp parallel     {        const int ithread = omp_get_thread_num();        for(int i=0; i<nbins; i++) hista[nbins*ithread+i] = 0;        #pragma omp for        for(int i=0; i<n; i++) {            float x = reconstruct_data(data[i]);            fill_hist(&hista[nbins*ithread], nbins, max, x);        }        #pragma omp for        for(int i=0; i<nbins; i++) {            for(int t=0; t<nthreads; t++) {                hist[i] += hista[nbins*t + i];            }        }    }    delete[] hista;}
查看完整描述

3 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

我創建了一種改進的方法,稱為process_data_v3


#define ROUND_DOWN(x, s) ((x) & ~((s)-1))

void proccess_data_v2(float *data, int *hist, const int n, const int nbins, float max) {

    int* hista;

    #pragma omp parallel 

    {

        const int nthreads = omp_get_num_threads();

        const int ithread = omp_get_thread_num();


        int lda = ROUND_DOWN(nbins+1023, 1024);  //1024 ints = 4096 bytes -> round to a multiple of page size

        #pragma omp single

        hista = (int*)_mm_malloc(lda*sizeof(int)*nthreads, 4096);  //align memory to page size


        for(int i=0; i<nbins; i++) hista[lda*ithread+i] = 0;

        #pragma omp for

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

            float x = reconstruct_data(data[i]);

            fill_hist(&hista[lda*ithread], nbins, max, x);

        }


        #pragma omp for

        for(int i=0; i<nbins; i++) {

            for(int t=0; t<nthreads; t++) {

                hist[i] += hista[lda*t + i];

            }

        }


    }

    _mm_free(hista);

}


查看完整回答
反對 回復 2019-11-19
?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

您可以在并行區域內分配大數組,您可以在其中查詢所使用的實際線程數:


int *hista;

#pragma omp parallel 

{

    const int nthreads = omp_get_num_threads();

    const int ithread = omp_get_thread_num();


    #pragma omp single

    hista = new int[nbins*nthreads];


    ...

}

delete[] hista;

為了獲得更好的性能,我建議您將每個線程的塊的大小四舍五入為hista系統內存頁面大小的倍數,即使這可能在不同的部分直方圖之間留下空白。這樣,您既可以防止在NUMA系統上進行錯誤共享,又可以防止對遠程內存的訪問(但不能在最后的還原階段)。


查看完整回答
反對 回復 2019-11-19
?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

這實際上取決于所使用的內存管理器。例如,在某些發行版中,glibc配置為使用每個線程的競技場,并且每個線程都有自己的堆空間。較大的分配通常實現為匿名mmap,因此總是獲得新的頁面。但是,哪個線程分配了內存并不重要。哪個胎面首先接觸每個特定頁面很重要-Linux上當前的NUMA策略是“首次接觸”,即物理內存頁面來自NUMA節點,在該節點中,第一次接觸該頁面的代碼在此運行。

查看完整回答
反對 回復 2019-11-19
  • 3 回答
  • 0 關注
  • 866 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號