Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. //put numbers 0 to N into an array
  7. //sum numers from 2 arrs into 3rd
  8. #define N 12//note this is 12 and not 10
  9. long thread_elems = N/4;
  10.  
  11. /* A structure we'll use to pass arguments to our thread function. */
  12. struct thread_arg {
  13. //sum numbers from 2 arrs into 3rd
  14.    long arr1[N];
  15.    long arr2[N];
  16.    long arr3[N];
  17.    int id;
  18. };
  19.  
  20. void *sum(void *varg){
  21.     struct thread_arg chunk= *((struct thread_arg*)varg);
  22.     long start = (chunk.id)*thread_elems;
  23.     long end = start + thread_elems;
  24.    
  25.     long i,sum=0;
  26.     for (i=start; i<end;i++){
  27.         //store the int to our retArr
  28.         //if end < N{
  29.                 printf("%d %d ", chunk.arr1[i], chunk.arr2[i]);
  30.         chunk.arr3[i] = chunk.arr1[i] + chunk.arr2[i];
  31.                 (*((struct thread_arg*)varg)).arr3[i] = (*((struct thread_arg*)varg)).arr1[i] + (*((struct thread_arg*)varg)).arr2[i];
  32.         printf("  %d  ", chunk.arr3[i]);
  33.                 //}
  34.     }
  35.         printf("\n");
  36. }
  37.  
  38. int main(){
  39.     pthread_t tid[4];
  40. //  ids[4];
  41.    
  42.     long ret_arr[N];
  43.    
  44.     struct thread_arg *arguments = malloc(4 * sizeof(struct thread_arg));
  45.     //build arrs in struct from arrays in HOST memory
  46.     int i, j;
  47.     for (i=0; i<4; i++){
  48.         for(j=0;j<N;j++){
  49.             (arguments[i]).arr1[j]= j;
  50.             (arguments[i]).arr2[j]= j;
  51.                         printf("%d %d ", (arguments[i]).arr1[j], (arguments[i]).arr2[j]);
  52.         }
  53.     }
  54.  
  55.        printf("\n");
  56.    
  57.     for (i=0; i<4; i++){
  58.         (arguments[i]).id = i;
  59.         pthread_create(&tid[i], NULL, sum, (void*)&arguments[i]);
  60.     }
  61.     //don't destroy threads until they are all done!
  62.     for (i=0;i<4;i++){
  63.         pthread_join(tid[i], NULL);
  64.     }
  65.  
  66.  
  67.                 for(j=0;j<N;j++){
  68.                         printf("  %d  ", (arguments[0]).arr3[j] );
  69.                 }
  70.  
  71.    
  72.     //get the results into a single arr
  73.     //arg arrays are disjoint and don't share mem!
  74.     long t, start, end;
  75.     for (i=0;i<4;i++){
  76.         start = i*thread_elems;
  77.         end = start + thread_elems;
  78.         for(t=start; t<end; t++){
  79.             ret_arr[t] = (arguments[i]).arr3[t];
  80.         }
  81.     }
  82.    
  83.     for (i=0;i<N;i++){
  84.         printf("%d ", ret_arr[i]);
  85.     }
  86.     printf("\n");
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement