Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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
C++ 如何在mpi中传输数据?_C++_Set_Mpi - Fatal编程技术网

C++ 如何在mpi中传输数据?

C++ 如何在mpi中传输数据?,c++,set,mpi,C++,Set,Mpi,正如我所知,在mpi中,我们可以在计算机之间传输一些数组。我们可以传输其他东西而不是数组吗? 例如,是否可以直接传输“set”数据?是的,您可以使用MPI传输任意复杂类型的数据。所有通信操作都采用数据类型参数。这可能是与C/C++中的内置类型相对应的基本预定义数据类型(例如,MPI_INT/INT或MPI_DOUBLE/DOUBLE)或更复杂的用户定义类型 我最近刚刚完成了一个完整的工作(普通C)示例,其中在MPI\u Type\u create\u struct的帮助下,传输一个整数值,后跟一

正如我所知,在mpi中,我们可以在计算机之间传输一些数组。我们可以传输其他东西而不是数组吗?
例如,是否可以直接传输“set”数据?

是的,您可以使用MPI传输任意复杂类型的数据。所有通信操作都采用数据类型参数。这可能是与C/C++中的内置类型相对应的基本预定义数据类型(例如,
MPI_INT
/
INT
MPI_DOUBLE
/
DOUBLE
)或更复杂的用户定义类型

我最近刚刚完成了一个完整的工作(普通C)示例,其中在
MPI\u Type\u create\u struct
的帮助下,传输一个
整数
值,后跟一个
double
s数组,该值可用于定义这样一个用户定义的数据类型:

#include <mpi.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
    MPI_Init(&argc, &argv);

    int i, rank, tag = 1;
    MPI_Status status;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // Array of doubles plus element count
    typedef struct {
        int row;
        double elements[4];
    } My_array;

    // Derived datatype for an array of doubles plus element count
    MPI_Datatype MY_ARRAY_TYPE;
    const int nr_blocks = 2;
    int blocklengths[2] = {1, 4};
    MPI_Datatype oldtypes[2] = {MPI_INT, MPI_DOUBLE};
    MPI_Aint displacements[2];
    displacements[0] = offsetof(My_array, row);
    displacements[1] = offsetof(My_array, elements);
    MPI_Type_create_struct(nr_blocks, blocklengths, displacements,
               oldtypes, &MY_ARRAY_TYPE);
    MPI_Type_commit(&MY_ARRAY_TYPE);

    if(rank == 0) {
        My_array array1  = {3, 3.1, 3.2, 3.3, 3.4};
        MPI_Send(&array1, 1, MY_ARRAY_TYPE, 1, tag, MPI_COMM_WORLD);
    }
    if(rank == 1) {
        My_array array2;
        MPI_Recv(&array2, 1, MY_ARRAY_TYPE, 0, tag, MPI_COMM_WORLD, &status);
        printf("Rank %d received elements of row %d:\n", rank, array2.row);
        for(i = 0; i < 4; i++)
            printf("\t%.1f\n", array2.elements[i]);
    }
    MPI_Type_free(&MY_ARRAY_TYPE);
    MPI_Finalize();
}
#包括
#包括
int main(int argc,char*argv[]){
MPI_Init(&argc,&argv);
int i,秩,tag=1;
MPI_状态;
MPI通信等级(MPI通信世界和等级);
//双精度数组加元素计数
类型定义结构{
int行;
双元素[4];
}我的_数组;
//double加元素计数数组的派生数据类型
MPI_数据类型MY_数组_类型;
const int nr_blocks=2;
int blocklength[2]={1,4};
MPI_数据类型oldtypes[2]={MPI_INT,MPI_DOUBLE};
MPI_Aint位移[2];
位移[0]=偏移量(My_数组,行);
位移[1]=偏移(My_数组,元素);
MPI类型创建结构(块数、块长度、位移、,
旧类型和我的数组类型);
MPI\u类型\u提交(&MY\u数组\u类型);
如果(秩==0){
My_数组1={3,3.1,3.2,3.3,3.4};
MPI_发送(&array1,1,我的数组类型,1,标记,MPI_通信世界);
}
如果(秩==1){
我的_数组2;
MPI_Recv(&array2,1,MY_ARRAY_TYPE,0,标记,MPI_COMM_WORLD,&status);
printf(“秩%d接收到第%d行的元素:\n”,秩,array2.row);
对于(i=0;i<4;i++)
printf(“\t%.1f\n”,array2.elements[i]);
}
MPI类型\u自由(&MY\u数组\u类型);
MPI_Finalize();
}
还有其他更容易使用的、不太通用的派生数据类型构造函数;您可以在中看到两个示例