constint NUM_THREADS = 20; typedeflonglong ll; int m = 10000, n = 10000; int mat[10000][10000]; int vec[10000], ans[10000];
voidmakeRandomMatrix() { srand(time(NULL)); int i, j; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { mat[i][j] = rand() % 10 + 1; } } }
voidmakeRandomVector() { srand(time(NULL)); int i; for (i = 0; i < n; i++) { vec[i] = rand() % 10 + 1; } }
voidfuny(int a[], int cur) { int i; for (i = 0; i < n; i++) { ans[cur] += a[i] * vec[i]; } }
voidf() { int i; for (i = 0; i < m; i++) { funy(mat[i], i); } }
voidfp() { int i; #pragma omp parallel { int id = omp_get_thread_num(); #pragma omp parallel for for (i = id; i < m; i += NUM_THREADS) { funy(mat[i], i); } } }
intmain() { printf("Makeing matrix(10000*10000) & vector(10000*1)...\n"); makeRandomMatrix(); makeRandomVector(); double start_time = omp_get_wtime(); f(); // for (int i = 0; i < m; i ++) printf("%d%c\n", ans[i], ' \n'[i==n-1]); double end_time = omp_get_wtime(); printf("1 thread --- Running time=%f s\n", end_time - start_time); start_time = omp_get_wtime(); fp(); end_time = omp_get_wtime(); // for (int i = 0; i < m; i ++) printf("%d%c\n", ans[i], ' \n'[i==n-1]); printf("20 threads --- Running time=%f s\n", end_time - start_time); return0; }