/[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

revision 1361 by gross, Fri Dec 14 09:26:51 2007 UTC revision 1628 by phornby, Fri Jul 11 13:12:46 2008 UTC
# Line 28  Line 28 
28  /**************************************************************/  /**************************************************************/
29    
30  /* some system values */  /* some system values */
31    /* FIXME: This is not satisfactory.                                */
32    /* _ECC, __INTEL_COMPILER, and other                               */
33    /* intel compiler pre-defines need to be handled                   */
34    /* (__ICL, __ICC come to mind)                                     */
35  #if ( defined __INTEL_COMPILER )  #if ( defined __INTEL_COMPILER )
36  #include <mathimf.h>  #include <mathimf.h>
37  #else  #else
38  #include <math.h>  #include <math.h>
39  #endif  #endif
40    
41    #if (defined __OPENMP)
42    #include <omp.h>
43    #endif
44    
45  #include <float.h>  #include <float.h>
46  #include <stdio.h>  #include <stdio.h>
47  #include <limits.h>  #include <limits.h>
# Line 54  typedef int err_t; Line 62  typedef int err_t;
62    
63  #define INDEX_T_MAX INT_MAX  #define INDEX_T_MAX INT_MAX
64  #define EPSILON DBL_EPSILON  #define EPSILON DBL_EPSILON
65    #define LARGE_POSITIVE_FLOAT DBL_MAX
66    
67  /**************************************************************/  /**************************************************************/
68    
# Line 76  typedef int err_t; Line 85  typedef int err_t;
85  #define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))  #define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))
86  #define ABS(_arg_) MAX((_arg_),-(_arg_))  #define ABS(_arg_) MAX((_arg_),-(_arg_))
87  #define SIGN(_arg_) ((_arg_)>0 ?  1  : ((_arg_)<0 ? -1 : 0 ))  #define SIGN(_arg_) ((_arg_)>0 ?  1  : ((_arg_)<0 ? -1 : 0 ))
88    #define SWAP(_a0_,_a1_,_type_) { \
89                                    _type_ s; \
90                                    s=(_a0_); \
91                                    _a0_= (_a1_); \
92                                    _a1_=s; \
93                                   }
94  /**************************************************************/  /**************************************************************/
   
95  /*    memory allocation:                                      */  /*    memory allocation:                                      */
96    /*    Wise to not use PASO_MALLOC/FREE/REALLOC and            */
97    /*    PASO_THREAD... directly. These are only for tailoring   */
98    /*    the main macros that follow                             */
99    /**************************************************************/
100    
101  /*  #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_)))  
102    
103      This is useful for seeing where memory is being freed if you get an "glibc detected...free" error    #include <python.h>
104  #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__); }  
105  */    #define PASO_MALLOC PyMem_Malloc
106      #define PASO_FREE PyMem_Free
107      #define PASO_REALLOC PyMem_Realloc
108    
109    #else
110    
111      #include <malloc.h>
112    
113      #define PASO_MALLOC malloc
114      #define PASO_FREE free
115      #define PASO_REALLOC realloc
116    
117    #endif
118    
 #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_) ); }  
119    
120    /* FIXME: This is not satisfactory.                                */
121    /* _ECC, __INTEL_COMPILER, and other                               */
122    /* intel compiler pre-defines need to be handled                   */
123    /* (__ICL, __ICC come to mind)                                     */
124    /* Also, _WIN32 may take this branch one day...                    */
125    /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
126    
127    #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
128      #include <omp.h>
129      #define PASO_THREAD_MALLOC kmp_malloc
130      #define PASO_THREAD_FREE kmp_free
131  #else  #else
132    #define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))    #define PASO_THREAD_MALLOC PASO_MALLOC
133    #define MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }    #define PASO_THREAD_FREE PASO_FREE
   #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  
   #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)  
   #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_) ); }  
134  #endif  #endif
135    
136    /******************The main macros ************************************/
137    
138    #define MEMALLOC(_LENGTH_,_TYPE_)                                     \
139      (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
140    
141    /* do {} while(0) -  an old trick for bracketing a macro that */
142    /* makes sure a semi-colon does no harm.                      */
143    
144    #define MEMFREE(_PTR_)                                                  \
145    do                                                                      \
146    {                                                                       \
147      if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; }   \
148    } while(0)
149    
150    #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_)                             \
151    do                                                                        \
152    {                                                                         \
153       if( (_POINTER_)!=NULL )                                                \
154       {                                                                      \
155          _POINTER_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_),               \
156                                       ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
157       }                                                                      \
158       else                                                                   \
159       {                                                                      \
160          _POINTER_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
161       }                                                                      \
162    } while(0)
163    
164    #define TMPMEMALLOC MEMALLOC
165    #define TMPMEMFREE MEMFREE
166    
167    #define THREAD_MEMALLOC(_LENGTH_,_TYPE_)                          \
168       (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
169    
170    #define THREAD_MEMFREE(_PTR_)                                                \
171    do                                                                           \
172    {                                                                            \
173      if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
174    } while(0)
175    
176    
177    /*
178        This was useful for seeing what memory is being allocated if you get an "Out of memory" error
179            if used again, bracket with do {} while(0)
180    #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_)))
181    
182        This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
183            if used again, bracket with do {} while(0)
184    #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__); }
185    */
186    
187  #endif /* #ifndef INC_PASO_COMMON */  #endif /* #ifndef INC_PASO_COMMON */

Legend:
Removed from v.1361  
changed lines
  Added in v.1628

  ViewVC Help
Powered by ViewVC 1.1.26