简体   繁体   中英

How to optimize matrix vector multiplication with openmp?

I have created a program in C that does matrix-vector multiplication. I used openMP directives to execute the calculations in parallel. Is there a way though to further optimize (= less execution time) matrix vector multiplication with openMP without optimizations flags when compiling the code?

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).

(Getting this right is non-trivial, and "The best code is the code you do not have to write.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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