3 回答

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);
}

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系統上進行錯誤共享,又可以防止對遠程內存的訪問(但不能在最后的還原階段)。

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