Chud Pi and "raw" output
cino hilliard
hillcino368 at hotmail.com
Tue Nov 13 22:29:11 CET 2007
Hi,
I have been playing with the chudnovsky.c pi program and have found some
interesting things. Thanks Jim, for the suggestion to use "raw" output. I translated
one of my pi programs shortpi.c to use the features of gmp output and input.
First consider chudnovsky.c
Add the following to the main()
int main(int argc, char *argv[])
{
mpf_t pi,qi;
mpz_t piz;
FILE *fptr1;
mp_exp_t expptr;
char *pi_str;
pi_str = calloc(200000000,1);
char fil[] = "zpi.txt";
fptr1=fopen(fil,"w+");
...
...
add/modify this in the output routine
/* output Pi and timing statistics */
if (out&1) {
printf("pi(0,%ld)=\n", terms);
begin=clock();
mpf_get_str(pi_str,&expptr,10,d+2,qi);
mpz_set_str(piz,pi_str,10);
mpz_out_raw(fptr1,piz); //save compressed
end=clock();
rewind(fptr1);
// mpz_inp_raw(piz,fptr1); //read compressed file
// mpz_out_str(stdout,10,piz); //display it
fclose(fptr1);
printf("\n");
printf("Time to compute = %6.3f\n",t) ;
printf("Time to convert raw and save to %s = %6.3f\n",fil,(double)(end begin)/CLOCKS_PER_SEC);
}
Run for 10 million
f:\GmpGcc>chudpi 10000000 1
#terms=705136, depth=21
sieve time = 0.312
...................................................
bs time = 60.516
gcd time = 10.789
div time = 8.515
sqrt time = 4.672
mul time = 3.281
P size=14561191 digits (1.456119)
Q size=14561184 digits (1.456118)
pi(0,705136)=
Time to compute = 77.343
Time to convert raw and save to zpi.txt = 58.344
f:\GmpGcc>dir zpi.txt
Volume in drive F is New Volume
Volume Serial Number is 186E-0843
Directory of f:\GmpGcc
11/13/2007 12:41 PM 4,168,682 zpi.txt
1 File(s) 4,168,682 bytes
0 Dir(s) 451,458,469,888 bytes free
This output indicates that it takes 58 sec to convert mpf to a string, convert the string
mpz and out put raw to a file. This is a little more costly than saving mpf but the real
savings come from the file compression to 42% of the org file.
The raw converts the numbers base 10 to base 255 using the ascii character set.
This in essance compresses the numbers to a smaller file of the ascii character set
representing the numbers.
Such savings are quite useful especially storing large amounts of data such as a table
of primes. At 42% compression, we can store primes less than 1 trillion in a 126 gig file.
This of course would have to be done in fixed record lengths so we could compute the
location of a prime and get the nth-prime.
At the bottom is a pi program which shows how to compute pi, store raw and read it back.
You can toggle the commented code to get output. This code computes pi in mpz and mpf.
mpz is about 3.5 times faster than mpf.
Have fun,
Cino hilliard
> Today's Topics:
>
> 1. Pi_Chud Demo, mpf_out_str, etc (Jim White)
> 2. Pi_Chud Demo, mpf_out_str, etc (Torbjorn Granlund)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 12 Nov 2007 20:00:41 +0000 (GMT)
> From: Jim White
> Subject: Pi_Chud Demo, mpf_out_str, etc
> To: gmp-discuss at swox.com
> Message-ID:
> Content-Type: text/plain; charset=iso-8859-1
>
>
> To the best of my recollection:
>
> 1. Some convert-to-string functions in gmp were quite
> inefficient, until improvements were recently made
> (these from Paul Zimmermann, MPFR team).
>
> 2. Since GMP 4.2, I have had no such problems, nor
> have I had any problems with the pi-chudnovsky demo
> program. Moving on from GMP 4.1 seems to be a prudent
> step.
>
> 3. In any case, if you compute any number to a million
> digits precision, why on Earth would you convert that
> to a base-10 representation at all? Who might be
> interested in reading this data?
>
> If the value needs to be recorded, it's surely better
> to save it in raw binary form, isn't it?
>
> Regards
>
> Jim White
> MSI, ANU
> Canberra
>
>
>
>
>
> ------------------------------
>
> Message: 2
> Date: Tue, 13 Nov 2007 01:38:43 +0100
> From: Torbjorn Granlund
> Subject: Pi_Chud Demo, mpf_out_str, etc
> To: gmp-discuss at swox.com
> Message-ID:
> Content-Type: text/plain; charset=iso-8859-1
>
> To the best of my recollection:
>
> 1. Some convert-to-string functions in gmp were quite
> inefficient, until improvements were recently made
> (these from Paul Zimmermann, MPFR team).
>
> MPFR and GMP are distinct, and the gmp-chudnovsky.c program uses just
> GMP. Perhaps Paul Zimmermann made some improvements to the mpfr
> conversion routines, but he did not, as far as I can tell, do any
> public work on GMP's mpf conversion routines. (The one culprit behind
> the conversion code released with GMP is myself.)
>
> 2. Since GMP 4.2, I have had no such problems, nor
> have I had any problems with the pi-chudnovsky demo
> program. Moving on from GMP 4.1 seems to be a prudent
> step.
>
> Both mpf_get_str and mpf_out_str used to need runtime quadratic to the
> number of digits produced. Now it is O(M(n)).
>
> For those really concerned with conversion speed, I've made a number
> of improvements to the underlying mpn_get_str code, see
> http://gmplib.org/devel/. That code improves conversion of operands
> of at least a few hundred digits, but there should soon be code in the
> same place that is about twice as fast for smaller operands.
>
> --
> Torbj?rn
>
>
> ------------------------------
>
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at swox.com
> https://gmplib.org/mailman/listinfo/gmp-discuss
>
>
> End of gmp-discuss Digest, Vol 51, Issue 8
> ******************************************
CODE CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CODE
/* Gmp gcc 4.1.2 shortpi. Short algotithm to compute Pi using
pi = 6*asin(1/2). I stumbled on this fooling around with
an HP 55 calculator. Then asin(x) =
x + (1/2/3)*x^3 + (1*2/2/4/5)*x^5 + (1*3*5/2/4/6/7)*x^7 ...
and pi=6/2 + (6/2/3)*1/2^3 + ...
The straight c loop is
for(n=0;n<=places*1.7;n++)
{
x = x*(2*n+1)/(n+1)/8;
pi+=x/(n+n+3);
}
It would be nice if variables in gcc linked with gmp could be
declared mp?_t x,y,z and called and operated on in gcc as
literals x,y,z.
*/
#include
#include
#include
#include
#define timer GetTickCount()/1000.0
pif(mpf_t pi,unsigned long m)
{
unsigned long n;
mpf_t x,x2;
mpf_init(x2);
mpf_init_set_ui(x,3);
mpf_init_set_ui(pi,3);
m*=1.7;
for(n=0;n<=m;n++)
{
mpf_set_prec_raw(x,2*(n+1)); //speeds things up a lot
mpf_mul_ui(x,x,(n<<=m;n++)
{
mpz_mul_ui(x,x,(n<
More information about the gmp-discuss
mailing list