在 MPI 程序中转换参数时出错
Error when converting argument in MPI program
我现在正在做我的 MPI 练习。在我的工作中,我想计算 N 的阶乘,所以我在命令行中将 N 传递给第二个参数,除了我尝试从 char* 转换为 int 的行(这适用于一个进程)之外,我的所有代码都运行良好), 请在这种情况下给我一些建议,这是我的代码:
int main(int argc, char* argv[]){
if(argc != 2){
fprintf(stderr, "\nYou must follow this format to run the program: [Program's name] [N]");
exit(0);
MPI_Finalize();
}
long int n = 1;
n = strtol(argv[1], NULL, 10); // <-- problem happens here
// Initialize MPI
MPI_Init(&argc, &argv);
// Receive the whole size and id of each rank
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0){
// Process for master
master(n);
}
else{
// Process for slaves
slave();
}
// Finish MPI
MPI_Finalize();
return 1;
}
感谢大家阅读我的post,欢迎大家的回答。
在调用 MPI_Init
之前,您不应该对 argv
和 argc
做任何事情。事实上,您应该尽快致电 MPI_Init
。肯定不会在 MPI_Init
.
之前调用 MPI_Finalize
引用标准:
Certain implementations use the (inout) argc, argv arguments of the C
version of MPI_INIT in order to propagate values for argc and argv to
all executing processes.
只需将您的 MPI_Init
呼叫移动到 main
的开头即可。
我现在正在做我的 MPI 练习。在我的工作中,我想计算 N 的阶乘,所以我在命令行中将 N 传递给第二个参数,除了我尝试从 char* 转换为 int 的行(这适用于一个进程)之外,我的所有代码都运行良好), 请在这种情况下给我一些建议,这是我的代码:
int main(int argc, char* argv[]){
if(argc != 2){
fprintf(stderr, "\nYou must follow this format to run the program: [Program's name] [N]");
exit(0);
MPI_Finalize();
}
long int n = 1;
n = strtol(argv[1], NULL, 10); // <-- problem happens here
// Initialize MPI
MPI_Init(&argc, &argv);
// Receive the whole size and id of each rank
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0){
// Process for master
master(n);
}
else{
// Process for slaves
slave();
}
// Finish MPI
MPI_Finalize();
return 1;
}
感谢大家阅读我的post,欢迎大家的回答。
在调用 MPI_Init
之前,您不应该对 argv
和 argc
做任何事情。事实上,您应该尽快致电 MPI_Init
。肯定不会在 MPI_Init
.
MPI_Finalize
引用标准:
Certain implementations use the (inout) argc, argv arguments of the C version of MPI_INIT in order to propagate values for argc and argv to all executing processes.
只需将您的 MPI_Init
呼叫移动到 main
的开头即可。