正确使用MPI_发送和MPI_接收 我正在使用C++中的一个简单程序,使用MPI在两个进程之间进行通信。如果要将数组发送到另一个进程,则MPI_send和MPI_Recv函数需要指向所述数组的指针: int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status);

正确使用MPI_发送和MPI_接收 我正在使用C++中的一个简单程序,使用MPI在两个进程之间进行通信。如果要将数组发送到另一个进程,则MPI_send和MPI_Recv函数需要指向所述数组的指针: int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status);,c++,mpi,C++,Mpi,在一个示例中,我看到了MPI_Send和MPI_Recv的以下用法: int values[5] = {1, 2, 3, 4, 5}; MPI_Send(values, 5, MPI_INT, 1, 0, MPI_COMM_WORLD); 及 为什么本教程在一种情况下仅使用值,而在另一种情况下还使用地址运算符&值 我编写了一个程序来在两个进程之间发送和接收数组,它似乎可以在使用和不使用地址操作符的情况下工作。为什么会这样?我的想法肯定是错的。你能帮我找到我的错误吗 这是我的密码: #inclu

在一个示例中,我看到了MPI_Send和MPI_Recv的以下用法:

int values[5] = {1, 2, 3, 4, 5};
MPI_Send(values, 5, MPI_INT, 1, 0, MPI_COMM_WORLD);

为什么本教程在一种情况下仅使用
,而在另一种情况下还使用地址运算符
&值

我编写了一个程序来在两个进程之间发送和接收数组,它似乎可以在使用和不使用地址操作符的情况下工作。为什么会这样?我的想法肯定是错的。你能帮我找到我的错误吗

这是我的密码:

#include <iostream>
#include <mpi.h>

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

    // Reading size and rank
    int size, rank;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // For every process create array 
    double array[2];
    if (rank == 0) {
        array[0] = 0.1;
        array[1] = 0.2;
    } else {
        if (rank == 1) {
            array[0] = 1.1;
            array[1] = 1.2;
        }
    }

    // Send and receive
    double other_array[2];
    if (rank == 0) {
        MPI_Send(&array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD);
        MPI_Recv(&other_array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        // OR
        // MPI_Send(array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD);
        // MPI_Recv(other_array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        std::cout << rank << " " << other_array[0] << " " << other_array[1] << std::endl;
    } else {
        MPI_Recv(&other_array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Send(&array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD);
        // OR
        // MPI_Recv(other_array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        // MPI_Send(array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD);
        std::cout << rank << " " << other_array[0] << " " << other_array[1] << std::endl;
    }

    // Finalisation
    int MPI_Finalize(); 

    return 0;
}
#包括
#包括
int main(int argc,字符**argv){
MPI_Init(&argc,&argv);
//阅读规模和排名
整数大小、等级;
MPI_通信大小(MPI_通信世界和大小);
MPI通信等级(MPI通信世界和等级);
//为每个进程创建一个数组
双数组[2];
如果(秩==0){
数组[0]=0.1;
数组[1]=0.2;
}否则{
如果(秩==1){
数组[0]=1.1;
数组[1]=1.2;
}
}
//收发
双倍其他_数组[2];
如果(秩==0){
MPI_发送(&array,2,MPI_DOUBLE,1,99,MPI_COMM_WORLD);
MPI_Recv(&其他_数组,2,MPI_双精度,1,99,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
//或
//MPI_发送(阵列、2、MPI_双精度、1、99、MPI_通信世界);
//MPI_Recv(其他_数组,2,MPI_双精度,1,99,MPI_通信世界,MPI_状态忽略);

std::cout
&
很可能是一个输入错误,您不需要它。@GillesGouaillardet但是为什么这两个版本都可以工作?为什么编译器没有抛出错误?可能是
#include <iostream>
#include <mpi.h>

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

    // Reading size and rank
    int size, rank;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    // For every process create array 
    double array[2];
    if (rank == 0) {
        array[0] = 0.1;
        array[1] = 0.2;
    } else {
        if (rank == 1) {
            array[0] = 1.1;
            array[1] = 1.2;
        }
    }

    // Send and receive
    double other_array[2];
    if (rank == 0) {
        MPI_Send(&array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD);
        MPI_Recv(&other_array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        // OR
        // MPI_Send(array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD);
        // MPI_Recv(other_array, 2, MPI_DOUBLE, 1, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        std::cout << rank << " " << other_array[0] << " " << other_array[1] << std::endl;
    } else {
        MPI_Recv(&other_array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        MPI_Send(&array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD);
        // OR
        // MPI_Recv(other_array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        // MPI_Send(array, 2, MPI_DOUBLE, 0, 99, MPI_COMM_WORLD);
        std::cout << rank << " " << other_array[0] << " " << other_array[1] << std::endl;
    }

    // Finalisation
    int MPI_Finalize(); 

    return 0;
}