[petsc-users] Getting a local vector with ghosts knowing ghost indices

Praveen C cpraveen at gmail.com
Sun Jan 29 03:45:03 CST 2017


Dear all

In my problem, I know the ghost vertices in each partition since I have
used metis to partition my unstructured grid. I want to get a vector ul
with ghosts, from a global vector ug.

I will use ul to assemble my non-linear rhs vector.

Is the following approach optimal for this purpose ?

PetscInt nvar; // number of variables at each vertex
PetscInt nvl; // number of local vertices in this partition
PetscInt nvg; // number of ghost vertices for this partition
PetscInt *vghost; // global index of ghost vertices for this partition
PetscReal **u; // size u[nvl+nvg][nvar]
Vec ul; // vector with ghosts
Vec ug; // global vector without ghosts

   VecCreate(PETSC_COMM_WORLD, &ug);
   VecSetSizes(ug, nvar*nvl, PETSC_DECIDE);
   VecSetFromOptions(ug);

   VecCreateGhostBlockWithArray(PETSC_COMM_WORLD, nvar, nvar*nvl,
                                       PETSC_DECIDE, nvg, vghost,
                                       &u, ul);

// Following would be inside RHSFunction
   double **array;
   VecGetArray2d(ug, nvl, nvar, 0, 0, &array);
   for(unsigned int i=0; i<nvl; ++i)
      for(unsigned int j=0; j<nvar; ++j)
         u[i][j] = array[i][j];
   VecRestoreArray2d(ug, nvl, nvar, 0, 0, &array);

   // Fill ghost values
   VecGhostUpdateBegin(ul, INSERT_VALUES, SCATTER_FORWARD);
   VecGhostUpdateEnd  (ul, INSERT_VALUES, SCATTER_FORWARD);

// now use u[][] to compute local part of rhs vector

Thanks
praveen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20170129/7bdb2cd1/attachment.html>


More information about the petsc-users mailing list