pio_util.c 8.2 KB
Newer Older
1
2
3
4
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

5
6
#include <errno.h>
#include <stdarg.h>
7
8
#include <stdio.h>
#include <stdlib.h>
9
#include <string.h>
Deike Kleberg's avatar
Deike Kleberg committed
10

11
#include "pio_util.h"
12
#include "cdi.h"
Deike Kleberg's avatar
Deike Kleberg committed
13

14
char commands[][13] = { "FINALIZE\0",
Deike Kleberg's avatar
Deike Kleberg committed
15
                        "RESOURCES\0",
16
17
18
                        "WINCREATE\0",
                        "WRITETS\0"};

Deike Kleberg's avatar
Deike Kleberg committed
19

Uwe Schulzweida's avatar
Uwe Schulzweida committed
20
void pcdiAssert   ( bool assumption, const char * filename,
Deike Kleberg's avatar
Deike Kleberg committed
21
22
23
24
25
                    const char * functionname, int line )
{
  if ( !assumption )
    {
#ifdef USE_MPI
26
27
28
      int rank = -1, init_flag = 0;
      if (MPI_Initialized(&init_flag) == MPI_SUCCESS && init_flag)
        MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
29
      fprintf ( stderr, "ERROR, FALSE ASSUMPTION: PE%d in %s, %s, line %d\n",
Deike Kleberg's avatar
Deike Kleberg committed
30
                rank, functionname, filename, line );
31
      fflush ( stderr );
32
33
34
      if (init_flag)
        MPI_Abort ( MPI_COMM_WORLD, 1 );
      else
35
        abort();
Deike Kleberg's avatar
Deike Kleberg committed
36
#else
37
      fprintf ( stderr, "ERROR, FALSE ASSUMPTION, %s, %s, line %d\n",
Deike Kleberg's avatar
Deike Kleberg committed
38
                functionname, filename, line );
39
      fflush ( stderr );
40
      abort();
Deike Kleberg's avatar
Deike Kleberg committed
41
42
43
44
#endif
    }
}

Deike Kleberg's avatar
Deike Kleberg committed
45
/****************************************************/
46

47
48
void pcdiAbort(const char * filename, const char *functionname, int line,
               const char * errorString, ... )
Deike Kleberg's avatar
Deike Kleberg committed
49
{
50
51
  va_list ap;
  va_start(ap, errorString);
52
#ifdef USE_MPI
53
54
55
56
57
58
59
60
  {
    int rank;
    MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
    fprintf(stderr, "ERROR, pe%d in %s, %s, line %d, errorString: \"",
            rank, functionname, filename, line);
  }
  vfprintf(stderr, errorString, ap);
  fputs("\"\n", stderr);
Deike Kleberg's avatar
Deike Kleberg committed
61
  MPI_Abort ( MPI_COMM_WORLD, 1 );
62
#else
63
64
65
66
  fprintf(stderr, "ERROR, %s, %s, line %d, errorString: \"",
          functionname, filename, line);
  vfprintf(stderr, errorString, ap);
  fputs("\"\n", stderr);
67
#endif
68
  exit ( EXIT_FAILURE );
69
  va_end(ap);
Deike Kleberg's avatar
Deike Kleberg committed
70
71
}

72
/*****************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
73

Uwe Schulzweida's avatar
Uwe Schulzweida committed
74
void * pcdiXmalloc ( size_t size, const char *filename, const char *functionname,
Deike Kleberg's avatar
Deike Kleberg committed
75
		     int line )
76
{
77
  void * value = calloc (1, size );
Deike Kleberg's avatar
Deike Kleberg committed
78
79

  if ( value == NULL )
80
81
    pcdiAbort (filename, functionname, line, "malloc failed: %s",
               strerror(errno));
82

Deike Kleberg's avatar
Deike Kleberg committed
83
84
85
  return value;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
86
void * pcdiXcalloc ( size_t nmemb, size_t size, const char *filename,
Deike Kleberg's avatar
Deike Kleberg committed
87
		     const char *functionname, int line )
88
89
90
91
{
  void * value = calloc ( nmemb, size );

  if ( value == NULL )
92
93
    pcdiAbort (filename, functionname, line, "calloc failed: %s",
               strerror(errno) );
94
95
96
97

  return value;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
98
void * pcdiXrealloc ( void *p, size_t size, const char *functionname,
Deike Kleberg's avatar
Deike Kleberg committed
99
		      const char *filename, int line )
100
{
101
  void * value = realloc ( p, size );
102

103
  if ( value == NULL )
104
105
    pcdiAbort(filename, functionname, line, "realloc failed: %s",
              strerror(errno));
106

107
  return value;
108
109
}

110
/***************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
111

112
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
113
void pcdiXMPI ( int iret, const char *filename, int line )
Deike Kleberg's avatar
Deike Kleberg committed
114
{
115
  char errorString[2][MPI_MAX_ERROR_STRING + 1];
Deike Kleberg's avatar
Deike Kleberg committed
116
117
  int len, errorClass, rank;

Deike Kleberg's avatar
Deike Kleberg committed
118
119
  if ( iret != MPI_SUCCESS )
    {
120
121
      MPI_Comm_rank ( MPI_COMM_WORLD, &rank );

Deike Kleberg's avatar
Deike Kleberg committed
122
      MPI_Error_class ( iret, &errorClass );
123
124
125
126
      MPI_Error_string ( errorClass, errorString[0], &len );
      errorString[0][len] = '\0';
      MPI_Error_string ( iret, errorString[1], &len);
      errorString[1][len] = '\0';
127

Deike Kleberg's avatar
Deike Kleberg committed
128
      fprintf ( stderr, "MPI ERROR, pe%d, %s, line %d,"
129
130
131
                "errorClass: \"%s\""
                "errorString: \"%s\"\n",
                rank, filename, line,
132
                errorString[0], errorString[1]);
Deike Kleberg's avatar
Deike Kleberg committed
133
134
135
136
137

      MPI_Abort ( MPI_COMM_WORLD, iret );
    }
}

Deike Kleberg's avatar
Deike Kleberg committed
138
/*****************************************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
139

140
141
142
143
144
145
146
147
148
149
void pcdiXMPIStat ( int iret, const char *filename, int line, MPI_Status *status )
{
  char errorString[MPI_MAX_ERROR_STRING + 1];
  int len, rank;

  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );

  if ( iret == MPI_ERR_IN_STATUS )
    {
      switch ( status->MPI_ERROR )
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        {
          fprintf ( stderr, "------- checking error in request ----------\n" );
        case MPI_SUCCESS :
          fprintf ( stderr, "-------- mpi_success -----------\n" );
          break;
        case MPI_ERR_PENDING:
          fprintf ( stderr, "-------- mpi_err_pending ----------\n");
          break;
        default:
          MPI_Error_string ( status->MPI_ERROR, errorString, &len );
          errorString[len] = '\0';
          fprintf ( stderr,"MPI ERROR in request, pe%d, %s, line %d,"
                    "return value: %d, error_string: %s\n",
                    rank, filename, line, iret, errorString );
          MPI_Abort ( MPI_COMM_WORLD, iret );
        }
166
    }
167
  else
168
    xmpi ( iret );
169

170
171
  return;
}
172
#endif
173

174
/****************************************************/
175

176
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
177
void pcdiDebugComm ( const char *filename, const char *functionname, int line, MPI_Comm *comm )
Deike Kleberg's avatar
Deike Kleberg committed
178
179
180
181
{
  int rank, size, len, rankGlob;
  char *name;

Deike Kleberg's avatar
Deike Kleberg committed
182
  name = ( char * ) xmalloc ( MPI_MAX_OBJECT_NAME );
183
  memset ( name, 0, ( MPI_MAX_OBJECT_NAME ) * sizeof ( char ));
184
  MPI_Comm_get_name ( * comm, name, &len );
185
  MPI_Comm_size ( * comm, &size );
186
187
188
189
190
191
  MPI_Comm_rank ( * comm, &rank );
  MPI_Comm_rank ( MPI_COMM_WORLD, &rankGlob );
  fprintf ( stdout,
            "pe%d in %s, %s, line %d: comm: name=%s, size=%d, rank=%d\n",
            rankGlob, functionname, filename, line,
            name, size, rank );
Deike Kleberg's avatar
Deike Kleberg committed
192
193
194
  free ( name );

}
195
#endif
Deike Kleberg's avatar
Deike Kleberg committed
196
197
198

/****************************************************/

199
#ifdef USE_MPI
Uwe Schulzweida's avatar
Uwe Schulzweida committed
200
201
void pcdiDebugMsg ( const char * cdiPioDebugString, const char *filename,
                    const char *functionname, int line, int tag, int source,
202
                    int nfinished )
Deike Kleberg's avatar
Deike Kleberg committed
203
204
205
206
207
{
  int rank;

  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );

208
  fprintf ( stdout,
209
210
            "%s pe%d in %s, %s, line %d: command %s, source %d, finalized=%d\n",
            cdiPioDebugString, rank, functionname, filename, line,
211
            &commands[tag][0], source, nfinished );
Deike Kleberg's avatar
Deike Kleberg committed
212
}
213
#endif
Deike Kleberg's avatar
Deike Kleberg committed
214
215
/****************************************************/

216
#ifdef USE_MPI
Deike Kleberg's avatar
Deike Kleberg committed
217
void pcdiDebugMsg2 ( const char *filename, const char *functionname, int line,
218
                   int tag, int source, char * text )
Deike Kleberg's avatar
Deike Kleberg committed
219
220
221
222
223
{
  int rank;

  MPI_Comm_rank ( MPI_COMM_WORLD, &rank );

224
225
226
227
  fprintf ( stdout,
            "pe%d in %s, %s, line %d: command %s, source %d, %s\n",
            rank, functionname, filename, line,
            &commands[tag][0], source, text );
Deike Kleberg's avatar
Deike Kleberg committed
228
}
229
#endif
Deike Kleberg's avatar
Deike Kleberg committed
230

Deike Kleberg's avatar
Deike Kleberg committed
231

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/****************************************************/


int xmaxInt ( int a, int b )
{
  return a >= b ? a : b;
}


/****************************************************/


int xminInt ( int a, int b )
{
  return a <= b ? a : b;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
250
/****************************************************/
251
252


Deike Kleberg's avatar
Deike Kleberg committed
253
254
255
256
257
258
int xsum ( int n, int * argarray )
{
  int i, sum = 0;

  for ( i = 0; i < n; i++ )
    sum += * ( argarray + i );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
259

Deike Kleberg's avatar
Deike Kleberg committed
260
261
262
263
  return sum;
}


Uwe Schulzweida's avatar
Uwe Schulzweida committed
264
/****************************************************/
Deike Kleberg's avatar
Deike Kleberg committed
265
266


Deike Kleberg's avatar
Deike Kleberg committed
267
double xchecksum ( int type, int count, void * buffer )
268
{
Deike Kleberg's avatar
Deike Kleberg committed
269
  return 0.0;
270
}
Deike Kleberg's avatar
Deike Kleberg committed
271
272


273
274
/****************************************************/

Uwe Schulzweida's avatar
Uwe Schulzweida committed
275
void printArray ( const char * cdiPioDebugString, char * ps, const void * array, int n,
276
                  int datatype, const char * funname, const char * filename, int line )
Uwe Schulzweida's avatar
Uwe Schulzweida committed
277
{
278
279
280
281
282
  int i, rank;
  int * iArray;
  double * dArray;

#ifdef USE_MPI
283
284
285
286
287
288
289
  {
    int init_flag;
    if (MPI_Initialized(&init_flag) == MPI_SUCCESS && init_flag)
      MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
    else
      rank = -1;
  }
290
291
  fprintf ( stdout, "%s pe%d in %s, %s, line %d: %s = ",
	    cdiPioDebugString, rank, funname, filename, line, ps );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
292
#else
293
294
  fprintf ( stdout, "%s %s, %s, line %d: %s = ",
	    cdiPioDebugString, funname, filename, line, ps );
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#endif

  switch ( datatype )
    {
    case DATATYPE_INT:
      iArray = ( int * ) array;
      for ( i = 0; i < n-1; i++ )
	fprintf ( stdout, "%d ", * ( iArray + i ));
      fprintf ( stdout, "%d\n", * ( iArray + n - 1 ));
      break;
    case DATATYPE_FLT:
      dArray = ( double * ) array;
      for ( i = 0; i < n-1; i++ )
	fprintf ( stdout, "%.2f ", * ( dArray + i ));
      fprintf ( stdout, "%.2f\n", * ( dArray + n-1 ));
      break;
    default:
      fprintf ( stdout, " ... no datatype defined\n" );
Uwe Schulzweida's avatar
Uwe Schulzweida committed
313
314
    }

315
316
317
  return;
}

Uwe Schulzweida's avatar
Uwe Schulzweida committed
318
/****************************************************/
319
320
321
322
323
324
325
326
327
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */