使用MPI_Pack打包阵列 [英] Packing Arrays using MPI_Pack

查看:106
本文介绍了使用MPI_Pack打包阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打包一个数组,并将其从一个进程发送到另一个进程.我仅在2个进程上执行此操作.我已经编写了以下代码.

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;

}

我期望输出-> 1 2 3 4 ,但是我在输出中仅得到 0 0 0 0 .

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

我怀疑这是否是snd_buffer的问题,但是snd_buffer似乎很好,因为它正确地包含了所有 1 2 3 4 元素.

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);
                 }
        

仍然,这没有帮助,输出仅为0s.

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.

推荐答案

回答我自己的问题. Gilles 在评论中指出了我犯的错误.

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

发送/接收 outs MPI_PACKED (而不是 r ).

send/recv outs MPI_PACKED (instead of r).

PS->考虑将 send_buf recv_buf 声明为 char [] ,以避免这种混淆. char [] 不能解决问题,但会使代码更具可读性(并且更明显的发送/接收 r MPI_PACKED 是不正确的要做的事情

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)

这篇关于使用MPI_Pack打包阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆