pio_util.h 3.85 KB
Newer Older
Deike Kleberg's avatar
Deike Kleberg committed
1
2
3
#ifndef PIO_UTIL_
#define PIO_UTIL_

Deike Kleberg's avatar
Deike Kleberg committed
4
#include <stdbool.h>
Deike Kleberg's avatar
Deike Kleberg committed
5
6
7
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
8

9
#include <mpi.h>
Deike Kleberg's avatar
Deike Kleberg committed
10

11
#ifndef ERROR_H
12
#include "error.h"
Uwe Schulzweida's avatar
Uwe Schulzweida committed
13
#endif
Deike Kleberg's avatar
Deike Kleberg committed
14
15

#define MAXDEBUG           3
16

17
#define ddebug             0
Deike Kleberg's avatar
Deike Kleberg committed
18

19
#define debugString "#####"
Deike Kleberg's avatar
Deike Kleberg committed
20

Thomas Jahns's avatar
Thomas Jahns committed
21
22
23
24
25
void
cdiAbortC_MPI(const char * caller, const char * filename,
              const char *functionname, int line,
              const char * errorString, va_list ap)
  __attribute__((noreturn));
26
27
28

void cdiPioWarning(const char *caller, const char *fmt, va_list ap);

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
static inline int
callsToMPIAreAllowed()
{
  int init_flag = 0, finished_flag = 0;
  return MPI_Initialized(&init_flag) == MPI_SUCCESS && init_flag
    && MPI_Finalized(&finished_flag) == MPI_SUCCESS && !finished_flag;
}

static inline int
getMPICommWorldRank()
{
  int rank = -1;
  if (callsToMPIAreAllowed())
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  return rank;
}

46
47
#define xdebug(fmt, ...)                                                \
  if ( ddebug ){                                                        \
48
    int rank = getMPICommWorldRank();                                   \
49
50
    fprintf ( stderr, "%s pe%d in %s, %s, line %d: " fmt "\n",          \
              debugString, rank,  __func__, __FILE__,  __LINE__,        \
51
              __VA_ARGS__ );                                            \
52
  }
Deike Kleberg's avatar
Deike Kleberg committed
53

54
55
56
57
58
59
#define xdebug3(fmt, ...)                                               \
  if ( ddebug == MAXDEBUG ){                                            \
    int rank = getMPICommWorldRank();                                   \
    fprintf ( stderr, "pe%d in %s, %s, line %d: " fmt "\n",             \
              rank,  __func__, __FILE__,  __LINE__,                     \
              __VA_ARGS__ );                                            \
60
61
  }

62
63
64
65
66
67
void pcdiXMPI(int iret, const char *, int);
#define xmpi(ret) do {                                  \
    int tmpIRet = (ret);                                   \
    if (tmpIRet != MPI_SUCCESS)                            \
      pcdiXMPI(tmpIRet, __FILE__, __LINE__ );              \
  } while(0)
68

69
70
void pcdiXMPIStat ( int, const char *, int, MPI_Status * );
#define xmpiStat(ret,stat) pcdiXMPIStat ( ret, __FILE__, __LINE__, stat )
71

72
void pcdiDebugMsg ( const char * cdiDebugString, const char *filename, const char *functionname, int line, \
Thomas Jahns's avatar
Thomas Jahns committed
73
                    int tag, int source, int nfinished );
74
75
#define xdebugMsg(tag,source,nfinished) \
  if ( ddebug ) \
76
      pcdiDebugMsg ( debugString, __FILE__, __func__, __LINE__, tag, source, nfinished )
Deike Kleberg's avatar
Deike Kleberg committed
77
78

void pcdiDebugMsg2 ( const char *filename, const char *functionname, int line, \
Thomas Jahns's avatar
Thomas Jahns committed
79
                    int tag, int source, char * text );
80
81
82
#define xdebugMsg2(tag,source,text) \
  if ( ddebug ) pcdiDebugMsg ( __FILE__, __func__,  __LINE__, tag, source, text )

Thomas Jahns's avatar
Thomas Jahns committed
83
84
85
86
87
88
89
90
91
static inline int
sum_int(size_t n, int *a)
{
  int sum = 0;
  for (size_t i = 0; i < n; ++i)
    sum += a[i];
  return sum;
}

92

93
void printArray ( const char *, const char *, const void *, int, int, const char *, const char *, int );
Deike Kleberg's avatar
Deike Kleberg committed
94
95
#define xprintArray(ps,array,n,datatype)                                \
  if ( ddebug )                                                         \
96
      printArray ( debugString, ps, array, n, datatype,  __func__, __FILE__, __LINE__ )
97

Deike Kleberg's avatar
Deike Kleberg committed
98
99
#define xprintArray3(ps,array,n,datatype)                                \
  if ( ddebug == MAXDEBUG )                                                         \
100
      printArray ( debugString, ps, array, n, datatype,  __func__, __FILE__, __LINE__ )
101

102
103
104
/**
 * @return number of dimensions
 */
105
int
106
107
cdiPioQueryVarDims(int varShape[3], int vlistID, int varID);

108
109
110
111
void
cdiPioDeco1D_CCP(size_t n, const size_t weightPfxSums[n],
                 size_t nparts, size_t separators[nparts + 1]);

112
#endif
113
114
115
116
117
118
119
120
121
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */