Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:10:35+00:00 2026-05-31T19:10:35+00:00

I have defined a custom struct which I need to send over to another

  • 0

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

Here is the struct:

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

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”.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:10:36+00:00Added an answer on May 31, 2026 at 7:10 pm

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

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have scoped macros using custom defined macros through boost wave?
I have a problem using my url view helper. I have defined custom routes
I have defined a custom tag to take one parameter, a number, which it
here's my issue. I have defined custom themes and styles, so as to customize
I have defined a custom ListViewItem contorl in XAML, which we'll call Item.xaml. These
I have defined a custom Sharepoint list for special attributes related to a software
I have defined a custom list template type for SharePoint . I install it
I have defined a custom IPrincipal and custom IIdentity based on a website that
I have defined a custom file type with these lines: syn region SubSubtitle start=+=+
I've defined a custom content type - news. I also have a menu item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.