IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends
Mem_dh.c
00001 /*@HEADER
00002 // ***********************************************************************
00003 //
00004 //       Ifpack: Object-Oriented Algebraic Preconditioner Package
00005 //                 Copyright (2002) 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 */
00042 
00043 #include "Parser_dh.h"
00044 #include "Mem_dh.h"
00045 
00046 /* TODO: error checking is not complete; memRecord_dh  need to
00047          be done in Mem_dhMalloc() and Mem_dhFree()l
00048 */
00049 
00050 
00051   /* a memRecord_dh is pre and post-pended to every 
00052    * piece of memory obtained by calling MALLOC_DH 
00053    */
00054 typedef struct
00055 {
00056   double size;
00057   double cookie;
00058 } memRecord_dh;
00059 
00060 struct _mem_dh
00061 {
00062   double maxMem;        /* max allocated at any point in time */
00063   double curMem;        /* total currently allocated */
00064   double totalMem;      /* total cumulative malloced */
00065   double mallocCount;       /* number of times mem_dh->malloc has been called. */
00066   double freeCount;     /* number of times mem_dh->free has been called. */
00067 };
00068 
00069 
00070 #undef __FUNC__
00071 #define __FUNC__ "Mem_dhCreate"
00072 void
00073 Mem_dhCreate (Mem_dh * m)
00074 {
00075   START_FUNC_DH
00076     struct _mem_dh *tmp =
00077     (struct _mem_dh *) PRIVATE_MALLOC (sizeof (struct _mem_dh));
00078   CHECK_V_ERROR;
00079   *m = tmp;
00080   tmp->maxMem = 0.0;
00081   tmp->curMem = 0.0;
00082   tmp->totalMem = 0.0;
00083   tmp->mallocCount = 0.0;
00084   tmp->freeCount = 0.0;
00085 END_FUNC_DH}
00086 
00087 
00088 #undef __FUNC__
00089 #define __FUNC__ "Mem_dhDestroy"
00090 void
00091 Mem_dhDestroy (Mem_dh m)
00092 {
00093   START_FUNC_DH if (Parser_dhHasSwitch (parser_dh, "-eu_mem"))
00094     {
00095       Mem_dhPrint (m, stdout, false);
00096       CHECK_V_ERROR;
00097     }
00098 
00099   PRIVATE_FREE (m);
00100 END_FUNC_DH}
00101 
00102 
00103 #undef __FUNC__
00104 #define __FUNC__ "Mem_dhMalloc"
00105 void *
00106 Mem_dhMalloc (Mem_dh m, size_t size)
00107 {
00108   START_FUNC_DH_2 void *retval;
00109   memRecord_dh *tmp;
00110   size_t s = size + 2 * sizeof (memRecord_dh);
00111   void *address;
00112 
00113   address = PRIVATE_MALLOC (s);
00114 
00115   if (address == NULL)
00116     {
00117       sprintf (msgBuf_dh,
00118            "PRIVATE_MALLOC failed; totalMem = %g; requested additional = %i",
00119            m->totalMem, (int) s);
00120       SET_ERROR (NULL, msgBuf_dh);
00121     }
00122 
00123   retval = (char *) address + sizeof (memRecord_dh);
00124 
00125   /* we prepend and postpend a private record to the 
00126    * requested chunk of memory; this permits tracking the
00127    * sizes of freed memory, along with other rudimentary
00128    * error checking.  This is modeled after the PETSc code.
00129    */
00130   tmp = (memRecord_dh *) address;
00131   tmp->size = (double) s;
00132 
00133   m->mallocCount += 1;
00134   m->totalMem += (double) s;
00135   m->curMem += (double) s;
00136   m->maxMem = MAX (m->maxMem, m->curMem);
00137 
00138 END_FUNC_VAL_2 (retval)}
00139 
00140 
00141 #undef __FUNC__
00142 #define __FUNC__ "Mem_dhFree"
00143 void
00144 Mem_dhFree (Mem_dh m, void *ptr)
00145 {
00146   START_FUNC_DH_2 double size;
00147   char *tmp = (char *) ptr;
00148   memRecord_dh *rec;
00149   tmp -= sizeof (memRecord_dh);
00150   rec = (memRecord_dh *) tmp;
00151   size = rec->size;
00152 
00153   mem_dh->curMem -= size;
00154   mem_dh->freeCount += 1;
00155 
00156   PRIVATE_FREE (tmp);
00157 END_FUNC_DH_2}
00158 
00159 
00160 #undef __FUNC__
00161 #define __FUNC__ "Mem_dhPrint"
00162 void
00163 Mem_dhPrint (Mem_dh m, FILE * fp, bool allPrint)
00164 {
00165   START_FUNC_DH_2 if (fp == NULL)
00166     SET_V_ERROR ("fp == NULL");
00167   if (myid_dh == 0 || allPrint)
00168     {
00169       double tmp;
00170       fprintf (fp, "---------------------- Euclid memory report (start)\n");
00171       fprintf (fp, "malloc calls = %g\n", m->mallocCount);
00172       fprintf (fp, "free   calls = %g\n", m->freeCount);
00173       fprintf (fp, "curMem          = %g Mbytes (should be zero)\n",
00174            m->curMem / 1000000);
00175       tmp = m->totalMem / 1000000;
00176       fprintf (fp, "total allocated = %g Mbytes\n", tmp);
00177       fprintf (fp,
00178            "max malloc      = %g Mbytes (max allocated at any point in time)\n",
00179            m->maxMem / 1000000);
00180       fprintf (fp, "\n");
00181       fprintf (fp, "---------------------- Euclid memory report (end)\n");
00182     }
00183 END_FUNC_DH_2}
 All Classes Files Functions Variables Enumerations Friends