【发布时间】:2021-08-23 21:51:07
【问题描述】:
我尝试将#pragma omp simd 应用于以下代码(循环),但它似乎不起作用(没有速度提升)。我也尝试了#pragma omp simd linear,但我所有的尝试都导致了段错误。
https://github.com/Rdatatable/data.table/blob/master/src/fsort.c#L209 https://github.com/Rdatatable/data.table/blob/master/src/fsort.c#L184
甚至可以用simd 增加一个向量吗?示例:
#include <stdio.h>
#include <stdlib.h>
int main() {
int len = 1000;
int tmp[len];
for(int i=0; i<len; ++i) {
tmp[i]=rand()%100;
}
int *thisCounts = (int *) calloc(len, sizeof(int));
for (int j=0; j<len; ++j) {
thisCounts[tmp[j]]++;
}
for (int j=0; j<len; ++j) {
printf("%d, ",thisCounts[j]);
}
free(thisCounts);
return 0;
}
仅供参考,第 209 行是最耗时的,我正在努力改进。
谢谢
【问题讨论】:
-
请edit您的问题使其独立(将您要优化的代码减少到minimal reproducible example)。
-
这样够好吗?
-
这本质上是一个直方图操作,不容易用 SIMD 向量化。
-
也是半相关的:Micro Optimization of a 4-bucket histogram of a large array or list 确实涵盖了一般情况(桶比 SIMD 向量的元素多,这就是使该版本高效的原因)。 How to speed up this histogram of LUT lookups? 底部有一些 AVX2 / AVX-512 链接。
标签: c parallel-processing histogram openmp simd