~vcs-imports/escript-finley/trunk

« back to all changes in this revision

Viewing changes to paso/src/Common.h

  • Committer: jfenwick
  • Date: 2010-10-11 01:48:14 UTC
  • Revision ID: svn-v4:77569008-7704-0410-b7a0-a92fef0b09fd:trunk:3259
Merging dudley and scons updates from branches

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
/**************************************************************/
28
28
 
29
 
/* 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
 
29
#include "esysUtils/maths.h"
 
30
#include "esysUtils/mem.h"
 
31
#include "esysUtils/index.h"
 
32
#include "esysUtils/types.h"
39
33
 
40
 
#if (defined __OPENMP)
41
 
#include <omp.h>
42
 
#endif
43
34
 
44
35
#include <float.h>
45
36
#include <stdio.h>
49
40
 
50
41
#define LenString_MAX FILENAME_MAX*2
51
42
#define LenErrorMsg_MAX LenString_MAX
52
 
 
53
 
/* on some arcitectures it could be a good idea to use long rather than int */
54
 
/* this has not really been tested */
55
 
 
56
 
typedef int dim_t;
57
 
typedef int index_t;
58
 
typedef int bool_t;
59
 
typedef int type_t;
60
 
typedef int err_t;
61
 
 
62
 
#define INDEX_T_MAX INT_MAX
63
 
#define INDEX_T_MIN -INT_MAX
64
 
#define EPSILON DBL_EPSILON
65
 
#define LARGE_POSITIVE_FLOAT DBL_MAX
66
 
#define SMALL_NEGATIVE_FLOAT -DBL_MAX
67
 
 
68
 
/*#ifndef NAN
69
 
   #define NAN (0.0/0.0)
70
 
#endif
71
 
*/
72
 
/*#define IS_NAN(__VAL__)  ( (__VAL__) == NAN )*/  /* this does not work */
73
 
/* #define IS_NAN(__VAL__)  ( ! ( ( (__VAL__) >= 0. ) ||  ( (__VAL__) <= 0. ) ) )  this does not work */
74
 
 
75
 
#ifdef isnan
76
 
  #define IS_NAN(__VAL__) (isnan(__VAL__))
77
 
#elif defined _isnan
78
 
  #define IS_NAN(__VAL__) (_isnan(__VAL__))
79
 
#else
80
 
/* If we do not have isnan then we can't reliably check for NaN - return false */
81
 
  #define IS_NAN(__VAL__) (0)
82
 
#endif
83
 
 
84
 
/**************************************************************/
85
 
 
86
 
/*   some useful functions: */
87
 
 
88
 
#define FALSE 0
89
 
#define TRUE 1
90
 
#define UNKNOWN -1
91
 
#define DBLE(_x_) (double)(_x_)
92
 
#define INDEX1(_X1_) (_X1_)
93
 
#define INDEX2(_X1_,_X2_,_N1_) ((_X1_)+(_N1_)*(_X2_))
94
 
#define INDEX3(_X1_,_X2_,_X3_,_N1_,_N2_) ((_X1_)+(_N1_)*INDEX2(_X2_,_X3_,_N2_))
95
 
#define INDEX4(_X1_,_X2_,_X3_,_X4_,_N1_,_N2_,_N3_) ((_X1_)+(_N1_)*INDEX3(_X2_,_X3_,_X4_,_N2_,_N3_))
96
 
#define INDEX5(_X1_,_X2_,_X3_,_X4_,_X5_,_N1_,_N2_,_N3_,_N4_) ((_X1_)+(_N1_)*INDEX4(_X2_,_X3_,_X4_,_X5_,_N2_,_N3_,_N4_))
97
 
#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_))
98
 
 
99
 
#define MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg1_) : (_arg2_))
100
 
#define MAX3(_arg1_,_arg2_,_arg3_) MAX(_arg1_,MAX(_arg2_,_arg3_))
101
 
#define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg2_) : (_arg1_)) 
102
 
#define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))
103
 
#define ABS(_arg_) MAX((_arg_),-(_arg_))
104
 
#define SIGN(_arg_) ((_arg_)>0 ?  1  : ((_arg_)<0 ? -1 : 0 ))
105
 
#define SWAP(_a0_,_a1_,_type_) { \
106
 
                                _type_ s; \
107
 
                                s=(_a0_); \
108
 
                                _a0_= (_a1_); \
109
 
                                _a1_=s; \
110
 
                               }
111
 
#define XNOR(_a0_,_a1_) ( ( (_a0_) && (_a1_) ) || ( !(_a0_) && !(_a1_) ) )
112
 
/**************************************************************/
113
 
/*    memory allocation:                                      */
114
 
/*    Wise to not use PASO_MALLOC/FREE/REALLOC and            */
115
 
/*    PASO_THREAD... directly. These are only for tailoring   */
116
 
/*    the main macros that follow                             */
117
 
/**************************************************************/
118
 
 
119
 
#if defined(_WIN32) /* Use python for memory management on windows. */
120
 
 
121
 
  #include <python.h>
122
 
 
123
 
  #define PASO_MALLOC PyMem_Malloc
124
 
  #define PASO_FREE PyMem_Free
125
 
  #define PASO_REALLOC PyMem_Realloc
126
 
 
127
 
#else
128
 
 
129
 
  #include <stdlib.h>
130
 
 
131
 
  #define PASO_MALLOC malloc
132
 
  #define PASO_FREE free
133
 
  #define PASO_REALLOC realloc
134
 
 
135
 
#endif
136
 
 
137
 
#ifndef __const
138
 
# if (defined __STDC__ && __STDC__)
139
 
#  define __const       const
140
 
# else
141
 
#  define __const
142
 
# endif
143
 
#endif
144
 
 
145
 
 
146
 
/* FIXME: This is not satisfactory.                                */
147
 
/* _ECC, __INTEL_COMPILER, and other                               */
148
 
/* intel compiler pre-defines need to be handled                   */
149
 
/* (__ICL, __ICC come to mind)                                     */
150
 
/* Also, _WIN32 may take this branch one day...                    */
151
 
/* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
152
 
 
153
 
#if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
154
 
  #include <omp.h>
155
 
  #define PASO_THREAD_MALLOC kmp_malloc
156
 
  #define PASO_THREAD_FREE kmp_free
157
 
#else
158
 
  #define PASO_THREAD_MALLOC PASO_MALLOC
159
 
  #define PASO_THREAD_FREE PASO_FREE
160
 
#endif
161
 
 
162
 
 
163
 
/* Prepare for the day that this becomes sharable. */
164
 
/* and we wish to do multi-file optimisations on windows */
165
 
 
166
 
#define PASO_DLL_API
167
 
 
168
 
#ifdef _WIN32
169
 
#   ifndef PASO_STATIC_LIB
170
 
#      undef PASO_DLL_API
171
 
#      ifdef PASO_EXPORTS
172
 
#         define PASO_DLL_API __declspec(dllexport)
173
 
#      else
174
 
#         define PASO_DLL_API __declspec(dllimport)
175
 
#      endif
176
 
#   endif
177
 
#endif
178
 
 
179
 
 
180
 
/******************The main macros ************************************/ 
181
 
 
182
 
#define MEMALLOC(_LENGTH_,_TYPE_)                                     \
183
 
  (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
184
 
 
185
 
/* do {} while(0) -  an old trick for bracketing a macro that */
186
 
/* makes sure a semi-colon does no harm.                      */
187
 
 
188
 
#define MEMFREE(_PTR_)                                                  \
189
 
do                                                                      \
190
 
{                                                                       \
191
 
  if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; }   \
192
 
} while(0)
193
 
 
194
 
#define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_)                    \
195
 
do                                                                        \
196
 
{                                                                         \
197
 
   if( (_POINTER_)!=NULL )                                                \
198
 
   {                                                                      \
199
 
      _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_),               \
200
 
                                   ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
201
 
   }                                                                      \
202
 
   else                                                                   \
203
 
   {                                                                      \
204
 
      _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
205
 
   }                                                                      \
206
 
} while(0)
207
 
 
208
 
#define TMPMEMALLOC MEMALLOC
209
 
#define TMPMEMFREE MEMFREE
210
 
#define TMPMEMREALLOC MEMREALLOC
211
 
 
212
 
#define THREAD_MEMALLOC(_LENGTH_,_TYPE_)                          \
213
 
   (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
214
 
 
215
 
#define THREAD_MEMFREE(_PTR_)                                                \
216
 
do                                                                           \
217
 
{                                                                            \
218
 
  if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
219
 
} while(0)
220
 
 
221
 
 
222
 
/*
223
 
        This was useful for seeing what memory is being allocated if you get an "Out of memory" error
224
 
        if used again, bracket with do {} while(0)
225
 
#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_)))
226
 
 
227
 
        This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
228
 
        if used again, bracket with do {} while(0)
229
 
#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__); }
230
 
*/
231
43
 
232
44
#ifdef USE_LAPACK
233
45