Advertisement
am1x

erdes001.c

Aug 14th, 2022 (edited)
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | Source Code | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #include <primesieve.h>
  7.  
  8. /*
  9.     SIZE%(NTHREADS*2) must be == 0
  10. */
  11.  
  12. #define FILENAME "/dev/stdout"
  13. #define SIZE 100000000 // number of fractions to compute
  14. #define NTHREADS 4 // number of threads to utilize
  15. /*digests per thread. please, choose reasonable number,
  16. not smth like 123. checks are not performed to guarantee accuracy*/
  17. #define RESOLUTION 10
  18.  
  19. long double sum;
  20. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  21. pthread_barrier_t barrier;
  22.  
  23. unsigned long int primes[NTHREADS] = { 0 };
  24.  
  25. void *job (void *arg)
  26. {
  27.     unsigned long int index, n, p, size, end;
  28.     long double localsum;
  29.     primesieve_iterator it;
  30.     time_t t;
  31.     struct tm *time_p;
  32.     FILE *f;
  33.     char buf[BUFSIZ];
  34.  
  35.     pthread_mutex_lock(&mutex);
  36.  
  37.     index = *((unsigned long int*)arg);
  38.     size = SIZE/NTHREADS;
  39.     n = size*index;
  40.  
  41.     if (index == NTHREADS - 1)
  42.     {
  43.         end = SIZE - 1;
  44.     }
  45.     else
  46.     {
  47.         end = n + size - 1;
  48.     }
  49.  
  50.     f = fopen(FILENAME, "w");
  51.     setvbuf(f, buf, _IOLBF, BUFSIZ);
  52.  
  53.     fprintf(f, "%ld: from %ld to %ld\n", index, n + 1, end + 1);
  54.  
  55.     time(&t);
  56.     time_p = localtime(&t);
  57.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: skipping...\n",
  58.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  59.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index);
  60.  
  61.     if (primes[index])
  62.     {
  63.         primesieve_init(&it);
  64.         p = primes[index];
  65.         primesieve_skipto(&it, p - 1, p + size);
  66.     }
  67.     else
  68.     {
  69.         primesieve_init(&it);
  70.         p = primesieve_nth_prime(n + 1, 0);
  71.         printf("%ld\n", p);
  72.         primesieve_skipto(&it, p - 1, p + size);
  73.     }
  74.  
  75.     pthread_mutex_unlock(&mutex);
  76.  
  77.     pthread_barrier_wait(&barrier);
  78.  
  79.     time(&t);
  80.     time_p = localtime(&t);
  81.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: calculating...\n",
  82.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  83.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index);
  84.  
  85.     while (n <= end)
  86.     {
  87.         localsum -= (++n)/(long double)primesieve_next_prime(&it);
  88.        
  89.         time(&t);
  90.         time_p = localtime(&t);
  91.         fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: %ld\n",
  92.             time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  93.             time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index, n);
  94.  
  95.         localsum += (++n)/(long double)primesieve_next_prime(&it);
  96.  
  97.         time(&t);
  98.         time_p = localtime(&t);
  99.         fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: %ld\n",
  100.             time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  101.             time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index, n);
  102.  
  103.         for (unsigned long int i = 0; i < size/RESOLUTION - 1; i++)
  104.         {
  105.             localsum -= (++n)/(long double)primesieve_next_prime(&it);
  106.             localsum += (++n)/(long double)primesieve_next_prime(&it);
  107.         }
  108.     }
  109.  
  110.     primesieve_free_iterator(&it);
  111.  
  112.     time(&t);
  113.     time_p = localtime(&t);
  114.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: calculated! partial result: %.20Lf\n",
  115.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  116.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index, localsum);
  117.  
  118.     fclose(f);
  119.  
  120.     sum += localsum;
  121.  
  122.     return NULL;
  123. }
  124.  
  125. int main (int argc, char *argv[])
  126. {
  127.     pthread_t threads[NTHREADS];
  128.     unsigned long int args[NTHREADS];
  129.     int i;
  130.     FILE *f;
  131.     char buf[BUFSIZ];
  132.     time_t t;
  133.     struct tm *time_p;
  134.  
  135.     sum = 0.0L;
  136.  
  137.     pthread_barrier_init(&barrier, NULL, NTHREADS);
  138.  
  139.     f = fopen(FILENAME, "w");
  140.     setvbuf(f, buf, _IOLBF, BUFSIZ);
  141.  
  142.     time(&t);
  143.     time_p = localtime(&t);
  144.  
  145.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] M: started\n\n",
  146.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  147.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec);
  148.  
  149.     for (i = 0; i < argc - 1; i++)
  150.     {
  151.         primes[i] = strtoul(argv[i + 1], NULL, 10);
  152.         printf("%ld\n", primes[i]);
  153.     }
  154.  
  155.     for (i = 0; i < NTHREADS; i++)
  156.     {
  157.         args[i] = i;
  158.         pthread_create(&threads[i], NULL, job, &args[i]);
  159.     }
  160.  
  161.     for (i = 0; i < NTHREADS; i++)
  162.     {
  163.         pthread_join(threads[i], NULL);
  164.     }
  165.  
  166.     fprintf(f, "\nM: total result: %.20Lf\n", sum);
  167.  
  168.     fclose(f);
  169.  
  170.     return EXIT_SUCCESS;
  171. }
  172.  
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement