矩阵转置实际上是很简单的,但是数据类型是复数的时候,编译器会认为实部虚部有数据依赖从而不能自动向量化,通过编译指导#pragma ivdep可以解除这种貌似的数据依赖,但是自动向量化的执行速度应该没有OpenMP快,干脆改成OpenMP并行得了。
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 10240
#include <time.h>
#define NUM_THREADS 4
int main( int argc, char *argv[] )
{
#if 1
int c[N][N],trans[N][N];
for (int i=0; i<N; i++){
for(int j = 0; j <N; ++ j){
c[i][j] = i;
trans[i][j] = 0;
}
}
double start = omp_get_wtime();
int i,j,temp;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for private(j)
for(i = 0; i <N; ++ i){
for(j = 0; j <N; ++ j){//
//c[j][i] = c[i][j] ;
c [i][j] = c[j][i];//这样比上面一种要快
}
}
double end = omp_get_wtime();
double runtime = end-start;
#endif
printf("runtime = %f\n",runtime);
return 0;
}