#ifdef PETSC_RCS_HEADER
static char vcid[] = "$Id: mmdense.c,v 1.17 1999/02/07 15:57:46 bsmith Exp bsmith $";
#endif

/*
   Support for the parallel dense matrix vector multiply
*/
#include "src/mat/impls/dense/mpi/mpidense.h"
#include "src/vec/vecimpl.h"

#undef __FUNC__  
#define __FUNC__ "MatSetUpMultiply_MPIDense"
int MatSetUpMultiply_MPIDense(Mat mat)
{
  Mat_MPIDense *mdn = (Mat_MPIDense *) mat->data;
  int          ierr,n;
  IS           tofrom;
  Vec          gvec;

  PetscFunctionBegin;
  /* Create local vector that is used to scatter into */
  ierr = VecCreateSeq(PETSC_COMM_SELF,mdn->N,&mdn->lvec); CHKERRQ(ierr);

  /* Create temporary index set for building scatter gather */
  ierr = ISCreateStride(PETSC_COMM_SELF,mdn->N,0,1,&tofrom); CHKERRQ(ierr);

  /* Create temporary global vector to generate scatter context */
  n    = mdn->cowners[mdn->rank+1] - mdn->cowners[mdn->rank];

  /* the partitioning of this vector is not right! you don't really know
     what it is; this will only work for default (PETSC_DECIDE) matrix layouts*/
  ierr = VecCreateMPI(mat->comm,PETSC_DECIDE,mdn->N,&gvec); CHKERRQ(ierr);

  /* Generate the scatter context */
  ierr = VecScatterCreate(gvec,tofrom,mdn->lvec,tofrom,&mdn->Mvctx); CHKERRQ(ierr);
  PLogObjectParent(mat,mdn->Mvctx);
  PLogObjectParent(mat,mdn->lvec);
  PLogObjectParent(mat,tofrom);
  PLogObjectParent(mat,gvec);

  ierr = ISDestroy(tofrom); CHKERRQ(ierr);
  ierr = VecDestroy(gvec); CHKERRQ(ierr);
  PetscFunctionReturn(0);
}

extern int MatGetSubMatrices_MPIDense_Local(Mat,int,IS*,IS*,MatReuse,Mat*);
#undef __FUNC__  
#define __FUNC__ "MatGetSubMatrices_MPIDense" 
int MatGetSubMatrices_MPIDense(Mat C,int ismax,IS *isrow,IS *iscol,
                             MatReuse scall,Mat **submat)
{ 
  Mat_MPIDense  *c = (Mat_MPIDense *) C->data;
  int           nmax,nstages_local,nstages,i,pos,max_no,ierr;

  PetscFunctionBegin;
  /* Allocate memory to hold all the submatrices */
  if (scall != MAT_REUSE_MATRIX) {
    *submat = (Mat *)PetscMalloc((ismax+1)*sizeof(Mat));CHKPTRQ(*submat);
  }
  /* Determine the number of stages through which submatrices are done */
  nmax          = 20*1000000 / (c->N * sizeof(int));
  if (nmax == 0) nmax = 1;
  nstages_local = ismax/nmax + ((ismax % nmax)?1:0);

  /* Make sure every processor loops through the nstages */
  ierr = MPI_Allreduce(&nstages_local,&nstages,1,MPI_INT,MPI_MAX,C->comm);CHKERRQ(ierr);


  for ( i=0,pos=0; i<nstages; i++ ) {
    if (pos+nmax <= ismax) max_no = nmax;
    else if (pos == ismax) max_no = 0;
    else                   max_no = ismax-pos;
    ierr = MatGetSubMatrices_MPIDense_Local(C,max_no,isrow+pos,iscol+pos,scall,*submat+pos);CHKERRQ(ierr);
    pos += max_no;
  }
  PetscFunctionReturn(0);
}
/* -------------------------------------------------------------------------*/
#undef __FUNC__  
#define __FUNC__ "MatGetSubMatrices_MPIDense_Local" 
int MatGetSubMatrices_MPIDense_Local(Mat C,int ismax,IS *isrow,IS *iscol,MatReuse scall,Mat *submats)
{ 
  Mat_MPIDense  *c = (Mat_MPIDense *) C->data;
  Mat            A = c->A;
  Mat_SeqDense  *a = (Mat_SeqDense*)A->data, *mat;
  int           N = c->N, rstart = c->rstart,count;
  int           **irow,**icol,*nrow,*ncol,*w1,*w2,*w3,*w4,*rtable,start,end,size;
  int           **sbuf1, rank, m,i,j,k,l,ct1,ierr, **rbuf1,row,proc;
  int           nrqs, msz, **ptr,index,*ctr,*pa,*tmp,bsz,nrqr;
  int           is_no,jmax,*irow_i,**rmap,*rmap_i;
  int           len,ctr_j,*sbuf1_j,*rbuf1_i;
  int           tag0,tag1;
  MPI_Request   *s_waits1,*r_waits1,*s_waits2,*r_waits2;
  MPI_Status    *r_status1,*r_status2,*s_status1,*s_status2;
  MPI_Comm      comm;
  Scalar        **rbuf2,**sbuf2;

  PetscFunctionBegin;
  comm   = C->comm;
  tag0   = C->tag;
  size   = c->size;
  rank   = c->rank;
  m      = c->M;
  
  /* Get some new tags to keep the communication clean */
  ierr = PetscObjectGetNewTag((PetscObject)C,&tag1); CHKERRQ(ierr);

    /* Check if the col indices are sorted */
  for ( i=0; i<ismax; i++ ) {
    ierr = ISSorted(isrow[i],(PetscTruth*)&j);
    if (!j) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"ISrow is not sorted");
    ierr = ISSorted(iscol[i],(PetscTruth*)&j);
    if (!j) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"IScol is not sorted");
  }

  len    =  2*ismax*(sizeof(int *)+sizeof(int)) + (m+1)*sizeof(int);
  irow   = (int **)PetscMalloc(len); CHKPTRQ(irow);
  icol   = irow + ismax;
  nrow   = (int *) (icol + ismax);
  ncol   = nrow + ismax;
  rtable = ncol + ismax;

  for ( i=0; i<ismax; i++ ) { 
    ierr = ISGetIndices(isrow[i],&irow[i]);  CHKERRQ(ierr);
    ierr = ISGetIndices(iscol[i],&icol[i]);  CHKERRQ(ierr);
    ierr = ISGetSize(isrow[i],&nrow[i]);  CHKERRQ(ierr);
    ierr = ISGetSize(iscol[i],&ncol[i]);  CHKERRQ(ierr);
  }

  /* Create hash table for the mapping :row -> proc*/
  for ( i=0,j=0; i<size; i++ ) {
    jmax = c->rowners[i+1];
    for ( ; j<jmax; j++ ) {
      rtable[j] = i;
    }
  }

  /* evaluate communication - mesg to who, length of mesg, and buffer space
     required. Based on this, buffers are allocated, and data copied into them*/
  w1     = (int *)PetscMalloc(size*4*sizeof(int));CHKPTRQ(w1); /* mesg size */
  w2     = w1 + size;      /* if w2[i] marked, then a message to proc i*/
  w3     = w2 + size;      /* no of IS that needs to be sent to proc i */
  w4     = w3 + size;      /* temp work space used in determining w1, w2, w3 */
  PetscMemzero(w1,size*3*sizeof(int)); /* initialize work vector*/
  for ( i=0; i<ismax; i++ ) { 
    PetscMemzero(w4,size*sizeof(int)); /* initialize work vector*/
    jmax   = nrow[i];
    irow_i = irow[i];
    for ( j=0; j<jmax; j++ ) {
      row  = irow_i[j];
      proc = rtable[row];
      w4[proc]++;
    }
    for ( j=0; j<size; j++ ) { 
      if (w4[j]) { w1[j] += w4[j];  w3[j]++;} 
    }
  }
  
  nrqs     = 0;              /* no of outgoing messages */
  msz      = 0;              /* total mesg length (for all procs) */
  w1[rank] = 0;              /* no mesg sent to self */
  w3[rank] = 0;
  for ( i=0; i<size; i++ ) {
    if (w1[i])  { w2[i] = 1; nrqs++;} /* there exists a message to proc i */
  }
  pa = (int *)PetscMalloc((nrqs+1)*sizeof(int));CHKPTRQ(pa); /*(proc -array)*/
  for ( i=0, j=0; i<size; i++ ) {
    if (w1[i]) { pa[j] = i; j++; }
  } 

  /* Each message would have a header = 1 + 2*(no of IS) + data */
  for ( i=0; i<nrqs; i++ ) {
    j     = pa[i];
    w1[j] += w2[j] + 2* w3[j];   
    msz   += w1[j];  
  }
  /* Do a global reduction to determine how many messages to expect*/
  {
    int *rw1, *rw2;
    rw1   = (int *)PetscMalloc(2*size*sizeof(int)); CHKPTRQ(rw1);
    rw2   = rw1+size;
    ierr  = MPI_Allreduce(w1, rw1, size, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
    bsz   = rw1[rank];
    ierr  = MPI_Allreduce(w2, rw2, size, MPI_INT, MPI_SUM, comm);CHKERRQ(ierr);
    nrqr  = rw2[rank];
    PetscFree(rw1);
  }

  /* Allocate memory for recv buffers . Prob none if nrqr = 0 ???? */ 
  len      = (nrqr+1)*sizeof(int*) + nrqr*bsz*sizeof(int);
  rbuf1    = (int**) PetscMalloc(len);  CHKPTRQ(rbuf1);
  rbuf1[0] = (int *) (rbuf1 + nrqr);
  for ( i=1; i<nrqr; ++i ) rbuf1[i] = rbuf1[i-1] + bsz;
  
  /* Post the receives */
  r_waits1 = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(r_waits1);
  for ( i=0; i<nrqr; ++i ) {
    ierr = MPI_Irecv(rbuf1[i],bsz,MPI_INT,MPI_ANY_SOURCE,tag0,comm,r_waits1+i);CHKERRQ(ierr);
  }

  /* Allocate Memory for outgoing messages */
  len      = 2*size*sizeof(int*) + 2*msz*sizeof(int) + size*sizeof(int);
  sbuf1    = (int **)PetscMalloc(len); CHKPTRQ(sbuf1);
  ptr      = sbuf1 + size;   /* Pointers to the data in outgoing buffers */
  PetscMemzero(sbuf1,2*size*sizeof(int*));
  /* allocate memory for outgoing data + buf to receive the first reply */
  tmp      = (int *) (ptr + size);
  ctr      = tmp + 2*msz;

  {
    int *iptr = tmp,ict = 0;
    for ( i=0; i<nrqs; i++ ) {
      j         = pa[i];
      iptr     += ict;
      sbuf1[j]  = iptr;
      ict       = w1[j];
    }
  }

  /* Form the outgoing messages */
  /* Initialize the header space */
  for ( i=0; i<nrqs; i++ ) {
    j           = pa[i];
    sbuf1[j][0] = 0;
    PetscMemzero(sbuf1[j]+1, 2*w3[j]*sizeof(int));
    ptr[j]      = sbuf1[j] + 2*w3[j] + 1;
  }
  
  /* Parse the isrow and copy data into outbuf */
  for ( i=0; i<ismax; i++ ) {
    PetscMemzero(ctr,size*sizeof(int));
    irow_i = irow[i];
    jmax   = nrow[i];
    for ( j=0; j<jmax; j++ ) {  /* parse the indices of each IS */
      row  = irow_i[j];
      proc = rtable[row];
      if (proc != rank) { /* copy to the outgoing buf*/
        ctr[proc]++;
        *ptr[proc] = row;
        ptr[proc]++;
      }
    }
    /* Update the headers for the current IS */
    for ( j=0; j<size; j++ ) { /* Can Optimise this loop too */
      if ((ctr_j = ctr[j])) {
        sbuf1_j        = sbuf1[j];
        k              = ++sbuf1_j[0];
        sbuf1_j[2*k]   = ctr_j;
        sbuf1_j[2*k-1] = i;
      }
    }
  }

  /*  Now  post the sends */
  s_waits1 = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(s_waits1);
  for ( i=0; i<nrqs; ++i ) {
    j = pa[i];
    ierr = MPI_Isend( sbuf1[j], w1[j], MPI_INT, j, tag0, comm, s_waits1+i);CHKERRQ(ierr);
  }

  /* Post recieves to capture the row_data from other procs */
  r_waits2 = (MPI_Request *) PetscMalloc((nrqs+1)*sizeof(MPI_Request));CHKPTRQ(r_waits2);
  rbuf2    = (Scalar**)PetscMalloc((nrqs+1)*sizeof(Scalar *));CHKPTRQ(rbuf2);
  for ( i=0; i<nrqs; i++ ) {
    j        = pa[i];
    count    = (w1[j] - (2*sbuf1[j][0] + 1))*N;
    rbuf2[i] = (Scalar *)PetscMalloc((count+1)*sizeof(Scalar)); CHKPTRQ(rbuf2[i]);
    ierr     = MPI_Irecv( rbuf2[i], count, MPIU_SCALAR, j, tag1, comm, r_waits2+i);CHKERRQ(ierr);
  }

  /* Receive messages(row_nos) and then, pack and send off the rowvalues
     to the correct processors */

  s_waits2  = (MPI_Request *) PetscMalloc((nrqr+1)*sizeof(MPI_Request));CHKPTRQ(s_waits2);
  r_status1 = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(r_status1);
  sbuf2       = (Scalar **) PetscMalloc((nrqr+1)*sizeof(Scalar*));CHKPTRQ(sbuf2);
 
  {
    Scalar *sbuf2_i,*v_start;
    int    s_proc;
    for ( i=0; i<nrqr; ++i ) {
      ierr = MPI_Waitany(nrqr, r_waits1, &index, r_status1+i);CHKERRQ(ierr);
      s_proc          = r_status1[i].MPI_SOURCE; /* send processor */
      rbuf1_i         = rbuf1[index]; /* Actual message from s_proc */
      /* no of rows = end - start; since start is array index[], 0index, whel end
         is length of the buffer - which is 1index */
      start           = 2*rbuf1_i[0] + 1;
      MPI_Get_count(r_status1+i,MPI_INT, &end);
      /* allocate memory sufficinet to hold all the row values */
      sbuf2[index] = (Scalar *)PetscMalloc((end-start)*N*sizeof(Scalar));CHKPTRQ(sbuf2[index]);
      sbuf2_i      = sbuf2[index];
      /* Now pack the data */
      for ( j=start; j<end; j++ ) {
        row = rbuf1_i[j] - rstart;
        v_start = a->v + row;
        for ( k=0; k<N; k++ ) {
          sbuf2_i[0] = v_start[0];
          sbuf2_i++; v_start+=a->m;
        }
      }
      /* Now send off the data */
      ierr = MPI_Isend(sbuf2[index],(end-start)*N,MPIU_SCALAR,s_proc,tag1,comm,s_waits2+i); CHKERRQ(ierr);
    }
  }
  /* End Send-Recv of IS + row_numbers */
  PetscFree(r_status1); PetscFree(r_waits1);
  s_status1 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(s_status1);
  ierr      = MPI_Waitall(nrqs,s_waits1,s_status1);CHKERRQ(ierr);
  PetscFree(s_status1); PetscFree(s_waits1);

  /* Create the submatrices */
  if (scall == MAT_REUSE_MATRIX) {
    for ( i=0; i<ismax; i++ ) {
      mat = (Mat_SeqDense *)(submats[i]->data);
      if ((mat->m != nrow[i]) || (mat->n != ncol[i])) {
        SETERRQ(PETSC_ERR_ARG_SIZ,0,"Cannot reuse matrix. wrong size");
      }
      PetscMemzero(mat->v,mat->m*mat->n*sizeof(Scalar));
      submats[i]->factor = C->factor;
    }
  } else {
    for ( i=0; i<ismax; i++ ) {
      ierr = MatCreateSeqDense(PETSC_COMM_SELF,nrow[i],ncol[i],PETSC_NULL,submats+i);CHKERRQ(ierr);
    }
  }
  
  /* Assemble the matrices */
  {
    int    col;
    Scalar *imat_v,*mat_v,*imat_vi,*mat_vi;
  
    for ( i=0; i<ismax; i++ ) {
      mat       = (Mat_SeqDense *) submats[i]->data;
      mat_v     = a->v;
      imat_v    = mat->v;
      irow_i    = irow[i];
      m         = nrow[i];
      for ( j=0; j<m; j++ ) {
        row      = irow_i[j] ;
        proc     = rtable[row];
        if (proc == rank) {
          row      = row - rstart;
          mat_vi   = mat_v + row;
          imat_vi  = imat_v + j;
          for ( k=0; k<ncol[i]; k++ ) {
            col = icol[i][k];
            imat_vi[k*m] = mat_vi[col*a->m];
          }
        } 
      }
    }
  }

  /* Create row map. This maps c->row to submat->row for each submat*/
  /* this is a very expensive operation wrt memory usage */
  len     = (1+ismax)*sizeof(int *) + ismax*c->M*sizeof(int);
  rmap    = (int **)PetscMalloc(len); CHKPTRQ(rmap);
  rmap[0] = (int *)(rmap + ismax);
  PetscMemzero(rmap[0],ismax*c->M*sizeof(int));
  for ( i=1; i<ismax; i++ ) { rmap[i] = rmap[i-1] + c->M;}
  for ( i=0; i<ismax; i++ ) {
    rmap_i = rmap[i];
    irow_i = irow[i];
    jmax   = nrow[i];
    for ( j=0; j<jmax; j++ ) { 
      rmap_i[irow_i[j]] = j; 
    }
  }
 
  /* Now Receive the row_values and assemble the rest of the matrix */

  r_status2 = (MPI_Status *) PetscMalloc((nrqs+1)*sizeof(MPI_Status));CHKPTRQ(r_status2);

  {
    int    is_max,tmp1,col,*sbuf1_i,is_sz;
    Scalar *rbuf2_i,*imat_v,*imat_vi;
  
    for ( tmp1=0; tmp1<nrqs; tmp1++ ) { /* For each message */
      ierr = MPI_Waitany(nrqs,r_waits2,&i,r_status2+tmp1);CHKERRQ(ierr);
      /* Now dig out the corresponding sbuf1, which contains the IS data_structure */
      sbuf1_i = sbuf1[pa[i]];
      is_max  = sbuf1_i[0];
      ct1     = 2*is_max+1;
      rbuf2_i = rbuf2[i];
      for ( j=1; j<=is_max; j++ ) { /* For each IS belonging to the message */
        is_no     = sbuf1_i[2*j-1];
        is_sz     = sbuf1_i[2*j];
        mat       = (Mat_SeqDense *) submats[is_no]->data;
        imat_v    = mat->v;
        rmap_i    = rmap[is_no];
        m         = nrow[is_no];
        for ( k=0; k<is_sz; k++,rbuf2_i+=N) {  /* For each row */
          row      = sbuf1_i[ct1]; ct1++;
          row      = rmap_i[row];
          imat_vi  = imat_v + row;
          for ( l=0; l<ncol[is_no]; l++ ) { /* For each col */
            col = icol[is_no][l];
            imat_vi[l*m] = rbuf2_i[col];
          }
        }
      }
    }
  }
  /* End Send-Recv of row_values */
  PetscFree(r_status2); PetscFree(r_waits2);
  s_status2 = (MPI_Status *) PetscMalloc((nrqr+1)*sizeof(MPI_Status));CHKPTRQ(s_status1);
  ierr      = MPI_Waitall(nrqr,s_waits2,s_status2); CHKERRQ(ierr);
  PetscFree(s_status2); PetscFree(s_waits2);

  /* Restore the indices */
  for ( i=0; i<ismax; i++ ) {
    ierr = ISRestoreIndices(isrow[i], irow+i); CHKERRQ(ierr);
    ierr = ISRestoreIndices(iscol[i], icol+i); CHKERRQ(ierr);
  }

  /* Destroy allocated memory */
  PetscFree(irow);
  PetscFree(w1);
  PetscFree(pa);


  for ( i=0; i<nrqs; ++i ) {
    PetscFree(rbuf2[i]);
  }
  PetscFree(rbuf2);
  PetscFree(sbuf1);
  PetscFree(rbuf1);

  for ( i=0; i<nrqr; ++i ) {
    PetscFree(sbuf2[i]);
  }

  PetscFree(sbuf2);
  PetscFree(rmap);

  ierr = PetscObjectRestoreNewTag((PetscObject)C,&tag1); CHKERRQ(ierr);

  for ( i=0; i<ismax; i++ ) {
    ierr = MatAssemblyBegin(submats[i], MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
    ierr = MatAssemblyEnd(submats[i], MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
  }

  PetscFunctionReturn(0);
}

