/[escript]/trunk/esysUtils/src/maths.h
ViewVC logotype

Diff of /trunk/esysUtils/src/maths.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/paso/src/Common.h revision 1565 by gross, Thu May 22 10:19:47 2008 UTC branches/domexper/paso/src/Common.h revision 3079 by jfenwick, Tue Aug 3 04:04:51 2010 UTC
# Line 1  Line 1 
1    
 /* $Id$ */  
   
2  /*******************************************************  /*******************************************************
3   *  *
4   *           Copyright 2003-2007 by ACceSS MNRF  * Copyright (c) 2003-2010 by University of Queensland
5   *       Copyright 2007 by University of Queensland  * Earth Systems Science Computational Center (ESSCC)
6   *  * http://www.uq.edu.au/esscc
7   *                http://esscc.uq.edu.au  *
8   *        Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
9   *  Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
10   *     http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
11   *  *
12   *******************************************************/  *******************************************************/
13    
14    
15  #ifndef INC_PASO_COMMON  #ifndef INC_PASO_COMMON
16  #define INC_PASO_COMMON  #define INC_PASO_COMMON
# Line 28  Line 27 
27  /**************************************************************/  /**************************************************************/
28    
29  /* some system values */  /* some system values */
30  #if ( defined __INTEL_COMPILER )  /* FIXME: This is not satisfactory.                                */
31    /* _ECC, __INTEL_COMPILER, and other                               */
32    /* intel compiler pre-defines need to be handled                   */
33    /* (__ICL, __ICC come to mind)                                     */
34    #if defined(_WIN32) && defined(__INTEL_COMPILER)
35  #include <mathimf.h>  #include <mathimf.h>
36  #else  #else
37  #include <math.h>  #include <math.h>
# Line 57  typedef int type_t; Line 60  typedef int type_t;
60  typedef int err_t;  typedef int err_t;
61    
62  #define INDEX_T_MAX INT_MAX  #define INDEX_T_MAX INT_MAX
63    #define INDEX_T_MIN -INT_MAX
64  #define EPSILON DBL_EPSILON  #define EPSILON DBL_EPSILON
65  #define LARGE_POSITIVE_FLOAT DBL_MAX  #define LARGE_POSITIVE_FLOAT DBL_MAX
66    #define SMALL_NEGATIVE_FLOAT -DBL_MAX
67    /*#ifndef NAN
68       #define NAN (0.0/0.0)
69    #endif
70    */
71    /*#define IS_NAN(__VAL__)  ( (__VAL__) == NAN )*/  /* this does not work */
72    /* #define IS_NAN(__VAL__)  ( ! ( ( (__VAL__) >= 0. ) ||  ( (__VAL__) <= 0. ) ) )  this does not work */
73    
74    #ifdef isnan
75      #define IS_NAN(__VAL__) (isnan(__VAL__))
76    #elif defined _isnan
77      #define IS_NAN(__VAL__) (_isnan(__VAL__))
78    #else
79    /* If we do not have isnan then we can't reliably check for NaN - return false */
80      #define IS_NAN(__VAL__) (0)
81    #endif
82    
83  /**************************************************************/  /**************************************************************/
84    
# Line 87  typedef int err_t; Line 107  typedef int err_t;
107                                  _a0_= (_a1_); \                                  _a0_= (_a1_); \
108                                  _a1_=s; \                                  _a1_=s; \
109                                 }                                 }
110    #define XNOR(_a0_,_a1_) ( ( (_a0_) && (_a1_) ) || ( !(_a0_) && !(_a1_) ) )
111  /**************************************************************/  /**************************************************************/
   
112  /*    memory allocation:                                      */  /*    memory allocation:                                      */
113    /*    Wise to not use PASO_MALLOC/FREE/REALLOC and            */
114    /*    PASO_THREAD... directly. These are only for tailoring   */
115    /*    the main macros that follow                             */
116    /**************************************************************/
117    
118  /*  #if defined(_WIN32) /* Use python for memory management on windows. */
     This is useful for seeing what memory is being allocated if you get an "Out of memory" error  
 #define TMPMEMALLOC(_LENGTH_,_TYPE_) (printf("TMPMEMALLOC at %s %d #bytes=%d*%d=%d\n", __FILE__, __LINE__, _LENGTH_, sizeof(_TYPE_), ((size_t)(_LENGTH_) * sizeof(_TYPE_))) , (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)))  
119    
120      This is useful for seeing where memory is being freed if you get an "glibc detected...free" error    #include <python.h>
121  #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { printf("TMPMEMFREE AAA %s %d\n", __FILE__, __LINE__); free(_PTR_); (_PTR_) = NULL; printf("TMPMEMFREE BBB %s %d\n", __FILE__, __LINE__); }  
122  */    #define PASO_MALLOC PyMem_Malloc
123      #define PASO_FREE PyMem_Free
124      #define PASO_REALLOC PyMem_Realloc
125    
126    #else
127    
128      #include <stdlib.h>
129    
130      #define PASO_MALLOC malloc
131      #define PASO_FREE free
132      #define PASO_REALLOC realloc
133    
134    #endif
135    
136    #ifndef __const
137    # if (defined __STDC__ && __STDC__)
138    #  define __const   const
139    # else
140    #  define __const
141    # endif
142    #endif
143    
 #define TMPMEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
 #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }  
 #ifdef __ECC  
   /*#define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)); if( !(_LENGTH_) ) printf( "Trying malloc(0) at %s %d\n", __FILE__, __LINE__ ); //else printf( "malloc(%d) at %s %d\n", _LENGTH_, __FILE__, __LINE__ );*/  
   #define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
   #define MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }  
   #ifdef _OPENMP  
       #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) kmp_malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
       #define THREAD_MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { kmp_free(_PTR_); (_PTR_) = NULL; }  
   #else  
      #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  
      #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)  
   #endif  
   #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_) if( (_POINTER_)!=NULL ){ _POINTER_ = (_TYPE_*)realloc((void*)(_POINTER_),((size_t)(_LENGTH_))*sizeof(_TYPE_) );  }else{ _POINTER_ = (_TYPE_*)malloc( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); }  
144    
145    /* FIXME: This is not satisfactory.                                */
146    /* _ECC, __INTEL_COMPILER, and other                               */
147    /* intel compiler pre-defines need to be handled                   */
148    /* (__ICL, __ICC come to mind)                                     */
149    /* Also, _WIN32 may take this branch one day...                    */
150    /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
151    
152    #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
153      #include <omp.h>
154      #define PASO_THREAD_MALLOC kmp_malloc
155      #define PASO_THREAD_FREE kmp_free
156  #else  #else
157    #define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))    #define PASO_THREAD_MALLOC PASO_MALLOC
158    #define MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }    #define PASO_THREAD_FREE PASO_FREE
159    #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  #endif
160    #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)  
161    #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_) if( (_POINTER_)!=NULL ){ _POINTER_ = (_TYPE_*)realloc((void*)(_POINTER_),((size_t)(_LENGTH_))*sizeof(_TYPE_) );  }else{ _POINTER_ = (_TYPE_*)malloc( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); }  
162    /* Prepare for the day that this becomes sharable. */
163    /* and we wish to do multi-file optimisations on windows */
164    
165    #define PASO_DLL_API
166    
167    #ifdef _WIN32
168    #   ifndef PASO_STATIC_LIB
169    #      undef PASO_DLL_API
170    #      ifdef PASO_EXPORTS
171    #         define PASO_DLL_API __declspec(dllexport)
172    #      else
173    #         define PASO_DLL_API __declspec(dllimport)
174    #      endif
175    #   endif
176  #endif  #endif
177    
178  #define TMPMEMALLOC_AND_PASOCHECK(_LENGTH_,_TYPE_) ( MEMALLOC(_LENGTH_,_TYPE_);  
179    /******************The main macros ************************************/
180    
181    #define MEMALLOC(_LENGTH_,_TYPE_)                                     \
182      (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
183    
184    /* do {} while(0) -  an old trick for bracketing a macro that */
185    /* makes sure a semi-colon does no harm.                      */
186    
187    #define MEMFREE(_PTR_)                                                  \
188    do                                                                      \
189    {                                                                       \
190      if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; }   \
191    } while(0)
192    
193    #define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_)                    \
194    do                                                                        \
195    {                                                                         \
196       if( (_POINTER_)!=NULL )                                                \
197       {                                                                      \
198          _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_),               \
199                                       ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
200       }                                                                      \
201       else                                                                   \
202       {                                                                      \
203          _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
204       }                                                                      \
205    } while(0)
206    
207    #define TMPMEMALLOC MEMALLOC
208    #define TMPMEMFREE MEMFREE
209    #define TMPMEMREALLOC MEMREALLOC
210    
211    #define THREAD_MEMALLOC(_LENGTH_,_TYPE_)                          \
212       (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
213    
214    #define THREAD_MEMFREE(_PTR_)                                                \
215    do                                                                           \
216    {                                                                            \
217      if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
218    } while(0)
219    
220    
221    /*
222        This was useful for seeing what memory is being allocated if you get an "Out of memory" error
223            if used again, bracket with do {} while(0)
224    #define TMPMEMALLOC(_LENGTH_,_TYPE_) (printf("TMPMEMALLOC at %s %d #bytes=%d*%d=%d\n", __FILE__, __LINE__, _LENGTH_, sizeof(_TYPE_), ((size_t)(_LENGTH_) * sizeof(_TYPE_))) , (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)))
225    
226        This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
227            if used again, bracket with do {} while(0)
228    #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { printf("TMPMEMFREE AAA %s %d\n", __FILE__, __LINE__); free(_PTR_); (_PTR_) = NULL; printf("TMPMEMFREE BBB %s %d\n", __FILE__, __LINE__); }
229    */
230    
231  #endif /* #ifndef INC_PASO_COMMON */  #endif /* #ifndef INC_PASO_COMMON */

Legend:
Removed from v.1565  
changed lines
  Added in v.3079

  ViewVC Help
Powered by ViewVC 1.1.26