Matrix and Vector Multiplication In C using OpenMP Model
This is a program which will multiply a vector and a matrix and display it. Basically a 1D Dynamic array and another 2D Dynamic array gets created and values are automatically assigned to it using the rand(). Here, OpenMP model is used for parallel computation of the task.
Screenshot
The Code:
Have Doubts, ask us. We will try our level best to make you understand !
Screenshot
The Code:
#include<stdio.h> #include<malloc.h> #include<omp.h> #include<time.h> int main() { int **a,n,i,j,*b,*c,k; clock_t st,en; double t; printf("Enter the N: "); scanf("%d",&n); // For the A Matrix a=(int**)malloc(sizeof(int*)*n); for(i=0;i<n;i++) a[i]=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { a[i][j]=rand()/100000000; } } //For the B Matrix b=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) b[i]=rand()/100000000; //For The C Matrix c=(int*)malloc(sizeof(int)*n); for(i=0;i<n;i++) c[i]=0; printf("The A MAtrix :\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d ",a[i][j]); } printf("\n"); } printf("The B Vector:\n"); for(j=0;j<n;j++) { printf("%d ",b[j]); } printf("\n"); st=clock(); #pragma omp parallel num_threads(4) { #pragma omp for for(i=0;i<n;i++) { for(k=0;k<n;k++) { c[i]=c[i]+(a[i][k]*b[k]); } } } en=clock(); for(i=0;i<n;i++) { printf("%d ",c[i]); } t=(double)(en-st)/CLOCKS_PER_SEC; printf("\n%lf Time Required !\n",t); return 0; }
Have Doubts, ask us. We will try our level best to make you understand !
Comments
Post a Comment