Advertisement
am1x

erdes002.c

Aug 14th, 2022 (edited)
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.49 KB | Source Code | 0 0
  1. /*
  2. ** gcc -Wall -Wextra -pedantic -pedantic-errors
  3. ** -std=gnu99 -lpthread -lgmp -lprimesieve
  4. */
  5.  
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10.  
  11. #include <gmp.h>
  12. #include <primesieve.h>
  13.  
  14. #define FILENAME "/dev/stdout"
  15. #define SIZE 1000000000000
  16. #define NTHREADS 4
  17. #define PRECISION 512
  18. #define LENGTH 1000
  19. #define RESOLUTION 1000
  20.  
  21. mpf_t Sum;
  22. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  23. pthread_barrier_t barrier;
  24.  
  25. unsigned long int primes[NTHREADS] = { 0 };
  26.  
  27. void *job (void *arg)
  28. {
  29.     unsigned long int index, n, p, size, end;
  30.     primesieve_iterator it;
  31.     time_t t;
  32.     struct tm *time_p;
  33.     FILE *f;
  34.     char buf[BUFSIZ];
  35.     mpf_t N, Localsum;
  36.  
  37.     pthread_mutex_lock(&mutex);
  38.  
  39.     index = *((unsigned long int*)arg);
  40.     size = SIZE/NTHREADS;
  41.     n = size*index;
  42.  
  43.     if (index == NTHREADS - 1)
  44.     {
  45.         end = SIZE - 1;
  46.     }
  47.     else
  48.     {
  49.         end = n + size - 1;
  50.     }
  51.  
  52.     mpf_set_default_prec((mp_bitcnt_t)PRECISION);
  53.     mpf_inits(N, Localsum, NULL);
  54.  
  55.     f = fopen(FILENAME, "w");
  56.     setvbuf(f, buf, _IOLBF, BUFSIZ);
  57.  
  58.     fprintf(f, "%ld: from %ld to %ld\n", index, n + 1, end + 1);
  59.  
  60.     time(&t);
  61.     time_p = localtime(&t);
  62.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: skipping...\n",
  63.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  64.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index);
  65.  
  66.     if (primes[index])
  67.     {
  68.         primesieve_init(&it);
  69.         p = primes[index];
  70.         primesieve_skipto(&it, p - 1, p + size);
  71.     }
  72.     else
  73.     {
  74.         primesieve_init(&it);
  75.         p = primesieve_nth_prime(n + 1, 0);
  76.         printf("%ld\n", p);
  77.         primesieve_skipto(&it, p - 1, p + size);
  78.     }
  79.  
  80.     pthread_mutex_unlock(&mutex);
  81.  
  82.     pthread_barrier_wait(&barrier);
  83.  
  84.     time(&t);
  85.     time_p = localtime(&t);
  86.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: calculating...\n",
  87.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  88.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index);
  89.  
  90.     while (n <= end)
  91.     {
  92.         mpf_set_ui(N, ++n);
  93.         mpf_div_ui(N, N, (unsigned long int)primesieve_next_prime(&it));
  94.         mpf_sub(Localsum, Localsum, N);
  95.  
  96.         time(&t);
  97.         time_p = localtime(&t);
  98.         fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: %ld\n",
  99.             time_p->tm_year + 1900, time_p->tm_mon + 1,
  100.             time_p->tm_mday, time_p->tm_hour,
  101.             time_p->tm_min, time_p->tm_sec, index, n);
  102.  
  103.         mpf_set_ui(N, ++n);
  104.         mpf_div_ui(N, N, (unsigned long int)primesieve_next_prime(&it));
  105.         mpf_add(Localsum, Localsum, N);
  106.  
  107.         time(&t);
  108.         time_p = localtime(&t);
  109.         fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: %ld\n",
  110.             time_p->tm_year + 1900, time_p->tm_mon + 1,
  111.             time_p->tm_mday, time_p->tm_hour,
  112.             time_p->tm_min, time_p->tm_sec, index, n);
  113.  
  114.         for (unsigned long int i = 0; i < size/RESOLUTION - 1; i++)
  115.         {
  116.             mpf_set_ui(N, ++n);
  117.             mpf_div_ui(N, N,
  118.                 (unsigned long int)primesieve_next_prime(&it));
  119.             mpf_sub(Localsum, Localsum, N);
  120.  
  121.             mpf_set_ui(N, ++n);
  122.             mpf_div_ui(N, N,
  123.                 (unsigned long int)primesieve_next_prime(&it));
  124.             mpf_add(Localsum, Localsum, N);
  125.         }
  126.     }
  127.  
  128.     primesieve_free_iterator(&it);
  129.  
  130.     time(&t);
  131.     time_p = localtime(&t);
  132.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] %ld: done! partial result: ",
  133.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  134.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec, index);
  135.     mpf_out_str(f, 10, (size_t)LENGTH, Localsum);
  136.     fprintf(f, "\n");
  137.  
  138.     fclose(f);
  139.  
  140.     mpf_add(Sum, Sum, Localsum);
  141.  
  142.     mpf_clears(N, Localsum, NULL);
  143.  
  144.     return NULL;
  145. }
  146.  
  147. int main (int argc, char *argv[])
  148. {
  149.     pthread_t threads[NTHREADS];
  150.     unsigned long int args[NTHREADS];
  151.     int i;
  152.     FILE *f;
  153.     char buf[BUFSIZ];
  154.     time_t t;
  155.     struct tm *time_p;
  156.  
  157.     mpf_set_default_prec((mp_bitcnt_t)PRECISION);
  158.     mpf_init(Sum);
  159.  
  160.     pthread_barrier_init(&barrier, NULL, NTHREADS);
  161.  
  162.     f = fopen(FILENAME, "w");
  163.     setvbuf(f, buf, _IOLBF, BUFSIZ);
  164.  
  165.     time(&t);
  166.     time_p = localtime(&t);
  167.  
  168.     fprintf(f, "[%04d-%02d-%02d %02d:%02d:%02d] M: started\n",
  169.         time_p->tm_year + 1900, time_p->tm_mon + 1, time_p->tm_mday,
  170.         time_p->tm_hour, time_p->tm_min, time_p->tm_sec);
  171.  
  172.     fprintf(f, "precision: %d bits\n\n", (int)mpf_get_prec(Sum));
  173.  
  174.     for (i = 0; i < argc - 1; i++)
  175.     {
  176.         primes[i] = strtoul(argv[i + 1], NULL, 10);
  177.         printf("%ld\n", primes[i]);
  178.     }
  179.  
  180.     for (i = 0; i < NTHREADS; i++)
  181.     {
  182.         args[i] = i;
  183.         pthread_create(&threads[i], NULL, job, &args[i]);
  184.     }
  185.  
  186.     for (i = 0; i < NTHREADS; i++)
  187.     {
  188.         pthread_join(threads[i], NULL);
  189.     }
  190.  
  191.     fprintf(f, "\nM: total result: ");
  192.     mpf_out_str(f, 10, (size_t)LENGTH, Sum);
  193.     fprintf(f, "\n");
  194.  
  195.     fclose(f);
  196.  
  197.     mpf_clear(Sum);
  198.  
  199.     return EXIT_SUCCESS;
  200. }
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement