简体   繁体   English

如何使用openmp优化矩阵向量乘法?

[英]How to optimize matrix vector multiplication with openmp?

I have created a program in C that does matrix-vector multiplication.我在 C 中创建了一个执行矩阵向量乘法的程序。 I used openMP directives to execute the calculations in parallel.我使用 openMP 指令并行执行计算。 Is there a way though to further optimize (= less execution time) matrix vector multiplication with openMP without optimizations flags when compiling the code?在编译代码时,有没有办法在没有优化标志的情况下使用 openMP 进一步优化(= 更少的执行时间)矩阵向量乘法?

C code:代码:

#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;
}

Don't do this.不要这样做。 Find a good BLAS library (there are many free ones and Google is your friend).找一个好的 BLAS 库(有很多免费的,谷歌是你的朋友)。

(Getting this right is non-trivial, and "The best code is the code you do not have to write.") (做到这一点很重要,并且“最好的代码是您不必编写的代码。”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM