该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
vs下编译并行是串行时间一半,但linux下就不行,gcc -fopenmp 编译的
#include
#include
#include
#define N 4000
#define num_thread 4
int A[N][N], B[N][N];
long long C[N][N];
void arr(int n)
{
int i, j, k, t;
double paralleltime, serialtime;
clock_t startTime, endTime;
for (i = 0; i
{
t = i + 1;
for (j = 0; j
{
A[i][j] = t++;
B[i][j] = 1;
}
}
omp_set_num_threads(num_thread);
//----------------矩阵乘法并行算法------------------
startTime = clock();
#pragma omp parallel shared(A,B,C) private(i,j,k)
{
#pragma omp for schedule(dynamic)
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
C[i][j] = 0;
for (k = 0; k < n; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
//for (i = 0; i < 200; i++)
//{
// if (i % 199 == 0)
// printf("\n");
// for (j = 0; j < 200; j++)
// {
// printf("%lld \t", C[i][j]);
// }
// printf("\n");
//}
endTime = clock();
paralleltime =(endTime - startTime) / CLOCKS_PER_SEC;
//----------------矩阵乘法串行算法------------------
startTime = clock();
for (i = 0; i
{
for (j = 0; j
{
C[i][j] = 0;
for (k = 0; k
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
endTime = clock();
serialtime = (endTime - startTime) / CLOCKS_PER_SEC;
printf("paralleltime=%f s\n", paralleltime);
printf("serialtime=%f s\n", serialtime);
}
int main()
{
int n=4000;
arr(n);
system("pause");
return 0;
}