CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   understanding autoPtr (http://www.cfd-online.com/Forums/openfoam-programming-development/104220-understanding-autoptr.html)

anishtain4 July 5, 2012 08:26

understanding autoPtr
 
in the solver rhoCentralFoam there is a piece of code:

Code:

autoPtr<basicPsiThermo> pThermo
(
    basicPsiThermo::New(mesh)
);
basicPsiThermo& thermo = pThermo();

I think that I understand basicPsiThermo but what is this doing? What is in autoPtr pointer that our thermo should be made by it?

tomislav_maric July 10, 2012 05:42

autoPtr and tmp are OpenFOAM implementations of a Concept called "smart pointer".

These types are used when a function needs to return an actual object (not its copy). In C++, the return statement (if the return is done by value) will envoke the types copy constructor and create a copy of the data. Imagine having a function where you initialize a volScalarField of 10^6 doubles and then copying this in order to return it.

Nowadays, modern compilers support something called NRVO and RVO (named return value optimization, and unamed -//-) which enables the compiler to recognize that you are returning an automatic object (local one) and the copy is not done.

You can make an easy experiment: derive infoField<Type> from Field<Type> and put "Info" statements in the constructors, destructor and assignment operator. Try returning an infoField object from a function, of considerable size (watch out for your memory constraints). You should not see any copying, even if the return is done by value.

The paradigm behind autoPtr and tmp is called RAII : resource acquisition is initialization, which is a complex way of saying:

if I wrap my newly allocated piece of data in a proper class with a destructor, it will get cleaned up automatically, as soon as the destructor of this class is called.

Smart pointers have smart destructors and special copy semantics: copy by value is a transfer of ownership, and the destructor of tmp for example won't kill the object if it is being referenced by some other smart pointer: this is why Field<Type> inherits from refCount, a reference counter. This is why if you want to use tmp<T> with your class, you need to inherit from refCount.

Besides, there is a great explanation of tmp by David Gaden (marupio) on the wiki:
http://openfoamwiki.net/index.php/OpenFOAM_guide/tmp

So, in your case, the autoPtr will be constructed from the newly allocated basicPsiThermo. The New selector is there to support Run Time selection, you can read about this here:

http://openfoamwiki.net/index.php/Op...tion_mechanism

anishtain4 July 10, 2012 16:39

Thank you Tomislav, it was a really great help, but I have two more questions.
1. If you don't use this pointers and programming tricks what actually happens is that your program goes unoptimized, but not wrong. right? because I'm a M.Sc. student and I don't have enough time to learn them for doing my thesis.
2. Is there a good programming guide which describes these kinda of things? something better than the official guide

tomislav_maric July 10, 2012 16:46

Quote:

Originally Posted by anishtain4 (Post 370696)
Thank you Tomislav, it was a really great help, but I have two more questions.
1. If you don't use this pointers and programming tricks what actually happens is that your program goes unoptimized, but not wrong. right? because I'm a M.Sc. student and I don't have enough time to learn them for doing my thesis.
2. Is there a good programming guide which describes these kinda of things? something better than the official guide


1. Oh, how I remember how this feels like. If you go into OpenFOAM, even after your masters degree, there will never be enough time, that's my experience anyway. ;)

Use smart pointers when you need to access any large data from within a body of a function. Also, use smart pointers instead of ordinary pointers in order to use RAII to do the memory cleanup for you.

2. There's a great sentence coming from Star Wars: Not (pause) from a jedi.

You won't learn this from OF documentation, get yourself a C++ book. Take Scott Meyers, Effective C++ for example. If its too advanced, go through a short book on C++ for Scientists and Engineers (search on amazon,there's plenty of those).

If you don't have time for that now, don't sweat it: do the best you can, make it work. There is an 80-20 rule in software engineering: a 20% of your code will take 80% of your time and resources. Optimize at the end, make it run first.

anishtain4 July 11, 2012 11:51

Yeah, I should first try to run the code. maybe in between of my M.Sc. and starting PhD. I had a little time to read the advanced topics in c++.
btw, darmstadt is a really good university, I will for sure apply for it this year (of course if I manage to finish my thesis :-" )

tomislav_maric July 12, 2012 03:43

Quote:

Originally Posted by anishtain4 (Post 370874)
Yeah, I should first try to run the code. maybe in between of my M.Sc. and starting PhD. I had a little time to read the advanced topics in c++.
btw, darmstadt is a really good university, I will for sure apply for it this year (of course if I manage to finish my thesis :-" )

You'll manage, I'm sure. :) "Just keep swimming."


All times are GMT -4. The time now is 00:32.