我希望使用OpenMP并行地填充直方图。我已经用C/C++实现了两种不同的OpenMP方法。
第一种方法
第二种方法
对我来说,第二种方法似乎更好,因为它避免了关键部分,并且可以并行地对直方图进行求和。但是,它需要知道线程数并调用
基于 @HristoIliev 的建议,我创建了一个名为
第一种方法
proccess_data_v1
为每个线程创建一个私有直方图变量hist_private
,在并行环境下填充它们,然后在critical
部分中将私有直方图总和到共享直方图hist
中。第二种方法
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;
}
基于 @HristoIliev 的建议,我创建了一个名为
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);
}
proccess_data_v1
不是最快的吗?因为我们不需要共享内存。我尝试了版本2和3,它们比v1慢。有什么建议吗? - Ardian