Alasdair McAndrew on Sat, 23 Jun 2012 17:14:13 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

A question about memory and garbage collection


I am testing some prng routines, and using PARI to speed up the computations.

My basic routine contains something like

int main(int argc, char *argv[])
{
  pari_init(1000000,0);
  n = gp_read_str(argv[1]);
  i = gen_0;
  do 
    {
      GEN t = prng();
    }
  while (mpcmp(i,n)<0);

  pari_close();
  return 0;
}

Here prng() is a function which returns a GEN.  Basically it takes a value x, does some computations (using a few auxiliary variables), and returns an updated value.  It may do this 10^12 times.  Clearly I need to have some sort of memory management: once a value x is updated and used to create the next value, it is needed no longer.  So I need some way of removing the previous value of x so that the stack doesn't overflow.

Here's an example of the sort of prng() I might use (I'm not actually using this one, but you get the idea):

GEN prng(void)
{
  y = Fp_pow(x,7,p);
  x = y;
  return(x);
}

(where p is a GEN set to a large prime).  How do I keep the memory and stack at a reasonable size?  I've read the manual, and experimented with cgiv, and with avma, gerepile etc, but so far none of my attempts has worked.

I'm sorry if this is a stupid question, but I am quite new to PARI, and not a very experienced C programmer: most of my work up to now has been in high level languages (Maple, Sage, Maxima, etc).

Thanks,
Alasdair



--
Blog: http://amca01.wordpress.com
Web:  http://sites.google.com/site/amca01/
Facebook: http://www.facebook.com/alasdair.mcandrew