如何将文本文件转换为 MPI_Bcast 可以发送的格式?
How can I convert a text file into a form that MPI_Bcast can send?
我是 MPI 和 C 的初学者,我试图提高自己的能力,但我遇到了问题,但还无法解决。
我有一个文本文件,里面有我的名字。我需要做的是 MPI 中的进程 0 应该读取这个文本文件并将它发送给其他进程。我遵循的方式如下:
if (myid == 0) {
// Read myname.txt file and convert it to a string
char name[100];
ifstream input("myname.txt");
for (int i = 1; i < nprocs; i++){
input >> name;
cout << name;
}
input.close();
// Send name and alphabet strings to all processors by means of broadcasting
MPI_Bcast(&name, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
}
当我这样做时,在其他进程上使用这个"name"缓冲区的操作无法完成并给出这样的错误:
'name' was not declared in this scope.
我想我在生成这个“&name”地址时出错了。你能帮我解决这个问题吗?提前致谢。
你需要提供一个地方来存储你的数组在所有行列,并且name
已经是一个指针:
char name[100];
if (myid == 0) {
// Read myname.txt file and convert it to a string
ifstream input("myname.txt");
for (int i = 1; i < nprocs; i++){
input >> name;
cout << name;
}
input.close();
}
// Send name and alphabet strings to all processors by means of broadcasting
MPI_Bcast(name, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
对MPI_Bcast
的调用在所有行列都是一样的,不需要放在if
里面。在等级 0 上唯一要做的就是读取文件。
我是 MPI 和 C 的初学者,我试图提高自己的能力,但我遇到了问题,但还无法解决。
我有一个文本文件,里面有我的名字。我需要做的是 MPI 中的进程 0 应该读取这个文本文件并将它发送给其他进程。我遵循的方式如下:
if (myid == 0) {
// Read myname.txt file and convert it to a string
char name[100];
ifstream input("myname.txt");
for (int i = 1; i < nprocs; i++){
input >> name;
cout << name;
}
input.close();
// Send name and alphabet strings to all processors by means of broadcasting
MPI_Bcast(&name, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
}
当我这样做时,在其他进程上使用这个"name"缓冲区的操作无法完成并给出这样的错误:
'name' was not declared in this scope.
我想我在生成这个“&name”地址时出错了。你能帮我解决这个问题吗?提前致谢。
你需要提供一个地方来存储你的数组在所有行列,并且name
已经是一个指针:
char name[100];
if (myid == 0) {
// Read myname.txt file and convert it to a string
ifstream input("myname.txt");
for (int i = 1; i < nprocs; i++){
input >> name;
cout << name;
}
input.close();
}
// Send name and alphabet strings to all processors by means of broadcasting
MPI_Bcast(name, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
对MPI_Bcast
的调用在所有行列都是一样的,不需要放在if
里面。在等级 0 上唯一要做的就是读取文件。