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

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

  ViewVC Help
Powered by ViewVC 1.1.26