perlmemorystack-size

allocatemem() function in Pari for perl


I was generating random numbers using the Crypt::Random module for perl which relies on Math::Pari, a perl interface for Pari/GP, and got this error when trying to generate a 1.5M digit number:

C:\Users\Jlinne\Documents>perl -MCrypt::Random=makerandom_itv -E "say makerandom_itv(Lower => '10^1499999', Upper => '10^1500000')" > num_1500000.txt
PARI:   ***   the PARI stack overflows !
        current stack size: 4.0 Mbytes
  [hint] you can increase GP stack with allocatemem()

C:\Users\Ryan\Documents\Perl Scripts>perl scripts1.pl
"use" not allowed in expression at scripts1.pl line 6, at end of line
syntax error at scripts1.pl line 6, near "use Math::Pari
use Math::PariInit "
BEGIN not safe after errors--compilation aborted at scripts1.pl line 7.

My Script:

#!/usr/bin/env perl
use warnings;
use strict;
use feature 'say';
use Math::Pari
use Math::PariInit qw( primes=12000000 stack=1e8 );
use Crypt::Random

I had guessed the allocatemem() function was a Math::Pari function, but it is not. Does anyone know to change the GP stack size to 8.0 Mbytes using a script compared to one-liner? Thanks.

Problem with the stack to 1e+32

C:\Users\Jlinne\Documents\Perl Scripts>perl scripts1.pl > BIGINT1500000.txt
PARI:   ***   the PARI stack overflows !
        current stack size: 0.0 Mbytes
  [hint] you can increase GP stack with allocatemem()
Compilation failed in require at C:/Strawberry/perl/site/lib/Math/PariInit.pm line 26.
BEGIN failed--compilation aborted at scripts1.pl line 6.

Script:

use warnings 'all';
use strict;
use feature 'say';

# use Math::Pari;
use Math::PariInit qw( stack=1e32 );
use Crypt::Random qw(makerandom_itv);

say makerandom_itv(Lower => '10^1499999', Upper => '10^1500000');

Solution

  • You do not have to reach for allocatemem(), which is listed as one of the functions that can be used but aren't directly supported, see this in Math::Pari.

    Instead, from the INITIALIZATION in Math::Pari

    When Math::Pari is loaded, it examines variables $Math::Pari::initmem and $Math::Pari::initprimes. They specify up to which number the initial list of primes should be precalculated, and how large should be the arena for PARI calculations (in bytes). (These values have safe defaults.)

    Since setting these values before loading requires either a BEGIN block, or postponing the loading (use vs. require), it may be more convenient to set them via Math::PariInit:

    use Math::PariInit qw( primes=12000000 stack=1e8 );
    

    See the (short) page for Math::PariInit.

    A complete example

    use warnings 'all';
    use strict;
    use feature 'say';
    
    # use Math::Pari;
    use Math::PariInit qw( stack=1e8 );
    use Crypt::Random qw(makerandom_itv);
    
    say makerandom_itv(Lower => '10^1499999', Upper => '10^1500000')";
    

    Running script.pl > BIG_num.txt generates a 1.5Mb file with the number (in 11 minutes).

    In this way the stack size is set at compilation. See the first link for changing it dynamically.