|
[Sponsors] |
July 5, 2012, 08:26 |
understanding autoPtr
|
#1 |
Senior Member
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18 |
in the solver rhoCentralFoam there is a piece of code:
Code:
autoPtr<basicPsiThermo> pThermo ( basicPsiThermo::New(mesh) ); basicPsiThermo& thermo = pThermo(); |
|
July 10, 2012, 05:42 |
|
#2 |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
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 |
|
July 10, 2012, 16:39 |
|
#3 |
Senior Member
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18 |
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 |
|
July 10, 2012, 16:46 |
|
#4 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
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. |
||
July 11, 2012, 11:51 |
|
#5 |
Senior Member
Mahdi Hosseinali
Join Date: Apr 2009
Location: NB, Canada
Posts: 273
Rep Power: 18 |
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 :-" ) |
|
July 12, 2012, 03:43 |
|
#6 | |
Senior Member
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 284
Blog Entries: 5
Rep Power: 21 |
Quote:
|
||
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Problems in understanding BuoyantBoussinesqSimpleFoam | Anne Lincke | OpenFOAM | 9 | June 8, 2019 05:27 |
autoPtr variable becomes invalid | djpiro | OpenFOAM Programming & Development | 7 | February 1, 2013 13:18 |
Lookup autoPtr from fvMesh | Misha | OpenFOAM | 2 | January 24, 2011 06:20 |
[snappyHexMesh] basic understanding of sHM | colinB | OpenFOAM Meshing & Mesh Conversion | 1 | October 6, 2010 08:17 |
Understanding of channelOodles and oodles solver | fs82 | OpenFOAM | 1 | September 23, 2009 10:36 |