text: /*! \file
  Copyright (c) 2003, The Regents of the University of California, through
  Lawrence Berkeley National Laboratory (subject to receipt of any required
  approvals from U.S. Dept. of Energy)
  
  All rights reserved.
  
  The source code is distributed under BSD license, see the file License.txt
  at the top-level directory.
  */
  /*! @file p@(pre)GetDiagU.c
   * \brief Extracts the main diagonal of matrix U
   *
   * <pre>
   * -- Auxiliary routine in distributed SuperLU (version 5.1.0) --
   * Lawrence Berkeley National Lab, Univ. of California Berkeley.
   * Xiaoye S. Li
   * Created:  April 16, 2002
   * Modified: May 15, 2016
   * </pre>
   */
  
  
  #include "superlu_zdefs.h"
  
  /*! \brief
   *
   * <pre>
   * Purpose
   * =======
   *
   * GetDiagU extracts the main diagonal of matrix U of the LU factorization.
   *
   * Arguments
   * =========
   *
   * n        (input) int
   *          Dimension of the matrix.
   *
   * LUstruct (input) LUstruct_t*
   *          The data structures to store the distributed L and U factors.
   *          see superlu_ddefs.h for its definition.
   *
   * grid     (input) gridinfo_t*
   *          The 2D process mesh. It contains the MPI communicator, the number
   *          of process rows (NPROW), the number of process columns (NPCOL),
   *          and my process rank. It is an input argument to all the
   *          parallel routines.
   *
   * diagU    (output) double*, dimension (n)
   *          The main diagonal of matrix U.
   *          On exit, it is available on all processes.
   *
   *
   * Note
   * ====
   *
   * The diagonal blocks of the L and U matrices are stored in the L
   * data structures, and are on the diagonal processes of the
   * 2D process grid.
   *
   * This routine is modified from gather_diag_to_all() in pzgstrs_Bglobal.c.
   * </pre>
   */
  void pzGetDiagU(int_t n, LUstruct_t *LUstruct, gridinfo_t *grid,
                    doublecomplex *diagU)
  {
  
      int_t *xsup;
      int iam, knsupc, pkk;
      int nsupr; /* number of rows in the block L(:,k) (LDA) */
      int_t i, j, jj, k, lk, lwork, nsupers, p;
      int_t num_diag_procs, *diag_procs, *diag_len;
      Glu_persist_t *Glu_persist = LUstruct->Glu_persist;
      LocalLU_t *Llu = LUstruct->Llu;
      doublecomplex *zblock, *zwork, *lusup;
  
      iam = grid->iam;
      nsupers = Glu_persist->supno[n-1] + 1;
      xsup = Glu_persist->xsup;
  
      get_diag_procs(n, Glu_persist, grid, &num_diag_procs,
  		   &diag_procs, &diag_len);
      jj = diag_len[0];
      for (j = 1; j < num_diag_procs; ++j) jj = SUPERLU_MAX( jj, diag_len[j] );
      if ( !(zwork = doublecomplexMalloc_dist(jj)) ) ABORT("Malloc fails for zwork[]");
  
      for (p = 0; p < num_diag_procs; ++p) {
  	pkk = diag_procs[p];
  	if ( iam == pkk ) {
  	    /* Copy diagonal into buffer dwork[]. */
  	    lwork = 0;
  	    for (k = p; k < nsupers; k += num_diag_procs) {
  		knsupc = SuperSize( k );
  		lk = LBj( k, grid );
  		nsupr = Llu->Lrowind_bc_ptr[lk][1]; /* LDA of lusup[] */
  		lusup = Llu->Lnzval_bc_ptr[lk];
  		for (i = 0; i < knsupc; ++i) /* Copy the diagonal. */
  		    zwork[lwork+i] = lusup[i*(nsupr+1)];
  		lwork += knsupc;
  	    }
  	    MPI_Bcast( zwork, lwork, SuperLU_MPI_DOUBLE_COMPLEX, pkk, grid->comm );
  	} else {
  	    MPI_Bcast( zwork, diag_len[p], SuperLU_MPI_DOUBLE_COMPLEX, pkk, grid->comm );
  	}
  
  	/* Scatter zwork[] into global diagU vector. */
  	lwork = 0;
  	for (k = p; k < nsupers; k += num_diag_procs) {
  	    knsupc = SuperSize( k );
  	    zblock = &diagU[FstBlockC( k )];
  	    for (i = 0; i < knsupc; ++i) zblock[i] = zwork[lwork+i];
  	    lwork += knsupc;
  	}
      } /* for p = ... */
  
      SUPERLU_FREE(diag_procs);
      SUPERLU_FREE(diag_len);
      SUPERLU_FREE(zwork);
  }
  