-1

I am trying to pack an array and send it from one process to another. I am doing the operation on only 2 processes. I have written the following code.

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

int main( int argc, char *argv[] )
{
    MPI_Init(&argc, &argv);
    
     int myrank, size; //size will take care of number of processes 
         
      MPI_Comm_rank(MPI_COMM_WORLD, &myrank) ;
      MPI_Comm_size(MPI_COMM_WORLD, &size);


    //declaring the matrix

    double mat[4]={1,   2,  3,  4};



    int r=4;


    //Now we will send from one matrix to another using MPI_Pack
    
    // For that we will need buffers which will have same number of rows as number of columns
    
    double snd_buf[r];
    double recv_buf[r];
    double buf[r];
    

    
    //total size of the data that is beign sent
    int outs=r*8;
    
    int position=0;
    
    
    MPI_Status status[r];
    
    //packing and sending the data
    if(myrank==0)
        {
            
                for(int i=0;i<r;i++)
                {
                    MPI_Pack(&mat[i], 1 , MPI_DOUBLE,snd_buf,outs,&position,MPI_COMM_WORLD);
                }
            
            
            
            
                MPI_Send (&snd_buf, r , MPI_PACKED, 1 /*dest*/ , 100 /*tag*/ , MPI_COMM_WORLD);
            
            
            
            
        }   
    
    
    
    //receiving the data
    
    if(myrank==1)
        {
                
                 MPI_Recv(recv_buf, r, MPI_PACKED, 0 /*src*/ , 100 /*tag*/, MPI_COMM_WORLD,&status[0]);
                
                position=0;
                 for(int i=0;i<r;i++)
                 {
                    MPI_Unpack(recv_buf,outs,&position,&buf[i], 1, MPI_DOUBLE, MPI_COMM_WORLD);
                 }
            
            
            
                
            
        }
        
        
        
        
        //checking whether the packing in snd_buff is taking place correctly or not
        if(myrank==1)
        {
                
                for(int i=0;i<r;i++)
                 {
                    printf("%lf ",buf[i]);
                 
                 }
                 
                 printf("\n");
                 
                
        }
        
        

    
        MPI_Finalize();
        return 0;

}

I am expecting the output--> 1 2 3 4 but I am only getting 0 0 0 0 in my output.

I was suspecting whether it is a problem of the snd_buffer, but the snd_buffer seems to be fine as it is having all elements 1 2 3 4 correctly.

I have also tried to send and receive like this

//packing and sending the data
    if(myrank==0)
        {
            
        
                {
                    MPI_Pack(&mat[0], 4 , MPI_DOUBLE,snd_buf,outs,&position,MPI_COMM_WORLD);
                }

                MPI_Send (snd_buf, r , MPI_PACKED, 1 /*dest*/ , 100 /*tag*/ , MPI_COMM_WORLD);

        }   
    
    
    
    //receiving the data
    
    if(myrank==1)
        {
                
                 MPI_Recv(recv_buf, r, MPI_PACKED, 0 /*src*/ , 100 /*tag*/, MPI_COMM_WORLD,&status[0]);
                
                position=0;
                
                 {
                    MPI_Unpack(recv_buf,outs,&position,&buf[0], 4, MPI_DOUBLE, MPI_COMM_WORLD);
                 }
        

Still, this was of no help and the output was only 0s.

I am not able to get why I am facing this error. Any help will be appreciated. Thank you.

Turing101
  • 347
  • 3
  • 15

1 Answers1

0

Answering my own question. The mistake that I have done was pointed out by Gilles in the comments.

This is the solution to the problem that I have faced

send/recv outs MPI_PACKED (instead of r).

PS--> Consider declaring send_buf and recv_bufas char[] in order to avoid this kind of confusion. char[] won't solve the issue, but make the code more readable (and more obvious sending/receiving r MPI_PACKED is not the right thing to do)

Turing101
  • 347
  • 3
  • 15