/[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 155 by jgs, Wed Nov 9 02:02:19 2005 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 (c) 2003-2010 by University of Queensland
5    * Earth Systems Science Computational Center (ESSCC)
6    * http://www.uq.edu.au/esscc
7    *
8    * Primary Business: Queensland, Australia
9    * Licensed under the Open Software License version 3.0
10    * 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 15  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(_WIN32) && defined(__INTEL_COMPILER)
35    #include <mathimf.h>
36    #else
37    #include <math.h>
38    #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>
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <string.h>  #include <string.h>
 #include <math.h>  
49    
50  #define LenString_MAX FILENAME_MAX*2  #define LenString_MAX FILENAME_MAX*2
51  #define LenErrorMsg_MAX LenString_MAX  #define LenErrorMsg_MAX LenString_MAX
# Line 36  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    /*#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 54  typedef int err_t; Line 96  typedef int err_t;
96  #define INDEX6(_X1_,_X2_,_X3_,_X4_,_X5_,_X6_,_N1_,_N2_,_N3_,_N4_,_N5_) ((_X1_)+(_N1_)*INDEX5(_X2_,_X3_,_X4_,_X5_,_X6_,_N2_,_N3_,_N4_,_N5_))  #define INDEX6(_X1_,_X2_,_X3_,_X4_,_X5_,_X6_,_N1_,_N2_,_N3_,_N4_,_N5_) ((_X1_)+(_N1_)*INDEX5(_X2_,_X3_,_X4_,_X5_,_X6_,_N2_,_N3_,_N4_,_N5_))
97    
98  #define MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg1_) : (_arg2_))  #define MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg1_) : (_arg2_))
99    #define MAX3(_arg1_,_arg2_,_arg3_) MAX(_arg1_,MAX(_arg2_,_arg3_))
100  #define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg2_) : (_arg1_))  #define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg2_) : (_arg1_))
101    #define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))
102  #define ABS(_arg_) MAX((_arg_),-(_arg_))  #define ABS(_arg_) MAX((_arg_),-(_arg_))
103    #define SIGN(_arg_) ((_arg_)>0 ?  1  : ((_arg_)<0 ? -1 : 0 ))
104    #define SWAP(_a0_,_a1_,_type_) { \
105                                    _type_ s; \
106                                    s=(_a0_); \
107                                    _a0_= (_a1_); \
108                                    _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. */
119    
120      #include <python.h>
121    
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 TMPMEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))    #define PASO_MALLOC malloc
131  #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }    #define PASO_FREE free
132  #ifdef __ECC    #define PASO_REALLOC realloc
133    #define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
134    #define MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }  #endif
135    #ifdef _OPENMP  
136        #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) kmp_malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  #ifndef __const
137        #define THREAD_MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { kmp_free(_PTR_); (_PTR_) = NULL; }  # if (defined __STDC__ && __STDC__)
138    #else  #  define __const   const
139       #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  # else
140       #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)  #  define __const
141    #endif  # endif
142    #endif
143    
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
   #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  
   #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)  
159  #endif  #endif
160    
161  #endif /* #ifndef INC_PASO_COMMON */  
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
177    
178    
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   * $Log$      This was useful for seeing what memory is being allocated if you get an "Out of memory" error
223   * Revision 1.2  2005/09/15 03:44:38  jgs          if used again, bracket with do {} while(0)
224   * Merge of development branch dev-02 back to main trunk on 2005-09-15  #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   * Revision 1.1.2.1  2005/09/05 06:29:46  gross      This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
227   * These files have been extracted from finley to define a stand alone libray for iterative          if used again, bracket with do {} while(0)
228   * linear solvers on the ALTIX. main entry through Paso_solve. this version compiles but  #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   * has not been tested yet.  */
230   *  
231   *  #endif /* #ifndef INC_PASO_COMMON */
  */  

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

  ViewVC Help
Powered by ViewVC 1.1.26