/[escript]/trunk/paso/src/Common.h
ViewVC logotype

Diff of /trunk/paso/src/Common.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3258 by gross, Mon Aug 30 10:48:00 2010 UTC revision 3259 by jfenwick, Mon Oct 11 01:48:14 2010 UTC
# Line 26  Line 26 
26    
27  /**************************************************************/  /**************************************************************/
28    
29  /* some system values */  #include "esysUtils/maths.h"
30  /* FIXME: This is not satisfactory.                                */  #include "esysUtils/mem.h"
31  /* _ECC, __INTEL_COMPILER, and other                               */  #include "esysUtils/index.h"
32  /* intel compiler pre-defines need to be handled                   */  #include "esysUtils/types.h"
33  /* (__ICL, __ICC come to mind)                                     */  
 #if defined(_WIN32) && defined(__INTEL_COMPILER)  
 #include <mathimf.h>  
 #else  
 #include <math.h>  
 #endif  
   
 #if (defined __OPENMP)  
 #include <omp.h>  
 #endif  
34    
35  #include <float.h>  #include <float.h>
36  #include <stdio.h>  #include <stdio.h>
# Line 49  Line 40 
40    
41  #define LenString_MAX FILENAME_MAX*2  #define LenString_MAX FILENAME_MAX*2
42  #define LenErrorMsg_MAX LenString_MAX  #define LenErrorMsg_MAX LenString_MAX
   
 /* on some arcitectures it could be a good idea to use long rather than int */  
 /* this has not really been tested */  
   
 typedef int dim_t;  
 typedef int index_t;  
 typedef int bool_t;  
 typedef int type_t;  
 typedef int err_t;  
   
 #define INDEX_T_MAX INT_MAX  
 #define INDEX_T_MIN -INT_MAX  
 #define EPSILON DBL_EPSILON  
 #define LARGE_POSITIVE_FLOAT DBL_MAX  
 #define SMALL_NEGATIVE_FLOAT -DBL_MAX  
   
 /*#ifndef NAN  
    #define NAN (0.0/0.0)  
 #endif  
 */  
 /*#define IS_NAN(__VAL__)  ( (__VAL__) == NAN )*/  /* this does not work */  
 /* #define IS_NAN(__VAL__)  ( ! ( ( (__VAL__) >= 0. ) ||  ( (__VAL__) <= 0. ) ) )  this does not work */  
   
 #ifdef isnan  
   #define IS_NAN(__VAL__) (isnan(__VAL__))  
 #elif defined _isnan  
   #define IS_NAN(__VAL__) (_isnan(__VAL__))  
 #else  
 /* If we do not have isnan then we can't reliably check for NaN - return false */  
   #define IS_NAN(__VAL__) (0)  
 #endif  
   
 /**************************************************************/  
   
 /*   some useful functions: */  
   
 #define FALSE 0  
 #define TRUE 1  
 #define UNKNOWN -1  
 #define DBLE(_x_) (double)(_x_)  
 #define INDEX1(_X1_) (_X1_)  
 #define INDEX2(_X1_,_X2_,_N1_) ((_X1_)+(_N1_)*(_X2_))  
 #define INDEX3(_X1_,_X2_,_X3_,_N1_,_N2_) ((_X1_)+(_N1_)*INDEX2(_X2_,_X3_,_N2_))  
 #define INDEX4(_X1_,_X2_,_X3_,_X4_,_N1_,_N2_,_N3_) ((_X1_)+(_N1_)*INDEX3(_X2_,_X3_,_X4_,_N2_,_N3_))  
 #define INDEX5(_X1_,_X2_,_X3_,_X4_,_X5_,_N1_,_N2_,_N3_,_N4_) ((_X1_)+(_N1_)*INDEX4(_X2_,_X3_,_X4_,_X5_,_N2_,_N3_,_N4_))  
 #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 MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg1_) : (_arg2_))  
 #define MAX3(_arg1_,_arg2_,_arg3_) MAX(_arg1_,MAX(_arg2_,_arg3_))  
 #define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ?  (_arg2_) : (_arg1_))  
 #define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))  
 #define ABS(_arg_) MAX((_arg_),-(_arg_))  
 #define SIGN(_arg_) ((_arg_)>0 ?  1  : ((_arg_)<0 ? -1 : 0 ))  
 #define SWAP(_a0_,_a1_,_type_) { \  
                                 _type_ s; \  
                                 s=(_a0_); \  
                                 _a0_= (_a1_); \  
                                 _a1_=s; \  
                                }  
 #define XNOR(_a0_,_a1_) ( ( (_a0_) && (_a1_) ) || ( !(_a0_) && !(_a1_) ) )  
 /**************************************************************/  
 /*    memory allocation:                                      */  
 /*    Wise to not use PASO_MALLOC/FREE/REALLOC and            */  
 /*    PASO_THREAD... directly. These are only for tailoring   */  
 /*    the main macros that follow                             */  
 /**************************************************************/  
   
 #if defined(_WIN32) /* Use python for memory management on windows. */  
   
   #include <python.h>  
   
   #define PASO_MALLOC PyMem_Malloc  
   #define PASO_FREE PyMem_Free  
   #define PASO_REALLOC PyMem_Realloc  
   
 #else  
   
   #include <stdlib.h>  
   
   #define PASO_MALLOC malloc  
   #define PASO_FREE free  
   #define PASO_REALLOC realloc  
   
 #endif  
   
 #ifndef __const  
 # if (defined __STDC__ && __STDC__)  
 #  define __const   const  
 # else  
 #  define __const  
 # endif  
 #endif  
   
   
 /* FIXME: This is not satisfactory.                                */  
 /* _ECC, __INTEL_COMPILER, and other                               */  
 /* intel compiler pre-defines need to be handled                   */  
 /* (__ICL, __ICC come to mind)                                     */  
 /* Also, _WIN32 may take this branch one day...                    */  
 /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */  
   
 #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */  
   #include <omp.h>  
   #define PASO_THREAD_MALLOC kmp_malloc  
   #define PASO_THREAD_FREE kmp_free  
 #else  
   #define PASO_THREAD_MALLOC PASO_MALLOC  
   #define PASO_THREAD_FREE PASO_FREE  
 #endif  
   
   
 /* Prepare for the day that this becomes sharable. */  
 /* and we wish to do multi-file optimisations on windows */  
   
 #define PASO_DLL_API  
   
 #ifdef _WIN32  
 #   ifndef PASO_STATIC_LIB  
 #      undef PASO_DLL_API  
 #      ifdef PASO_EXPORTS  
 #         define PASO_DLL_API __declspec(dllexport)  
 #      else  
 #         define PASO_DLL_API __declspec(dllimport)  
 #      endif  
 #   endif  
 #endif  
   
   
 /******************The main macros ************************************/  
   
 #define MEMALLOC(_LENGTH_,_TYPE_)                                     \  
   (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
   
 /* do {} while(0) -  an old trick for bracketing a macro that */  
 /* makes sure a semi-colon does no harm.                      */  
   
 #define MEMFREE(_PTR_)                                                  \  
 do                                                                      \  
 {                                                                       \  
   if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; }   \  
 } while(0)  
   
 #define MEMREALLOC(_RETP_,_POINTER_,_LENGTH_,_TYPE_)                    \  
 do                                                                        \  
 {                                                                         \  
    if( (_POINTER_)!=NULL )                                                \  
    {                                                                      \  
       _RETP_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_),               \  
                                    ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \  
    }                                                                      \  
    else                                                                   \  
    {                                                                      \  
       _RETP_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \  
    }                                                                      \  
 } while(0)  
   
 #define TMPMEMALLOC MEMALLOC  
 #define TMPMEMFREE MEMFREE  
 #define TMPMEMREALLOC MEMREALLOC  
   
 #define THREAD_MEMALLOC(_LENGTH_,_TYPE_)                          \  
    (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
   
 #define THREAD_MEMFREE(_PTR_)                                                \  
 do                                                                           \  
 {                                                                            \  
   if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \  
 } while(0)  
   
   
 /*  
     This was useful for seeing what memory is being allocated if you get an "Out of memory" error  
         if used again, bracket with do {} while(0)  
 #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_)))  
   
     This was useful for seeing where memory is being freed if you get an "glibc detected...free" error  
         if used again, bracket with do {} while(0)  
 #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__); }  
 */  
43    
44  #ifdef USE_LAPACK  #ifdef USE_LAPACK
45    

Legend:
Removed from v.3258  
changed lines
  Added in v.3259

  ViewVC Help
Powered by ViewVC 1.1.26