Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <math.h>
  7. #include <sys/types.h>
  8.  
  9. #define NUM_THREADS 2
  10. #define ITERATIONS 10000000
  11.  
  12. typedef struct monte_carlo_context_t
  13. {
  14.     int iterations;
  15.     double result;
  16. } mc_context, *pmc_context;
  17.  
  18. int timespec_subtract (struct timespec *result, struct timespec *x, struct timespec *y)
  19. {
  20.   /* Perform the carry for the later subtraction by updating y. */
  21.   if (x->tv_nsec < y->tv_nsec) {
  22.     int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
  23.     y->tv_nsec -= 1000000000 * nsec;
  24.     y->tv_sec += nsec;
  25.   }
  26.   if (x->tv_nsec - y->tv_nsec > 1000000000) {
  27.     int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
  28.     y->tv_nsec += 1000000000 * nsec;
  29.     y->tv_sec -= nsec;
  30.   }
  31.  
  32.   /* Compute the time remaining to wait.
  33.      tv_usec is certainly positive. */
  34.   result->tv_sec = x->tv_sec - y->tv_sec;
  35.   result->tv_nsec = x->tv_nsec - y->tv_nsec;
  36.  
  37.   /* Return 1 if result is negative. */
  38.   return x->tv_sec < y->tv_sec;
  39. }
  40.  
  41. // Generate random numbers and count those inside the circle
  42. void *pi_monte_carlo(void* context)
  43. {
  44.     int i;
  45.     double rx, ry, rs, rt;
  46.     int count = 0;
  47.     pmc_context ctx = (pmc_context)context;
  48.     srand(time(NULL));
  49.    
  50.     for(i = 0; i < ctx->iterations; i++)
  51.     {
  52.         rx = rand() / (double)RAND_MAX;
  53.         ry = rand() / (double)RAND_MAX;
  54.  
  55.         double distance = sqrt(rx*rx + ry*ry);
  56.         if (distance <= 1.0)
  57.         {
  58.             count++;
  59.         }
  60.     }
  61.  
  62.     ctx->result = 4.0 * (count / (double)ctx->iterations);
  63. }
  64.  
  65. int main(int argc, char* argv[])
  66. {
  67.     int i;
  68.     double result = 0.0;
  69.     struct timespec clk_start, clk_end, clk_diff;
  70.  
  71.     // Start timer.
  72.     clock_gettime(0, &clk_start);
  73.  
  74.     // Threads and contexts
  75.     pthread_t threads[NUM_THREADS];
  76.     mc_context contexts[NUM_THREADS];
  77.  
  78.     for (i = 0; i < NUM_THREADS; i++)
  79.     {
  80.         contexts[i].iterations = ITERATIONS / NUM_THREADS;
  81.         pthread_create(&threads[i], NULL, pi_monte_carlo, (void*)&contexts[i]);
  82.     }
  83.  
  84.     for (i = 0; i < NUM_THREADS; i++)
  85.     {
  86.         pthread_join(threads[i], NULL);
  87.         result += contexts[i].result;
  88.     }
  89.    
  90.     // End timer.
  91.     clock_gettime(0, &clk_end);
  92.     timespec_subtract(&clk_diff, &clk_end, &clk_start);
  93.  
  94.     result /= NUM_THREADS;
  95.     printf("\nThreads: %d, Elapsed time: %d.%09d.\t|\tResult: %lf.\n", NUM_THREADS, clk_diff.tv_sec, clk_diff.tv_nsec, result);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement