[英]How to optimize matrix vector multiplication with openmp?
我在 C 中創建了一個執行矩陣向量乘法的程序。 我使用 openMP 指令並行執行計算。 在編譯代碼時,有沒有辦法在沒有優化標志的情況下使用 openMP 進一步優化(= 更少的執行時間)矩陣向量乘法?
代碼:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <omp.h>
#define SIZE 1000
int main() {
float A[SIZE][SIZE], b[SIZE], c[SIZE];
int i, j;
double tStart, tEnd;
/* Init */
for (i=0; i < SIZE; i++)
{
for (j=0; j < SIZE; j++)
/* set A_ij to the minimum of x and y */
A[i][j] = fminf(i*1.0/(j+1.0),j*1.0/(i+1.0));
b[i] = 1.0 * (i+1);
c[i] = 0.0;
}
tStart = omp_get_wtime();
#pragma omp parallel for private(i,j)
for (i=0; i < SIZE; i++)
for (j=0; j < SIZE; j++)
c[i] = c[i] + A[i][j] * b[j];
tEnd = omp_get_wtime();
printf("time taken = %.20f\n", tEnd - tStart);
return 0;
}
不要這樣做。 找一個好的 BLAS 庫(有很多免費的,谷歌是你的朋友)。
(做到這一點很重要,並且“最好的代碼是您不必編寫的代碼。”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.