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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:30:29+00:00 2026-06-12T05:30:29+00:00

I have a pipeline like pattern communication between MPI processes. where processes Send message

  • 0

I have a pipeline like pattern communication between MPI processes. where processes Send message to each other as pipeline stages.
The following example shows three processes communicating in such pattern.

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

    //declare stage function
    void* testcall(void* d);

    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 k;  
            int x[3] = {10,11,12};
            void* data = malloc(sizeof(int));             
            for( k=0 ; k< 3;k++){           
                data = &x[k];           
                MPI_Send(data,4,MPI_BYTE,1,0,MPI_COMM_WORLD);   
            }
        }

        if(rank==1){                                
            void* rcv = malloc(sizeof(int));
            void* snd = malloc(sizeof(int));
            int k;
            for( k=0 ; k< 3;k++){
                MPI_Recv(rcv,4,MPI_BYTE,0,0,MPI_COMM_WORLD,&status);              
                snd = testcall(rcv);                        
                int z = *(int *) snd;  
                printf("RCV 1: %d \n",z);
                MPI_Send(&snd,4,MPI_BYTE,2,0,MPI_COMM_WORLD);
            }
        }

        if(rank==2){
            void* rcv2 = malloc(sizeof(int));
            void* snd2 = malloc(sizeof(int));
            int k;
            for( k=0 ; k< 3;k++){
                MPI_Recv(rcv2,4,MPI_BYTE,1,0,MPI_COMM_WORLD,&status);
                snd2 = testcall(rcv2);
                int z = *(int *) snd2;
                printf("RCV 2: %d \n",z);
            }
        }

        MPI_Finalize();
        return 0;
    }

    void* testcall(void* d){
        int z = *(int *) d;
        int k = z * 2;
        void* rslt = malloc(sizeof(int));
        rslt = &k;
        return rslt;
    }

output:

RCV1: 20

RCV1: 22

RCV1: 24

RCV: 2136566600

RCV: 2136566600

RCV: 2136566600

I have one problem with the code though. Send from process 0 to process 1 succeed and give me the correct when I print it.

while Send from process 1 to process 2 seems succeed, but when I try to print it is just an unexpected number (as shown in the output above).

I don’t understand why this two sends behave differently. (both are sending a value pointed by a void pointer. why the second send is erroneous )??

help please.

  • 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-12T05:30:30+00:00Added an answer on June 12, 2026 at 5:30 am
    void* testcall(void* d){
        int z = *(int *) d;
        int k = z * 2;
        void* rslt = malloc(sizeof(int));
        rslt = &k;
        return rslt;
    }
    

    This code doesn’t do what you expect. The rslt = &k; line overwrites the value of the rslt pointer with the address of k (and you do this in several other statements). First, this leads to memory leaks, as the address of the memory region, allocated by malloc, is lost. Second, k is an automatic (stack) variable at its location would get used for other purposes once testcall returns. It only works in your case because no other function call is present between testcall() and int z = *(int *) snd;. The correct function should be:

    void* testcall(void* d){
        int z = *(int *) d;
        int k = z * 2;
        int* rslt = malloc(sizeof(int));
        *rslt = k;
        return rslt;
    }
    

    Then this line from the rank 1 code:

    MPI_Send(&snd,4,MPI_BYTE,2,0,MPI_COMM_WORLD);
    

    snd is a pointer itself. You are sending the address of the pointer and that’s why rank 2 prints strange values. The correct statement should read:

    MPI_Send(snd,4,MPI_BYTE,2,0,MPI_COMM_WORLD);
    

    Output:

    RCV 1: 20
    RCV 1: 22
    RCV 1: 24
    RCV 2: 40
    RCV 2: 44
    RCV 2: 48
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server reading inbound newline-delimited strings. The pipeline looks like this: ...
I have a 3x3 table of td's each with id's (id='a1'...id='c3'). I'd like to
Suppose I have some Python code like the following: input = open(input.txt) x =
If I have a pipeline like: gst-launch filesrc location=/home/dk/Music/Vangelis - Alpha.mp3 ! decodebin2 name=dec
Currently, I'm using the RX Framework to implement a workflow-like message handling pipeline. Essentially
I have a processing pipeline implemented, but I would like to improve it like
I have a netty pipeline such as: return Channels.pipeline( new ObjectEncoder(), new ObjectDecoder(), new
I have got a pipeline of tasks which basically is a variation of chain
I have a rails 3.2.6 app and I am using the asset pipeline and
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=

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.