Mem_dh.c

00001 /*@HEADER
00002 // ***********************************************************************
00003 //
00004 //       Ifpack: Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2009) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 */
00029 
00030 #include "Parser_dh.h"
00031 #include "Mem_dh.h"
00032 
00033 /* TODO: error checking is not complete; memRecord_dh  need to
00034          be done in Mem_dhMalloc() and Mem_dhFree()l
00035 */
00036 
00037 
00038   /* a memRecord_dh is pre and post-pended to every 
00039    * piece of memory obtained by calling MALLOC_DH 
00040    */
00041 typedef struct
00042 {
00043   double size;
00044   double cookie;
00045 } memRecord_dh;
00046 
00047 struct _mem_dh
00048 {
00049   double maxMem;        /* max allocated at any point in time */
00050   double curMem;        /* total currently allocated */
00051   double totalMem;      /* total cumulative malloced */
00052   double mallocCount;       /* number of times mem_dh->malloc has been called. */
00053   double freeCount;     /* number of times mem_dh->free has been called. */
00054 };
00055 
00056 
00057 #undef __FUNC__
00058 #define __FUNC__ "Mem_dhCreate"
00059 void
00060 Mem_dhCreate (Mem_dh * m)
00061 {
00062   START_FUNC_DH
00063     struct _mem_dh *tmp =
00064     (struct _mem_dh *) PRIVATE_MALLOC (sizeof (struct _mem_dh));
00065   CHECK_V_ERROR;
00066   *m = tmp;
00067   tmp->maxMem = 0.0;
00068   tmp->curMem = 0.0;
00069   tmp->totalMem = 0.0;
00070   tmp->mallocCount = 0.0;
00071   tmp->freeCount = 0.0;
00072 END_FUNC_DH}
00073 
00074 
00075 #undef __FUNC__
00076 #define __FUNC__ "Mem_dhDestroy"
00077 void
00078 Mem_dhDestroy (Mem_dh m)
00079 {
00080   START_FUNC_DH if (Parser_dhHasSwitch (parser_dh, "-eu_mem"))
00081     {
00082       Mem_dhPrint (m, stdout, false);
00083       CHECK_V_ERROR;
00084     }
00085 
00086   PRIVATE_FREE (m);
00087 END_FUNC_DH}
00088 
00089 
00090 #undef __FUNC__
00091 #define __FUNC__ "Mem_dhMalloc"
00092 void *
00093 Mem_dhMalloc (Mem_dh m, size_t size)
00094 {
00095   START_FUNC_DH_2 void *retval;
00096   memRecord_dh *tmp;
00097   size_t s = size + 2 * sizeof (memRecord_dh);
00098   void *address;
00099 
00100   address = PRIVATE_MALLOC (s);
00101 
00102   if (address == NULL)
00103     {
00104       sprintf (msgBuf_dh,
00105            "PRIVATE_MALLOC failed; totalMem = %g; requested additional = %i",
00106            m->totalMem, (int) s);
00107       SET_ERROR (NULL, msgBuf_dh);
00108     }
00109 
00110   retval = (char *) address + sizeof (memRecord_dh);
00111 
00112   /* we prepend and postpend a private record to the 
00113    * requested chunk of memory; this permits tracking the
00114    * sizes of freed memory, along with other rudimentary
00115    * error checking.  This is modeled after the PETSc code.
00116    */
00117   tmp = (memRecord_dh *) address;
00118   tmp->size = (double) s;
00119 
00120   m->mallocCount += 1;
00121   m->totalMem += (double) s;
00122   m->curMem += (double) s;
00123   m->maxMem = MAX (m->maxMem, m->curMem);
00124 
00125 END_FUNC_VAL_2 (retval)}
00126 
00127 
00128 #undef __FUNC__
00129 #define __FUNC__ "Mem_dhFree"
00130 void
00131 Mem_dhFree (Mem_dh m, void *ptr)
00132 {
00133   START_FUNC_DH_2 double size;
00134   char *tmp = (char *) ptr;
00135   memRecord_dh *rec;
00136   tmp -= sizeof (memRecord_dh);
00137   rec = (memRecord_dh *) tmp;
00138   size = rec->size;
00139 
00140   mem_dh->curMem -= size;
00141   mem_dh->freeCount += 1;
00142 
00143   PRIVATE_FREE (tmp);
00144 END_FUNC_DH_2}
00145 
00146 
00147 #undef __FUNC__
00148 #define __FUNC__ "Mem_dhPrint"
00149 void
00150 Mem_dhPrint (Mem_dh m, FILE * fp, bool allPrint)
00151 {
00152   START_FUNC_DH_2 if (fp == NULL)
00153     SET_V_ERROR ("fp == NULL");
00154   if (myid_dh == 0 || allPrint)
00155     {
00156       double tmp;
00157       fprintf (fp, "---------------------- Euclid memory report (start)\n");
00158       fprintf (fp, "malloc calls = %g\n", m->mallocCount);
00159       fprintf (fp, "free   calls = %g\n", m->freeCount);
00160       fprintf (fp, "curMem          = %g Mbytes (should be zero)\n",
00161            m->curMem / 1000000);
00162       tmp = m->totalMem / 1000000;
00163       fprintf (fp, "total allocated = %g Mbytes\n", tmp);
00164       fprintf (fp,
00165            "max malloc      = %g Mbytes (max allocated at any point in time)\n",
00166            m->maxMem / 1000000);
00167       fprintf (fp, "\n");
00168       fprintf (fp, "---------------------- Euclid memory report (end)\n");
00169     }
00170 END_FUNC_DH_2}

Generated on Wed May 12 21:30:18 2010 for IFPACK by  doxygen 1.4.7