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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:02:21+00:00 2026-06-04T13:02:21+00:00

I was wondering why this program actually works in MPI (openMPI 1.5/1.6. ) #include

  • 0

I was wondering why this program actually works in MPI (openMPI 1.5/1.6. )

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

#define VECTOR_SIZE 100

int main(int argc,char ** argv) {
  int A[VECTOR_SIZE];
  int sub_size=2;
  int count=10;
  MPI_Datatype partial_array;
  int rank,size;
  MPI_Status status;

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

  MPI_Type_vector(count, sub_size,
          2*sub_size, MPI_INT, &partial_array);

  MPI_Type_commit(&partial_array);

  if (rank == 0) {
    int i;
    // server - initialize data and send
    for (i = 0; i< VECTOR_SIZE; i++) {   
      A[i] = i;
    }
    MPI_Send(&(A[0]), 1, partial_array, 1, 0, MPI_COMM_WORLD);
  } else if (rank==1) {
    int i;
    for (i = 0; i< VECTOR_SIZE; i++) {

        A[i] = 0;

    }
    // vector is composed by 20 MPI_INT elements
    MPI_Recv(&(A[0]),20, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);

    printf("\n");

    for (i = 0; i<VECTOR_SIZE; i++) {
      printf("%d ",A[i]);

    }
    printf("\n");
  }

  MPI_Finalize();

}

while this other program where Send and Receive primitives are exchanged does not terminate (the receive never completes):

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

#define VECTOR_SIZE 100

int main(int argc,char ** argv) {
    int A[VECTOR_SIZE];
    int sub_size=2;
    int count=10;
    MPI_Datatype partial_array;
    int rank,size;
    MPI_Status status;

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

    MPI_Type_vector(count, sub_size,
                    2*sub_size, MPI_INT, &partial_array);

    MPI_Type_commit(&partial_array);

    if (rank == 0) {
        int i;
        // server - initialize data and send
        for (i = 0; i< VECTOR_SIZE; i++) {

            A[i] = i;

        }
        MPI_Send(&(A[0]),20, MPI_INT, 0, 0, MPI_COMM_WORLD);

    } else if (rank==1) {
        int i;
        // client - receive data and print
        for (i = 0; i< VECTOR_SIZE; i++) {

            A[i] = 0;

        }

        MPI_Recv(&(A[0]), 1, partial_array, 1, 0, MPI_COMM_WORLD, &status);

        printf("\n");

        for (i = 0; i<VECTOR_SIZE; i++) {
            printf("%d ",A[i]);

        }
        printf("\n");
    }

    MPI_Finalize();

}

If I understand MPI type mathing rules correctly neither of the two should complete.

  • 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-04T13:02:24+00:00Added an answer on June 4, 2026 at 1:02 pm

    Obviously in the second program rank 0 is sending to itself and rank 1 is expecting message also from itself:

    MPI_Send(&(A[0]),20, MPI_INT, 0, 0, MPI_COMM_WORLD);
    

    destination rank should be 1, not 0

    MPI_Recv(&(A[0]), 1, partial_array, 1, 0, MPI_COMM_WORLD, &status);
    

    source rank should be 0, not 1.

    Otherwise you do not understand the MPI type matching correctly. It only states that underlying primitive types in the type maps on both ends should match. You are creating a vector whose type map has 20 primitive integers. If you send one element of this type, your message will actually contain 20 integers. On the receiver side you provide space for at least 20 integers so this is correct. The opposite is also correct.

    It is not correct if you send only 10 or 18 integers in the second program since they will not make a complete element of the vector type. Nevertheless, the receive operation will complete but if you call MPI_Get_count() on the status, if will return MPI_UNDEFINED because from the number of received primitive integer elements one cannot construct an integer number of vector elements. It is also not correct to mix primitive types, e.g. send MPI_DOUBLE (or vector, or structure, or whatever other type that has doubles) and receive it as MPI_INT.

    Please also note that MPI messages do not carry their type map or type ID with them so most MPI implementations do not check if types match. It is possible to send MPI_FLOAT and receive it as MPI_INT (because both are 4 bytes on most systems) but it is not correct to do so.

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

Sidebar

Related Questions

I am wondering how this happens: how is a Java program mapped to an
Im wondering whether this snippet of code will actually store a pointer pointing to
Just wondering why this works: window.NewListView = Backbone.View.extend({ template: _.template('<a href=/list class=button new-list>Create New
Just curious, what actually happens if I define a zero-length array int array[0]; in
I am wondering this syntax {:age.gt => 60} , which I find this syntax
I've been wondering this for a while now; but especially more so since I've
I was wondering this and couldn't find anything except this Thread scheduler bug fixes
I've been wondering this for a while, and Google hasn't provided me with the
Just wondering is this kind of code recommended to increase performance? void functionCalledLotsofTimes() {
Just wondering about this code below... when I turn off my internet connection and

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.