Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. /*Name: Sushant Baskota*/
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #define NUMBER_OF_POINTS 50000000
  8.  
  9.  
  10. void *runner (void *param);
  11.  
  12. int circle_count = 0;
  13. double random_double ()
  14. {
  15. return random () / ((double) RAND_MAX + 1);
  16. }
  17.  
  18. int main (int argc, char *argv[])
  19. {
  20.  
  21. if(argc<2||argc>2){
  22. printf("<number of threads> should be a positive integer");
  23. return 0;
  24. }
  25.  
  26. int thikxa =atoi(argv[1]);
  27. if(thikxa<=0){
  28. printf("Number of threads needs to be more than 0");
  29. return 0;
  30. }
  31. int points_per_thread = NUMBER_OF_POINTS / thikxa;
  32. int i;
  33. double Pi;
  34. pthread_t workers[thikxa];
  35. srandom((unsigned)time(NULL));
  36.  
  37. for(i=0; i<thikxa; i++)
  38. pthread_create(&workers[i], 0, runner, &points_per_thread);
  39. for(i=0; i<thikxa; i++)
  40. pthread_join(workers[i], NULL);
  41.  
  42. Pi = 4.0*circle_count/NUMBER_OF_POINTS;
  43.  
  44. printf("Pi: = %f\n", Pi);
  45.  
  46. return 0;
  47.  
  48.  
  49. }
  50.  
  51. void *runner(void *param)
  52. {
  53. int POINTS;
  54. POINTS = *((int *)param);
  55. int i;
  56. int hit_count = 0;
  57. double x,y;
  58. for(i=0; i<POINTS; i++)
  59. {
  60. x=random_double()*2.0-1.0;
  61. y=random_double()*2.0-1.0;
  62.  
  63. if(sqrt(x*x+y*y)<1.0)
  64. ++hit_count;
  65. }
  66.  
  67. circle_count += hit_count;
  68. pthread_exit(0);
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement