-1

I have written an implementation of Toom multiplication with GMP. I tested multiplication of 2 numbers of around 7000 digits (23,000 bits) and it worked perfectly. However when I tried it with 70000 digits (230,000 bits) the program started producing wrong answers.

I am using a 32 bit system. Could GMP run out of memory to use? I did not get any error so I think this is unlikely. If this is not the case I suspect some loss of precision somewhere.

qwr
  • 9,525
  • 5
  • 58
  • 102
  • 3
    Your question doesn't make any sense. It reads like "my program produces wrong answers, could it be because GMP is running out of memory?" That seems like a totally random diagnosis that bears no resemblance whatsoever to your description of the problem. You should put much more effort into describing the problem and, if you want to propose a diagnosis, provide some evidence to back that diagnosis up. Otherwise, let other people diagnose the problem. – David Schwartz Aug 25 '14 at 04:09
  • @DavidSchwartz I don't understand how my question doesn't make sense. The program explicitly works for small values but not large; that's why I believe there is an issue of precision. That is my evidence, in addition to the fact that the entire program makes extensive usage of GMP features. – qwr Aug 25 '14 at 04:39
  • 1
    Isn't it far more likely that the bug is in your code than that's in a widely-used and extensively-reviewed library like GMP? – David Schwartz Aug 25 '14 at 05:17
  • 1
    What is the size of limb i.e. `sizeof(mp_limb_t)` for your configuration (even if you have 32-bit OS, the limb might be defined as `unsigned long long` as your CPU might have 64-bit registers) ? Why you are implementing Toom multiplication if it's implemented by library itself (that is, `mpz_mul` "redirects" to proper algorithm based essentially on number of limbs of operands) ? The library has defined multiple thresholds for various mul implementations (defined in gmp-impl.h as number of limbs). For very large operands (thousends limbs) FFT multiplication might be preffered. – Grzegorz Szpetkowski Aug 25 '14 at 16:30

1 Answers1

3

In general GMP allocates data on heap with "malloc and friends". If data cannot be allocated properly, then it invokes standard abort() function (so process should be simply aborted), see memory.c:

ret = malloc (size);
if (ret == 0)
  {
    fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
    abort ();
  }

It's rather impossible to tell what went wrong in your calculations, without taking much more details (see my comment).

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137