在MPI中运行我的代码时,根进程似乎未执行

时间:2019-04-03 09:43:08

标签: c parallel-processing mpi

在尝试使用MPI库执行用C编写的代码时,遇到了非常奇怪的事情。 尝试时,我的代码尚未产生语法错误

 mpirun -n 3 ./q4

我明白了

hello
hello
hello
from the other side.
from the other side.
from the other side.

似乎永远不会进入等级0进程。我不知道为什么会这样。该代码在结构上与我编写过的其他一些示例相同(如果需要,我可以提供完整的代码) 但是,如果我在第六行之后输入任意两个随机内容,我会得到

1213
123
Enter a length for the string that is divisible by the number of processes Number of vowels 27 

我真的不知道该怎么做,只是我检查了我的代码中是否存在逻辑错误,这些错误没有,即使存在,也要晚得多,这意味着至少第一个代码是如果应该执行情况。

int main(int argc, char * argv[])
{
 printf("hello\n");
 int rank,m,size,num;
 MPI_Init(&argc,&argv);
 MPI_Comm_rank(MPI_COMM_WORLD,&rank);
 MPI_Comm_size(MPI_COMM_WORLD,&size);
 printf("from the other side.\n");
 char str[100];
 if (rank == 0 )
 {
  printf("Enter a length for the string that is divisible by the number of processes ");
  scanf("%d",&m);
  scanf("%s",str);
 }
.
.

如果相关,我正在运行Ubuntu 18.04。

2 个答案:

答案 0 :(得分:2)

您需要在最后一个let data = () => { let key = {}; let setKey = () => { key.val = Math.floor( Math.random() * 6); } let getKey = () => { return key; } let changeKey = (newKey) => { key = newKey; } return { key, setKey, getKey, changeKey } } let dataArray = [] for( let i = 0; i < 3; i++){ dataArray.push(data()) } console.log('data.key'); dataArray.forEach( data => {console.log(' ', data.key)}) // **{}** console.log('data.getKey() before set'); dataArray.forEach( data => {console.log(' ', data.getKey())}) // **{}** dataArray.forEach( data => {data.setKey()}) console.log('data.getKey() after set'); dataArray.forEach( data => {console.log(' ', data.getKey())}) // **rundom numbers** console.log('data.key after set'); dataArray.forEach( data => {console.log(' ', data.key)}) // **rundom numbers** console.log('changing key with changeKey("boo")'); dataArray.forEach( data => {data.changeKey('boo')}) console.log('data.key after changeKey'); dataArray.forEach( data => {console.log(' ', data.key)}) // **not a real reference: you still get the value of object you returned, not the new value** console.log('data.getKey() after changeKey'); dataArray.forEach( data => {console.log(' ', data.getKey())}) // **getKey returns the local variable so the changes are reflected here**之后添加一些fflush(stdout);,以强制刷新文本。

printf文本中没有\n时,不会立即显示该文本。

所以您应该写:

printf

或更简单:

printf("Enter a length for the string that is divisible by the number of processes ");
fflush(stdout);
scanf("%d",&m);
....

puts("Enter a length for the string that is divisible by the number of processes "); scanf("%d",&m); .... 将在一行上打印一条消息(它将创建新行)。而且它没有缓冲。

答案 1 :(得分:0)

万一将来有人读过这篇文章,这是对Matthieu所说的补充。 代码应该是这样

 if (rank == 0 )
 {
  printf("Enter a length for the string that is divisible by the number of processes \n");
    fflush(stdout);
.
.
.