[petsc-users] VecGet/RestoreArray and related functions

Matthew Knepley knepley at gmail.com
Thu Dec 1 16:54:35 CST 2011


On Thu, Dec 1, 2011 at 4:48 PM, Mohammad Mirzadeh <mirzadeh at gmail.com>wrote:

> Hi guys,
>
> I am having a conceptual problem trying to design my vector wrappers for
> my application. The problem is some of vector functionality in PETSc
> require extra function calls which makes it hard to design a good wrapper.
> One example that I encountered was when trying to write the destructor for
> my vector object. You see, for example, the following code is not good
> enough
>

1) We asked before, what could you possibly gain by writing a wrapper?

2) If you insist on doing this, register PetscFinalize() with atexit().

   Matt


> Vector::~Vector(){
> VecDestroy(v_local);
> }
>
> This, in itself wont work since all calls to VecDestroy() should be made
> before PetscFinalize() and this wont happen if you define a Vector in your
> main function since the destructor is called upon exit ... So, I had to fix
> this by actually having a destructor function memer that I could call
> explicitly before PetscFinalize() or create the Vector object on the heap
> so that I can manually invoke the destructor via delete operator!
>
> Another example I'm struggling with is the following: I am trying to write
> accessors so that I can do the following:
>
> Vector a (PETSC_COMM_WORLD, PETSC_DECIDE,10); // make a petsc vector of
> global length 10 using my own wrappers
> for (int i=0; i<a.GetLocalSize(); i++){
> a(i) = i;
> }
>
> For this to work, I am using the VecGetArray() and VecRestoreArray()
> functions. The problem is you cannot easily use them inside the accessor
> which basically looks something like:
>
> inline double& Vector::operator()(int i){
> return ptr_to_dataArray[i];
> }
>
> since you need to call to VecRestoreArray after the assignment which means
> after return!. The simple fix to this is to call the VecGetArray() and
> VecRestoreArray() in the main function where you are putting in the values
> but this does not look very neat to me ... so I ended up putting the
> VecGetArray() inside the constructor and VecRestoreArray() inside the
> destructor to automate the process.
>
> Now I have the following two questions. 1) Is this safe? As far as I could
> understand from the manual, VecGetArray() "returns" a pointer to internal
> data stored on this processor. Then I guess VecRestoreArray() is there to
> somehow nullify the pointer to prevent accidental use of pointer? Since all
> pointers all private members of my class I assume this is safe then unless
> these two functions are actually doing some extra work?
>
> 2) Is there any better way of writing these wrappers and get around the
> problem of having to call "supplemental" functions safely?
>
> Sorry if my lack of knowledge is causing all these confusion.
>
> Thanks,
> Mohammad
>
>
>


-- 
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20111201/1110a18f/attachment.htm>


More information about the petsc-users mailing list