User avatar
scruss
Posts: 2229
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

PARI/GP Computer Algebra System: gp2c can't produce executables

Mon May 06, 2019 4:05 am

Heater wrote:
Sun May 05, 2019 12:49 pm
Can you show how to use gp2c to make your solution an actual stand alone executable?
I'm not sure it can, but I'd really like to be proved wrong.

Background: PARI/GP is a computer algebra system that can use arbitrary precision numbers. There was a long-running learning challenge to see what systems that could run on a Raspberry Pi that were capable of calculating Fibonacci(4784969), a million-digit number. The series is defined as:

Code: Select all

Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2),
where:
	Fibonacci(0) = 0
	Fibonacci(1) = 1
A number of solutions were listed here: https://github.com/ZiCog/fibo_4784969

You can also calculate it using:

Code: Select all

Fibonaacci(n) = (((1 + sqrt(5)) / 2)^n) / sqrt(5)
I'd previously listed a solution that worked quite well. This runs on a Raspberry Pi Zero even fairly quickly:

Code: Select all

$ time gp -s $((128*1024**2)) -q fibo.gp |tail -1 | head -c 33 ; time gp -s $((128*1024**2)) -q fibo.gp |tail -1 | tail -c 40
1.0727395641800477229364813596225
real	0m26.910s
user	0m26.113s
sys	0m0.256s
4856539211500699706378405156269 E999999

real	0m27.160s
user	0m26.150s
sys	0m0.233s
You can produce compiled versions, but they don't appear faster or be able to stand alone. Here's a log of compiling it:

Code: Select all

$ sudo apt install pari-gp pari-gp2c
 ...
 
 $ cat fibc.gp 
 /* 
sudo apt install pari-gp2c
gp2c-run -g -W fibc.gp
*/

fibc(n) = (((1 + sqrt(5)) / 2)^n) / sqrt(5)

$ cat $bigfib.gc
\r ./fibc.gp.run
\p 1000000
allocatemem(134217728)
print(fibc(4784969))
quit

$ gp2c-run -g -W fibc.gp
 …
 
 $ cat 
 
 $ cat 
 $ time gp -s $((128*1024**2)) -q bigfib.gp |tail -1 | head -c 33 ; time gp -s $((128*1024**2)) -q bigfib.gp |tail -1 | tail -c 40
  ***   Warning: new stack size = 134217728 (128.000 Mbytes).
1.0727395641800477229364813596225
real	0m27.008s
user	0m26.086s
sys	0m0.330s
  ***   Warning: new stack size = 134217728 (128.000 Mbytes).
4856539211500699706378405156269 E999999

real	0m26.698s
user	0m25.962s
sys	0m0.367s

$ cat fibc.gp.run

install("init_fibc","v","init_fibc","./fibc.gp.so");
install("fibc","D0,G,p","fibc","./fibc.gp.so");

$ cat bigfib.gp 
\r ./fibc.gp.run
\p 1000000
allocatemem(134217728)
print(fibc(4784969))
quit

$ time gp -s $((128*1024**2)) -q bigfib.gp |tail -1 | head -c 33 ; time gp -s $((128*1024**2)) -q bigfib.gp |tail -1 | tail -c 40
  ***   Warning: new stack size = 134217728 (128.000 Mbytes).
1.0727395641800477229364813596225
real	0m27.008s
user	0m26.086s
sys	0m0.330s
  ***   Warning: new stack size = 134217728 (128.000 Mbytes).
4856539211500699706378405156269 E999999

real	0m26.698s
user	0m25.962s
sys	0m0.367s
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Heater
Posts: 12216
Joined: Tue Jul 17, 2012 3:02 pm

Re: PARI/GP Computer Algebra System: gp2c can't produce executables

Tue May 07, 2019 4:20 am

Well, never mind, building executables from pari-gp was just a thought.

I've added your pari-gp solution to the fibo(4784969) challenge repository: https://github.com/ZiCog/fibo_4784969/b ... gp/fibo.gp

Runs fine on my PC, fast too:

Code: Select all

$ sudo apt-get install pari-gp
$ time gp -s $((128*1024**2)) -q fibo.gp |tail -1 | head -c 33 ; time gp -s $((128*1024**2)) -q fibo.gp |tail -1 | tail -c 40
1.0727395641800477229364813596225
real    0m1.054s
user    0m0.859s
sys     0m0.188s
4856539211500699706378405156269 E999999

real    0m1.050s
user    0m0.797s
sys     0m0.281s
I love the way it uses a totally different technique to all our other big fibo solutions and gives a floating point result!

I should try and find the time to run all the fibo(4784969) challenge entries on my Pi 3 and tabulate the run times. Not that performance was the main point of the exercise. I could include other useless stats, like lines of code etc...

Return to “Off topic discussion”