CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

understanding autoPtr

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree10Likes
  • 9 Post By tomislav_maric
  • 1 Post By tomislav_maric

Reply
 
LinkBack Thread Tools Display Modes
Old   July 5, 2012, 08:26
Default understanding autoPtr
  #1
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Posts: 124
Rep Power: 8
anishtain4 is on a distinguished road
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?
anishtain4 is offline   Reply With Quote

Old   July 10, 2012, 05:42
Default
  #2
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 260
Blog Entries: 5
Rep Power: 11
tomislav_maric is on a distinguished road
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
tomislav_maric is offline   Reply With Quote

Old   July 10, 2012, 16:39
Default
  #3
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Posts: 124
Rep Power: 8
anishtain4 is on a distinguished road
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
anishtain4 is offline   Reply With Quote

Old   July 10, 2012, 16:46
Default
  #4
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 260
Blog Entries: 5
Rep Power: 11
tomislav_maric is on a distinguished road
Quote:
Originally Posted by anishtain4 View Post
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.
tomislav_maric is offline   Reply With Quote

Old   July 11, 2012, 11:51
Default
  #5
Senior Member
 
Mahdi Hosseinali
Join Date: Apr 2009
Posts: 124
Rep Power: 8
anishtain4 is on a distinguished road
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 :-" )
anishtain4 is offline   Reply With Quote

Old   July 12, 2012, 03:43
Default
  #6
Senior Member
 
Tomislav Maric
Join Date: Mar 2009
Location: Darmstadt, Germany
Posts: 260
Blog Entries: 5
Rep Power: 11
tomislav_maric is on a distinguished road
Quote:
Originally Posted by anishtain4 View Post
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."
anishtain4 likes this.
tomislav_maric is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
autoPtr variable becomes invalid djpiro OpenFOAM Programming & Development 7 February 1, 2013 14:18
Problems in understanding BuoyantBoussinesqSimpleFoam Anne Lincke OpenFOAM 8 September 14, 2011 04:16
Lookup autoPtr from fvMesh Misha OpenFOAM 2 January 24, 2011 07:20
basic understanding of sHM colinB OpenFOAM Native Meshers: snappyHexMesh and Others 1 October 6, 2010 08:17
Understanding of channelOodles and oodles solver fs82 OpenFOAM 1 September 23, 2009 10:36


All times are GMT -4. The time now is 08:45.