-
Notifications
You must be signed in to change notification settings - Fork 53
/
pioc_support.c
3380 lines (2998 loc) · 116 KB
/
pioc_support.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
* Support functions for the PIO library.
*/
#include "config.h"
#if PIO_ENABLE_LOGGING
#include <stdarg.h>
#include <unistd.h>
#endif /* PIO_ENABLE_LOGGING */
#include <pio.h>
#include <pio_internal.h>
#include <execinfo.h>
/** This is used with text decomposition files. */
#define VERSNO 2001
/** Used to shift file index to first two bytes of ncid. */
#define ID_SHIFT 16
/** In decomposition files, backtraces are included. This is the max
* number of trace levels that will be used. */
#define MAX_BACKTRACE 10
/* Some logging constants. */
#if PIO_ENABLE_LOGGING
#define MAX_LOG_MSG 1024
#define MAX_RANK_STR 12
#define ERROR_PREFIX "ERROR: "
#define NC_LEVEL_DIFF 3
int pio_log_level = 0;
int pio_log_ref_cnt = 0;
int my_rank;
FILE *LOG_FILE = NULL;
#endif /* PIO_ENABLE_LOGGING */
/**
* The PIO library maintains its own set of ncids. This is the next
* ncid number that will be assigned.
*/
extern int pio_next_ncid;
/** The default error handler used when iosystem cannot be located. */
extern int default_error_handler;
#ifdef NETCDF_INTEGRATION
/* This is used as the default iosysid for the netcdf integration
* code. */
extern int diosysid;
/** This prototype from netCDF is required for netCDF integration to
* work. */
int nc4_file_change_ncid(int ncid, unsigned short new_ncid_index);
#endif /* NETCDF_INTEGRATION */
/**
* Start the PIO timer.
*
* @param name name of the timer.
* @return 0 for success, error code otherwise.
* @author Ed Hartnett
*/
int
pio_start_timer(const char *name)
{
#ifdef TIMING
GPTLstart(name);
#endif /* TIMING */
return PIO_NOERR;
}
/**
* Stop the PIO timer.
*
* @param name name of the timer.
* @return 0 for success, error code otherwise.
* @author Ed Hartnett
*/
int
pio_stop_timer(const char *name)
{
#ifdef TIMING
GPTLstop(name);
#endif /* TIMING */
return PIO_NOERR;
}
/**
* Return a string description of an error code. If zero is passed,
* the errmsg will be "No error".
*
* @param pioerr the error code returned by a PIO function call.
* @param errmsg Pointer that will get the error message. The message
* will be PIO_MAX_NAME chars or less.
* @return 0 on success.
* @author Jim Edwards
*/
int
PIOc_strerror(int pioerr, char *errmsg)
{
PLOG((1, "PIOc_strerror pioerr = %d", pioerr));
/* Caller must provide this. */
pioassert(errmsg, "pointer to errmsg string must be provided", __FILE__,
__LINE__);
/* System error? NetCDF and pNetCDF errors are always negative. */
if (pioerr > 0)
{
const char *cp = (const char *)strerror(pioerr);
if (cp)
strncpy(errmsg, cp, PIO_MAX_NAME);
else
strcpy(errmsg, "Unknown Error");
}
else if (pioerr == PIO_NOERR)
strcpy(errmsg, "No error");
else if (pioerr <= NC2_ERR && pioerr >= NC4_LAST_ERROR) /* NetCDF error? */
strncpy(errmsg, nc_strerror(pioerr), PIO_MAX_NAME);
#if defined(_PNETCDF)
else if (pioerr > PIO_FIRST_ERROR_CODE) /* pNetCDF error? */
strncpy(errmsg, ncmpi_strerror(pioerr), PIO_MAX_NAME);
#endif /* defined( _PNETCDF) */
else
/* Handle PIO errors. */
switch(pioerr)
{
case PIO_EBADIOTYPE:
strcpy(errmsg, "Bad IO type");
break;
case PIO_EVARDIMMISMATCH:
strcpy(errmsg, "Variable dim mismatch in multivar call");
break;
case PIO_EBADREARR:
strcpy(errmsg, "Rearranger mismatch in async mode");
break;
default:
strcpy(errmsg, "Unknown Error: Unrecognized error code");
}
return PIO_NOERR;
}
/**
* Set the logging level if PIO was built with
* PIO_ENABLE_LOGGING. Set to -1 for nothing, 0 for errors only, 1 for
* important logging, and so on. Log levels below 1 are only printed
* on the io/component root.
*
* A log file is also produced for each task. The file is called
* pio_log_X.txt, where X is the (0-based) task number.
*
* If the library is not built with logging, this function does
* nothing.
*
* @param level the logging level, 0 for errors only, 5 for max
* verbosity.
* @returns 0 on success, error code otherwise.
* @author Ed Hartnett
*/
int
PIOc_set_log_level(int level)
{
#if PIO_ENABLE_LOGGING
/* Set the log level. */
pio_log_level = level;
if(!LOG_FILE)
pio_init_logging();
PLOG((0,"set loglevel to %d", level));
#endif /* PIO_ENABLE_LOGGING */
return PIO_NOERR;
}
/**
* Set the logging level value from the root compute task on all tasks
* if PIO was built with
* PIO_ENABLE_LOGGING. Set to -1 for nothing, 0 for errors only, 1 for
* important logging, and so on. Log levels below 1 are only printed
* on the io/component root.
*
* A log file is also produced for each task. The file is called
* pio_log_X.txt, where X is the (0-based) task number.
*
* If the library is not built with logging, this function does
* nothing.
*
* @param iosysid the IO system ID
* @param level the logging level, 0 for errors only, 5 for max
* verbosity.
* @returns 0 on success, error code otherwise.
* @author Jim Edwards
*/
int PIOc_set_global_log_level(int iosysid, int level)
{
#if PIO_ENABLE_LOGGING
iosystem_desc_t *ios;
int mpierr=0, mpierr2;
if (!(ios = pio_get_iosystem_from_id(iosysid)))
return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__);
if (ios->async)
{
if(!ios->ioproc)
{
int msg = PIO_MSG_SETLOGLEVEL;
if (ios->compmain == MPI_ROOT)
mpierr = MPI_Send(&msg, 1, MPI_INT, ios->ioroot, 1, ios->union_comm);
if (!mpierr)
mpierr = MPI_Bcast(&iosysid, 1, MPI_INT, ios->compmain, ios->intercomm);
}
}
if (!mpierr)
mpierr = MPI_Bcast(&level, 1, MPI_INT, ios->comproot, ios->union_comm);
/* Handle MPI errors. */
if ((mpierr2 = MPI_Bcast(&mpierr, 1, MPI_INT, ios->comproot, ios->my_comm)))
check_mpi(ios, NULL, mpierr2, __FILE__, __LINE__);
if (mpierr)
return check_mpi(ios, NULL, mpierr, __FILE__, __LINE__);
/* Set the log level on all tasks */
PIOc_set_log_level(level);
PLOG((level, "set_global_log_level, level = %d", level));
#endif
return PIO_NOERR;
}
#ifdef USE_MPE
/* This array holds even numbers for MPE. */
int event_num[2][NUM_EVENTS];
/* This keeps track of whether MPE has been initialized. */
int mpe_logging_initialized = 0;
/** This will set up the MPE logging event numbers. The calling
* program does not need to call MPE_Init_log(), that is done by the
* mpe library in MPI_Init(). MPE must be installed, get it from
* https://www.mcs.anl.gov/research/projects/perfvis/software/MPE/. PIO
* and the whole I/O stack must be built with MPE.
*
* @param my_rank rank of processor in MPI_COMM_WORLD.
* @author Ed Hartnett
*/
int
init_mpe(int my_rank)
{
/* If we've already initialized MPE states, just return. */
if (mpe_logging_initialized++)
return 0;
/* Get a bunch of event numbers. */
event_num[START][INIT] = MPE_Log_get_event_number();
event_num[END][INIT] = MPE_Log_get_event_number();
event_num[START][DECOMP] = MPE_Log_get_event_number();
event_num[END][DECOMP] = MPE_Log_get_event_number();
event_num[START][CREATE] = MPE_Log_get_event_number();
event_num[END][CREATE] = MPE_Log_get_event_number();
event_num[START][OPEN] = MPE_Log_get_event_number();
event_num[END][OPEN] = MPE_Log_get_event_number();
event_num[START][DARRAY_WRITE] = MPE_Log_get_event_number();
event_num[END][DARRAY_WRITE] = MPE_Log_get_event_number();
event_num[START][CLOSE] = MPE_Log_get_event_number();
event_num[END][CLOSE] = MPE_Log_get_event_number();
event_num[START][DARRAY_READ] = MPE_Log_get_event_number();
event_num[END][DARRAY_READ] = MPE_Log_get_event_number();
/* On rank 0, set up the info states. */
if (!my_rank)
{
/* Available colors: "white", "black", "red", "yellow", "green",
"cyan", "blue", "magenta", "aquamarine", "forestgreen",
"orange", "marroon", "brown", "pink", "coral", "gray" */
MPE_Describe_info_state(event_num[START][INIT], event_num[END][INIT],
"PIO init", "green", "%s");
MPE_Describe_info_state(event_num[START][DECOMP],
event_num[END][DECOMP], "PIO decomposition",
"cyan", "%s");
MPE_Describe_info_state(event_num[START][CREATE], event_num[END][CREATE],
"PIO create file", "red", "%s");
MPE_Describe_info_state(event_num[START][OPEN], event_num[END][OPEN],
"PIO open file", "orange", "%s");
MPE_Describe_info_state(event_num[START][DARRAY_WRITE],
event_num[END][DARRAY_WRITE], "PIO darray write",
"pink", "%s");
MPE_Describe_info_state(event_num[START][DARRAY_READ],
event_num[END][DARRAY_READ], "PIO darray read",
"magenta", "%s");
MPE_Describe_info_state(event_num[START][CLOSE],
event_num[END][CLOSE], "PIO close",
"white", "%s");
}
return 0;
}
/**
* Start MPE logging.
*
* @param state_num the MPE event state number to START (ex. INIT).
* @author Ed Hartnett
*/
void
pio_start_mpe_log(int state)
{
if (MPE_Log_event(event_num[START][state], 0, NULL))
pio_err(NULL, NULL, PIO_EIO, __FILE__, __LINE__);
}
/**
* End MPE logging.
*
* @param state one of the MPE states defined in pio_internal.h.
* @param msg a text message to describe the state. Will be truncated
* to MPE_MAX_MSG_LEN.
* @author Ed Hartnett
*/
void
pio_stop_mpe_log(int state, const char *msg)
{
MPE_LOG_BYTES bytebuf;
int pos = 0;
int msglen;
int ret;
/* Truncate messages longer than MPE_MAX_MSG_LEN. */
msglen = strlen(msg) > MPE_MAX_MSG_LEN ? MPE_MAX_MSG_LEN : strlen(msg);
/* Tell MPE to stop the state, with a message. */
MPE_Log_pack(bytebuf, &pos, 's', msglen, msg);
if ((ret = MPE_Log_event(event_num[END][state], 0, bytebuf)))
pio_err(NULL, NULL, PIO_EIO, __FILE__, __LINE__);
}
#endif /* USE_MPE */
/**
* Initialize logging. Open log file, if not opened yet, or increment
* ref count if already open.
*
* @author Jayesh Krishna, Ed Hartnett
*/
int
pio_init_logging(void)
{
int ret = PIO_NOERR;
#ifdef USE_MPE
{
int mpe_rank;
int mpierr;
if ((mpierr = MPI_Comm_rank(MPI_COMM_WORLD, &mpe_rank)))
return check_mpi(NULL, NULL, mpierr, __FILE__, __LINE__);
if ((ret = init_mpe(mpe_rank)))
return pio_err(NULL, NULL, ret, __FILE__, __LINE__);
}
#endif /* USE_MPE */
#if PIO_ENABLE_LOGGING
if (!LOG_FILE && pio_log_level > 0)
{
char log_filename[PIO_MAX_NAME];
int mpierr;
/* Create a filename with the rank in it. */
if ((mpierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank)))
return check_mpi(NULL, NULL, mpierr, __FILE__, __LINE__);
sprintf(log_filename, "pio_log_%d.log", my_rank);
/* Open a file for this rank to log messages. */
if (!(LOG_FILE = fopen(log_filename, "w")))
return pio_err(NULL, NULL, PIO_EIO, __FILE__, __LINE__);
pio_log_ref_cnt = 1;
}
else if(LOG_FILE)
{
pio_log_ref_cnt++;
}
#endif /* PIO_ENABLE_LOGGING */
return ret;
}
/**
* Finalize logging - close log files, if open.
*/
void
pio_finalize_logging(void)
{
#if PIO_ENABLE_LOGGING
pio_log_ref_cnt -= 1;
if (LOG_FILE)
{
if (pio_log_ref_cnt == 0)
{
fclose(LOG_FILE);
LOG_FILE = NULL;
}
else
PLOG((2, "pio_finalize_logging, postpone close, ref_cnt = %d",
pio_log_ref_cnt));
}
#endif /* PIO_ENABLE_LOGGING */
}
#if PIO_ENABLE_LOGGING
/**
* This function prints out a message, if the severity of the message
* is lower than the global pio_log_level. To use it, do something
* like this:
*
* pio_log(0, "this computer will explode in %d seconds", i);
*
* After the first arg (the severity), use the rest like a normal
* printf statement. Output will appear on stdout.
* This function is heavily based on the function in section 15.5 of
* the C FAQ.
*
* In code this functions should be wrapped in the PLOG(()) macro.
*
* @param severity the severity of the message, 0 for error messages,
* then increasing levels of verbosity.
* @param fmt the format string.
* @param ... the arguments used in format string.
* @author Ed Hartnett
*/
void
pio_log(int severity, const char *fmt, ...)
{
va_list argp;
int t;
int rem_len = MAX_LOG_MSG;
char msg[MAX_LOG_MSG];
char *ptr = msg;
char rank_str[MAX_RANK_STR];
/* If the severity is greater than the log level, we don't print
this message. */
if (severity > pio_log_level)
return;
/* If the severity is 0, only print on rank 0. */
if (severity < 1 && my_rank != 0)
return;
/* If the severity is zero, this is an error. Otherwise insert that
many tabs before the message. */
if (!severity)
{
strncpy(ptr, ERROR_PREFIX, (rem_len > 0) ? rem_len : 0);
ptr += strlen(ERROR_PREFIX);
rem_len -= strlen(ERROR_PREFIX);
}
for (t = 0; t < severity; t++)
{
strncpy(ptr++, "\t", (rem_len > 0) ? rem_len : 0);
rem_len--;
}
/* Show the rank. */
snprintf(rank_str, MAX_RANK_STR, "%d ", my_rank);
strncpy(ptr, rank_str, (rem_len > 0) ? rem_len : 0);
ptr += strlen(rank_str);
rem_len -= strlen(rank_str);
/* /\* Show the severity. *\/ */
/* snprintf(rank_str, MAX_RANK_STR, ":%d ", severity); */
/* strncpy(ptr, rank_str, (rem_len > 0) ? rem_len : 0); */
/* ptr += strlen(rank_str); */
/* rem_len -= strlen(rank_str); */
/* Print out the variable list of args with vprintf. */
va_start(argp, fmt);
vsnprintf(ptr, ((rem_len > 0) ? rem_len : 0), fmt, argp);
va_end(argp);
/* Put on a final linefeed. */
ptr = msg + strlen(msg);
rem_len = MAX_LOG_MSG - strlen(msg);
strncpy(ptr, "\n\0", (rem_len > 0) ? rem_len : 0);
/* Send message to stdout. */
fprintf(stdout, "%s", msg);
/* Send message to log file. */
if (LOG_FILE)
fprintf(LOG_FILE, "%s", msg);
/* Ensure an immediate flush of stdout. */
fflush(stdout);
if (LOG_FILE)
fflush(LOG_FILE);
}
#endif /* PIO_ENABLE_LOGGING */
/**
* Obtain a backtrace and print it to stderr. This is appended to the
* text decomposition file.
*
* Note from Jim:
*
* The stack trace can be used to identify the usage in
* the model code of the particular decomposition in question and so
* if using the pio performance tool leads to tuning that could be
* applied in the model you know more or less where to do it.
*
* It's also useful if you have a model bug - then you have 20 or so
* decomp files and you need to identify the one that was problematic.
* So it's used as an add to the developer and not used at all by any
* automated process or tools.
*
* @param fp file pointer to send output to
* @author Jim Edwards
*/
void
print_trace(FILE *fp)
{
void *array[10];
size_t size;
char **strings;
size_t i;
/* Note that this won't actually work. */
if (fp == NULL)
fp = stderr;
size = backtrace(array, 10);
strings = backtrace_symbols(array, size);
fprintf(fp,"Obtained %zd stack frames.\n", size);
for (i = 0; i < size; i++)
fprintf(fp,"%s\n", strings[i]);
free(strings);
}
/**
* Abort program and call MPI_Abort().
*
* @param msg an error message
* @param fname name of code file where error occured
* @param line the line of code where the error occurred.
* @author Jim Edwards
*/
void
piodie(const char *msg, const char *fname, int line)
{
fprintf(stderr,"Abort with message %s in file %s at line %d\n",
msg ? msg : "_", fname ? fname : "_", line);
print_trace(stderr);
#ifdef _MPISERIAL
abort();
#else
MPI_Abort(MPI_COMM_WORLD, -1);
#endif
}
/**
* Perform an assert. Note that this function does nothing if NDEBUG
* is defined.
*
* @param expression the expression to be evaluated
* @param msg an error message
* @param fname name of code file where error occured
* @param line the line of code where the error occurred.
* @author Jim Edwards
*/
void
pioassert(_Bool expression, const char *msg, const char *fname, int line)
{
#ifndef NDEBUG
if (!expression)
piodie(msg, fname, line);
#endif
}
/**
* Handle MPI errors. An error message is sent to stderr, then the
* check_netcdf() function is called with PIO_EIO.
*
* @param ios pointer to the iosystem_info_t. May be NULL.
* @param file pointer to the file_desc_t info. Ignored if NULL.
* @param mpierr the MPI return code to handle
* @param filename the name of the code file where error occured.
* @param line the line of code where error occured.
* @return PIO_NOERR for no error, otherwise PIO_EIO.
* @author Ed Hartnett
*/
int
check_mpi(iosystem_desc_t *ios, file_desc_t *file, int mpierr,
const char *filename, int line)
{
if (mpierr)
{
char errstring[MPI_MAX_ERROR_STRING];
int errstrlen;
/* If we can get an error string from MPI, print it to stderr. */
if (!MPI_Error_string(mpierr, errstring, &errstrlen))
fprintf(stderr, "MPI ERROR: %s in file %s at line %d\n",
errstring, filename ? filename : "_", line);
/* Handle all MPI errors as PIO_EIO. */
return pio_err(ios, file, PIO_EIO, filename, line);
}
return PIO_NOERR;
}
/**
* Check the result of a netCDF API call.
*
* @param file pointer to the PIO structure describing this
* file. Ignored if NULL.
* @param status the return value from the netCDF call.
* @param fname the name of the code file.
* @param line the line number of the netCDF call in the code.
* @return the error code
* @author Ed Hartnett
*/
int
check_netcdf(file_desc_t *file, int status, const char *fname, int line)
{
return check_netcdf2(NULL, file, status, fname, line);
}
/**
* Check the result of a netCDF API call. This is the same as
* check_netcdf() except for the extra iosystem_desc_t pointer, which
* is used to determine error handling when there is no file_desc_t
* pointer.
*
* @param ios pointer to the iosystem description struct. Ignored if NULL.
* @param file pointer to the PIO structure describing this file. Ignored if NULL.
* @param status the return value from the netCDF call.
* @param fname the name of the code file.
* @param line the line number of the netCDF call in the code.
* @return the error code
* @author Ed Hartnett
*/
int
check_netcdf2(iosystem_desc_t *ios, file_desc_t *file, int status,
const char *fname, int line)
{
int eh = default_error_handler; /* Error handler that will be used. */
int rbuf;
/* User must provide this. */
pioassert(fname, "code file name must be provided", __FILE__, __LINE__);
if (file && file->iosystem->ioproc &&
(file->iotype == PIO_IOTYPE_PNETCDF || file->iotype == PIO_IOTYPE_NETCDF4P))
{
if (file->iosystem->io_rank == 0)
MPI_Reduce(MPI_IN_PLACE, &status, 1, MPI_INT, MPI_MIN, 0, file->iosystem->io_comm);
else
MPI_Reduce(&status, &rbuf, 1, MPI_INT, MPI_MIN, 0, file->iosystem->io_comm);
}
PLOG((1, "check_netcdf2 status = %d fname = %s line = %d", status, fname, line));
/* Pick an error handler. */
if (ios)
eh = ios->error_handler;
if (file)
eh = file->iosystem->error_handler;
pioassert(eh == PIO_INTERNAL_ERROR || eh == PIO_BCAST_ERROR || eh == PIO_RETURN_ERROR,
"invalid error handler", __FILE__, __LINE__);
PLOG((2, "check_netcdf2 chose error handler = %d", eh));
/* Decide what to do based on the error handler. */
if (eh == PIO_INTERNAL_ERROR && status != PIO_NOERR)
{
char errmsg[PIO_MAX_NAME + 1]; /* Error message. */
PIOc_strerror(status, errmsg);
piodie(errmsg, fname, line); /* Die! */
}
else if (eh == PIO_BCAST_ERROR)
{
if (ios)
MPI_Bcast(&status, 1, MPI_INT, ios->ioroot, ios->my_comm);
else if (file)
MPI_Bcast(&status, 1, MPI_INT, file->iosystem->ioroot, file->iosystem->my_comm);
PLOG((2, "check_netcdf2 status returned = %d", status));
}
/* For PIO_RETURN_ERROR, just return the error. */
return status;
}
/**
* Handle an error in PIO. This will consult the error handler
* settings and either call MPI_Abort() or return an error code.
*
* The error hanlder has three settings:
*
* Errors cause abort: PIO_INTERNAL_ERROR.
*
* Error codes are broadcast to all tasks: PIO_BCAST_ERROR.
*
* Errors are returned to caller with no internal action:
* PIO_RETURN_ERROR.
*
* @param ios pointer to the IO system info. Ignored if NULL.
* @param file pointer to the file description data. Ignored if
* NULL. If provided file->iosystem is used as ios pointer.
* @param err_num the error code
* @param fname name of code file where error occured.
* @param line the line of code where the error occurred.
* @returns err_num if abort is not called.
* @author Jim Edwards
*/
int
pio_err(iosystem_desc_t *ios, file_desc_t *file, int err_num, const char *fname,
int line)
{
char err_msg[PIO_MAX_NAME + 1];
int err_handler = default_error_handler; /* Default error handler. */
int ret;
/* User must provide this. */
pioassert(fname, "file name must be provided", __FILE__, __LINE__);
/* No harm, no foul. */
if (err_num == PIO_NOERR)
return PIO_NOERR;
/* Get the error message. */
if ((ret = PIOc_strerror(err_num, err_msg)))
return ret;
/* If logging is in use, log an error message. */
PLOG((0, "%s err_num = %d fname = %s line = %d", err_msg, err_num, fname ? fname : '\0', line));
/* What error handler should we use? */
if (file)
err_handler = file->iosystem->error_handler;
else if (ios)
err_handler = ios->error_handler;
PLOG((2, "pio_err chose error handler = %d", err_handler));
/* Should we abort? */
if (err_handler == PIO_INTERNAL_ERROR)
{
/* For debugging only, this will print a traceback of the call tree. */
print_trace(stderr);
MPI_Abort(MPI_COMM_WORLD, -1);
}
/* Do nothing, error is bcast to all tasks and application will handle */
// if (err_handler == PIO_BCAST_ERROR)
// {
// }
/* If abort was not called, we'll get here. */
return err_num;
}
/**
* Allocate a region struct, and initialize it.
*
* @param ios pointer to the IO system info, used for error
* handling. Ignored if NULL.
* @param ndims the number of dimensions for the data in this region.
* @param regionp a pointer that gets a pointer to the newly allocated
* io_region struct.
* @returns 0 for success, error code otherwise.
* @author Jim Edwards
*/
int
alloc_region2(iosystem_desc_t *ios, int ndims, io_region **regionp)
{
io_region *region;
/* Check inputs. */
pioassert(ndims >= 0 && regionp, "invalid input", __FILE__, __LINE__);
PLOG((1, "alloc_region2 ndims = %d sizeof(io_region) = %d", ndims,
sizeof(io_region)));
/* Allocate memory for the io_region struct. */
if (!(region = calloc(1, sizeof(io_region))))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
/* Allocate memory for the array of start indicies. */
if (!(region->start = calloc(ndims, sizeof(PIO_Offset))))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
/* Allocate memory for the array of counts. */
if (!(region->count = calloc(ndims, sizeof(PIO_Offset))))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
/* Return pointer to new region to caller. */
*regionp = region;
return PIO_NOERR;
}
/**
* Given a PIO type, find the MPI type and the type size.
*
* @param pio_type a PIO type, PIO_INT, PIO_FLOAT, etc.
* @param mpi_type a pointer to MPI_Datatype that will get the MPI
* type that coresponds to the PIO type. Ignored if NULL.
* @param type_size a pointer to int that will get the size of the
* type, in bytes. (For example, 4 for PIO_INT). Ignored if NULL.
* @returns 0 for success, error code otherwise.
* @author Jim Edwards
*/
int
find_mpi_type(int pio_type, MPI_Datatype *mpi_type, int *type_size)
{
MPI_Datatype my_mpi_type;
int my_type_size;
/* Decide on the base type. */
switch(pio_type)
{
case PIO_BYTE:
#ifdef _MPISERIAL
my_mpi_type = MPI_BYTE;
#else
my_mpi_type = MPI_SIGNED_CHAR;
#endif
my_type_size = NETCDF_CHAR_SIZE;
break;
case PIO_CHAR:
my_mpi_type = MPI_CHAR;
my_type_size = NETCDF_CHAR_SIZE;
break;
case PIO_SHORT:
my_mpi_type = MPI_SHORT;
my_type_size = NETCDF_SHORT_SIZE;
break;
case PIO_INT:
my_mpi_type = MPI_INT;
my_type_size = NETCDF_INT_FLOAT_SIZE;
break;
case PIO_FLOAT:
my_mpi_type = MPI_FLOAT;
my_type_size = NETCDF_INT_FLOAT_SIZE;
break;
case PIO_DOUBLE:
my_mpi_type = MPI_DOUBLE;
my_type_size = NETCDF_DOUBLE_INT64_SIZE;
break;
#ifdef _NETCDF4
case PIO_UBYTE:
my_mpi_type = MPI_UNSIGNED_CHAR;
my_type_size = NETCDF_CHAR_SIZE;
break;
case PIO_USHORT:
my_mpi_type = MPI_UNSIGNED_SHORT;
my_type_size = NETCDF_SHORT_SIZE;
break;
case PIO_UINT:
my_mpi_type = MPI_UNSIGNED;
my_type_size = NETCDF_INT_FLOAT_SIZE;
break;
case PIO_INT64:
my_mpi_type = MPI_LONG_LONG;
my_type_size = NETCDF_DOUBLE_INT64_SIZE;
break;
case PIO_UINT64:
my_mpi_type = MPI_UNSIGNED_LONG_LONG;
my_type_size = NETCDF_DOUBLE_INT64_SIZE;
break;
case PIO_STRING:
my_mpi_type = MPI_CHAR;
my_type_size = NETCDF_CHAR_SIZE;
break;
#endif /* _NETCDF4 */
default:
return PIO_EBADTYPE;
}
/* If caller wants MPI type, set it. */
if (mpi_type)
*mpi_type = my_mpi_type;
/* If caller wants type size, set it. */
if (type_size)
*type_size = my_type_size;
return PIO_NOERR;
}
/**
* Allocate space for an IO description struct, and initialize it.
*
* @param ios pointer to the IO system info, used for error
* handling.
* @param piotype the PIO data type (ex. PIO_FLOAT, PIO_INT, etc.).
* @param ndims the number of dimensions.
* @param iodesc pointer that gets the newly allocated io_desc_t.
* @returns 0 for success, error code otherwise.
* @author Jim Edwards
*/
int
malloc_iodesc(iosystem_desc_t *ios, int piotype, int ndims,
io_desc_t **iodesc)
{
MPI_Datatype mpi_type;
PIO_Offset type_size;
int mpierr;
int ret;
/* Check input. */
pioassert(ios && piotype > 0 && ndims >= 0 && iodesc,
"invalid input", __FILE__, __LINE__);
PLOG((1, "malloc_iodesc piotype = %d ndims = %d", piotype, ndims));
/* Get the MPI type corresponding with the PIO type. */
if ((ret = find_mpi_type(piotype, &mpi_type, NULL)))
return pio_err(ios, NULL, ret, __FILE__, __LINE__);
/* What is the size of the pio type? */
if ((ret = pioc_pnetcdf_inq_type(0, piotype, NULL, &type_size)))
return pio_err(ios, NULL, ret, __FILE__, __LINE__);
/* Allocate space for the io_desc_t struct. */
if (!(*iodesc = calloc(1, sizeof(io_desc_t))))
return pio_err(ios, NULL, PIO_ENOMEM, __FILE__, __LINE__);
/* Remember the pio type and its size. */
(*iodesc)->piotype = piotype;
(*iodesc)->piotype_size = type_size;
/* Remember the MPI type. */
(*iodesc)->mpitype = mpi_type;
/* Get the size of the type. */
if (mpi_type == MPI_DATATYPE_NULL)
(*iodesc)->mpitype_size = 0;
else
if ((mpierr = MPI_Type_size((*iodesc)->mpitype, &(*iodesc)->mpitype_size)))
return check_mpi(ios, NULL, mpierr, __FILE__, __LINE__);
/* Initialize some values in the struct. */
(*iodesc)->maxregions = 1;
(*iodesc)->ioid = -1;
(*iodesc)->ndims = ndims;
(*iodesc)->readonly = 0;
/* Allocate space for, and initialize, the first region. */
if ((ret = alloc_region2(ios, ndims, &((*iodesc)->firstregion))))
return pio_err(ios, NULL, ret, __FILE__, __LINE__);
/* Set the swap memory settings to defaults for this IO system. */
(*iodesc)->rearr_opts = ios->rearr_opts;
return PIO_NOERR;
}
/**
* Free a region list.
*
* top a pointer to the start of the list to free.
* @author Jim Edwards
*/
void
free_region_list(io_region *top)
{
io_region *ptr, *tptr;
ptr = top;
while (ptr)
{
if (ptr->start)
free(ptr->start);
if (ptr->count)
free(ptr->count);
tptr = ptr;
ptr = ptr->next;
free(tptr);
}
}
/**
* Free a decomposition map.
*
* @param iosysid the IO system ID.
* @param ioid the ID of the decomposition map to free.
* @returns 0 for success, error code otherwise.
* @ingroup PIO_freedecomp_c
* @author Jim Edwards
*/
int
PIOc_freedecomp(int iosysid, int ioid)
{
iosystem_desc_t *ios;
io_desc_t *iodesc;