int main (void) { mpf_set_default_prec (300000); mpf_t x0, y0, resA, resB, Z, var; mpf_init_set_ui (x0, 1); mpf_init_set_d (y0, 0.5); mpf_sqrt (y0, y0); mpf_init (resA); mpf_init (resB); mpf_init_set_d (Z, 0.25); mpf_init (var); int n = 1; for(int i=0; i<8; i++){ agm(x0, y0, resA, resB); mpf_sub(var, resA, x0); mpf_mul(var, var, var); mpf_mul_ui(var, var, n); mpf_sub(Z, Z, var); n += n; agm(resA, resB, x0, y0); mpf_sub(var, x0, resA); mpf_mul(var, var, var); mpf_mul_ui(var, var, n); mpf_sub(Z, Z, var); n += n; } mpf_mul(x0, x0, x0); mpf_div(x0, x0, Z); gmp_printf ("%.100000Ff\n", x0); return 0; }
void check_rand (void) { unsigned long min_prec = __GMPF_BITS_TO_PREC (1); gmp_randstate_t rands; mpf_t got, u; unsigned long prec, v; int i; /* The nails code in mpf_mul_ui currently isn't exact, so suppress these tests for now. */ if (BITS_PER_UI > GMP_NUMB_BITS) return; mpf_init (got); mpf_init (u); gmp_randinit_default(rands); for (i = 0; i < 200; i++) { /* got precision */ prec = min_prec + gmp_urandomm_ui (rands, 15L); refmpf_set_prec_limbs (got, prec); /* u precision */ prec = min_prec + gmp_urandomm_ui (rands, 15L); refmpf_set_prec_limbs (u, prec); /* u, possibly negative */ mpf_rrandomb (u, rands, PREC(u), (mp_exp_t) 20); if (gmp_urandomb_ui (rands, 1L)) mpf_neg (u, u); /* v, 0 to BITS_PER_ULONG bits (inclusive) */ prec = gmp_urandomm_ui (rands, BITS_PER_ULONG+1); v = gmp_urandomb_ui (rands, prec); if ((i % 2) == 0) { /* separate */ mpf_mul_ui (got, u, v); check_one ("separate", got, u, v); } else { /* overlap */ prec = refmpf_set_overlap (got, u); mpf_mul_ui (got, got, v); check_one ("overlap src==dst", got, u, v); mpf_set_prec_raw (got, prec); } } mpf_clear (got); mpf_clear (u); gmp_randclear(rands); }
/* r = sqrt(x) */ void my_sqrt_ui(mpf_t r, unsigned long x) { unsigned long prec, bits, prec0; prec0 = mpf_get_prec(r); if (prec0<=DOUBLE_PREC) { mpf_set_d(r, sqrt(x)); return; } bits = 0; for (prec=prec0; prec>DOUBLE_PREC;) { int bit = prec&1; prec = (prec+bit)/2; bits = bits*2+bit; } mpf_set_prec_raw(t1, DOUBLE_PREC); mpf_set_d(t1, 1/sqrt(x)); while (prec<prec0) { prec *=2; if (prec<prec0) { /* t1 = t1+t1*(1-x*t1*t1)/2; */ mpf_set_prec_raw(t2, prec); mpf_mul(t2, t1, t1); /* half x half -> full */ mpf_mul_ui(t2, t2, x); mpf_ui_sub(t2, 1, t2); mpf_set_prec_raw(t2, prec/2); mpf_div_2exp(t2, t2, 1); mpf_mul(t2, t2, t1); /* half x half -> half */ mpf_set_prec_raw(t1, prec); mpf_add(t1, t1, t2); } else { break; } prec -= (bits&1); bits /=2; } /* t2=x*t1, t1 = t2+t1*(x-t2*t2)/2; */ mpf_set_prec_raw(t2, prec0/2); mpf_mul_ui(t2, t1, x); mpf_mul(r, t2, t2); /* half x half -> full */ mpf_ui_sub(r, x, r); mpf_mul(t1, t1, r); /* half x half -> half */ mpf_div_2exp(t1, t1, 1); mpf_add(r, t1, t2); }
/* merit (rop, t, v, m) -- calculate merit for spectral test result in dimension T, see Knuth p. 105. BUGS: Only valid for 2 <= T <= 6. */ void merit (mpf_t rop, unsigned int t, mpf_t v, mpz_t m) { int f; mpf_t f_m, f_const, f_pi; mpf_init (f_m); mpf_set_z (f_m, m); mpf_init_set_d (f_const, M_PI); mpf_init_set_d (f_pi, M_PI); switch (t) { case 2: /* PI */ break; case 3: /* PI * 4/3 */ mpf_mul_ui (f_const, f_const, 4); mpf_div_ui (f_const, f_const, 3); break; case 4: /* PI^2 * 1/2 */ mpf_mul (f_const, f_const, f_pi); mpf_div_ui (f_const, f_const, 2); break; case 5: /* PI^2 * 8/15 */ mpf_mul (f_const, f_const, f_pi); mpf_mul_ui (f_const, f_const, 8); mpf_div_ui (f_const, f_const, 15); break; case 6: /* PI^3 * 1/6 */ mpf_mul (f_const, f_const, f_pi); mpf_mul (f_const, f_const, f_pi); mpf_div_ui (f_const, f_const, 6); break; default: fprintf (stderr, "spect (merit): can't calculate merit for dimensions > 6\n"); mpf_set_ui (f_const, 0); break; } /* rop = v^t */ mpf_set (rop, v); for (f = 1; f < t; f++) mpf_mul (rop, rop, v); mpf_mul (rop, rop, f_const); mpf_div (rop, rop, f_m); mpf_clear (f_m); mpf_clear (f_const); mpf_clear (f_pi); }
depth_t frac_generalized_celtic_gmp( depth_t depth, mpf_t bail, mpf_t wim, mpf_t wre, mpf_t c_im, mpf_t c_re, mpf_t wim2, mpf_t wre2, mpf_t t1) { depth_t wz; for (wz = 1; wz <= depth; wz++) { /* wim = 2.0 * wre * wim + c_im; */ mpf_mul( t1, wre, wim); mpf_mul_ui(t1, t1, 2); mpf_add( wim, t1, c_im); /* wre = wre2 - wim2 + c_re; */ mpf_sub( t1, wre2, wim2); mpf_abs( t1, t1); mpf_add( wre, t1, c_re); /* wim2 = wim * wim; */ mpf_mul( wim2, wim, wim); /* wre2 = wre * wre; */ mpf_mul( wre2, wre, wre); /* if ((wim2 + wre2) > frs_bail) */ mpf_add( t1, wim2, wre2); if (mpf_cmp(t1, bail) > 0) return wz; } return 0; }
void extrapolate(index_t num_samples, mpf_t *samples, mpf_t ans) { // The Richardson extrapolation recursive formula is // // A_n+1(x) = (2^n A_n(2x) - A_n(x)) / (2^n - 1) mpf_t mult; // mult = 2^n mpf_init_set_d(mult, 1); mpf_t denom; // denom = 1 / (mult - 1) mp_bitcnt_t precision = mpf_get_prec(ans); mpf_init2(denom, precision); mpf_t *end = samples + num_samples; for (mpf_t *lim = samples; lim < end; lim++) { // lim - samples = n mpf_mul_ui(mult, mult, 2); mpf_set(denom, mult); mpf_sub_ui(denom, denom, 1); mpf_ui_div(denom, 1, denom); // evaluate all extrapolations for (mpf_t *ptr = end; --ptr > lim; ) { // A_n+1(x) = (mult A_n(2x) - A_n(x)) * denom mpf_mul(*ptr, *ptr, mult); mpf_sub(*ptr, *ptr, *(ptr - 1)); mpf_mul(*ptr, *ptr, denom); } } mpf_set(ans, *(end - 1)); // move to ans // Clean mpf_clear(mult); mpf_clear(denom); }
void mpf_fac_ui(mpf_t rop, unsigned int n) { unsigned int i; gmp_printf("n: %d\n",n); mpf_set_ui(rop,1); for(i = n; i > 1; i--) mpf_mul_ui(rop,rop,i); }
void FractalTexture::computeColor(unsigned char *pixel) { #ifdef ROE_FRACTAL_GMP_USE_C mpf_set(xPos, m_xPos); mpf_set(yPos, m_yPos); #else xPos = m_xPos; yPos = m_yPos; #endif for(long iteration = 0; iteration < m_iMaxIterations; ++iteration) { #ifdef ROE_FRACTAL_GMP_USE_C mpf_mul(xPos2, xPos, xPos); mpf_mul(yPos2, yPos, yPos); mpf_add(tmp, xPos2, yPos2); //save sum temporarily if(mpf_cmp_ui(tmp,4) >= 0) { #else xPos2 = xPos*xPos; yPos2 = yPos*yPos; if(xPos2 + yPos2 > 4.0) { #endif //(coloured) outer const double ratio = iteration*m_dInvMaxIterations; //const Color c = Color::WHITE; const Color c = colorInterpolation.interpolate(ratio); pixel[0] = c.byter(); pixel[1] = c.byteg(); pixel[2] = c.byteb(); return; } #ifdef ROE_FRACTAL_GMP_USE_C mpf_mul(yPos, yPos, xPos); mpf_mul_ui(yPos, yPos, 2); mpf_add(yPos, yPos, m_yPos); mpf_sub(xPos, xPos2, yPos2); mpf_add(xPos, xPos, m_xPos); #else yPos *= xPos; yPos *= 2; yPos += m_yPos; xPos = xPos2; xPos -= yPos2; xPos += m_xPos; #endif } //inner pixel[0] = m_innerR; pixel[1] = m_innerG; pixel[2] = m_innerB; } void FractalTexture::updateMipmaps() { S_Texture *subtex1 = &texture, *subtex2 = nullptr; while (subtex1->next_mipmap) { subtex2 = subtex1->next_mipmap; Texture::scaleImage2(subtex1->data, subtex1->width, subtex1->height, subtex2->data, subtex2->width, subtex2->height, subtex1->bpp); //scaling image to new size subtex1 = subtex2; //one level deeper } }
void correction_factor_Q(mpf_t Q, int N,mpf_t rho,int k){ unsigned long int f; mpf_t tmp,tmp1,tmp2; /* if (PNK_N_1_rho_1_PN == 0) cout << "Algun factor es 0 N = " << N << " rho = " << rho << " k = " << k << " P__0 = " << P__0 << " P__N = " << P__N << endl; */ mpf_init(tmp); mpf_init_set_d(tmp1,pow(N,k-1)); // N^(k-1) f = 1; mpf_set_ui(Q,0); for (unsigned long int j = k;j < N;j++) { /*Q += (N - j) * pow(N,j) * pow(rho,j - k) * P__0 * N_k_1 / (factorial(j - k) * PNK_N_1_rho_1_PN); */ /* Q += [(N - j) * N^j * rho^(j - k) / (j - k)!] * P__0 * N_k_1 / PNK_N_1_rho_1_PN; */ mpf_mul_ui(tmp1,tmp1,N); // * N if (j - k > 1) mpf_div_ui(tmp1,tmp1,j - k); // / (j - k) mpf_mul_ui(tmp,tmp1,N - j); // * (N - j) mpf_add(Q,Q,tmp); // Q += (N - j) * N^j * rho^(j - k) / (j - k)! mpf_mul(tmp1,tmp1,rho); // * rho } mpf_init(tmp2); mpf_ui_sub(tmp2,1,P__N); // 1 - P__N mpf_pow_ui(tmp1,tmp2,k); // (1 - P__N)^k mpf_mul(tmp2,tmp2,rho); // rho * (1 - P__N) mpf_ui_sub(tmp2,1,tmp2); // 1 - rho * (1 - P__N) mpf_mul(Q,Q,P__0); // * P__0 mpf_mul_ui(Q,Q,factorial(N-k-1)); // * (N - k - 1)! /* pow(1 - P__N,k) * factorial(N) * (1 - rho * (1 - P__N)) */ mpf_mul(tmp2,tmp1,tmp2); // (1 - P__N)^k * (1 - rho * (1 - P__N)) mpf_mul_ui(tmp2,tmp2,factorial(N)); // (1 - P__N)^k * (1 - rho * (1 - P__N)) * N! mpf_div(Q,Q,tmp2); // / [(1 - P__N)^k * (1 - rho * (1 - P__N)) * N!] mpf_clear(tmp); mpf_clear(tmp1); mpf_clear(tmp2); }
void gmp_monte_carlo_par (int numthr, int numpts) { /*Para realizar o metodo Monte Carlo foi utilizado uma circunferencia de raio igual a 1 (para facilitar a comparacao que verifica se o ponto esta dentro da circunferencia) inscrita em uma quadrado de lado igual a 2. Foi considerado apenas o primeiro quadrante, pois assim as coordenadas dos pontos variam de 0 a 1. */ int i =0; mpf_t pi; mpf_t intern_points, total_points, ratio; pthread_t tabela_thr[1000]; /*Variavel que armazena quantos pontos vao ficar dentro da circunferencia*/ /*A precisao padrao sera no minimo 10.000.000*log10.*/ mpf_set_default_prec (34000000); /*O objeto mpf_t deve ser inicializado antes de receber algum valor.*/ mpf_init(pi),mpf_init(intern_points),mpf_init(total_points),mpf_init(ratio); /* Inicializa a semente para calcular o valor aleatorio*/ srand (time(NULL)); /* Para uma quantidade de quase 1 bi de pontos*/ i=0; if(numthr>1000) numthr=1000; while(i<numthr) { //printf("\nIniciando Thread: %d", i); mpf_init(param[i].intern_points); mpf_init(param[i].total_points); param[i].numpoints=numpts; pthread_create(&tabela_thr[i], NULL, (void*)&montecarlo_thr, (void*) & param[i]); i++; } i=0; while(i<numthr){ pthread_join(tabela_thr[i], NULL); mpf_add(intern_points,intern_points,param[i].intern_points); mpf_add(total_points,total_points,param[i].total_points); i++; } /*calculando o valor de pi pi = 4* (double) intern_points /total_points */ mpf_div (ratio,intern_points,total_points); /*ratio = intern_points / total_points*/ mpf_mul_ui (pi,ratio,4); /* pi = 4*ratio*/ mpf_out_str (stdout,10,10000000,pi); }
void my_out_str_raw(FILE *fp, unsigned long digits, mpf_t f, unsigned long offset) { unsigned long d; if (digits <= LINE_SIZE*NUM_BLOCKS) { unsigned long cursor = offset % LINE_SIZE; for (d = 0; d < digits; ) { mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1)); mpf_mul_ui(f, f, UNIT_MOD); unsigned long i = mpf_get_ui(f); mpf_sub_ui(f, f, i); utoa(i, UNIT_SIZE); *out_ptr++ = ' '; d += UNIT_SIZE; cursor += UNIT_SIZE; if (cursor == LINE_SIZE) { cursor = 0; *out_ptr++ = ':'; *out_ptr++ = ' '; utoa(offset + d, 0); *out_ptr++ = '\n'; if ((offset + d) % (LINE_SIZE*10) == 0) flush_out(fp); } } } else { mpf_t block, mod; unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE; unsigned long block_size = (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE; mpf_set_default_prec((int)(block_size*BITS_PER_DIGIT+1)); mpf_init(block); mpf_init_set_ui(mod, 10); mpf_pow_ui(mod, mod, block_size); for (d = 0; d < digits; d += block_size) { unsigned long size = block_size < digits - d ? block_size : digits - d; mpf_set_prec_raw(block, (int)(size*BITS_PER_DIGIT+1)); mpf_set(block, f); my_out_str_raw(fp, size, block, offset+d); if (block_size < digits - d) { mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1)); mpf_mul(f, f, mod); mpf_floor(trunk, f); mpf_sub(f, f, trunk); } } mpf_clear(block); mpf_clear(mod); } }
void UniDFormF(poly_f & r, poly_f & f) { if(f.size()<=1) { r.resize(0); return ; } r.resize(f.size()-1); for(size_t i=0;i<r.size();i++) { mpf_mul_ui(r[i],f[i+1],i+1); } r.normalize(); }
void check_various (void) { mpf_t u, got, want; char *s; mpf_init2 (u, 2*8*sizeof(long)); mpf_init2 (got, 2*8*sizeof(long)); mpf_init2 (want, 2*8*sizeof(long)); s = "0 * GMP_UI_MAX"; mpf_set_ui (u, 0L); mpf_mul_ui (got, u, GMP_UI_MAX); MPF_CHECK_FORMAT (got); mpf_set_ui (want, 0L); if (mpf_cmp (got, want) != 0) { error: printf ("Wrong result from %s\n", s); mpf_trace ("u ", u); mpf_trace ("got ", got); mpf_trace ("want", want); abort (); } s = "1 * GMP_UI_MAX"; mpf_set_ui (u, 1L); mpf_mul_ui (got, u, GMP_UI_MAX); MPF_CHECK_FORMAT (got); mpf_set_ui (want, GMP_UI_MAX); if (mpf_cmp (got, want) != 0) goto error; mpf_clear (u); mpf_clear (got); mpf_clear (want); }
static void Pks (mpf_t p, mpf_t x) { double dt; /* temp double */ mpf_set (p, x); mpf_mul (p, p, p); /* p = x^2 */ mpf_mul_ui (p, p, 2); /* p = 2*x^2 */ mpf_neg (p, p); /* p = -2*x^2 */ /* No pow() in gmp. Use doubles. */ /* FIXME: Use exp()? */ dt = pow (M_E, mpf_get_d (p)); mpf_set_d (p, dt); mpf_ui_sub (p, 1, p); }
// The Brent-Salamin algorithm int main(int argc, char* argv[]) { if (argc < 2) return -1; int n = (int)strtol(argv[1], NULL, 10); mpf_set_default_prec(1000); mpf_t a, b, t, c, sum; // a=1 mpf_init_set_ui(a, 1); mpf_init_set_ui(sum, 0); mpf_init(b); mpf_init(t); mpf_init(c); mpf_init(sum); // b=1/sqrt(2) mpf_sqrt_ui(b, 2); mpf_ui_div(b, 1, b); // n次迭代的误差小于\frac{2^{n+9}}{20^{2n+1}} for (int i = 1; i <= n; ++i) { // t=(a+b)/2 mpf_add(t, a, b); mpf_div_ui(t, t, 2); // b=sqrt(a*b); mpf_mul(b, a, b); mpf_sqrt(b, b); // a=t mpf_swap(t, a); mpf_mul(t, a, a); mpf_mul(c, b, b); mpf_sub(c, t, c); mpf_mul_2exp(c, c, i + 1); mpf_add(sum, sum, c); } mpf_mul(t, a, a); mpf_mul_ui(t, t, 4); mpf_ui_sub(sum, 1, sum); mpf_div(t, t, sum); mpf_out_str(stdout, 10, 0, t); printf("\n"); mpf_clear(a); mpf_clear(b); mpf_clear(t); mpf_clear(c); mpf_clear(sum); return 0; }
int main(void) { int i ; char buf[4096] ; mpf_t x, y , result ; mpf_set_default_prec(LIMIT); mpf_init ( x ); mpf_init ( y ); mpf_init (result) ; mpf_set_str(result, "1" , 10 ); for ( i = 1 ; i < LIMIT ; i++ ) { sprintf ( buf , "%d" , (2*i + 1) ) ; mpf_set_str(x, buf , 10 ); mpf_ui_div (y, 1, x) ; if ( i % 2 ) { mpf_sub ( x , result , y ) ; mpf_set (result , x) ; } else { mpf_add ( x , result , y ) ; mpf_set (result , x) ; } } mpf_mul_ui (x, result, 4 ) ; printf("\n pi = " ) ; mpf_out_str (stdout , 10, 0, x) ; printf ("\n") ; mpf_clear (x); mpf_clear (y); mpf_clear (result); return EXIT_SUCCESS; }
void mpc_mod (mpc_t *rop, mpc_t op1, mpc_t op2) { /* I am 90% sure that this doesn't work. However, it works for * integers, so I'll put off fixing it for a little while. */ mpf_t hold_op1; mpf_t hold_op2; mpf_t hold_res; unsigned int prec; mpf_init (hold_op1); mpf_init (hold_op2); mpf_init (hold_res); mpf_set_z (hold_op1, op1.object); mpf_set_z (hold_op2, op2.object); // Get the largest precision. prec = (op1.precision > op2.precision) ? op1.precision : op2.precision; // Set the scalar values while (op1.precision-- > 0) mpf_div_ui (hold_op1, hold_op1, 10); while (op2.precision-- > 0) mpf_div_ui (hold_op2, hold_op2, 10); // Get the value mpf_div (hold_res, hold_op1, hold_op2); mpf_floor (hold_res, hold_res); mpf_mul (hold_res, hold_res, hold_op2); mpf_sub (hold_res, hold_op1, hold_res); for (rop->precision = prec; prec > 0; prec--) mpf_mul_ui (hold_res, hold_res, 10); mpz_set_f (rop->object, hold_res); }
void ks_table (mpf_t p, mpf_t val, const unsigned int n) { /* We use Eq. (27), Knuth p.58, skipping O(1/n) for simplicity. This shortcut will result in too high probabilities, especially when n is small. Pr(Kp(n) <= s) = 1 - pow(e, -2*s^2) * (1 - 2/3*s/sqrt(n) + O(1/n)) */ /* We have 's' in variable VAL and store the result in P. */ mpf_t t1, t2; mpf_init (t1); mpf_init (t2); /* t1 = 1 - 2/3 * s/sqrt(n) */ mpf_sqrt_ui (t1, n); mpf_div (t1, val, t1); mpf_mul_ui (t1, t1, 2); mpf_div_ui (t1, t1, 3); mpf_ui_sub (t1, 1, t1); /* t2 = pow(e, -2*s^2) */ #ifndef OLDGMP mpf_pow_ui (t2, val, 2); /* t2 = s^2 */ mpf_set_d (t2, exp (-(2.0 * mpf_get_d (t2)))); #else /* hmmm, gmp doesn't have pow() for floats. use doubles. */ mpf_set_d (t2, pow (M_E, -(2 * pow (mpf_get_d (val), 2)))); #endif /* p = 1 - t1 * t2 */ mpf_mul (t1, t1, t2); mpf_ui_sub (p, 1, t1); mpf_clear (t1); mpf_clear (t2); }
int main(void){ /*inicializa as variaveis*/ int cont, numThread[NUMTHREADS]; mpf_set_default_prec((long)(DIGITS*BITS_PER_DIGIT+16)); mpf_t PI, dentroCircunferencia, TOTAL; pthread_t tID[NUMTHREADS]; // ID das threads mpf_init(PI); mpf_init_set_d(dentroCircunferencia, 0.0); mpf_init_set_d(TOTAL, 0.0); srand48(time(NULL)); // Para todas as threads, cria a i-esima thread for (cont = 0; cont< NUMTHREADS ; cont++){ numThread[cont] = cont; pthread_create (&tID[cont], NULL, calcula, &numThread[cont]); } // Para cada thread, espera que as threads terminem for (cont = 0; cont< NUMTHREADS ; cont++) pthread_join (tID[cont], NULL); //Para cada thread, soma a sua parcela na conta total for (cont = 0; cont< NUMTHREADS ; cont++){ mpf_add_ui(TOTAL, TOTAL, TOTALParcial[cont]); mpf_add_ui(dentroCircunferencia, dentroCircunferencia, dentroCircunferenciaParcial[cont]); } mpf_div (PI, dentroCircunferencia, TOTAL); mpf_mul_ui (PI, PI, 4); mpf_out_str(stdout, 10, DIGITS, PI); printf("\n"); mpf_clear (PI); mpf_clear (TOTAL); mpf_clear (dentroCircunferencia); return 0; }
void topsin_series (mpf_t a_k, unsigned int k, unsigned int prec) { unsigned int n; mpf_t fourpi, numer, fact, low_bound, term, xterm; mpz_t bino; mpf_set_ui(a_k, 0); /* If k == 0 then we are done */ if (0 == k) return; mpf_init(fourpi); mpf_init(numer); mpf_init(fact); mpf_init(low_bound); mpf_init(term); mpf_init(xterm); mpz_init(bino); fp_two_pi(numer, prec); mpf_neg(numer, numer); // fourpi is actually -4pi^2 mpf_mul(fourpi, numer, numer); mpf_neg(fourpi, fourpi); mpf_set_ui(fact, 1); /* Get the number of binary bits from prec = log_2 10 * prec */ long nbits = (long) floor (3.321 * prec); mpf_div_2exp(low_bound, fact, nbits+32); for (n=0; n<2023123123; n++) { i_binomial(bino, 2*n+k, 2*n); mpf_set_z(term, bino); mpf_mul(xterm, term, numer); mpf_div(term, xterm, fact); mpf_add(a_k, a_k, term); // #define DEBUG 1 #ifdef DEBUG { double h_f, q_f, b_f; h_f = mpf_get_d(h); q_f = mpf_get_d(qmark); b_f = mpf_get_d(bits); printf("duuude place=%d, bitsdone=%ld h=%g q=%g bits=%f s=%d\n", place, bitsdone, h_f, q_f, b_f, mpf_sgn(h)); } #endif // If the term is small enough, we are done. mpf_abs(xterm, term); if (mpf_cmp(xterm, low_bound) < 0) break; // Now iterate mpf_mul(numer, numer, fourpi); mpf_mul_ui(fact, fact, (2*n+3)*2*(n+1)); } if (k%2 == 0) mpf_neg(a_k, a_k); mpf_clear(fourpi); mpf_clear(numer); mpf_clear(fact); mpf_clear(low_bound); mpf_clear(term); mpf_clear(xterm); mpz_clear(bino); }
void mpfc_mul_ui(mpfc_ptr r,mpfc_ptr x,uint a) { mpf_mul_ui(r->Re,x->Re,a); mpf_mul_ui(r->Im,x->Im,a); }
int main() { pthread_t thread_a, thread_b; /* My threads*/ int i; FILE *filePi, *fileTime; clock_t start, end; double cpu_time_used; mpf_set_default_prec(BITS_PER_DIGIT * 11000000); /* Borwein Variable Initialization */ for(i=0; i<2; i++) for(j=0; j<2; j++) mpf_init(params[i][j]); mpf_init(real_pi); mpf_init(y0Aux); mpf_init(y0Aux2); mpf_init(a0Aux); mpf_init(a0Aux2); mpf_init(pi[0]); mpf_init(pi[1]); mpf_init_set_str(error, "1e-10000000", 10); /* Initial value setting */ mpf_sqrt_ui(params[A][0], 2.0); /* a0 = sqrt(2)*/ mpf_mul_ui(params[A][0], params[A][0], 4.0); /* a0 = 4 * sqrt(2) */ mpf_ui_sub(params[A][0], 6.0, params[A][0]); /* a0 = 6 - 4 * sqrt(2) */ mpf_sqrt_ui(params[Y][0], 2.0); /* y0 = sqrt(2) */ mpf_sub_ui(params[Y][0], params[Y][0], 1.0); /* y0 = sqrt(2) - 1 */ mpf_set_ui(pi[0], 0); mpf_set_ui(pi[1], 0); i = 1; j = 1; iteracoes = 0; x = 0; /* Load the reals digits of pi */ filePi = fopen("pi.txt", "r"); gmp_fscanf(filePi, "%Ff", real_pi); fclose(filePi); start = clock(); while(1){ /* y = ( 1 - (1 - y0 ^ 4) ^ 0.25 ) / ( 1 + ( 1 - y0 ^ 4) ^ 0.25 ) */ mpf_pow_ui(y0Aux, params[Y][0], 4); mpf_ui_sub(y0Aux, 1.0, y0Aux); mpf_sqrt(y0Aux, y0Aux); mpf_sqrt(y0Aux, y0Aux); mpf_add_ui(y0Aux2, y0Aux, 1.0); mpf_ui_sub(y0Aux, 1.0, y0Aux); mpf_div(params[Y][1], y0Aux, y0Aux2); /* a = a0 * ( 1 + params[Y][1] ) ^ 4 - 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */ /* Threads creation */ pthread_create(&thread_a, NULL, calc_a, NULL); pthread_create(&thread_b, NULL, calc_b, NULL); pthread_join(thread_a, NULL); pthread_join(thread_b, NULL); /* 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */ mpf_mul(a0Aux, a0Aux, a0Aux2); /*a0 * ( 1 + params[Y][1] ) ^ 4*/ mpf_add_ui(a0Aux2, params[Y][1], 1); mpf_pow_ui(a0Aux2, a0Aux2, 4); mpf_mul(a0Aux2, params[A][0], a0Aux2); /* form the entire expression */ mpf_sub(params[A][1], a0Aux2, a0Aux); mpf_set(params[A][0], params[A][1]); mpf_set(params[Y][0], params[Y][1]); mpf_ui_div(pi[j], 1, params[A][0]); gmp_printf("\nIteracao %d | pi = %.25Ff", iteracoes, pi[j]); /* Calculate the error */ mpf_sub(pi[(j+1)%2], real_pi, pi[j]); mpf_abs(pi[(j+1) % 2], pi[(j+1) % 2]); if(mpf_cmp(pi[(j+1)%2], error) < 0){ printf("\n%d iteracoes para alcancar 10 milhoes de digitos de corretos.", iteracoes); break; } j = (j+1) % 2; iteracoes++; i++; } end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; fileTime = fopen("execution_time.txt", "w"); fprintf(fileTime, "Execution time: %f\n", cpu_time_used); fclose(fileTime); /* Clean up*/ for(i=0; i<2; i++) for(j=0; j<2; j++) mpf_clear(params[i][j]); mpf_clear(pi[0]); mpf_clear(pi[1]); mpf_clear(real_pi); mpf_clear(error); return 0; }
long int julia(const mpf_t x, const mpf_t xr, long int xres, const mpf_t y, const mpf_t yr, long int yres, mpf_t *c, int flag, long int max_iteration, float *iterations, int my_rank, int p, MPI_Comm comm) { double t0 = MPI_Wtime(); int i,j; //------------julia gmp const double maxRadius = 4.0; // double xi, yi, savex, savex2, savey, radius; mpf_t xi, yi, x_min, x_max, y_min, y_max, savex, savex2, savey, radius, xgap, ygap, savex_a, savex_b, savey_a, savey_b, tmp, tmp1; mpf_init(xi); mpf_init(yi); mpf_init(x_min); mpf_init(x_max); mpf_init(y_min); mpf_init(y_max); mpf_init(savex); mpf_init(savex2); mpf_init(savey); mpf_init(radius); mpf_init(xgap); mpf_init(ygap); mpf_init(savex_a); mpf_init(savex_b); mpf_init(savey_a); mpf_init(savey_b); mpf_init(tmp); mpf_init(tmp1); //double x_min = x - xr; mpf_sub(x_min, x, xr); //double x_max = x + xr; mpf_add(x_max, x, xr); //double y_min = y - yr; mpf_sub(y_min, y, yr); //double y_max = y + yr; mpf_add(y_max, y, yr); // spaceing between x and y points //double xgap = (x_max - x_min) / xres; mpf_sub(xgap, x_max, x_min); mpf_div_ui(xgap, xgap, xres); //double ygap = (y_max - y_min) / yres; mpf_sub(ygap, y_max, y_min); mpf_div_ui(ygap, ygap, yres); //---------------------------- long long int iteration; long long int total_number_iterations = 0; int k = 0; for (j = 0; j < yres; j++){ for (i = 0; i < xres; i++){ //xi = x_min + i * xgap; mpf_mul_ui(tmp, xgap, i); mpf_add(xi, x_min, tmp); //yi = y_min + j * ygap; mpf_mul_ui(tmp, ygap, j); mpf_add(yi, y_min, tmp); //flag betwee[n julia or mandelbrot //savex = flag * c[0] + (1 - flag) * xi; mpf_mul_ui(savex_a, c[0], flag); mpf_mul_ui(savex_b, xi, (1-flag)); mpf_add(savex, savex_a, savex_b); //savey = flag * c[1] + (1 - flag) * yi; mpf_mul_ui(savey_a, c[1], flag); mpf_mul_ui(savey_b, yi, (1-flag)); mpf_add(savey, savey_a, savey_b); //radius = 0; mpf_set_ui(radius, 0); iteration = 0; //while ((radius <= maxRadius) && (iteration < max_iteration)){ while ((mpf_cmp_d(radius, maxRadius)<=0) && (iteration < max_iteration)){ //savex2 = xi; mpf_add_ui(savex2, xi, 0); //xi = xi * xi - yi * yi + savex; mpf_mul(xi, xi, xi); mpf_mul(tmp, yi, yi); mpf_sub(xi, xi, tmp); mpf_add(xi, xi, savex); //yi = 2.0f * savex2 * yi + savey; mpf_mul_ui(tmp, savex2, 2); mpf_mul(yi, yi, tmp); mpf_add(yi, yi, savey); //radius = xi * xi + yi * yi; mpf_mul(tmp, xi, xi); mpf_mul(tmp1, yi, yi); mpf_add(radius, tmp, tmp1); iteration++; } total_number_iterations += iteration; float *p = iterations + k*xres + i; //if (radius > maxRadius){ if (mpf_cmp_d(radius, maxRadius)>0){ //float zn = sqrt(xi*xi + yi*yi); mpf_t zn; mpf_init(zn); mpf_mul(tmp, xi, xi); mpf_mul(tmp1, yi, yi); mpf_add(zn, tmp, tmp1); mpf_sqrt(zn, zn); double n = mpf_get_d(zn); //float nu = log(log(zn) / log(2))/log(2); double nu = log(log(n) / log(2))/log(2); //the point has escaped at iteration at any of the iterations 0,1,2,3... *p = iteration + 1 - nu; } else // zij stays within the region up to max_iteration { assert(iteration==max_iteration); *p = -1; } } k++; } //reduce max iteration count long long int total_reduced_iterations = -1; //printf("rank: %i, total_reduced_iterations: %i\n", my_rank, total_number_iterations); MPI_Reduce(&total_number_iterations, &total_reduced_iterations, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, comm); double t4 = MPI_Wtime(); double max_reduced_time = -1; double total_time = t4 - t0; MPI_Reduce(&total_time, &max_reduced_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm); printf("np: %i, time: %f , iterations: %lld\n",p, max_reduced_time, total_reduced_iterations); //clear //printf("proc: %i, total time: %lf sec, init: %lf sec, calc: %lf sec, collect: %lf\n", my_rank, t4-t0, t1-t0, t2-t1, t3-t2); return total_reduced_iterations; }
int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget, uint64_t max_nonce, unsigned long *hashes_done) { uint32_t data[32] __attribute__((aligned(128))); uint32_t *data_p64 = data + (M7_MIDSTATE_LEN / sizeof(data[0])); uint32_t hash[8] __attribute__((aligned(32))); uint8_t bhash[7][64] __attribute__((aligned(32))); uint32_t n = pdata[19] - 1; const uint32_t first_nonce = pdata[19]; char data_str[161], hash_str[65], target_str[65]; uint8_t *bdata = 0; mpz_t bns[8]; int rc = 0; int bytes, nnNonce2; mpz_t product; mpz_init(product); for(int i=0; i < 8; i++){ mpz_init(bns[i]); } memcpy(data, pdata, 80); sph_sha256_context ctx_final_sha256; sph_sha256_context ctx_sha256; sph_sha512_context ctx_sha512; sph_keccak512_context ctx_keccak; sph_whirlpool_context ctx_whirlpool; sph_haval256_5_context ctx_haval; sph_tiger_context ctx_tiger; sph_ripemd160_context ctx_ripemd; sph_sha256_init(&ctx_sha256); sph_sha256 (&ctx_sha256, data, M7_MIDSTATE_LEN); sph_sha512_init(&ctx_sha512); sph_sha512 (&ctx_sha512, data, M7_MIDSTATE_LEN); sph_keccak512_init(&ctx_keccak); sph_keccak512 (&ctx_keccak, data, M7_MIDSTATE_LEN); sph_whirlpool_init(&ctx_whirlpool); sph_whirlpool (&ctx_whirlpool, data, M7_MIDSTATE_LEN); sph_haval256_5_init(&ctx_haval); sph_haval256_5 (&ctx_haval, data, M7_MIDSTATE_LEN); sph_tiger_init(&ctx_tiger); sph_tiger (&ctx_tiger, data, M7_MIDSTATE_LEN); sph_ripemd160_init(&ctx_ripemd); sph_ripemd160 (&ctx_ripemd, data, M7_MIDSTATE_LEN); sph_sha256_context ctx2_sha256; sph_sha512_context ctx2_sha512; sph_keccak512_context ctx2_keccak; sph_whirlpool_context ctx2_whirlpool; sph_haval256_5_context ctx2_haval; sph_tiger_context ctx2_tiger; sph_ripemd160_context ctx2_ripemd; do { data[19] = ++n; nnNonce2 = (int)(data[19]/2); memset(bhash, 0, 7 * 64); ctx2_sha256 = ctx_sha256; sph_sha256 (&ctx2_sha256, data_p64, 80 - M7_MIDSTATE_LEN); sph_sha256_close(&ctx2_sha256, (void*)(bhash[0])); ctx2_sha512 = ctx_sha512; sph_sha512 (&ctx2_sha512, data_p64, 80 - M7_MIDSTATE_LEN); sph_sha512_close(&ctx2_sha512, (void*)(bhash[1])); ctx2_keccak = ctx_keccak; sph_keccak512 (&ctx2_keccak, data_p64, 80 - M7_MIDSTATE_LEN); sph_keccak512_close(&ctx2_keccak, (void*)(bhash[2])); ctx2_whirlpool = ctx_whirlpool; sph_whirlpool (&ctx2_whirlpool, data_p64, 80 - M7_MIDSTATE_LEN); sph_whirlpool_close(&ctx2_whirlpool, (void*)(bhash[3])); ctx2_haval = ctx_haval; sph_haval256_5 (&ctx2_haval, data_p64, 80 - M7_MIDSTATE_LEN); sph_haval256_5_close(&ctx2_haval, (void*)(bhash[4])); ctx2_tiger = ctx_tiger; sph_tiger (&ctx2_tiger, data_p64, 80 - M7_MIDSTATE_LEN); sph_tiger_close(&ctx2_tiger, (void*)(bhash[5])); ctx2_ripemd = ctx_ripemd; sph_ripemd160 (&ctx2_ripemd, data_p64, 80 - M7_MIDSTATE_LEN); sph_ripemd160_close(&ctx2_ripemd, (void*)(bhash[6])); for(int i=0; i < 7; i++){ set_one_if_zero(bhash[i]); mpz_set_uint512(bns[i],bhash[i]); } mpz_set_ui(bns[7],0); for(int i=0; i < 7; i++){ mpz_add(bns[7], bns[7], bns[i]); } mpz_set_ui(product,1); for(int i=0; i < 8; i++){ mpz_mul(product,product,bns[i]); } mpz_pow_ui(product, product, 2); bytes = mpz_sizeinbase(product, 256); bdata = (uint8_t *)realloc(bdata, bytes); mpz_export((void *)bdata, NULL, -1, 1, 0, 0, product); sph_sha256_init(&ctx_final_sha256); sph_sha256 (&ctx_final_sha256, bdata, bytes); sph_sha256_close(&ctx_final_sha256, (void*)(hash)); int digits=(int)((sqrt((double)(nnNonce2))*(1.+EPS))/9000+75); int iterations=20; mpf_set_default_prec((long int)(digits*BITS_PER_DIGIT+16)); mpz_t magipi; mpz_t magisw; mpf_t magifpi; mpf_t mpa1, mpb1, mpt1, mpp1; mpf_t mpa2, mpb2, mpt2, mpp2; mpf_t mpsft; mpz_init(magipi); mpz_init(magisw); mpf_init(magifpi); mpf_init(mpsft); mpf_init(mpa1); mpf_init(mpb1); mpf_init(mpt1); mpf_init(mpp1); mpf_init(mpa2); mpf_init(mpb2); mpf_init(mpt2); mpf_init(mpp2); uint32_t usw_; usw_ = sw_(nnNonce2, SW_DIVS); if (usw_ < 1) usw_ = 1; mpz_set_ui(magisw, usw_); uint32_t mpzscale=mpz_size(magisw); for(int i=0; i < NM7M; i++){ if (mpzscale > 1000) { mpzscale = 1000; } else if (mpzscale < 1) { mpzscale = 1; } mpf_set_ui(mpa1, 1); mpf_set_ui(mpb1, 2); mpf_set_d(mpt1, 0.25*mpzscale); mpf_set_ui(mpp1, 1); mpf_sqrt(mpb1, mpb1); mpf_ui_div(mpb1, 1, mpb1); mpf_set_ui(mpsft, 10); for(int j=0; j <= iterations; j++){ mpf_add(mpa2, mpa1, mpb1); mpf_div_ui(mpa2, mpa2, 2); mpf_mul(mpb2, mpa1, mpb1); mpf_abs(mpb2, mpb2); mpf_sqrt(mpb2, mpb2); mpf_sub(mpt2, mpa1, mpa2); mpf_abs(mpt2, mpt2); mpf_sqrt(mpt2, mpt2); mpf_mul(mpt2, mpt2, mpp1); mpf_sub(mpt2, mpt1, mpt2); mpf_mul_ui(mpp2, mpp1, 2); mpf_swap(mpa1, mpa2); mpf_swap(mpb1, mpb2); mpf_swap(mpt1, mpt2); mpf_swap(mpp1, mpp2); } mpf_add(magifpi, mpa1, mpb1); mpf_pow_ui(magifpi, magifpi, 2); mpf_div_ui(magifpi, magifpi, 4); mpf_abs(mpt1, mpt1); mpf_div(magifpi, magifpi, mpt1); mpf_pow_ui(mpsft, mpsft, digits/2); mpf_mul(magifpi, magifpi, mpsft); mpz_set_f(magipi, magifpi); mpz_add(product,product,magipi); mpz_add(product,product,magisw); mpz_set_uint256(bns[0], (void*)(hash)); mpz_add(bns[7], bns[7], bns[0]); mpz_mul(product,product,bns[7]); mpz_cdiv_q (product, product, bns[0]); if (mpz_sgn(product) <= 0) mpz_set_ui(product,1); bytes = mpz_sizeinbase(product, 256); mpzscale=bytes; bdata = (uint8_t *)realloc(bdata, bytes); mpz_export(bdata, NULL, -1, 1, 0, 0, product); sph_sha256_init(&ctx_final_sha256); sph_sha256 (&ctx_final_sha256, bdata, bytes); sph_sha256_close(&ctx_final_sha256, (void*)(hash)); } mpz_clear(magipi); mpz_clear(magisw); mpf_clear(magifpi); mpf_clear(mpsft); mpf_clear(mpa1); mpf_clear(mpb1); mpf_clear(mpt1); mpf_clear(mpp1); mpf_clear(mpa2); mpf_clear(mpb2); mpf_clear(mpt2); mpf_clear(mpp2); rc = fulltest_m7hash(hash, ptarget); if (rc) { if (opt_debug) { bin2hex(hash_str, (unsigned char *)hash, 32); bin2hex(target_str, (unsigned char *)ptarget, 32); bin2hex(data_str, (unsigned char *)data, 80); applog(LOG_DEBUG, "DEBUG: [%d thread] Found share!\ndata %s\nhash %s\ntarget %s", thr_id, data_str, hash_str, target_str); } pdata[19] = data[19]; goto out; } } while (n < max_nonce && !work_restart[thr_id].restart); pdata[19] = n; out: for(int i=0; i < 8; i++){ mpz_clear(bns[i]); } mpz_clear(product); free(bdata); *hashes_done = n - first_nonce + 1; return rc; }
/** * void calculate_p() * * Descricao: * Calcula o valor da variavel p na n-esima iteracao. * * Parametros de entrada: * - * * Parametros de retorno: * - */ void calculate_p(){ mpf_mul_ui(p_n[n_count+1], p_n[n_count], 2); }
bool bbp_pi(bool const & abortTask, std::string const & digitIndex, uint32_t const digitStep, std::string & piSequence) { unsigned long const mantissa_bits = 132; unsigned long const count_offset = 7; // settings above gives 32 hexadecimal digits unsigned long count = static_cast<unsigned long>( floor(log10(pow(2.0, static_cast<double>(mantissa_bits))))); count -= count_offset; // starting digit mpz_t digit; mpz_init(digit); if (mpz_set_str(digit, digitIndex.c_str(), 10) < 0) return false; mpz_sub_ui(digit, digit, 1); // subtract the 3 digit mpz_add_ui(digit, digit, digitStep); mpz_t tmpI[TEMPORARY_INTEGERS]; for (size_t i = 0; i < (sizeof(tmpI) / sizeof(mpz_t)); ++i) mpz_init(tmpI[i]); mpf_t tmpF[TEMPORARY_FLOATS]; for (size_t i = 0; i < (sizeof(tmpF) / sizeof(mpf_t)); ++i) mpf_init2(tmpF[i], mantissa_bits); // determine epsilon based on the number of digits required mpf_t epsilon; mpf_init2(epsilon, mantissa_bits); mpf_set_ui(epsilon, 10); mpf_pow_ui(epsilon, epsilon, count + count_offset); mpf_ui_div(epsilon, 1, epsilon); // integer constant mpz_t sixteen; mpz_init(sixteen); mpz_set_ui(sixteen, 16); // determine the series mpf_t s1, s2, s3, s4; mpf_init2(s1, mantissa_bits); mpf_init2(s2, mantissa_bits); mpf_init2(s3, mantissa_bits); mpf_init2(s4, mantissa_bits); series(abortTask, s1, 1, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; series(abortTask, s2, 4, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; series(abortTask, s3, 5, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; series(abortTask, s4, 6, tmpI, tmpF, sixteen, digit, epsilon); if (abortTask) return false; // pid = 4. * s1 - 2. * s2 - s3 - s4; mpf_mul_ui(s1, s1, 4); mpf_mul_ui(s2, s2, 2); mpf_t result; mpf_init2(result, mantissa_bits); mpf_sub(result, s1, s2); mpf_sub(result, result, s3); mpf_sub(result, result, s4); // pid = pid - (int) pid + 1.; mpf_t & tmp1 = tmpF[0]; mpf_floor(tmp1, result); mpf_sub(result, result, tmp1); mpf_add_ui(result, result, 1); mpf_abs(result, result); // output the result char resultStr[256]; mp_exp_t exponent; mpf_get_str(resultStr, &exponent, 16, 254, result); resultStr[count + 1] = '\0'; // cut off any erroneous bits piSequence.assign(&resultStr[1]); // cleanup for (size_t i = 0; i < (sizeof(tmpI) / sizeof(mpz_t)); ++i) mpz_clear(tmpI[i]); for (size_t i = 0; i < (sizeof(tmpF) / sizeof(mpf_t)); ++i) mpf_clear(tmpF[i]); mpz_clear(digit); mpf_clear(epsilon); mpz_clear(sixteen); mpf_clear(s1); mpf_clear(s2); mpf_clear(s3); mpf_clear(s4); mpf_clear(result); return true; }
int main(int argc, char *argv[]) { int my_rank, p; long long total; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &p); // MPI_Aint offsets[11]; int error_code; char *params; char *image; char *image_type; // char *timestats; // double starttime,endtime; int *flag; int *width; int *height; int *maxiter; int framepos=0; mpf_t *cc; mpf_t *xrange; mpf_t *yrange; mpf_t *cr; mpf_t *ci; mpf_t *x; mpf_t *y; mpf_t *init_xr; mpf_t *init_yr; mpf_t *final_xr; mpf_t *final_yr; mpf_t *zoom_rate; mpf_t *xd; mpf_t *yd; //Change for Int flag = (int*)malloc(sizeof(int)); width = (int*)malloc(sizeof(int)); height = (int*)malloc(sizeof(int)); maxiter = (int*)malloc(sizeof(int)); //zoom_rate = (double *)malloc(sizeof(double)); cr = (mpf_t*)malloc(sizeof(mpf_t)); ci = (mpf_t*)malloc(sizeof(mpf_t)); x = (mpf_t*)malloc(sizeof(mpf_t)); y = (mpf_t*)malloc(sizeof(mpf_t)); init_xr = (mpf_t*)malloc(sizeof(mpf_t)); // init_yr = (mpf_t*)malloc(sizeof(mpf_t)); // final_xr = (mpf_t*)malloc(sizeof(mpf_t)); // final_yr = (mpf_t*)malloc(sizeof(mpf_t)); // xd = (mpf_t*)malloc(sizeof(mpf_t)); yd = (mpf_t*)malloc(sizeof(mpf_t)); zoom_rate = (mpf_t*)malloc(sizeof(mpf_t)); image_type = (char*)malloc(sizeof(char)*STRLENTH); image = (char*)malloc(sizeof(char)*STRLENTH); // filename = (char*)malloc(sizeof(char)*STRLENTH); params = (char*)malloc(sizeof(char)*STRLENTH*15); xrange = (mpf_t*)malloc(sizeof(mpf_t)*2); yrange = (mpf_t*)malloc(sizeof(mpf_t)*2); cc = (mpf_t*)malloc(sizeof(mpf_t)*2); mpf_init(xrange[0]); mpf_init(xrange[1]); mpf_init(yrange[0]); mpf_init(yrange[1]); mpf_init(*xd); mpf_init(*yd); mpf_init(cc[0]); mpf_init(cc[1]); if (my_rank == 0) { if(argc >=2){ getParams(params, argv); } else { printf("Indicate the file name containing the parameters. kthxbye!\n"); } } //Receive data error_code = MPI_Bcast(params, STRLENTH*15, MPI_CHAR, 0, MPI_COMM_WORLD); #ifdef debug if (my_rank == 2) { printf("RANK = %d As chars-------------\n",my_rank); //See data as Char int i; for(i=0;i<=14;i++) { printf("%s\n",¶ms[i*256]); } } #endif MPI_Barrier(MPI_COMM_WORLD); xtractParams(flag, cr, ci, x, y, init_xr, init_yr,final_xr,final_yr, width, height, maxiter, zoom_rate, image,image_type, params, my_rank); #ifdef debug if (my_rank == 2) { printf("RANK = %d AS datatypes-------------\n",my_rank); //See data as their types printf("Flag: %d \n", *flag); gmp_printf ("cr: %.Ff\n", *cr); gmp_printf ("ci:%.Ff\n", *ci); gmp_printf ("x: %.Ff\n", *x); gmp_printf ("y: %.Ff\n", *y); gmp_printf ("init_xr: %.Ff\n", *init_xr); gmp_printf ("init_yr: %.Ff\n", *init_yr); gmp_printf ("final_xr: %.Ff\n", *final_xr); gmp_printf ("final_yr: %.Ff\n", *final_yr); printf("Width: %d \n", *width); printf("Height: %d \n", *height); printf("Maxiter: %d \n", *maxiter); printf("Zoom: %f \n", *zoom_rate); printf("file: %s\n",image); printf("Type: %s\n",image_type); } #endif mpf_mul_ui(*xd,*final_xr,2); //what is this for? mpf_mul_ui(*yd,*final_yr,2); //Why is this here? if(framepos==0){ if(my_rank ==0) printf("Creating Frames! Progress: \n\n"); } for(framepos = 0; framepos < FRAMECNT;framepos++ ) { char num[15]; sprintf(num, "%d", framepos); char filename[256]; strcpy(filename,image); strcat(filename,num); strcat(filename,image_type); //calculate progress! double numer = (double)framepos; double denum = (double)FRAMECNT; if(my_rank ==0) printf("Frame %d of %d ....%.1f%% \r", framepos+1, FRAMECNT,((numer/denum)*100)); //coordinate calculations //start if(framepos ==0) { mpf_set(cc[0], *cr); mpf_set(cc[1], *ci); mpf_sub(xrange[0], *x, *init_xr); mpf_add(xrange[1], *x, *init_xr); mpf_sub(yrange[0], *y, *init_yr); mpf_add(yrange[1], *y, *init_yr); total = parallelJulia(xrange, *width, yrange, *height, cc, *flag, maxiter, my_rank, p, MPI_COMM_WORLD, filename); } else { mpf_div(*init_xr,*init_xr,*zoom_rate); mpf_div(*init_yr,*init_yr,*zoom_rate); mpf_sub(xrange[0], *x, *init_xr); mpf_add(xrange[1], *x, *init_xr); mpf_sub(yrange[0], *y, *init_yr); mpf_add(yrange[1], *y, *init_yr); total = parallelJulia(xrange, *width, yrange, *height, cc, *flag, maxiter, my_rank, p, MPI_COMM_WORLD, filename); } MPI_Barrier(MPI_COMM_WORLD); } if(my_rank ==0){printf("Done!");} free(flag); free(width); free(height); free(maxiter); free(cr); free(ci); free(x); free(y); free(init_yr); free(init_xr); free(final_yr); free(final_xr); free(xd); free(yd); // free(iterations); free(cc); free(xrange); free(yrange); free(image); free(image_type); free(zoom_rate); free(params); // free(timestats); MPI_Finalize(); return 0; }
void cpx_confluent (cpx_t em, cpx_t a, cpx_t b, cpx_t z, unsigned int prec) { mpf_t fact; cpx_t zee, z_n, term; cpx_t ay, be, poch_a, poch_b; mpf_init (fact); cpx_init (zee); cpx_init (z_n); cpx_init (term); cpx_init (ay); cpx_init (be); cpx_init (poch_a); cpx_init (poch_b); /* Make copy of arguments now! */ cpx_set (zee, z); cpx_set (z_n, zee); cpx_set (ay, a); cpx_set (poch_a, a); cpx_set (be, b); cpx_set (poch_b, b); cpx_set_ui (em, 1, 0); mpf_set_ui (fact, 1); mpf_t mag; mpf_init (mag); /* Use 10^{-2 * prec} for smallest term in sum */ mpf_t maxterm; mpf_init (maxterm); fp_epsilon (maxterm, prec); mpf_mul (maxterm, maxterm, maxterm); unsigned int n=1; while(1) { cpx_div_mpf (term, z_n, fact); cpx_mul (term, term, poch_a); cpx_div (term, term, poch_b); cpx_add (em, em, term); /* Don't go no farther than this */ cpx_mod_sq (mag, term); if (mpf_cmp (mag, maxterm) < 0) break; n++; cpx_mul (z_n, z_n, zee); mpf_mul_ui (fact, fact, n); mpf_add_ui (ay[0].re, ay[0].re, 1); mpf_add_ui (be[0].re, be[0].re, 1); cpx_mul (poch_a, poch_a, ay); cpx_mul (poch_b, poch_b, be); } mpf_clear (fact); mpf_clear (mag); mpf_clear (maxterm); cpx_clear (zee); cpx_clear (z_n); cpx_clear (term); cpx_clear (ay); cpx_clear (be); cpx_clear (poch_a); cpx_clear (poch_b); }
long int julia(const mpf_t x, const mpf_t xr, long int xres, const mpf_t y, const mpf_t yr, long int yres, mpf_t *c, int flag, long int max_iteration, float *iterations, int my_rank, int p, MPI_Comm comm) { double t0 = MPI_Wtime(); int i,j; // Find how many rows per process. int *rows; rows = (int*)malloc(sizeof(int)*p); for (i=0; i < p; i++) rows[i] = yres/p; for (i=0; i < yres % p; i++) rows[i]++; //allocate memory for each processor if(my_rank > 0){ iterations = (float*)malloc( sizeof(float) * xres * rows[my_rank]); assert(iterations); } //------------julia gmp const double maxRadius = 4.0; mpf_t xi, yi, x_min, x_max, y_min, y_max, savex, savex2, savey, radius, xgap, ygap, savex_a, savex_b, savey_a, savey_b, tmp, tmp1; mpf_init(xi); mpf_init(yi); mpf_init(x_min); mpf_init(x_max); mpf_init(y_min); mpf_init(y_max); mpf_init(savex); mpf_init(savex2); mpf_init(savey); mpf_init(radius); mpf_init(xgap); mpf_init(ygap); mpf_init(savex_a); mpf_init(savex_b); mpf_init(savey_a); mpf_init(savey_b); mpf_init(tmp); mpf_init(tmp1); //double x_min = x - xr; mpf_sub(x_min, x, xr); //double x_max = x + xr; mpf_add(x_max, x, xr); //double y_min = y - yr; mpf_sub(y_min, y, yr); //double y_max = y + yr; mpf_add(y_max, y, yr); // spaceing between x and y points //double xgap = (x_max - x_min) / xres; mpf_sub(xgap, x_max, x_min); mpf_div_ui(xgap, xgap, xres); //double ygap = (y_max - y_min) / yres; mpf_sub(ygap, y_max, y_min); mpf_div_ui(ygap, ygap, yres); //---------------------------- long long int iteration; long long int total_number_iterations = 0; int k = 0; for (j = my_rank; j < yres; j+=p){ if(my_rank==0) k = j; //needed for root for (i = 0; i < xres; i++){ //xi = x_min + i * xgap; mpf_mul_ui(tmp, xgap, i); mpf_add(xi, x_min, tmp); //yi = y_min + j * ygap; mpf_mul_ui(tmp, ygap, j); mpf_add(yi, y_min, tmp); //flag betwee[n julia or mandelbrot //savex = flag * c[0] + (1 - flag) * xi; mpf_mul_ui(savex_a, c[0], flag); mpf_mul_ui(savex_b, xi, (1-flag)); mpf_add(savex, savex_a, savex_b); //savey = flag * c[1] + (1 - flag) * yi; mpf_mul_ui(savey_a, c[1], flag); mpf_mul_ui(savey_b, yi, (1-flag)); mpf_add(savey, savey_a, savey_b); //radius = 0; mpf_set_ui(radius, 0); iteration = 0; //while ((radius <= maxRadius) && (iteration < max_iteration)){ while ((mpf_cmp_d(radius, maxRadius)<=0) && (iteration < max_iteration)){ //savex2 = xi; mpf_add_ui(savex2, xi, 0); //xi = xi * xi - yi * yi + savex; mpf_mul(xi, xi, xi); mpf_mul(tmp, yi, yi); mpf_sub(xi, xi, tmp); mpf_add(xi, xi, savex); //yi = 2.0f * savex2 * yi + savey; mpf_mul_ui(tmp, savex2, 2); mpf_mul(yi, yi, tmp); mpf_add(yi, yi, savey); //radius = xi * xi + yi * yi; mpf_mul(tmp, xi, xi); mpf_mul(tmp1, yi, yi); mpf_add(radius, tmp, tmp1); iteration++; } total_number_iterations += iteration; float *p = iterations + k*xres + i; //if (radius > maxRadius){ if (mpf_cmp_d(radius, maxRadius)>0){ //float zn = sqrt(xi*xi + yi*yi); mpf_t zn; mpf_init(zn); mpf_mul(tmp, xi, xi); mpf_mul(tmp1, yi, yi); mpf_add(zn, tmp, tmp1); mpf_sqrt(zn, zn); double n = mpf_get_d(zn); //float nu = log(log(zn) / log(2))/log(2); double nu = log(log(n) / log(2))/log(2); //the point has escaped at iteration at any of the iterations 0,1,2,3... *p = iteration + 1 - nu; } else // zij stays within the region up to max_iteration { assert(iteration==max_iteration); *p = -1; } } k++; } //collect various data MPI_Status status; if(my_rank == 0){ int i,j; for(i = 1; i < p; i++){ for(j = 0; j < rows[i]; j++){ MPI_Recv((iterations + (i + j * p) * xres), xres, MPI_FLOAT, i, 0, comm, &status); //MPI_Irecv((iterations + (i + j * p) * xres), xres, MPI_FLOAT, i, 0, comm, NULL); } } } else{ int i; for(i = 0; i < rows[my_rank]; i++) MPI_Send((iterations + i *xres), xres, MPI_FLOAT, 0, 0, comm); } //reduce max iteration count long long int total_reduced_iterations = -1; //printf("rank: %i, total_reduced_iterations: %i\n", my_rank, total_number_iterations); MPI_Reduce(&total_number_iterations, &total_reduced_iterations, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, comm); double t4 = MPI_Wtime(); double max_reduced_time = -1; double total_time = t4 - t0; MPI_Reduce(&total_time, &max_reduced_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm); if(my_rank == 0){ printf("np: %i, time: %f , iterations: %lld\n",p, max_reduced_time, total_reduced_iterations); //printf("%i\t%.2e\n", p, max_reduced_time); } //clear //printf("proc: %i, total time: %lf sec, init: %lf sec, calc: %lf sec, collect: %lf\n", my_rank, t4-t0, t1-t0, t2-t1, t3-t2); return total_reduced_iterations; }
void mpc_div (mpc_t *rop, mpc_t op1, mpc_t op2) { /* This function needs to be fixed. At the current moment I am tired * of having to mess around with the precision values, so I'm just * going to convert the values to 'mpf's and call it quits for the * day. I'll fix it later. * Division by zero is not checked for in this function. */ mpf_t hold_op1; mpf_t hold_op2; mpf_t hold_res; unsigned int prec; mpf_init (hold_op1); mpf_init (hold_op2); mpf_init (hold_res); mpf_set_z (hold_op1, op1.object); mpf_set_z (hold_op2, op2.object); // Get the largest precision prec = (op1.precision > op2.precision) ? op1.precision : op2.precision; if (prec == 0) prec = default_prec; // Set the scalar values while (op1.precision-- > 0) mpf_div_ui (hold_op1, hold_op1, 10); while (op2.precision-- > 0) mpf_div_ui (hold_op2, hold_op2, 10); // Get the value mpf_div (hold_res, hold_op1, hold_op2); for (rop->precision = prec; prec > 0; prec--) mpf_mul_ui (hold_res, hold_res, 10); mpz_set_f (rop->object, hold_res); /* temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision; if (!op1.precision && !op2.precision) { temp.precision = default_prec; mpz_set (temp.object, op1.object); power_of_ten (temp.object, default_prec); mpz_tdiv_q (temp_res, temp.object, op2.object); } else if (op1.precision < op2.precision) { temp.precision = op2.precision; mpz_set (temp.object, op1.object); power_of_ten (temp.object, op2.precision - op1.precision); mpz_tdiv_q (temp_res, temp.object, op2.object); } else if (op1.precision > op2.precision) { temp.precision = op1.precision; mpz_set (temp.object, op2.object); power_of_ten (temp.object, op1.precision - op2.precision); mpz_tdiv_q (temp_res, op1.object, temp.object); } else { temp.precision = op1.precision; mpz_set (temp.object, op1.object); mpz_tdiv_q (temp_res, temp.object, op2.object); } rop->precision = temp.precision; mpz_set (rop->object, temp_res); */ }