next up previous
Next: How can I compile Up: A gp2c tutorial Previous: A gp2c tutorial


How can I compile and run my scripts?

The simplest way to use gp2c is to call gp2c-run. If you want to know what happens in detail, see next section.

To make the examples easier to follow, please move to the gp2c directory and link the root of your PARI source there:

ln -s .../pari .

As an example, we will take the file pari/examples/squfof.gp, which is a simple implementation of the well-known SQUFOF factoring method of D. Shanks.

We just run the command:

./gp2c-run pari/examples/squfof.gp

After a little processing we get a GP session. But this session is special, because it contains the compiled squfof function. Hence we can do the following:

parisize = 4000000, primelimit = 500000
? squfof(3097180303181) 
[419]
i = 596
Qfb(133225, 1719841, -261451, 0.E-28)
%1 = 1691693 
Let's try a bigger example:
? squfof(122294051504814979)
[20137]
  ***   the PARI stack overflows !
  current stack size: 4.0 Mbytes
  [hint] you can increase GP stack with allocatemem()
? allocatemem()
  ***   Warning: doubling stack size; new stack = 8.0 MBytes.
? squfof(122294051504814979)
[20137]
[20137, 3445]
i = 46474
Qfb(321233929, 131349818, -367273962, 0.E-28)
%2 = 73823023
We need a large stack because by default gp2c does not generate code to handle the stack (the so-called gerepile code). To instruct gp2c to add gerepile code automatically, we must use the -g option. So quit this GP session and launch a new one with -g. Oh well, before that type

ls pari/examples/squfof.gp*

pari/examples/squfof.gp    pari/examples/squfof.gp.o
pari/examples/squfof.gp.c  pari/examples/squfof.gp.so

These are the files generated by gp2c-run:

It is the shared library which is used by GP.

Now let's continue:

./gp2c-run -g pari/examples/squfof.gp

parisize = 4000000, primelimit = 500000
? squfof(122294051504814979)
[20137]
[20137, 3445]
i = 46474
Qfb(321233929, 131349818, -367273962, 0.E-28)
%1 = 73823023

This time it works with no difficulty using the default stack. We would like to know how much faster the compiled code runs, so we need to load the non compiled squfof file in GP:

? \r pari/examples/squfof.gp
  ***   unexpected character: squfof(n)=if(isprime(n),retur
                                       ^--------------------
Why?? Because squfof already exists as an installed function and GP refuses to overwrite it. To solve this problem, we will add a suffix to the name of the compiled function under GP. Quit the session and type:

./gp2c-run -g -s_c pari/examples/squfof.gp

Now the function squfof is named squfof_c instead, so we can do

parisize = 4000000, primelimit = 500000
? \r pari/examples/squfof.gp
? #
   timer = 1 (on)
? squfof(122294051504814979)
[20137]
[20137, 3445]
i = 46474
Qfb(321233929, 131349818, -367273962, 0.E-28)
time = 5,810 ms.
%1 = 73823023
? squfof_c(122294051504814979)
[20137]
[20137, 3445]
i = 46474
Qfb(321233929, 131349818, -367273962, 0.E-28)
time = 560 ms.
%2 = 73823023
So the compiled version is more than ten times faster than the noncompiled one. However for more complex examples, compiled code usually runs only three times faster on average.


next up previous
Next: How can I compile Up: A gp2c tutorial Previous: A gp2c tutorial
Bill Allombert 2006-01-28