LAM/MPI logo

LAM/MPI General User's Mailing List Archives

  |   Home   |   Download   |   Documentation   |   FAQ   |   all just in this list

From: Brian Barrett (brbarret_at_[hidden])
Date: 2005-05-09 14:15:26


On May 9, 2005, at 11:29 AM, Steve Lowder wrote:

> I’m trying to understand some of the memory allocation issues about
> this code.
>
> When the first two derived types are created, I assume that memory
> is allocated for opaque objects and the handle is stored in the
> variables oneslice and twoslice. If this was inside a subroutine
> and called many times, I would assume I need to explicitly free
> these objects inside the subroutine prior to exit otherwise I have
> small memory leak. Is this correct?
Correct - you need to use MPI_TYPE_FREE() to free created datatypes
or a resource leak will occur.
> When the third type is created (threeslice), does it copy the info
> from the previous type (twoslice) so I could free twoslice or does
> it just keep a reference implying that I can not free twoslice? I
> know that freeing threeslice does no affect twoslice, but what
> would freeing twoslice do to threeslice?

According to the MPI_TYPE_FREE() section of the MPI-1 standard:
"Derived datatypes that were defined from the freed datatype are not
affected". So you can free twoslice without any affect on threeslice.

> Some versions of this code have a MPI_TYPE_FREE of threeslice which
> I think leads people to believe that the free is necessary only
> because of the MPI_TYPE_COMMIT. I don’t know if the COMMIT creates
> more objects but I’m guessing the FREE is necessary first because
> of the initial object creation of threeslice. Is this correct?

MPI_TYPE_COMMIT can cause the MPI to create more internal objects and
the like. But those objects are "owned", if you will, by the
datatype object passed. So you should call TYPE_FREE on a committed
datatype once it's no longer in use. You should also call
MPI_TYPE_FREE on all the other datatypes you created at some point.

For your example, you could MPI_TYPE_FREE oneslice and twoslice
immediately after creating three slice (for readability, I would
probably do it after the MPI_TYPE_COMMIT, but that's just personal
preference). You can then MPI_TYPE_FREE threeslice whenever you
decide you no longer need to use threeslice.
> I’ve read some of the MPI standard and I what I understand from it
> is, if you create it (any derived type), you free it, period. Is
> this correct? I have heard people comment that if this code is
> inside a subroutine then the local variables (handles) will
> automatically mark their respective objects for deallocation on
> exit. I doubt this is true. Yes? No?
You are correct - you create it, you free it. Automatic variables
don't help you here - there's no way for the MPI library to know that
the datatype left scope, so it certainly can't automatically clean up
after you. Most MPI implementations have a fairly huge number of
datatypes that can be created, so that probably helps fuel the myth
that you don't have to worry about it and the MPI does everything for
you.

Hope this helps,

Brian