Addition of Two Matrix By OpenMP Model
This is a program in which the user gives the Number of Elements as input, then from that input 3 dynamic arrays are created in which first two are assigned with random values. Using the OpenMP model, the addition is distributed among different threads and then the addition of the above matrix is done and finally stored in the third Matrix.
The Code
To Compile
Found Suggestions
Improvements ? Comment them ! ;)
Screenshots [DEMO]
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<omp.h> int main() { int *a,*b,*c,i,N; printf("Enter The No of Elements for both arrays: "); scanf("%d",&N); a=(int*)malloc(sizeof(int)*N); b=(int*)malloc(sizeof(int)*N); c=(int*)malloc(sizeof(int)*N); for(i=0;i<N;i++) { //scanf("%d",&a[i]); a[i]=rand()/100000; } for(i=0;i<N;i++) { b[i]=rand()/100000; //scanf("%d",&b[i]); } printf("A[%d]:\t",N); for(i=0;i<N;i++) { printf("%d ",a[i]); //a[i]=rand(); } printf("\n"); printf("B[%d]:\t",N); for(i=0;i<N;i++) { printf("%d ",b[i]); //a[i]=rand(); } printf("\n"); printf("C[%d]:\t",N); #pragma omp parallel { int id,i,Ntrds,istart,iend; id=omp_get_thread_num(); Ntrds=omp_get_num_threads(); istart=id*N/Ntrds; iend=(id+1)*N/Ntrds; if(id==Ntrds-1) iend=N; for(i=istart;i<iend;i++) { c[i]=b[i]+a[i]; printf("%d ",c[i]); } } printf("\n"); return 0; }
To Compile
$ gcc -fopenmp -o <objname> <filename>.c
Found Suggestions
Improvements ? Comment them ! ;)
Comments
Post a Comment