[petsc-users] VecRestoreSubVector can't change value of original vector

Jed Brown jedbrown at mcs.anl.gov
Wed Oct 2 08:04:56 CDT 2013


mailinglist <mailinglistxf at yahoo.fr> writes:

> Hello,
>
> I have a vector(v). I use VecRestoreSubVector to get a sub vector(subv). 
> I change the value of the sub vector and want to give the new value back 
> to v using VecRestoreSubVector, but it only works for seqential 
> case(mpirun -np 1 ...). 

The interface is supposed to give you read access, but not currently
write access.  The interface doesn't say whether the values are copied.
Your subvector is not contiguous so it can't be done "in-place".  You
should probably use VecScatter for this purpose.

> This is my code. Thank you very much.
>
> #include "petsc.h"
> #include "mpi.h"
>
> int main(int argc, char** argv){
>
>    PetscInitialize(&argc,&argv,0,0);
>
>    int commSize;
>    int commRank;
>    MPI_Comm_rank(MPI_COMM_WORLD,&commRank);
>    MPI_Comm_size(MPI_COMM_WORLD,&commSize);
>
>    Vec v;
>    VecCreateMPI(PETSC_COMM_WORLD,5,5*commSize,&v);
>    VecSet(v,0.0);
>    VecAssemblyBegin(v);
>    VecAssemblyEnd(v);
>
>    // is
>    IS is;
>    int index[1], Nindex = 1;
>    index[0] = 0;
> ISCreateGeneral(PETSC_COMM_WORLD,Nindex,index,PETSC_COPY_VALUES,&is);
>
>    Vec vsub;
>    VecGetSubVector(v,is,&vsub);
>    VecSet(vsub,1);
>    VecAssemblyBegin(vsub);
>    VecAssemblyEnd(vsub);
>
>    // VecView(vsub,PETSC_VIEWER_STDOUT_WORLD);
>
>    VecRestoreSubVector(v,is,&vsub);
>    // VecAssemblyBegin(v);
>    // VecAssemblyEnd(v);
>
>    VecView(v,PETSC_VIEWER_STDOUT_WORLD);
>
>    PetscFinalize();
>
>    return 0;
> }
>
> Regards,
>
> fxing
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20131002/27abda5a/attachment-0001.pgp>


More information about the petsc-users mailing list