MemoryManager
=============

James Spencer, CUC3, University of Cambridge.

Memory manager provides book-keeping routines for logging memory usage in
Fortran programs.  This is useful to track where allocations and deallocations
occur and to know the peak memory usage.

MemoryManager contains a few ideas from the initialisation, output
and structure of the memory_manager module from CamCASP (formerly SITUS), 
written by Alston Misquitta, with permission.

Usage
-----

Memory usage can be logged in one of two ways::

#. Store everything.  The size of the log (MaxLen) had best be suitably large.
#. Store active allocations.  When the top most slot in use in the log is
   deallocated, free up all the slots at the top of the log that have been
   deallocated for logging later actions (i.e. psuedo-LIFO [last in, first
   out]).  This does allow a fractured log to occur, but memory
   allocation-deallocation is often LIFO and it allows us to be efficient in
   storing the actions without wasting much effort searching the log.

This behaviour is controlled by the CachingMemLog flag: if CachingMemLog is
true then a psuedo-LIFO cache is used to log the memory allocations and
deallocations.  Additional behaviour flags are in the configuration section
of the module (see source).

An integer "tag" is required for each array as this makes searching
the log trivial (and fast).

See individual routines for more details and optional arguments for
error checking the allocation/deallocation.

#. Initialise the logger::

     call InitMemoryManager(MemSize) 

   where:
     * MemSize (optional): max amount of memory available in MB.
       if MemSize is not given, than a default value (obtained via C
       preprocessing) is used.  This default value is set using the _MAXMEM
       macro (convenient for use on different platforms).  _MAXMEM is set to
       2GB if it is not otherwise specified.  A warning is printed if the
       memory limit is exceeded.

#. For each array to be logged, define an integer "tag" with an initial value of 0::

     real(8), allocatable :: testarr(:)
     integer :: tag_testarr=0

   Make sure you save the tag (and the array!) if used for an array stored in a 
   module which can go out of scope between allocation and deallocation.

#. At allocation, call the allocation logger.  For instance::

     allocate(testarr(N))
     call LogMemAlloc('testarr',N,8,'allocating_routine',tag_testarr)

   where:

     * 'testarr' is the name of the array being logged;
     *  N is the size of the array;
     *  8 is the number of bytes per element in the array;
     *  'allocating_routine' is the routine in which the allocation is performed;
     *  tag_testarr is the "tag" of the array being logged.

#. At deallocation, call the deallocation logger::

     deallocate(testarr)
     call LogMemDealloc('deallocating_routine',tag_testarr)

   where:

     * 'deallocating_routine' is the routine in which the deallocation is performed.

#. At the end of the calculation, print out memory usage::

     call LeaveMemoryManager()

   This causes a statement like the following to be printed out::

     ================================================================
     Memory usage
     Maximum memory defined is (MB) :    1024.0
     Maximum memory used is    (MB) :     381.5
     
     Large memory allocations:
     
     Name              Allocated in       Deallocated in         Size
     ----------------------------------------------------------------
     arr3              subroutine2        subroutine2         381.5MB
     arr2              subroutine1        subroutine2         156.3KB
     arr1              subroutine2        not deallocated      39.1KB
     arr1              subroutine1        subroutine1          39.1KB
     arr1              subroutine1        subroutine1          39.1KB
     ================================================================

Licence
-------

Essentially you can do whatever you want with the code.
Formally it's subject to the MIT licence::

    Copyright (c) 2009 James Spencer.

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
