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 8894565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:31:50+00:00 2026-06-14T23:31:50+00:00

I have some particular problem: in a simple MPI_Send/MPI_Recv program, it is assumed that

  • 0

I have some particular problem: in a simple MPI_Send/MPI_Recv program, it is assumed that we know the type of message we are about to send, but at the receiving end we don’t know which type of data we will receive.

SO I try first attempt as follows:

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

    int main(int args, char** argv){
            int rank, size;
            MPI_Status status;
            MPI_Init(&args,&argv);
            MPI_Comm_rank(MPI_COMM_WORLD,&rank);
            MPI_Comm_size(MPI_COMM_WORLD,&size);

            if(rank==0){
                    int x = 10;
                    MPI_Send(&x,4,MPI_BYTE,1,0,MPI_COMM_WORLD);
            }
            if(rank==1){
                    void* datax;
                    int count = sizeof(int);
                    datax = malloc(count);
                    MPI_Recv(datax,count,MPI_BYTE,0,0,MPI_COMM_WORLD,&status);
                    //Now check if the value is received correctly
                    int z = (int) datax;
                    printf("RCV: %d \n",z);
            }

            MPI_Finalize();
            return 0;
    }

the program compiles correctly and also run, but the received value is some memory garbage value not the correct one (10 in this case).

Any suggestion please?

thanks,

  • 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-06-14T23:31:51+00:00Added an answer on June 14, 2026 at 11:31 pm

    Although your comment addresses your programming mistake, there is a much better way to send and receive data of unknown types (and even unknown size) in MPI.

    One common way to differentiate different message types is to use different tag numbers for each type. In your code, you are using a tag number of 0 in your MPI_Send call. Use a tag number that describes the the type of message, and then use MPI_Probe before your MPI_Recv call to find out what type of message you are about to receive. For example, your code could be modified like so:

    if (rank == 0) {
        MPI_Send(data_buf, data_size, MPI_BYTE, 1, tag_that_describes_message_type, MPI_COMM_WORLD)
    } else if (rank == 1) {
        MPI_Status status;
        // Probe for a message coming from rank 0 with any tag.
        MPI_Probe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
        // Find out what type of message is ready to be received by accessing the MPI_TAG variable in the status struct
        if (status.MPI_TAG == 0) {
            // Receive messages of this type. Maybe you sent an array of ints?
            MPI_Recv(...)
        } else if (status.MPI_TAG == 1) {
            // Receive messages of this type. Maybe you sent an array of floats? Handle it here.
            MPI_Recv(...)
        } else if ...
    

    MPI_Probe can also be used to find out the size of a message before you receive a message, which allows you dynamically receive virtually any type of message. For a better explanation and illustration of using MPI_Probe for this purpose, go to this tutorial.

    If you need to send widely-different types of data (too many to enumerate with tag numbers), pack your messages into a Google Protocol Buffer, receive it as an array of MPI_BYTEs (as you do in your example), and then unpack it on the receiving side. A C implementation of Protocol Buffers is here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some MP3 files that are named with a particular syntax, for example:
I have a web-site that generates some simple tabular data as html tables, many
The problem: I have a legacy php app that is coded in no particular
I have a fairly simple console app that monitors an exchange mailbox, picks particular
I have a rather strange problem. I have a very simple application that reads
I have some particular need to clone an element (image) and be draggable inside
I have found some code which recognize circles in particular image and I was
I have downloaded some files from the internet related to a particular topic. Now
Here's the situation: On a particular branch, some changes have been merged in from
I have some arbitrary pixel data that I want to save as a PNG.

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.