简体   繁体   English

使用数组在C中使用MPI-使用MPI_Send()和MPI_Recv()实现MPI_Scatter()

[英]MPI in C using arrays - Implementing MPI_Scatter() using MPI_Send() and MPI_Recv()

I am trying to run a matrix multiplication program using MPI. 我正在尝试使用MPI运行矩阵乘法程序。 The arrays 'a' and 'b' are of type double and size 512*512. 数组“ a”和“ b”的类型为double,大小为512 * 512。 The array 'a' is to be scattered and array 'b' is to be broadcast. 阵列“ a”将被分散,阵列“ b”将被广播。 The final result after matrix multiplication is to be gathered in master process in array c[512][512] I am trying to implement MPI_Scatter() using MPI_Send() and MPI_Recv() functions but I am kind of stuck in an infinite loop (probably). 矩阵乘法之后的最终结果将在数组c [512] [512]的主进程中收集。我试图使用MPI_Send()和MPI_Recv()函数实现MPI_Scatter(),但我有点陷入无限循环(大概)。 P is the number of processes. P是进程数。

double a[512][512], b[512][512], c[512][512];
blksz = 512/P;
if(rank == 0) {
    // Scatter input matrix a, implementation of MPI_Scatter()
    for(j = 1 ; j < P ; j++ ) {
        MPI_Send(&a + j*blksz*N*sizeof(double), blksz*N, MPI_DOUBLE, j, 0, MPI_COMM_WORLD);
    }

    // Broadcast the input matrix b, implementation of MPI_Bcast()
    for(j = 1 ; j < P ; j++ ) {
        MPI_Send(&b, N*N, MPI_DOUBLE, j, 1, MPI_COMM_WORLD);
    }
}
else {
    MPI_Recv(&a, blksz*N, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
    MPI_Recv(&b, N*N, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &status);
}

for(i = 0 ; i < blksz; i++) {
    for(j = 0 ; j < N ; j++) {
        c[i][j] = 0;
        for(k = 0 ; k < N ; k++) {
            c[i][j] += a[i][k] * b[k][j];
        }
    }
}

// Gather result, implementation of MPI_Gather()
if(rank != 0) {
    MPI_Send(&c, blksz*N, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
}
else {
    for(i = 1 ; i < P ; i++) {
        MPI_Recv(&c+i*blksz*N*sizeof(double), blksz*N, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD, &status);
    }
}

I am a kind of a beginner to programming but I was up all night trying to figure it out but to no avail. 我是编程的初学者,但我整夜都想弄清楚,但无济于事。 I would really appreciate if someone could help me out here. 如果有人可以在这里帮助我,我将不胜感激。

When you send &a + j*blksize*N*sizeof(double) , you are not doing what you want to do. 发送&a + j*blksize*N*sizeof(double) ,您没有做您想做的事情。 First off, &a is the address of a , which is an array of arrays, which is not what you want to send, you want to send a pointer, or *a (technically this is an array, but it will be implicitly cast to a pointer to the first element of said array). 首先, &a&a的地址, a是数组的数组,不是您要发送的内容,不是您要发送的指针,或者*a (从技术上讲,这是一个数组,但是将被隐式转换为指向所述数组第一个元素的指针)。 Next, when doing pointer arithmetic, you do not need to (and in fact, should not) multiply by sizeof(type) ; 接下来,在执行指针算术时,您不需要(实际上也不应)乘以sizeof(type) that will be taken care of for you by the compiler. 编译器会为您解决。 So your first MPI_Send command should be 因此,您的第一个MPI_Send命令应为

MPI_Send(*a + j*blksz*N, blksz*N, MPI_DOUBLE, j, 0, MPI_COMM_WORLD);

Make similar changes (for all sends and receives) and your code should work. 进行类似的更改(对于所有发送和接收),您的代码应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM