title: MPI - Sending and Receiving a column
body: <p>I need to send a matrix column from one process and receive it from another. I tried running the following program, but I'm getting a weird result (at least I think so); Only the first element of the matrix is copied, and some matrix elements change unexpectedly.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  #include "mpi.h"
  
  void swap(int* a,int* b){
      int temp;
      temp=*a;
      *a=*b;
      *b=temp;
  }
  void print_matrix(double** A,int n){
      int i,j;
      for(i=0;i&lt;n;i++){
          for(j=0;j&lt;n;j++){
              printf("%f ",A[i][j]);
          }
          printf("\n");
      }
  }
  
  int main(int argc, char *argv[]){
      int i,j,k,l,n,myid,p,maxp;
      double **A;
      MPI_Datatype col_type;
      MPI_Status status;
  
      n=3;
      A=malloc(n*sizeof(double*)); /*allocating memory */
      for(i=0;i&lt;n;i++)
          A[i]=malloc(n*sizeof(double));
  
      A[0][0]=-1;
      A[0][1]=2;
      A[0][2]=-1;
      A[1][0]=2;
      A[1][1]=-1;
      A[1][2]=0;
      A[2][0]=1;
      A[2][1]=7;
      A[2][2]=-3;
  
      MPI_Init(&amp;argc,&amp;argv);
  
      MPI_Type_vector(n, 1, n, MPI_DOUBLE,&amp;col_type);
      MPI_Type_commit(&amp;col_type);
      MPI_Comm_size(MPI_COMM_WORLD,&amp;p);
      MPI_Comm_rank(MPI_COMM_WORLD,&amp;myid);
  
      if(myid==0){
          printf("Starting Method with p=%d\n",p);
          print_matrix(A,n);
      }
      if(myid==0){
              maxp=2;
              A[0][0]=-43;
              A[1][0]=-33;
              A[2][0]=-23;
              printf("BEFORE SENDING\n");
              print_matrix(A,n);
              for(l=0;l&lt;p;l++)
                  if(l!=myid){ 
                      MPI_Send(&amp;A[0][0], 1, col_type,l,0,MPI_COMM_WORLD);
                      MPI_Send(&amp;maxp,1,MPI_INT,l,1,MPI_COMM_WORLD);
                  }
              printf("AFTER SENDING\n");
              print_matrix(A,n);
      }
      else{
              //receive(k)
              printf("BEFORE RECIEVING\n");
              print_matrix(A,n);
              MPI_Recv(&amp;A[0][1],1,col_type,0,0,MPI_COMM_WORLD,&amp;status);
              MPI_Recv(&amp;maxp,1,MPI_INT,0,1,MPI_COMM_WORLD,&amp;status);
              printf("Just Recieved\n");
              print_matrix(A,n);
      }
  
      MPI_Finalize();
  }
  </code></pre>