[英]Abnormal Behaviour string MPI_Send() and MPI_Recv()
My program is compiling and showing abnormal behaviour. 我的程序正在编译并显示异常行为。
Used following commands for compiling 使用以下命令进行编译
mpicc one.c -o one mpicc one.c -o一
mpiexec -n 2 ./one mpiexec -n 2 ./一个
I tried to debug it but show no compilation error. 我尝试调试它,但未显示任何编译错误。 I can't understand why my program is behaving abnormally 我不明白为什么我的程序表现异常
#include<mpi.h>
#include<stdio.h>
#include<string.h>
int main(int argc,char * argv[])
{
char str[434];
int n;
int rank,size; MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank == 0)
{//Sender process
scanf("%s",&str);
MPI_Ssend(&n,1,MPI_INT,1,0,MPI_COMM_WORLD);
MPI_Ssend(&str,n,MPI_CHAR,1,0,MPI_COMM_WORLD);
printf("Sending word %s in process 0\n",str);
}
else
{//Receiver process
MPI_Recv(&n,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
MPI_Recv(&str,n,MPI_CHAR,0,0,MPI_COMM_WORLD,&status);
printf("Receiving word %s in process 1\n",str);
}
MPI_Finalize();
return 0;
}
Input: 输入:
haha 哈哈
Actual result 实际结果
Sending word haha in process 0 正在处理中发送单词haha
Receiving word haha in process 1 在过程1中接收单词haha
Exchange result 交换结果
Sending word haha in process 0 正在处理中发送单词haha
Receiving word in process 1 在过程1中接收单词
As people have kindly pointed out in the comments to your post, you can't use str
the way you were, because scanf()
expects a char *
, while you are passing it char (*)[434]
, which are not the same. 正如人们在对您的帖子的评论中所指出的那样,您不能像以前那样使用str
,因为scanf()
期望使用char *
,而您将其传递给char (*)[434]
却不是相同。
You would see this error (or warning), if you compiled with the "-Werror -Wformat"
flags. 如果使用"-Werror -Wformat"
标志进行编译,则会看到此错误(或警告)。
After modifying your code to use an pointer and not a char array, it looks as below: 修改代码以使用指针而不是char数组后,其外观如下:
#include<mpi/mpi.h>
#include<stdio.h>
#include<string.h>
#include <malloc.h>
int main(int argc, char * argv[])
{
const int size = 434;
char* str_send = (char*) malloc(size * sizeof(char)); // = "haha";
char* str_recv = (char*) malloc(size * sizeof(char));
int n = size;
int rank;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
if(rank == 0)
{//Sender process
scanf("%s", str_send);
MPI_Ssend(&n,1,MPI_INT,1,0,MPI_COMM_WORLD);
MPI_Ssend(str_send,n,MPI_CHAR,1,0,MPI_COMM_WORLD);
printf("Sending word %s in process 0\n",str_send);
}
else
{//Receiver process
MPI_Recv(&n,1,MPI_INT,0,0,MPI_COMM_WORLD,&status);
MPI_Recv(str_recv,n,MPI_CHAR,0,0,MPI_COMM_WORLD,&status);
printf("Receiving word %s in process 1\n",str_recv);
}
MPI_Finalize();
return 0;
}
Which gives the output: 给出输出:
junglefox@ubuntu:~/test_programs$ mpicc main.c junglefox @ ubuntu:〜/ test_programs $ mpicc main.c
junglefox@ubuntu:~/test_programs$ mpiexec -n 2 ./a.out junglefox @ ubuntu:〜/ test_programs $ mpiexec -n 2 ./a.out
haha 哈哈
Sending word haha in process 0 正在处理中发送单词haha
Receiving word haha in process 1 在过程1中接收单词haha
Since, I feel you want to test the MPI library and not get bogged down into other intricacies, I would suggest using a fixed input and looking for the fixed output, instead of using scanf
. 因为,我觉得您想测试MPI库,而不是陷入其他麻烦,所以我建议使用固定输入并寻找固定输出,而不是使用scanf
。 As an example, 举个例子,
const char* str_send = "haha";
And then remove or comment out the scanf line. 然后删除或注释掉scanf行。
// scanf("%s", str_send);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.