中的结构序列化并通过

中的结构序列化并通过

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

问题描述

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

I have defined a custom struct which I need to send over to anotherMPI 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?

推荐答案

Jeremiah 是对的 - MPI_Type_create_struct 是解决方案.

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

重要的是要记住 MPI 是一个库,而不是内置于语言中;所以它不能看到"一个结构的样子来自己序列化它.因此,要发送复杂的数据类型,您必须明确定义其布局.在原生支持序列化的语言中,一组 MPI 包装器可以令人信服地使用它;mpi4py 例如使用 python 的 pickle 透明地发送复杂的数据类型;但在 C 中,你必须卷起袖子自己做.

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.
");
        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
", 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
", rank,
                 recv.shifts, recv.topSpeed);
    }

    MPI_Type_free(&mpi_car_type);
    MPI_Finalize();

    return 0;
}

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

08-01 00:39