Advertisement
ahmad_zizo

NOUH

Apr 29th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/mman.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <sys/wait.h>
  10. #include <sys/shm.h>
  11. #include <sys/stat.h>
  12.  
  13. #define M 100
  14.  
  15. void printm(int size, double *m, char *s)
  16. {
  17.     printf("%s \n\n", s);
  18.     int i,j;
  19.     for(i=0; i<size; i++)
  20.     {
  21.         for(j=0; j<size; j++)
  22.         {
  23.             printf("%g\t" ,*(m+M*i+j));
  24.         }
  25.         printf("\n\n");
  26.     }
  27. }
  28.  
  29.  
  30. void populate (int size, double *a, double *b)
  31. {
  32.     int i,j;
  33.     for(i=0; i<size; i++)
  34.     {
  35.         for(j=0; j<size; j++)
  36.         {
  37.             *(a+i*M+j) = 1+rand()%100;
  38.             *(b+i*M+j) = 1+rand()%100;
  39.         }
  40.     }
  41. }
  42.  
  43. void computeRow(int size,int r,double *a,double *b,double *c)
  44. {
  45.  
  46.     int j,k;
  47.  
  48.     for(j=0; j<size; j++)
  49.     {
  50.         *(c+r*M+j)=0;
  51.  
  52.         for(k=0; k<size; k++)
  53.         {
  54.             *(c+r*M+j)=*(c+r*M+j)+*(a+r*M+k)* *(b+k*M+j);
  55.         }
  56.     }
  57. }
  58.  
  59. void printTime(struct timeval start, struct timeval end)
  60. {
  61.     int h,m;
  62.     double s,t;
  63.  
  64.     if (start.tv_usec>end.tv_usec)
  65.     {
  66.         t=1000000+end.tv_usec-start.tv_usec;
  67.         t=end.tv_sec - start.tv_sec + t/1000000 - 1;
  68.  
  69.  
  70.     }
  71.     else
  72.     {
  73.         t=end.tv_usec-start.tv_usec;
  74.         t=end.tv_sec - start.tv_sec + t/1000000;
  75.     }
  76.  
  77.     h=t/3600;
  78.     t=t-h*3600;
  79.     m=t/60;
  80.     s=t-m*60;
  81.  
  82.     if(h>0)
  83.         printf("Time: %d:%d:%f hours \n" ,h,m,s);
  84.     else if (m>0)
  85.         printf("Time: %d:%f minutes \n" ,m,s);
  86.     else
  87.         printf("Time: %g secondes \n" ,s);
  88.  
  89. }
  90.  
  91. int main()
  92. {
  93.     struct timeval tvalBefore, tvalAfter;
  94.     char buff[256];
  95.     pid_t childPID;
  96.     double t;
  97.     int i,j;
  98.     int shm_fd;
  99.     double* shared_memory;
  100.     /* the size (in bytes) of the shared memory segment */
  101.     const int msize = 3*M*M*sizeof(double);
  102.     const char *name = "MATRIX_MULT";
  103.  
  104.     shm_fd = shm_open (name, O_CREAT | O_EXCL | O_RDWR, S_IRWXU | S_IRWXG);
  105.     if (shm_fd < 0)
  106.     {
  107.         perror("In shm_open()");
  108.         exit(1);
  109.     }
  110.     fprintf(stderr, "Created shared memory object %s\n", name);
  111.     /* attach the shared memory segment */
  112.     ftruncate(shm_fd, msize);
  113.     printf("shmat returned\n");
  114.     shared_memory = (double *) mmap(NULL, msize, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
  115.     if (shared_memory == NULL)
  116.     {
  117.         perror("In mmap()");
  118.         exit(1);
  119.     }
  120.     fprintf(stderr, "Shared memory segment allocated correctly (%d bytes).\n", msize);
  121.  
  122.     double *a = shared_memory;
  123.     double *b = shared_memory + M*M;
  124.     double *c = b + M*M;
  125.  
  126.     gettimeofday (&tvalBefore, NULL);
  127.     populate(M,a,b);
  128.     printf("Done populating\n");
  129.     for(i=0; i<M; i++)
  130.     {
  131.         childPID=fork();
  132.         if(childPID >= 0)
  133.         {
  134.             if(childPID==0)
  135.             {
  136.  
  137.                 gettimeofday (&tvalBefore, NULL);
  138.                 computeRow(M, i, a, b, c);
  139.                 gettimeofday (&tvalAfter, NULL);
  140.  
  141.                 printf("Elapse time for parallel matrix multiplication row number %d \n", i);
  142.                 printTime(tvalBefore, tvalAfter);
  143.  
  144.                 exit(0);
  145.  
  146.             }
  147.         }
  148.     }
  149.     int x = 0;
  150.     int stat;
  151.     do
  152.     {
  153.         x = waitpid(-1,&stat,WNOHANG);
  154.     }
  155.     while (x==0);
  156.  
  157.     printm(M,a,"Matrix a");
  158.     printm(M,b,"Matrix b");
  159.     printm(M,c,"Matrix a*b");
  160.     /* now detach the shared memory segment */
  161.     shm_unlink(name);
  162.     gettimeofday (&tvalAfter, NULL);
  163.     printf("Elapse time for PARENT of parallel matrix multiplication \n");
  164.     printTime(tvalBefore, tvalAfter);
  165.  
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement