Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6: Cannot unbox 65536 bit wide bigint into native integer

I try some examples from Rosettacode and encounter an issue with the provided Ackermann example: When running it "unmodified" (I replaced the utf-8 variable names by latin-1 ones), I get (similar, but now copyable):

$ perl6 t/ackermann.p6
65533
19729 digits starting with 20035299304068464649790723515602557504478254755697...
Cannot unbox 65536 bit wide bigint into native integer
  in sub A at t/ackermann.p6 line 3
  in sub A at t/ackermann.p6 line 11
  in sub A at t/ackermann.p6 line 3
  in block <unit> at t/ackermann.p6 line 17

Removing the proto declaration in line 3 (by commenting out):

$ perl6 t/ackermann.p6
65533
19729 digits starting with 20035299304068464649790723515602557504478254755697...
Numeric overflow
  in sub A at t/ackermann.p6 line 8
  in sub A at t/ackermann.p6 line 11
  in block <unit> at t/ackermann.p6 line 17

What went wrong? The program doesn't allocate much memory. Is the natural integer kind-of limited?

I replaced in the code from Ackermann function the 𝑚 with m and the 𝑛 with n for better terminal interaction for copying errors and tried to comment out proto declaration. I also asked Liz ;)

use v6;

proto A(Int \m, Int \n) { (state @)[m][n] //= {*} }

multi A(0,      Int \n) { n + 1 }
multi A(1,      Int \n) { n + 2 }
multi A(2,      Int \n) { 3 + 2 * n }
multi A(3,      Int \n) { 5 + 8 * (2 ** n - 1) }

multi A(Int \m, 0     ) { A(m - 1, 1) }
multi A(Int \m, Int \n) { A(m - 1, A(m, n - 1)) }

# Testing:
say A(4,1);
say .chars, " digits starting with ", .substr(0,50), "..." given A(4,2);

A(4, 3).say;
like image 879
Sno Avatar asked Feb 05 '19 06:02

Sno


2 Answers

Please read JJ's answer first. It's breezy and led to this answer which is effectively an elaboration of it.

TL;DR A(4,3) is a very big number, one that cannot be computed in this universe. But raku(do) will try. As it does you will blow past reasonable limits related to memory allocation and indexing if you use the caching version and limits related to numeric calculations if you don't.


I try some examples from Rosettacode and encounter an issue with the provided Ackermann example

Quoting the task description with some added emphasis:

Arbitrary precision is preferred (since the function grows so quickly)

raku's standard integer type Int is arbitrary precision. The raku solution uses them to compute the most advanced answer possible. It only fails when you make it try to do the impossible.

When running it "unmodified" (I replaced the utf-8 variable names by latin-1 ones)

Replacing the variable names is not a significant change.

But adding the A(4,3) line shifted the code from being computable in reality to not being computable in reality.

The example you modified has just one explanatory comment:

Here's a caching version of that ... to make A(4,2) possible

Note that the A(4,2) solution is nearly 20,000 digits long.

If you look at the other solutions on that page most don't even try to reach A(4,2). There are comments like this one on the Phix version:

optimised. still no bignum library, so ack(4,2), which is power(2,65536)-3, which is apparently 19729 digits, and any above, are beyond (the CPU/FPU hardware) and this [code].

A solution for A(4,2) is the most advanced possible.

A(4,3) is not computable in practice

To quote Academic Kids: Ackermann function:

Even for small inputs (4,3, say) the values of the Ackermann function become so large that they cannot be feasibly computed, and in fact their decimal expansions cannot even be stored in the entire physical universe.

So computing A(4,3).say is impossible (in this universe).

It must inevitably lead to an overflow of even arbitrary precision integer arithmetic. It's just a matter of when and how.

Cannot unbox 65536 bit wide bigint into native integer

The first error message mentions this line of code:

proto A(Int \m, Int \n) { (state @)[m][n] //= {*} }

The state @ is an anonymous state array variable.

By default @ variables use the default concrete type for raku's abstract array type. This default array type provides a balance between implementation complexity and decent performance.

While computing A(4,2) the indexes (m and n) remain small enough that the computation completes without overflowing the default array's indexing limit.

This limit is a "native" integer (note: not a "natural" integer). A "native" integer is what raku calls the fixed width integers supported by the hardware it's running on, typically a long long which in turn is typically 64 bits.

A 64 bit wide index can handle indices up to 9,223,372,036,854,775,807.

But in trying to compute A(4,3) the algorithm generates a 65536 bits (8192 bytes) wide integer index. Such an integer could be as big as 265536, a 20,032 decimal digit number. But the biggest index allowed is a 64 bit native integer. So unless you comment out the caching line that uses an array, then for A(4,3) the program ends up throwing the exception:

Cannot unbox 65536 bit wide bigint into native integer

Limits to allocations and indexing of the default array type

As already explained, there is no array that could be big enough to help fully compute A(4,3). In addition, a 64 bit integer is already a pretty big index (9,223,372,036,854,775,807).

That said, raku can accommodate other array implementations such as Array::Sparse so I'll discuss that briefly below because such possibilities might be of interest for other problems.

But before discussing bigger arrays, running the code below on tio.run shows the practical limits for the default array type on that platform:

my @array;
@array[2**29]++; # works
@array[2**30]++; # could not allocate 8589967360 bytes
@array[2**60]++; # Unable to allocate ... 1152921504606846977 elements
@array[2**63]++; # Cannot unbox 64 bit wide bigint into native integer

(Comment out error lines to see later/greater errors.)

The "could not allocate 8589967360 bytes" error is a MoarVM panic. It's a result of tio.run refusing a memory allocation request.

I think the "Unable to allocate ... elements" error is a raku level exception that's thrown as a result of exceeding some internal Rakudo implementation limit.

The last error message shows the indexing limit for the default array type even if vast amounts of memory were made available to programs.

What if someone wanted to do larger indexing?

It's possible to create/use other @ (does Positional) data types that support things like sparse arrays etc.

And, using this mechanism, it's possible that someone could write an array implementation that supports larger integer indexing than is supported by the default array type (presumably by layering logic on top of the underlying platform's instructions; perhaps the Array::Sparse I linked above does).

If such an alternative were called BigArray then the cache line could be replaced with:

my @array is BigArray;
proto A(Int \𝑚, Int \𝑛) { @array[𝑚][𝑛] //= {*} }

Again, this still wouldn't be enough to store interim results for fully computing A(4,3) but my point was to show use of custom array types.

Numeric overflow

When you comment out the caching you get:

Numeric overflow

Raku/Rakudo do arbitrary precision arithmetic. While this is sometimes called infinite precision it obviously isn't actually infinite but is instead, well, "arbitrary", which in this context also means "sane" for some definition of "sane".

This classically means running out of memory to store a number. But in Rakudo's case I think there's an attempt to keep things sane by switching from a truly vast Int to a Num (a floating point number) before completely running out of RAM. But then computing A(4,3) eventually overflows even a double float.

So while the caching blows up sooner, the code is bound to blow up later anyway, and then you'd get a numeric overflow that would either manifest as an out of memory error or a numeric overflow error as it is in this case.

like image 191
raiph Avatar answered Nov 12 '22 08:11

raiph


Array subscripts use native ints; that's why you get the error in line 3, when you use the big ints as array subscripts. You might have to define a new BigArray that uses Ints as array subscripts.

The second problem arises in the ** operator: the result is a Real, and when the low-level operations returns a Num, it throws an exception. https://github.com/rakudo/rakudo/blob/master/src/core/Int.pm6#L391-L401

So creating a BigArray might not be helpful anyway. You'll have to create your own ** too, that always works with Int, but you seem to have hit the (not so infinite) limit of the infinite precision Ints.

like image 38
jjmerelo Avatar answered Nov 12 '22 09:11

jjmerelo