通过system()C/C+;将mpi秩传递给bash脚本;奇异错误
我有一个带有MPI的C代码,根据MPI级别,它应该在不同的目录中执行bash脚本。例如:如果我使用mpirun-np 10 mycode.o运行此命令,那么每个进程都应该执行调用系统,其中s是从snprintf创建的字符串(s,sizeof),“/home/myaccount/myscript.sh%d”,rank) 完整代码如下:通过system()C/C+;将mpi秩传递给bash脚本;奇异错误,c,linux,bash,mpi,system-calls,C,Linux,Bash,Mpi,System Calls,我有一个带有MPI的C代码,根据MPI级别,它应该在不同的目录中执行bash脚本。例如:如果我使用mpirun-np 10 mycode.o运行此命令,那么每个进程都应该执行调用系统,其中s是从snprintf创建的字符串(s,sizeof),“/home/myaccount/myscript.sh%d”,rank) 完整代码如下: #include <stdio.h> #include <mpi.h> /* MPI head
#include <stdio.h>
#include <mpi.h> /* MPI header file */
#include <stdlib.h>
void main(int argc, char *argv[])
{
int rank, size;
/* init into MPI */
MPI_Init(&argc, &argv);
/* my rank - my id */
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
/* how many processes in the virtual machine */
MPI_Comm_size(MPI_COMM_WORLD, &size);
/*create script input (convert rank to string)*/
char s[256];
snprintf(s, sizeof(s), "/home/myaccount/myscript.sh %d", rank);
/* run script */
system(s);
/* out of the virtual machine */
MPI_Finalize();
}
#包括
#包含/*MPI头文件*/
#包括
void main(int argc,char*argv[])
{
int等级、大小;
/*将init转换为MPI*/
MPI_Init(&argc,&argv);
/*我的等级-我的身份证*/
MPI通信等级(MPI通信世界和等级);
/*虚拟机中有多少个进程*/
MPI_通信大小(MPI_通信世界和大小);
/*创建脚本输入(将秩转换为字符串)*/
chars[256];
snprintf,sizeof,“/home/myaccount/myscript.sh%d”,排名;
/*运行脚本*/
系统;
/*退出虚拟机*/
MPI_Finalize();
}
测试脚本是:
#!/bin/bash
for((k=16;k<22;k+=2));
do echo cd /otherdirectory/test/"$k"/"$1"/;
done;
exit;
#/bin/bash
对于((k=16;k您在/test/$k从16运行到20之后输出$k
)
我假设您的主目录中有一个.bashrc,或者可能有一个/etc/bashrc,它垃圾$1,因此当您的脚本最终到达时,参数被其他内容覆盖
或者,您的系统shell可能是csh或tcsh或类似的,并且您在登录后切换到bash。当您从命令行启动脚本时,bash会直接执行它。但是system()调用将从/etc/passwd获取shell并运行-c
,因此csh在启动时执行的.login或.cshrc或类似文件也可能是罪魁祸首。如果让脚本只执行echo“$@”,您会看到什么
?检查您的.bashrc
是否有类似设置为\u csh=…
的内容,是的/etc/passwd已设置为csh。谢谢