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

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

  ViewVC Help
Powered by ViewVC 1.1.26