Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include<string.h>
  2. #include<stdio.h>
  3. #include<time.h>
  4. #include<math.h>
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9. /**
  10. * @author cristian.chilipirea
  11. *
  12. */
  13.  
  14. int N = 1024*1024*100;
  15. int P;
  16. int a;
  17.  
  18. void* threadFunction(void *var)
  19. {
  20. int thread_id = *(int*)var;
  21.  
  22. int start = thread_id * ceil((double)N/P);
  23. int end;
  24. if( N > (thread_id + 1) * ceil((double)N/P))
  25. end = (thread_id + 1) * ceil((double)N/P);
  26. else
  27. end = N;
  28.  
  29. int c;
  30. for (int i = start; i < end; ++i)
  31. c = sqrt(a);
  32. }
  33.  
  34. int main(int argc, char * argv[]) {
  35. P = atoi(argv[1]);
  36. clock_t begin = clock();
  37.  
  38. {
  39. pthread_t tid[P];
  40. int thread_id[P];
  41.  
  42. for(int i = 0; i < P; ++i)
  43. thread_id[i] = i;
  44.  
  45. for(int i = 0; i < P; i++)
  46. pthread_create(&(tid[i]), NULL, threadFunction, &(thread_id[i]));
  47.  
  48. for(int i = 0; i < P; i++)
  49. pthread_join(tid[i], NULL);
  50. }
  51.  
  52. clock_t end = clock();
  53. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  54.  
  55. printf("%f\n",time_spent);
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement