提问人:Giorgio Aveni 提问时间:11/7/2023 最后编辑:Giorgio Aveni 更新时间:11/7/2023 访问量:54
MPI_BCast不适用于MPI_Comm_Split
MPI_BCast doesn't work with MPI_Comm_Split
问:
我是 MPI 编程的菜鸟,所以我尝试实现一个小程序,将所有进程拆分为一个带有新通信器的组。
#include <mpi.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm central_server_comm, intermediary_server_comm, lolcal_devices_comm;
int new_color;
if (rank == 0) {
// The process with rank 0 is the central server
new_color = 0;
} else if (rank == 1) {
// The process with the rank 1 is the intermediary server
new_color = 1;
} else {
// The other processes are local devices
new_color = 2;
}
MPI_Comm_split(MPI_COMM_WORLD, new_color, rank, ¢ral_server_comm);
float message[4]; // Initializes an array of floating-point numbers
if (new_color == 0) {
// This is the central server
printf("I am the central server. Rank %d of %d.\n", rank, size);
// Initialize the array with the desired numbers
message[0] = 1.0;
message[1] = 2.0;
message[2] = 3.0;
message[3] = 4.0;
MPI_Send(message, 4, MPI_FLOAT, 1, 0, MPI_COMM_WORLD); // Broadcast to everyone
} else if (new_color == 1) {
// This is an intermediary server
printf("I am the intermediary server. Rank %d of %d.\n", rank, size);
MPI_Bcast(message, 4, MPI_FLOAT, 0, central_server_comm); // Ricevi il messaggio da server centrale
printf("Message received from central server: %f %f %f %f\n", message[0], message[1], message[2], message[3]);
} else {
//This is a local device
printf("I am a local device. Rank %d of %d.\n", rank, size);
MPI_Bcast(message, 4, MPI_FLOAT, 0, central_server_comm); // Ricevi il messaggio da server intermedio
printf("Message received from intermediary server: %f %f %f %f\n", message[0], message[1], message[2], message[3]);
}
MPI_Comm_free(¢ral_server_comm);
MPI_Finalize();
return 0;
}
我希望秩 = 0 的进程将数据发送到秩 = 1 的进程,最后一个进程将数据广播到所有其他进程,但是当我尝试发送整数数据时,一切正常,但是当我尝试发送其他类型的数据(如 float 或 char)时,它会发送 null valure(float 为 0.0,char 为 “ ”。我把代码放在这里。我希望有人能帮助我。我认为我在功能mpi_comm_split上弄错了什么,但我不知道是什么。
答: 暂无答案
评论
MPI_Bcast()
发生在通讯器内部。例如,如果您考虑 中的排名,则它是单独的通信器,并且是未初始化的。1
MPI_COMM_WORLD
message
MPI_Bcast()