0

I don't know Rust but I wanted to investigate the performance in scientific computing to compare it to Julia and Fortran. I managed to write the following program but the problem is that I get a runtime segmentation fault when MAX is larger than 1022. Any advice?

fn main() {
    const MAX: usize = 1023;
    let mut arr2: [[f64;  MAX];  MAX] = [[0.0;  MAX];  MAX]; 

    let pi: f64 = 3.1415926535;
    // compute something useless and put in matrix
    for ii in 0.. MAX {
        for jj in 0.. MAX {
            let i = ii as f64;
            let j = jj as f64;
            arr2[ii][jj] = ((i + j) * pi * 41.0).sqrt().sin();
        }
    }

let mut sum0:f64 = 0.0;

//collapse to scalar like sum(sum(array,1),2) in other langs
for iii in 0..MAX {
    let vec1:&[f64] = &arr2[iii][..];
    sum0 += vec1.iter().sum();
}
println!("this {}", sum0);
}

So no error just 'Segmentaion fault' in the terminal. I'm using Ubuntu 16 and installed with the command on www.rustup.rs. It is stable version rustc 1.12.1 (d4f39402a 2016-10-19).

Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27

1 Answers1

7

You have a Stack Overflow (how ironic, hey?).

There are two solutions to the issue:

  1. Do not allocate a large array on the stack, use the heap instead (Vec)
  2. Only do so on a large stack.

Needless to say, using a Vec is just much easier; and you can use a Vec[f64; MAX] if you wish.

If you insist on using the stack, then I will redirect you to this question.

Community
  • 1
  • 1
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • Thank you, I'll try the Vec. I really don't care if it's the stack or the heap and I wasn't aware I had made that choice by using an array. I assumed arrays are faster because they are type restricted and static. – Jonatan Öström Oct 26 '16 at 11:56
  • @JonatanÖström: in theory arrays may be faster because eliding bounds checks is easier for the optimizer, this means that using a `Box<[[f64; MAX]; MAX]>` to place the array on the heap could have some advantage... but there is no stable way in Rust today to place the array on the heap without first constructing it on the stack (there is a reflexion on the best way to accomplish this in a manner that would not be hardcoded). Using `Vec` might prevent some bounds-check elisions for index-based access, iterators optimize better. – Matthieu M. Oct 26 '16 at 12:14
  • Now that I read a bit more about the Vec structure, it seems as it is only one-dimensional, is that right? This example could be altered in that sense, but the ability to use a large multidimensional arrays is kind of important for the intended applications so maybe I'll check on Rust in the future to see it if these kinds of features have improved. – Jonatan Öström Oct 26 '16 at 12:27
  • 1
    @JonatanÖström: `Vec` is one dimensional, but nothing prevents you from storing arrays in `Vec` as in `Vec<[f64; MAX]>` :) Rust guarantees that you will get all the `Vec` elements assigned contiguously, so it'll just be one big blob of memory (`mem::size_of::() * MAX * MAX` bytes) but you'll be able to access it with `vec[i][j]` as if you had nested arrays. – Matthieu M. Oct 26 '16 at 12:29
  • OK, now that's a different story! So to create the largest possible square matrix consisting of a single Vec that consists of 1D-arrays I could have 1023^2 1D-arrys, each having 1023^2 entries. Which should be fine. I'll try that approach. Thank you again! – Jonatan Öström Oct 26 '16 at 13:34