Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/time.h>
- #include <sys/wait.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <omp.h>
- double *a,*b,*c;
- int M;
- void print_matrix(double *m)
- {
- int i,j;
- for(i=0; i<M; i++)
- {
- for(j=0; j<M; j++)
- {
- printf("%g\t" ,*(m+M*i+j));
- }
- printf("\n");
- }
- printf("\n");
- }
- void calc_row(int r)
- {
- int row = r;
- int j,k;
- for(j=0; j<M; j++)
- {
- *(c+row*M+j)=0;
- for(k=0; k<M; k++)
- {
- *(c+row*M+j)=*(c+row*M+j)+*(a+row*M+k)* *(b+k*M+j);
- }
- }
- }
- void generate_matrix(double *A)
- {
- int i,j;
- for(i=0; i<M; i++)
- {
- for(j=0; j<M; j++)
- {
- *(A+i*M+j) = 1+rand()%100;
- }
- }
- }
- int main(int argc, char *argv[])
- {
- srand(time(NULL));
- if(argc != 2)
- {
- printf("\nERROR\n");
- exit(0);
- }
- if(atoi(argv[1]) < 1 )
- {
- printf("\nERROR\n");
- exit(0);
- }
- clock_t t1,t2;
- int i;
- int args[2];
- M =args[1] = atoi(argv[1]);
- a = (double *)malloc(M*M*sizeof(double));
- b = (double *)malloc(M*M*sizeof(double));
- c = (double *)malloc(M*M*sizeof(double));
- generate_matrix(a);
- generate_matrix(b);
- t1 = clock();
- #pragma omp paralell num_threads(M)
- {
- #pragma omp for
- for(i=0 ; i< M ; i++)
- {
- calc_row(i);
- }
- }
- t2= clock();
- print_matrix(a);
- print_matrix(b);
- print_matrix(c);
- printf("\ntime taken = %f\n\n",(double)(t2-t1)/(CLOCKS_PER_SEC / 1000.0));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement