Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Apr 5th, 2020 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <time.h>
  5.  
  6. #define PI25DT 3.141592653589793238462643
  7.  
  8. #define INTERVALS 10000000
  9.  
  10. int thread_count = 0;
  11. double pi_global = 0;
  12. pthread_mutex_t pi_lock;
  13.  
  14. void *pi_calc(void *rank);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     double time2;
  19.     time_t time1 = clock();
  20.  
  21.     thread_count = strtol(argv[1], NULL, 10);
  22.     pthread_t threads[thread_count];
  23.  
  24.     for (long i = 0; i < thread_count; i++)
  25.     {
  26.         pthread_create(&(threads[i]), NULL, pi_calc, (void*)i);
  27.     }
  28.  
  29.     for (long i = 0; i < thread_count; i++)
  30.     {
  31.         pthread_join(threads[i], NULL);
  32.     }
  33.  
  34.     pthread_mutex_destroy(&pi_lock);
  35.  
  36.     double dx = 1.0 / (double)INTERVALS;
  37.     double pi = dx * pi_global;
  38.  
  39.     time2 = (clock() - time1) / (double)CLOCKS_PER_SEC;
  40.  
  41.     printf("Computed PI %.24f\n", pi);
  42.     printf("The true PI %.24f\n", PI25DT);
  43.     printf("Error       %.24f\n\n", PI25DT - pi);
  44.     printf("Elapsed time (s) = %f\n", time2);
  45.  
  46.     return 0;
  47. }
  48.  
  49. void *pi_calc(void *rank)
  50. {
  51.     long int i, my_rank = (long)rank, intervals = INTERVALS;
  52.     double x, dx, f, sum;
  53.     long remainder = 0;
  54.  
  55.     if (my_rank == thread_count-1) {
  56.         remainder = intervals %  thread_count;
  57.     }
  58.  
  59.     long int intervalStart = my_rank*(intervals/thread_count) + (intervals/thread_count);
  60.     long int intervalEnd = my_rank*(intervals/thread_count);
  61.  
  62.  
  63.     sum = 0.0;
  64.     dx = 1.0 / (double)intervals;
  65.  
  66.     for (i = intervalStart+remainder ; i > intervalEnd; i--)
  67.     {
  68.         x = dx * ((double)(i - 0.5));
  69.         f = 4.0 / (1.0 + x * x);
  70.         sum = sum + f;
  71.     }
  72.  
  73.     pthread_mutex_lock(&pi_lock);
  74.     pi_global += sum;
  75.     pthread_mutex_unlock(&pi_lock);
  76.  
  77.     return NULL;
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top