JustCurious
JustCurious

Reputation: 1

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

Upvotes: 0

Views: 516

Answers (1)

Jim Cownie
Jim Cownie

Reputation: 2859

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

Upvotes: 1

Related Questions