[petsc-users] VecGet/RestoreArray and related functions

Chetan Jhurani chetan.jhurani at gmail.com
Thu Dec 1 17:06:23 CST 2011


You may want to write a class, say petsc_init_uninit, whose constructor

initializes petsc and whose destructor finalizes it.  Then declare an object

of class petsc_init_uninit in main.  This will solve the first issue.

 

http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.2

 

Chetan

 

From: petsc-users-bounces at mcs.anl.gov [mailto:petsc-users-bounces at mcs.anl.gov] On Behalf Of Mohammad Mirzadeh
Sent: Thursday, December 01, 2011 5:48 PM
To: PETSc users list
Subject: [petsc-users] VecGet/RestoreArray and related functions

 

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

 

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

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20111201/9db53cf2/attachment.htm>


More information about the petsc-users mailing list