Monday 15 July 2013

C,open_MPI, user-defined-struct type not passing correctly -



C,open_MPI, user-defined-struct type not passing correctly -

i've used mpi_type_create_struct define mpi struct datatype. construction 1 int , 4 double. however, lastly element(double) in struct never passing correctly.

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include <mpi.h> struct c{ int index; double charge,x,y,z; }; main(int argc,char **argv) { int rank,p; int i,j; mpi_init(&argc,&argv); mpi_comm_rank(mpi_comm_world,&rank); mpi_comm_size(mpi_comm_world,&p); mpi_datatype old_type[2]={mpi_int,mpi_double}; mpi_datatype chargestruct;//create mpi info struct int blocklens[2]={1,4}; mpi_aint disa[2]; mpi_aint span,lb; mpi_type_get_extent(mpi_int,&lb,&span); disa[0]=0; disa[1]=span; mpi_type_create_struct(2,blocklens,disa,old_type,&chargestruct);//the struct has mpi_type chargestruct mpi_type_commit(&chargestruct); struct c buff,charge; mpi_status status; charge.z=1.0; int targetp,sourcep; targetp=(rank-1)<0?p-1:(rank-1); sourcep=(rank+1)==p?0:(rank+1); if(rank==0){ mpi_send(&charge,1,chargestruct,targetp,rank,mpi_comm_world); } else{ mpi_recv(&buff,1,chargestruct,sourcep,sourcep,mpi_comm_world,&status); } printf("%d %lf %lf\n",rank,charge.z,buff.z); mpi_finalize(); }

the lastly "z", getting 0.0000 in receiving buffer. knows why?

the problem padding,int 4 bytes, in struct defined in question. 4 bytes padding added after int. disa[1] should 8 rather 4(which returns mpi_type_get_extent call). in case, cannot handled send , recv, since origin of each elements not located correctly. easiest prepare might using mpi_get_address instead, safely gave origin of element.

mpi_get_address(struct c*.charge,&disa[1]) mpi_get_address(struct c*.index,&disa[0]) disa[1]-=disa[0]

this should gave right displacement, , much safer way create mpi info type

c struct

No comments:

Post a Comment