Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MPI_是否全部正确使用?_Mpi_Mpich - Fatal编程技术网

MPI_是否全部正确使用?

MPI_是否全部正确使用?,mpi,mpich,Mpi,Mpich,我打算使用MPI集体通信实现一个简单的任务,但由于对MPI不熟悉,我发现集体例程有些不直观。我有4个从机,每个从机必须读取一个唯一的字符串并将该字符串发送给所有其他从机 我查看了MPI\u广播、MPI\u分散和MPI\u全部。我接受了所有的MPI_,但程序以错误的终止结束 该计划是: int main(int argc,char *argv[]) { int my_rank, num_workers; MPI_Comm SLAVES_WORLD; MPI_Init(&a

我打算使用MPI集体通信实现一个简单的任务,但由于对MPI不熟悉,我发现集体例程有些不直观。我有4个从机,每个从机必须读取一个唯一的字符串并将该字符串发送给所有其他从机

我查看了MPI\u广播、MPI\u分散和MPI\u全部。我接受了所有的MPI_,但程序以错误的终止结束

该计划是:

int main(int argc,char *argv[])
{
    int my_rank, num_workers;
    MPI_Comm SLAVES_WORLD;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &num_workers);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    createSlavesCommunicator(&SLAVES_WORLD);

    char send_msg[20], recv_buf[20];
    sprintf(send_msg, "test string %d", my_rank); 

    MPI_Alltoall(send_buf, strlen(send_buf), MPI_CHAR, recv_buf, 20, MPI_CHAR, MPI_COMM_WORLD);
    printf("slave %d recvd message %s\n", my_rank, recv_buf);
}

MPI_AlltoAll()将消息从每个人发送到每个人。如果每个进程发送20个字符,则输入和输出缓冲区应该远远大于20个字符。从您的代码开始,以下是MPI_AlltoAll()的工作原理:

#包括
#包括
#包括
#包括
#包括“mpi.h”
int main(int argc,char*argv[])
{
在我的职级中,工人数量;
MPI_通信世界;
MPI_Init(&argc,&argv);
MPI通信大小(MPI通信世界和工作者数量);
MPI通信等级(MPI通信世界和我的通信等级);
//createSlavesCommunicator(和SLAVES_WORLD);
char send_msg[20*num_workers],recv_buf[20*num_workers];
int i;

对于(i=0;i)程序具体崩溃在哪里?是全对全还是通信器创建的问题?
 void createSlavesCommunicator(MPI_Comm *SLAVES_WORLD)
 {
    MPI_Group SLAVES_GROUP, MPI_COMM_GROUP;

    int ranks_to_excl[1];
    ranks_to_excl[0] = 0;

    MPI_Comm_group(MPI_COMM_WORLD, &MPI_COMM_GROUP);
    MPI_Group_excl(MPI_COMM_GROUP, 1, ranks_to_excl, &SLAVES_GROUP);

    MPI_Comm_create(MPI_COMM_WORLD, SLAVES_GROUP, SLAVES_WORLD);
 }
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "mpi.h"

int main(int argc,char *argv[])
{
    int my_rank, num_workers;
    MPI_Comm SLAVES_WORLD;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &num_workers);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    //createSlavesCommunicator(&SLAVES_WORLD);

    char send_msg[20*num_workers], recv_buf[20*num_workers];
    int i;
    for(i=0;i<num_workers;i++){
        sprintf(&send_msg[i*20], "test from %d to %d", my_rank,i);
    } 

    MPI_Barrier(MPI_COMM_WORLD);
    //MPI_Alltoall(send_msg, strlen(send_msg), MPI_CHAR, recv_buf, 20, MPI_CHAR, MPI_COMM_WORLD);
    MPI_Alltoall(send_msg, 20, MPI_CHAR, recv_buf, 20, MPI_CHAR, MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);


    for(i=0;i<num_workers;i++){
        printf("slave %d recvd message %s\n", my_rank, &recv_buf[20*i]);
    }

    MPI_Finalize();
    return 0;
}