Problem with large values in GMP...

Torbjörn Granlund tg at gmplib.org
Sun Mar 20 15:22:41 UTC 2016


wraithx at morpheus.net writes:

  I must apologize, I did unfortunately use %u to print out the mpz_sizeinbase.
  The code I was working on that led me to this incorrect print statement did have
  a problem on Win64 systems.  It used:
  unsigned long i = 0;
  for (i = mpz_sizeinbase(n, 2); i > 0; i--) {...}
  This "for" loop was behaving like n had only 30 bits.  I have switched i to use
  a larger type and it is behaving as expected.
  
Better that than a GMP bug.  :-)
  
  Thank you all very much for helping me track down that error.  Now I've been
  testing Rob's code and am wondering why we can create a number with
  slightly more than 2^32 bits, but above a certain point I get an error of:
  GNU MP: Cannot allocate memory (size=4294909984)
  
32-bit machines tend to have memory size limitations...  See e.g.,
<https://gmplib.org/list-archives/gmp-discuss/2012-April/005020.html>.

  For example, if we use these exponents:
  mpz_mul_2exp(num2, num2, 4006516961)
  mpz_mul_2exp(num2, num2, 500814592)
  The program will calculate mpz_mul(num3, num1, num2) with no problem.
  And the result will have:
  num bits in num3 = 4507331553
  
  However, with the following exponents:
  mpz_mul_2exp(num2, num2, 4006516961)
  mpz_mul_2exp(num2, num2, 500814593)
  The program will crash during mpz_mul(num3, num1, num2) saying:
  GNU MP: Cannot allocate memory (size=4294909984)
  
You've probably found the size where 4 GiB per-process address space is
insufficient for the calculation.  (You'll be more lucky generating huge
numbers using e.g. addition, since addition needs very limited scratch
space, while multiplication needs a lot.)

  Something else I'd like to report.  While trying to come up with a
  test case for my original "problem", I stumbled onto what appears to
  be another problem.  When I ran a particular mpz_ui_pow_ui it would
  succeed with GMP 6.0.0 and 6.1.0, but there was a memory leak with GMP
  5.1.2 and 5.1.3. (These are the only 4 versions I tested)
  
  For example with GMP 6.0.0 and 6.1.0, it would take 1m20s to calculate
  the value, and only use ~1600MB.  With 5.1.2 and 5.1.3, after 9m50s it
  had already used ~6470MB, and at that point I killed the program.

It is good to hear that our work on improving GMP is paying off.  
  

-- 
Torbjörn
Please encrypt, key id 0xC8601622


More information about the gmp-discuss mailing list