comp.lang.ada
 help / color / mirror / Atom feed
* Large arrays passed to arithmetic operators overflows GNAT stack
@ 2010-12-04  6:32 Jerry
  2010-12-04  9:19 ` Vinzent Hoefler
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jerry @ 2010-12-04  6:32 UTC (permalink / raw)


For what it's worth, passing large numerical arrays to the arithmetic
operators in GNAT can cause the stack to overflow (segfault) even when
the arrays are allocated from the heap. I suppose that indicates that
they are being copied rather than being passed by reference.

For example, on OS X which has a default stack size of 8192 KB, the
following program segfaults when N is greater than about 1_048_138
(about 8 MB per Long_Float array) but runs OK when it is somewhat less
than that number.

with
    Ada.Numerics.Long_Real_Arrays;
use
    Ada.Numerics.Long_Real_Arrays;
procedure array_test is
    type Real_Vector_Access    is access Real_Vector;
    N : Integer := 1_048_130;
    t_Ptr : Real_Vector_Access := new Real_Vector(0 .. N);
      t : Real_Vector renames t_Ptr.all;
    t_Diff_Ptr : Real_Vector_Access := new Real_Vector(0 .. N - 1);
      t_Diff : Real_Vector renames t_Diff_Ptr.all;
begin
    for i in t'range loop
        t(i) := 1.0;
    end loop;
    t_Diff := t(1 .. N) - t(0 .. N - 1);
end array_test;

The quick fix (in my case) is to increase the stack size from the
shell:

ulimit -s 65532

or whatever is appropriate for your machine--65532 is the hard limit
set by OS X.

Another way to expand the stack without invoking shell commands, as
noted by Björn Persson on this thread

http://groups.google.com/group/comp.lang.ada/browse_thread/thread/ae395e5c11de7bc9/bda8d61bd3a66ee9?hl=en&q=Jerry+stack&lnk=nl&

is to call getrlimit and/or setrlimit from within the program, linking
to these POSIX C routines.

Another fix would be to write operator procedures that specifically
take pointers as arguments; that would possibly be the only fix for
arrays that still overflow the stack when it is maxed out.

As usual, taking the dumb-guy approach, this seems like an unnecessary
nuisance.

Jerry



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-12-05  1:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-04  6:32 Large arrays passed to arithmetic operators overflows GNAT stack Jerry
2010-12-04  9:19 ` Vinzent Hoefler
2010-12-04 13:27 ` Jeffrey Creem
2010-12-04 14:17 ` Peter C. Chapin
2010-12-05  1:22   ` Jerry

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox