[petsc-users] PETSC address vector c++ access

Matthew Knepley knepley at gmail.com
Fri Nov 30 10:53:57 CST 2018


On Fri, Nov 30, 2018 at 9:50 AM RAELI ALICE via petsc-users <
petsc-users at mcs.anl.gov> wrote:

> Hi All,
> My team is working on a PETSC version of an existent code.
> In order to convert the main part of this work retaining the c++ levels of
> abstraction,
> we would access to c++ vector data structures easily.
>
> We would like to use the memory area allocated using c++ as a Petsc Vector
>
> a supposed pseudocode could be:
>
>
>
>
> *int data[] = { 1,2,3,4,5,6,7,8,9 };unsigned int sizeVectorPetsC =
> data.size();Vec v(data, sizeVectorPetsC); (Can it exists with easy access
> petsc routines?)*
> *cout<< "The mapped vector v is: [";*
>
> *    PetscScalar *vecArray;*
>
>
>
>
>
>
> *PetscGetArray(v, &array);for (unsigned int i = 0; i < sizeVectorPetsC;
> i++){Informations of v are the informations of data.(same memory read by
> both data structures)}PetscRestoreArray(v,&array);*
> * cout<< "]"<< endl;*
>
>
> Is it provided this kind of duality between c++ and Petsc objects without
> a re-copy of the concerned informations?
>

Yes, I think you want


https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecCreateSeqWithArray.html

https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecCreateMPIWithArray.html#VecCreateMPIWithArray

and you can even do advanced things with the storage


https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Vec/VecPlaceArray.html#VecPlaceArray

but you need to be more careful.

Let me know if this does not do what you wanted.

  Thanks,

     Matt


> Thank you,
> Alice
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.cse.buffalo.edu/~knepley/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20181130/fc4e922c/attachment.html>


More information about the petsc-users mailing list