How do I get more stack space for g++

25 Views Asked by At

I'm trying to allocate 3 fairly large matrices using g++ on a MacBook pro 2019 model running Monterrey 12.7.1 with 8GB "main" memory.

The purpose of this code to compare C++ to Octave.

Here's the code, stripped to the minimal code that still causes a segmentation fault:

int main(void){
   double m[500][1000];
   double n[1000][500];
   double p[500][500];
return 0;
}

The Octave program below actually runs without a hitch:

matrix multiplication 1000 times

m = rand(500, 1000);
n = rand(1000, 500);

i = 1;
while i <= 1000,
    p = m*n;
    i++;
end

Interestingly enough, with smaller matrices, Octave uses approximately as much user-mode time as C++, not "much slower" as people seem to claim at all. At this size, C++ won't even run as is!

With the Octave code as shown, user time is 29 to 30 seconds, but the wall clock time is only 6 - 12 seconds! Is that because it's a quad-core machine?

Back to discussing the C++ code: should I try making the matrices global? Should I try allocating on the heap? I'd like to understand what's going on as well. I'm teaching a class on programming for engineering students, and it'd be nice to explain these issues to the students.

Thanks.

0

There are 0 best solutions below