如何使用C在MPI中发送具有指针字段的(MPI_Send)嵌套结构 [英] How to send(MPI_Send) nested structure having pointer fields in MPI using C

查看:102
本文介绍了如何使用C在MPI中发送具有指针字段的(MPI_Send)嵌套结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构:

    struct vertex 
    {  
     double a; double b;
    }      

    struct polygon
    {
     int numofVertex;
     vertex *v;
    }

如何使用MPI_Send在MPI中发送此嵌套结构? 问题在于该结构包含指针字段"v",由于该指针字段,MPI_Send崩溃. 我试过MPI_Datatype定义新的数据类型,它不起作用. 我读到序列化是唯一的解决方案,但是C没有提供这种实现. 有什么建议可以解决这个问题吗?

How to send this nested structure in MPI using MPI_Send? The problem is that the structure contains pointer field "v" due to which MPI_Send crashes. I have tried MPI_Datatype to define new data type, it doesnot work. I read that serialization is the only solution but C doesnot provide such implementation. Any suggestions how to get around with this problem?

推荐答案

您将不得不通过两条消息发送它:

You'll have to send it in two messages:

// 'p' is a pointer to your polygon
vertex *tmp = p->v;
p->v = NULL;
MPI_Send(p, sizeof(struct polygon), MPI_BYTES, dest, 1, ...);
MPI_Send(tmp, sizeof(struct vertex), MPI_BYTES, dest, 2, ...);
p->v = tmp;

在接收端,您只需分两个步骤即可接收该结构:

On the receiving end, you simply receive the struct in two steps:

polygon p;
MPI_Recv(&p, sizeof(struct polygon), MPI_BYTES, src, 1, ...);
p.vertex = malloc(sizeof(struct vertex));
MPI_Recv(p.vertex, sizeof(struct vertex), MPI_BYTES, src, 2, ...);

显然这不是很好,所以如果不使用struct指针,则可以更轻松地进行发送.

Obviously this is not very nice, so it'll be easier if you keep your struct pointer-less so you can send it at once.

这篇关于如何使用C在MPI中发送具有指针字段的(MPI_Send)嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆