通过 MPI 世界大小,表达式在数组中必须有一个常量值错误
Expression must have a constant value error in array via MPI world size
最近,我开始学习 MPI 编程,并尝试在 Linux 和 Windows OS 上进行编程。 运行 Linux 上的 MPI 应用程序没有任何问题,但是,我在 Visual Studio
上偶然发现了 expression must have a constant value error
例如,我试图通过 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
获取 world_size
并基于 world_size
创建一个数组(例如)
代码示例:
#include <mpi.h>
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int database[world_size]; //error occured here
但是,当我在 Linux 上 运行 它时,它工作得非常好,因为我能够在声明我希望拥有的进程数的同时执行代码。我错过了什么吗?我按照这个特别的 youtube link 教我如何在我的 Visual Studio 2015 上安装 MS-MPI。
如有任何帮助,我们将不胜感激。
使用非 const 值自动调整数组大小实际上适用于 gcc (https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). However, it's considered a bad idea because (as you've just experienced) your code won't be portable anymore. You just need to change your code to create an array using new
. You might want to generate an error to make sure your code is portable:
最近,我开始学习 MPI 编程,并尝试在 Linux 和 Windows OS 上进行编程。 运行 Linux 上的 MPI 应用程序没有任何问题,但是,我在 Visual Studio
上偶然发现了expression must have a constant value error
例如,我试图通过 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
获取 world_size
并基于 world_size
创建一个数组(例如)
代码示例:
#include <mpi.h>
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int database[world_size]; //error occured here
但是,当我在 Linux 上 运行 它时,它工作得非常好,因为我能够在声明我希望拥有的进程数的同时执行代码。我错过了什么吗?我按照这个特别的 youtube link 教我如何在我的 Visual Studio 2015 上安装 MS-MPI。
如有任何帮助,我们将不胜感激。
使用非 const 值自动调整数组大小实际上适用于 gcc (https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html). However, it's considered a bad idea because (as you've just experienced) your code won't be portable anymore. You just need to change your code to create an array using new
. You might want to generate an error to make sure your code is portable: