在C中进行结构化序列化并通过MPI进行传输 [英] struct serialization in C and transfer over MPI

查看:113
本文介绍了在C中进行结构化序列化并通过MPI进行传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个自定义struct,需要将其发送给另一个 使用MPI_Bsend(或MPI_Send)进行MPI处理.

I have defined a custom struct which I need to send over to another MPI process using the MPI_Bsend (or MPI_Send).

这是结构:

struct car{
  int shifts;
  int topSpeed;
}myCar;

问题在于,除了原始类型之外,MPI似乎不支持复杂数据类型的直接传输",如上面所示的结构.我听说我可能必须使用序列化".

The issue is that apart from primitive types MPI doesn't seem to support direct "transmission" of complex data types like the struct shown above. I've heard that I might have to use "serialization".

我应该如何处理并成功通过myCar发送到进程5?

How should I approach this and successfully send over myCar to process 5?

推荐答案

耶利米是正确的-MPI_Type_create_struct是到达这里的方法.

Jeremiah is right - MPI_Type_create_struct is the way to go here.

请记住,MPI是一个库,不是语言的内置库,这一点很重要;因此它无法看到"结构将其自身序列化的样子.因此,要发送复杂的数据类型,必须显式定义其布局.在确实支持序列化的语言中,一组MPI包装器可以方便地利用它.例如, mpi4py 利用python的

It's important to remember that MPI is a library, not built into the language; so it can't "see" what a structure looks like to serialize it by itself. So to send complex data types, you have to explicitly define its layout. In a language that does have native support for serialization, a set of MPI wrappers can concievably make use of that; mpi4py for instance makes use of python's pickle to transparently send complex data types; but in C, you have to roll up your sleeves and do it yourself.

对于您的结构,它看起来像这样:

For your structure, it looks like this:

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

typedef struct car_s {
        int shifts;
        int topSpeed;
} car;

int main(int argc, char **argv) {

    const int tag = 13;
    int size, rank;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if (size < 2) {
        fprintf(stderr,"Requires at least two processes.\n");
        exit(-1);
    }

    /* create a type for struct car */
    const int nitems=2;
    int          blocklengths[2] = {1,1};
    MPI_Datatype types[2] = {MPI_INT, MPI_INT};
    MPI_Datatype mpi_car_type;
    MPI_Aint     offsets[2];

    offsets[0] = offsetof(car, shifts);
    offsets[1] = offsetof(car, topSpeed);

    MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_car_type);
    MPI_Type_commit(&mpi_car_type);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    if (rank == 0) {
        car send;
        send.shifts = 4;
        send.topSpeed = 100;

        const int dest = 1;
        MPI_Send(&send,   1, mpi_car_type, dest, tag, MPI_COMM_WORLD);

        printf("Rank %d: sent structure car\n", rank);
    }
    if (rank == 1) {
        MPI_Status status;
        const int src=0;

        car recv;

        MPI_Recv(&recv,   1, mpi_car_type, src, tag, MPI_COMM_WORLD, &status);
        printf("Rank %d: Received: shifts = %d topSpeed = %d\n", rank,
                 recv.shifts, recv.topSpeed);
    }

    MPI_Type_free(&mpi_car_type);
    MPI_Finalize();

    return 0;
}

这篇关于在C中进行结构化序列化并通过MPI进行传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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