|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <mpfr.h> |
| 4 | +#include <time.h> |
| 5 | +#include <assert.h> |
| 6 | +#include <pthread.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +#define STEP 25 |
| 10 | + |
| 11 | +struct do_calc_args |
| 12 | +{ |
| 13 | + double *times; |
| 14 | + int i; |
| 15 | + int max; |
| 16 | + int nproc; |
| 17 | +}; |
| 18 | + |
| 19 | +void *do_calc(void *vargp); |
| 20 | +long long unsigned run_calc(int); |
| 21 | + |
| 22 | +int main(int argc, char const *argv[]) |
| 23 | +{ |
| 24 | + long nproc = sysconf(_SC_NPROCESSORS_ONLN); |
| 25 | + pthread_t *threads; |
| 26 | + double *times; |
| 27 | + |
| 28 | + if (argc != 2 || nproc <= 0) |
| 29 | + exit(1); |
| 30 | + int max = strtol(argv[1], NULL, 10); |
| 31 | + |
| 32 | + printf("Using %ld threads\n", nproc); |
| 33 | + FILE *file = fopen("number.csv", "w"); |
| 34 | + fprintf(file, ""); |
| 35 | + fclose(file); |
| 36 | + |
| 37 | + times = calloc(max, sizeof(double)); |
| 38 | + if (times == NULL) |
| 39 | + exit(1); |
| 40 | + |
| 41 | + threads = calloc(nproc, sizeof(pthread_t)); |
| 42 | + if (threads == NULL) |
| 43 | + exit(1); |
| 44 | + |
| 45 | + for (int i = 0; i < nproc; i++) |
| 46 | + { |
| 47 | + struct do_calc_args *args = malloc(sizeof(struct do_calc_args)); |
| 48 | + assert(args != NULL); |
| 49 | + args->i = i; |
| 50 | + args->max = max; |
| 51 | + args->times = times; |
| 52 | + args->nproc = nproc; |
| 53 | + pthread_create(&threads[i], NULL, &do_calc, args); |
| 54 | + } |
| 55 | + |
| 56 | + for (int i = 0; i < nproc; i++) |
| 57 | + pthread_join(threads[i], NULL); |
| 58 | + |
| 59 | + free(times); |
| 60 | + free(threads); |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +void *do_calc(void *vargp) |
| 66 | +{ |
| 67 | + struct do_calc_args *args = (struct do_calc_args *)vargp; |
| 68 | + double *times = args->times; |
| 69 | + int j = args->i; |
| 70 | + int max = args->max; |
| 71 | + int nproc = args->nproc; |
| 72 | + for (int i = j; i < max; i += nproc) |
| 73 | + { |
| 74 | + times[i] = (double)run_calc(STEP * (i + 1)) / CLOCKS_PER_SEC; |
| 75 | + printf("Time for %ddp: %F\n", STEP * (i + 1), times[i]); |
| 76 | + } |
| 77 | + mpfr_free_cache(); |
| 78 | + free(vargp); |
| 79 | + return NULL; |
| 80 | +} |
| 81 | + |
| 82 | +long long unsigned |
| 83 | +run_calc(int dp) |
| 84 | +{ |
| 85 | + mpfr_t x, xold, tmp1, tmp2, precision_bits, e; |
| 86 | + long long unsigned wallclock; |
| 87 | + long long unsigned i = 0; |
| 88 | + long unsigned precision; |
| 89 | + FILE *file = fopen("number.csv", "a"); |
| 90 | + fprintf(file, "%d, ", dp); |
| 91 | + |
| 92 | + /* Calculate precision */ |
| 93 | + mpfr_init(precision_bits); |
| 94 | + mpfr_set_ui(precision_bits, 10, MPFR_RNDU); |
| 95 | + mpfr_pow_ui(precision_bits, precision_bits, dp, MPFR_RNDU); |
| 96 | + mpfr_log2(precision_bits, precision_bits, MPFR_RNDU); |
| 97 | + precision = mpfr_get_ui(precision_bits, MPFR_RNDU) + 1; |
| 98 | + printf("Using %lu precision bits (%.4FKB)\n", precision, (double)precision / (8 * 1024)); |
| 99 | + |
| 100 | + /* Init X, tmp1, tmp2 */ |
| 101 | + mpfr_inits2(precision, x, xold, tmp1, tmp2, e, NULL); |
| 102 | + mpfr_set_ui(x, 0, 0); |
| 103 | + mpfr_set_ui(xold, 1, 0); |
| 104 | + mpfr_set_ui(e, 10, 0); |
| 105 | + mpfr_pow_si(e, e, -(dp), 0); |
| 106 | + |
| 107 | + /* Perform the calculation */ |
| 108 | + clock_t start = clock(); |
| 109 | + while (1) |
| 110 | + { |
| 111 | + mpfr_sub(tmp1, x, xold, 0); |
| 112 | + if (mpfr_cmpabs(e, tmp1) >= 0) |
| 113 | + break; |
| 114 | + |
| 115 | + i++; |
| 116 | + mpfr_set(xold, x, 0); |
| 117 | + mpfr_add_ui(tmp1, x, 1, 0); |
| 118 | + mpfr_div(tmp2, x, tmp1, 0); |
| 119 | + mpfr_pow(x, tmp2, x, 0); |
| 120 | + } |
| 121 | + clock_t end = clock(); |
| 122 | + wallclock = ((end - start)); |
| 123 | + fprintf(file, "%llu, ", i); |
| 124 | + fprintf(file, "%.4F\n", (double)wallclock / CLOCKS_PER_SEC); |
| 125 | + fclose(file); |
| 126 | + |
| 127 | + mpfr_clears(x, xold, tmp1, tmp2, precision_bits, e, NULL); |
| 128 | + |
| 129 | + return wallclock; |
| 130 | +} |
0 commit comments