使用 scatter 发送 2 个暗淡数组

发布于 2024-09-02 04:23:48 字数 1086 浏览 6 评论 0原文

我是 MPI 的初学者,我正在使用 C 语言和处理器模拟器 (MPICH2),我编写了以下代码来发送 2D 数组以使 2 个处理器从中获取一行,但在运行 MPICH2 时会产生错误,代码是:

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

int main(int argc, char *argv[]) {
    int rank;
    int commsize;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD,&commsize);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    char** name=malloc(2*sizeof(char*));
    int i;

    for(i=0;i<2;i++){
        name[i]=malloc(15*sizeof(char));
    }
    name[0]="name";
    name[1]="age";
    /////////////////////
    if(rank==0) {
        char** mArray=malloc(2*sizeof(char*));
        MPI_Scatter(&name,1,MPI_CHAR,&mArray,1,MPI_CHAR,0,MPI_COMM_WORLD);//send
    }
    else {
        char** mArray=malloc(2*sizeof(char*));
        int k;

        for(k=0;k<2;k++){
            mArray[k]=malloc(15*sizeof(char));
        }
        MPI_Scatter(&mArray,1,MPI_CHAR,&mArray,1,MPI_CHAR,0,MPI_COMM_WORLD);//receive       
        printf("line is %s \n",mArray[rank-1]);
    }
    MPI_Finalize();
}

I am a beginner in MPI, and i am using C Language, and Simulator for Processors (MPICH2), i wrote the following code to send a 2D array to make 2 processors take a line from it but it produces error when running MPICH2, the code is:

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

int main(int argc, char *argv[]) {
    int rank;
    int commsize;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD,&commsize);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    char** name=malloc(2*sizeof(char*));
    int i;

    for(i=0;i<2;i++){
        name[i]=malloc(15*sizeof(char));
    }
    name[0]="name";
    name[1]="age";
    /////////////////////
    if(rank==0) {
        char** mArray=malloc(2*sizeof(char*));
        MPI_Scatter(&name,1,MPI_CHAR,&mArray,1,MPI_CHAR,0,MPI_COMM_WORLD);//send
    }
    else {
        char** mArray=malloc(2*sizeof(char*));
        int k;

        for(k=0;k<2;k++){
            mArray[k]=malloc(15*sizeof(char));
        }
        MPI_Scatter(&mArray,1,MPI_CHAR,&mArray,1,MPI_CHAR,0,MPI_COMM_WORLD);//receive       
        printf("line is %s \n",mArray[rank-1]);
    }
    MPI_Finalize();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-09-09 04:23:48
  name[0]="name";

  name[1]="age";

这不是字符串副本。嗯,可能不像你想象的那样。
您分配了空间并将其分配给 name[0] 和 name[1],然后用指向文字字符串“name”和“age”的指针覆盖这些指针。您分配的 char[15] 数组丢失(内存泄漏)。

然后,当您尝试使用 MPI_Scatter 发送它时,您实际上发送的是 name 指向的内容,这是一个指针,而不是字符串。 (下面的 --> 表示指向)

name --> [0x4321, 0x2348]
0x4321 --> "name"
0x2348 --> "age"

但是您分散了(至少部分)[0x4321, 0x2348] 甚至更多。

我以前没有使用过 MPI 库(我记得),但我怀疑您对 MPI_Scatter 的其他参数也不正确,因为对字符串(字符数组)和指针的误解。

我认为如果你这样做了:

char name[2][15] = {"name", "age"};

并且忘记了代码分散部分中的 malloc,你会更轻松,尽管多次尝试使用 2d C 数组也会让你陷入困境。这里的很多问题是由于人们误解了数组指针数组和二维数组之间的差异。

  name[0]="name";

  name[1]="age";

This is not a string copy. Well, not like you probably think it is.
You malloced space and assigned it to name[0] and name[1] and then you write over those pointers with pointers to the literal strings "name" and "age". The char[15] arrays you malloced are lost (memory leak).

Then when you try to send it using MPI_Scatter you are actually sending what name points to, which is a pointer, not the strings. (below --> means points to)

name --> [0x4321, 0x2348]
0x4321 --> "name"
0x2348 --> "age"

But you scatter (at least part of) [0x4321, 0x2348] and maybe more.

I haven't used the MPI library before (that I remember) but I suspect your other arguments to MPI_Scatter aren't right either because of misunderstand of strings (character arrays) and pointers.

I think that if you did:

char name[2][15] = {"name", "age"};

and forgot about your malloc in the scatter part of the code you would have an easier time, though many many many times trying to use 2d C arrays will get you messed up as well. Lots of questions on here are due to people misunderstanding the differences between arrays of pointers to arrays and 2d arrays.

病女 2024-09-09 04:23:48

正如 nategoose 指出的那样,您的字符串定义都是错误的。首先解决这些问题,在您可以 printf() 之前不要担心 MPI 调用。此外,只有当 age 字符串直接跟在内存中的 name 字符串后面时,Scatter 才会起作用:否则,它会用垃圾(或直接的段错误)填充消息的第二部分在你身上)。我不确定是否

char name[2][15] = {"name", "age"};

会将两个字符串放在一起,或者只是将两个指针放在内存中的任意位置。如果是前者,那么就使用这个。否则,我建议您根本不要使用二维数组,而是像这样声明您的字符串:

char[2*15] name = "name";
sprintf(name+15, "age");

您的 Scatter 参数也是错误的。第一个参数应该是 name,而不是 &name:Scatter 需要一个指针,传递 &name 将导致它尝试发送 name 的内存地址而不是其内容。

第二个和第五个参数也不正确:您想要发送 30 个字符(因为它是一个 2*15 数组),而不仅仅是 1。

As nategoose points out, your string definitions are all wrong. Fix those first, don't bother with MPI calls until you can printf() them. Moreover, Scatter will work only if the age string follows the name string in memory directly: otherwise, it will fill the second part of your message with junk (or straight up segfault on you). I'm not sure if

char name[2][15] = {"name", "age"};

will put the two Strings right next to each other, or it just puts two pointers next to each other that point whereever in memory. If it does the former, then use this. Otherwise, I would recommend that you don't use a 2d array at all, and rather declare your strings like this:

char[2*15] name = "name";
sprintf(name+15, "age");

Your parameters for Scatter are also wrong. The first parameter should be name, not &name: Scatter expects a pointer, passing &name will cause it to attempt sending name's memory address instead of its contents.

The second and fifth parameters aren't right either: you want to send 30 characters (since it's a 2*15 array), not just 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文