Advertisement
ahmad_zizo

test-1

Apr 29th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <sys/wait.h>
  10. #include <sys/stat.h>
  11.  
  12. double *a,*b,*c;
  13.  
  14. void print_matrix(int M, double *m)
  15. {
  16.     int i,j;
  17.     for(i=0; i<M; i++)
  18.     {
  19.         for(j=0; j<M; j++)
  20.         {
  21.             printf("%g\t" ,*(m+M*i+j));
  22.         }
  23.         printf("\n");
  24.     }
  25.     printf("\n");
  26. }
  27.  
  28. void calc_row(int *args)
  29. {
  30.     int row = args[0];
  31.     int M = args[1];
  32.     int j,k;
  33.  
  34.     for(j=0; j<M; j++)
  35.     {
  36.         *(c+row*M+j)=0;
  37.  
  38.         for(k=0; k<M; k++)
  39.         {
  40.             *(c+row*M+j)=*(c+row*M+j)+*(a+row*M+k)* *(b+k*M+j);
  41.         }
  42.     }
  43. }
  44.  
  45. void generate_matrix(int M, double *A)
  46. {
  47.     int i;
  48.     for(i=0; i<M*M; i++)
  49.     {
  50.             m[i] = 1+rand()%100;
  51.     }
  52. }
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.     double t1,t2;
  57.     int i;
  58.     int args[2];
  59.     int M;
  60.     M = args[1] = atoi(argv[1]);
  61.     pthread_t pthread[M];
  62.     t1 = clock();
  63.     printf("i will generate\n");
  64.     generate_matrix(M, a);
  65.     generate_matrix(M, b);
  66.  
  67.     printf("generated !\n");
  68.  
  69.     for(i=0 ; i< M ; i++){
  70.             args[0] = i;
  71.     if (pthread_create(&pthread[i], NULL, (void *) calc_row, (void *) &args) != 0)
  72.         {
  73.                 fprintf(stderr, "pthread create thread error");
  74.                 exit(1);
  75.         }
  76.     }
  77.     for(i = 0 ;i < M ; i++)
  78.     {
  79.         (void)pthread_join(pthread[i], NULL);
  80.     }
  81.     print_matrix(M, a);
  82.     print_matrix(M, b);
  83.     print_matrix(M, c);
  84.     t2= clock();
  85.     printf("\n\ntime taken = %f",t2-t1);
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement