Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUMTHREADS 4
  6. #define  N 3
  7.  
  8. double *a, *b;
  9.  
  10. void *dot(void *arg)
  11. {
  12.  
  13.    int  start, end  ;
  14.    long offset, i;
  15.    double  *mysum = malloc(sizeof(double));
  16.    *mysum=0.0;
  17.    offset = (long)arg;
  18.      
  19.    start = offset*N;
  20.    end   = start + N;
  21.    
  22.    for (i=start; i<end ; i++)
  23.     {
  24.       *mysum += (a[i] * b[i]);
  25.     }
  26.  
  27.    printf("ja   wyliczylem %d, %d,  %f \n", start,  end, *mysum);
  28.      pthread_exit((void *) mysum);
  29. }
  30.  
  31. int main (int argc, char *argv[])
  32. {
  33. pthread_t   watki[NUMTHREADS];
  34.  
  35. long i;
  36. double   iloczyn=0.0;
  37. double *loc_iloczyn;
  38. pthread_attr_t  atr;
  39. a= (double*) malloc (NUMTHREADS*N*sizeof(double));
  40. b= (double*) malloc (NUMTHREADS*N*sizeof(double));
  41.  
  42. for (i=0; i<N*NUMTHREADS; i++) {
  43.   a[i]=1.0;
  44.   b[i]=2.0*a[i];
  45.   }
  46.  
  47. pthread_attr_init (&atr);
  48. pthread_attr_setdetachstate( &atr, PTHREAD_CREATE_JOINABLE);
  49.  
  50. for(i=0; i<NUMTHREADS; i++)
  51.   {
  52.      pthread_create(&(watki[i]), NULL, dot, (void *)i);
  53.    }
  54.  
  55.  
  56. for(i=0; i<NUMTHREADS; i++) {
  57.   pthread_join(watki[i], (void **)&loc_iloczyn);
  58.   iloczyn = iloczyn + *loc_iloczyn;
  59. }
  60. pthread_attr_destroy(&atr);
  61. printf ("iloczyn =  %f \n", iloczyn);
  62. free(a);
  63. free(b);
  64. free(loc_iloczyn);
  65. pthread_exit(NULL);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement