大于1022x1022的阵列的分段错误 [英] Segmentation fault for array larger than 1022x1022
问题描述
我不了解Rust,但是我想研究一下科学计算的性能,以将其与Julia和Fortran进行比较。我设法编写了以下程序,但是问题是,当 MAX
大于1022时,我会遇到运行时分段错误。
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);
}
因此,终端中只有 Segmentaion fault(段错误)没有错误。我使用的是Ubuntu 16,并在www.rustup.rs上通过命令安装了该软件。它是稳定版 rustc 1.12.1(d4f39402a 2016-10-19)
。
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)
.
推荐答案
您有 Stack Overflow (多么讽刺,嘿?)。
You have a Stack Overflow (how ironic, hey?).
有两个解决方案:
- 不要在堆栈上分配大数组,使用堆代替(
Vec
) - 仅在大堆栈上使用。
不用说,使用 Vec
容易得多;您可以使用 Vec [f64; MAX]
。
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.
这篇关于大于1022x1022的阵列的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!