我正在尝试使用Intel AVX2和Posix线程进行一些基准测试。 假设我正在尝试查找样本中的最小值。 当我创建一个简单的程序时,我将运行avx_min函数。 当我创建一个内部创建Posix线程的程序时,已将avx_min的实现更改为avx_min_thread,如下所示,但实际实现保持不变。此函数可用于多个线程,并且不需要同步,因为线程不会“冲突”(tid = 0、1、2等)。
当我编译两个实现而没有指定任何优化标志时,它们给我两个相同的时间结果。另一个大小,当我使用-O3标志进行编译时,它们导致不同的执行时间,而我无法弄清楚为什么会发生这种情况。
P.S:我使用以下命令对其进行编译:
情况1(不创建线程):g ++ -mavx2 -O3 -o avxMinO3 avxMinO3.cpp
案例2(在内部创建posix线程):g ++ -mavx2 avxMinO3_t.cpp -lpthread -O3 -o avxMinO3_t
P.S 2:
Case 1:
double initialize_input(int32_t** relation, int32_t value_bound, int32_t input_size){
clock_t t;
srand(time(NULL));
t = clock();
for(int32_t i = 0 ; i < input_size ; i++){
(*relation)[i] = rand() % value_bound;
}
t = clock() - t;
return ((double) t) / CLOCKS_PER_SEC;
}
int* avx_min(int32_t** relation, int32_t rel_size, double* function_time){
clock_t tic, tac;
__m256i input_buffer;
int32_t* rel = (*relation);
__m256i min = _mm256_set_epi32(INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX);
tic = clock();
for(int i = 0 ; i < rel_size ; i += 8){
input_buffer = _mm256_stream_load_si256((__m256i*)&rel[i]);
min = _mm256_min_epi32(min, input_buffer);
}
tac = clock();
double time_diff = (double)(tac - tic);
(*function_time) = time_diff / CLOCKS_PER_SEC;
int* temp = (int*)&min;
return temp;
}
int main(int argc, char** argv) {
int32_t* relation;
double* function_time;
int32_t input_size = 1024 * 1024 * 1024;
int32_t value_bound = 1000;
int alloc_time = initialize_input(&relation, value_bound, input_size);
int* res = avx_min(&relation, input_size, function_time);
return 0;
}
情况2:
template<typename T>
struct thread_input {
T* relation;
T rel_size;
double function_time;
short numberOfThreads;
short tid;
};
template<typename T, typename S, typename I, typename RELTYPE>
T** createAndInitInputPtr(S numberOfThreads, I rel_size, S value_bound, RELTYPE** relation ){
T **result = new T*[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
result[i] = new T;
result[i]->rel_size = rel_size;
result[i]->relation = (*relation);
result[i]->numberOfThreads = numberOfThreads;
result[i]->tid = i;
}
return result;
}
void* avx_min_t(void* input){
clock_t tic, tac;
struct thread_input<int32_t> *input_ptr;
input_ptr = (struct thread_input<int32_t>*) input;
int32_t* relation = input_ptr->relation;
int32_t rel_size = input_ptr->rel_size;
int32_t start = input_ptr->tid * 8;
int32_t offset = input_ptr->numberOfThreads * 8;
__m256i input_buffer;
__m256i min = _mm256_set_epi32(INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX, INT32_MAX);
tic = clock();
for(int i = start ; i < rel_size ; i += offset){
input_buffer = _mm256_stream_load_si256((__m256i*)&relation[i]);
min = _mm256_min_epi32(min, input_buffer);
}
tac = clock();
double time_diff = (double)(tac-tic);
time_diff = time_diff / CLOCKS_PER_SEC;
input_ptr->function_time = time_diff;
}
int main(int argc, char* argv[]){
int rel_size = 1024 * 1024 * 1024;
short numberOfThreads = 1;
short value_bound = 1000;
pthread_t* threads = new pthread_t[numberOfThreads];
short flag = 1; // flag to check proper aligned memory allocations
int32_t* relation;
double alloc_time = 0.0;
flag = posix_memalign((void**)&relation, 32, rel_size * sizeof(int32_t));
if(flag) {
std::cout << "Memory allocation problem. Exiting..." << std::endl;
exit(1);
}
alloc_time += initialize_input(&relation, value_bound, rel_size);
struct thread_input<int32_t> **input_ptr = createAndInitInputPtr<struct thread_input<int32_t>, short, int, int32_t>(numberOfThreads, rel_size, value_bound, &relation);
clock_t tic = clock();
for (int i = 0; i < numberOfThreads; i++) {
pthread_create(&threads[i], NULL, avx_min_t,(void*) input_ptr[i]);
}
for (int i = 0; i < numberOfThreads; i++) {
pthread_join(threads[i], NULL);
}
tic = clock()-tic;
double time = tic / CLOCKS_PER_SEC;
std::cout << time << std::endl;
return 0;
}
答案 0 :(得分:3)
void* avx_min_t(void* input)
对min
无效,因此可以优化从阵列加载的SIMD工作。
其内部循环使用gcc -O3 -march=haswell
进行编译,并且clang基本相同。
.L3:
add ebx, r12d
cmp r13d, ebx
jg .L3
因此,它实际上只是asm中的一个空循环,需要0.04秒的时间将指针增加4GB / 32 bytes
次。
for(int i = start ; i < rel_size ; i += offset){
}
我认为您打算返回某项内容,因为该函数已声明为void*
,并且具有从非void函数的结尾掉下来的不确定行为。 默认情况下,GCC和clang会发出警告,甚至不需要-Wall
。 https://godbolt.org/z/Z1GWpU
<source>: In function 'void* avx_min_t(void*)':
<source>:66:1: warning: no return statement in function returning non-void [-Wreturn-type]
66 | }
| ^
始终检查编译器警告,尤其是在代码行为异常时。启用-Wall
并修复所有警告。