Subject: Re: [boost] Review of a safer memory management approach for
From: Bartlett, Roscoe A (rabartl_at_[hidden])
Date: 2010-06-04 13:33:01
Fernando and Jeffrey,
A few responses ...
> Message: 6
> Date: Fri, 04 Jun 2010 00:14:25 -0300
> From: Fernando Cacciola <fernando.cacciola_at_[hidden]>
> To: boost_at_[hidden]
> Subject: Re: [boost] Review of a safer memory management approach for
> Message-ID: <hu9r1a$57q$1_at_[hidden]>
> Content-Type: text/plain; charset=UTF-8; format=flowed
> For instance, it's been so long since I last wrote a virtual function
> recently, I made the silly mistake of forgetting that, in a derived
> class, an
> overrided function doesn't have to specify the virtual keyword ;)
[Bartlett, Roscoe A]
High warning levels (i.e. with GCC) catch most such mistakes that the language proper allows to go through.
BTW, are you gravitating to stack-based programming because of the memory management problems or because of problems of change propagation of shared mutable objects? These are almost 100% unrelated issues in my opinion.
Is the C++ boost community really ready to throw away OO programming? That just seems crazy to me.
> Message: 9
> Date: Thu, 03 Jun 2010 21:12:52 -0700
> From: "Jeffrey Lee Hellrung, Jr." <jhellrung_at_[hidden]>
> To: boost_at_[hidden]
> Subject: Re: [boost] FW: Boost Digest, Vol 2929, Issue 4
> Message-ID: <4C087D44.1080909_at_[hidden]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> > As for sharing of objects, it is critical for many types of software
> in the Computational Science and Engineering (CSE) domain. You have to
> share large objects in order to cut down on runtime and storage
> overhead. As one example, consider the Petra object model with the
> Trilinos packages Epetra and Tpetra (http://trilinos.sandia.gov/). In
> the Petra object model, you have objects with significant storage
> overhead like Maps and Graphs that are shared between many different
> objects like Vectors, MultiVectors, and Matrices. Yes, you could
> create deep copies of Maps and Graphs but that would waste a large
> amount of storage. The bottom line is that by sharing large objects
> like Maps and Graphs instead of making large numbers of copies, we can
> fit and solve much larger simulation problems on a computer than we
> could otherwise. You could template and statically allocate everything
> and you will never get around the fundamental need to share the data
> contained in large objects
> > these types of applications.
> > Note, however, that Maps in Epetra/Tpetra are shared as const
> immutable objects. That means that no-one can change a Map after it is
> first created. Therefore, all of the problems with unexpected updates
> goes away (as you mention above). However, the shared Maps must go
> away when they are not needed anymore and that is what the Teuchos::RCP
> class makes easy and robust. Under the covers, Tpetra uses
> Tpetra::ArrayRCP for similar purposes and much more. The situation
> with Graphs is different and issues of change propagation of shared
> objects are still present in some cases.
> > Come to think of it, most of the object sharing that used in the CSE
> software that I use and write mostly just share const immutable objects
> so problems of change propagation mostly goes away. However, there are
> some important use cases where all of the clients are not holding RCPs
> to const objects and the problem of change propagation remains. Again,
> to save in significant runtime and storage overhead, we can't just
> create deep copies of all of these objects. Templating and purely
> stack-based programs are not going to solve that problem.
> > The Teuchos MM approach, I believe, largely solves the memory
> management problems with dynamic memory allocation and object sharing
> while still yielding very high performance and mostly eliminating
> undefined behavior associated with the incorrect usage of memory. The
> most significant contribution of the Teuchos MM approach is the
> presents of Teuchos::Ptr and Teuchos::ArrayView and how they interact
> with the other classes.
> I don't see how shared object *ownership* is necessary in this
> example... :/ I don't think Dave was arguing that holding large
> by reference, or even dynamically allocating objects (large or not), is
> necessarily complex or "bad"...
> Or did I miss something?
[Bartlett, Roscoe A]
Have you read the key sections in the Teuchos MM approach in http://www.cs.sandia.gov/~rabartl/TeuchosMemoryManagementSAND.pdf or are you just responding to the list emails?
You should *never* hold an object by reference as a class data-member (except perhaps in very well encapsulated very low-level code). That is what leads to undefined behavior with dangling references. Early versions of Epetra did just that and it blew up in people's faces. Consider writing a factory method such as:
Epetra_CrsGraph graph(rowMap, colMap);
Epetra_CrsMatrix *matrix = new Epetra_CrsMatrix(graph);
In early Versions of Epetra, maps and graphs were held and references and the above factory function returned an Epetra_CrsMatrix object with now deleted map and graph objects. Later, Epetra was refactored to make Epetra_Map and Epetra_Graph handle classes with internal reference-counted handle/body implementation objects. This fixed the problem but it create lots of new problems including that now copying graphs silently went from deep copy to shallow copy (which broke some code in some rare cases).
The new Tpetra package has gone the down the road of making shared ownership explicit by exposing RCP-wrapped objects making factory functions similar to the above safe and obvious in behavior. It is working very well.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk