Advertisement
piffy

Esercizio_parallelo

Sep 3rd, 2014
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <sys/time.h>
  5.  
  6. const int DIM=1000;
  7.  
  8. float Nmassimo;
  9. float Nminimo;
  10. float Nsomma;
  11. float array[1000][1000];
  12.  
  13.  
  14. //TEMPO
  15. long long current_timestamp() {
  16.     struct timeval te;
  17.     gettimeofday(&te, NULL); // get current time
  18.     long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
  19.     // printf("milliseconds: %lld\n", milliseconds);
  20.     return milliseconds;
  21. }
  22. //TEMPO
  23.  
  24. void *massimo(void *ptr)
  25. {
  26.     Nmassimo=-20001;
  27.         int i=0,j=0;
  28.     for(i=0; i<DIM; i++)
  29.     {
  30.         for(j=0; j<DIM; j++)
  31.         {
  32.             if(array[i][j]>Nmassimo)
  33.                 Nmassimo=array[i][j];
  34.         }
  35.     }
  36. }
  37.  
  38. void *minimo(void *ptr)
  39. {
  40.     Nminimo=20001;
  41.     int i=0,j=0;
  42.     for(i=0; i<DIM; i++)
  43.     {
  44.         for(j=0; j<DIM; j++)
  45.         {
  46.             if(array[i][j]<Nminimo)
  47.                 Nminimo=array[i][j];
  48.         }
  49.     }
  50. }
  51.  
  52. void *somma(void *ptr)
  53. {
  54.     Nsomma=0;
  55.         int i=0,j=0;
  56.     for(i=0; i<DIM; i++)
  57.     {
  58.         for(j=0; j<DIM; j++)
  59.         {
  60.             Nsomma+=array[i][j];
  61.         }
  62.     }
  63. }
  64.  
  65. int main()
  66. {
  67.     long long tempo1=current_timestamp();
  68.     pthread_t tMassimo, tMinimo,tSomma;
  69.     srand (time(NULL));
  70.         int i=0,j=0;
  71.     for(i=0; i<DIM; i++)
  72.     {
  73.         for(j=0; j<DIM; j++)
  74.         {
  75.             array[i][j]=(rand() % 40001)+(-20000);
  76.         }
  77.     }
  78.  
  79.     pthread_create( &tMassimo, NULL, massimo, NULL);
  80.     pthread_create( &tMinimo, NULL, minimo, NULL);
  81.     pthread_create( &tSomma, NULL, somma, NULL);
  82.  
  83.     pthread_join( tMassimo, NULL);
  84.     pthread_join( tMinimo, NULL);
  85.     pthread_join( tSomma, NULL);
  86.    
  87.     printf("Numero massimo: %f \n",Nmassimo);
  88.     printf("Numero minimo: %f \n",Nminimo);
  89.     printf("Somma: %f \n",Nsomma);
  90.    
  91.     long long tempo2=current_timestamp();
  92.  
  93.     printf("Tempo impiegato: %lli millisencondi \n",(tempo2-tempo1));
  94.  
  95.     exit(0);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement