-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmiol_runner.c
6349 lines (5594 loc) · 205 KB
/
smiol_runner.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include "smiol.h"
#include "smiol_utils.h"
/*******************************************************************************
* SMIOL C Runner - Take SMIOL out for a run!
*******************************************************************************/
int test_init_finalize(FILE *test_log);
int test_open_close(FILE *test_log);
int test_dimensions(FILE *test_log);
int test_variables(FILE *test_log);
int test_attributes(FILE *test_log);
int test_decomp(FILE *test_log);
int test_build_exch(FILE *test_log);
int test_transfer(FILE *test_log);
int test_file_sync(FILE *test_log);
int test_utils(FILE *test_log);
int test_io_decomp(FILE *test_log);
int test_aggregate_list(FILE *test_log);
int test_set_get_frame(FILE* test_log);
int test_put_get_vars(FILE *test_log);
int test_io_aggregation(FILE *test_log);
int test_buffered_io(FILE *test_log);
int compare_decomps(struct SMIOL_decomp *decomp,
size_t n_comp_list, SMIOL_Offset *comp_list_correct,
size_t n_io_list, SMIOL_Offset *io_list_correct);
size_t elements_covered(int comm_size, size_t *io_start, size_t *io_count);
int range_intersection(size_t start1, size_t count1, size_t start2, size_t count2);
/*******************************************************************************
*
* transfer test macro function
*
* Tests the transfer_field utility for a given SMIOL_decomp
*
* Given a SMIOL_decomp and the number of compute and I/O elements assumed by
* the SMIOL_decomp, this function:
* 1) Allocates a compute field and fills it with a pattern of values
* 2) Transfers the field from compute to I/O tasks
* 3) Zeros-out the compute field
* 4) Adds 42 to the field on I/O tasks
* 5) Transfers the field from I/O tasks back to compute tasks
* 6) Checks the field on compute tasks.
*
* If the final values in the compute field are their original values plus 42,
* a value of 0 is returned. Otherwise, 1 is returned.
*
*******************************************************************************/
#define transfer_type(NAME, TYPE) \
static int NAME(size_t n_compute_elements, size_t n_io_elements, \
struct SMIOL_decomp *decomp) \
{ \
size_t i; \
TYPE *comp_field, *io_field; \
\
comp_field = malloc(sizeof(TYPE) * n_compute_elements); \
io_field = malloc(sizeof(TYPE) * n_io_elements); \
\
/* Initialize compute field */ \
for (i = 0; i < n_compute_elements; i++) { \
comp_field[i] = (TYPE)i; \
} \
\
/* Transfer field from compute to I/O tasks */ \
transfer_field(decomp, SMIOL_COMP_TO_IO, sizeof(TYPE), \
(const void *)comp_field, (void *)io_field); \
\
/* Zero-out the compute field */ \
for (i = 0; i < n_compute_elements; i++) { \
comp_field[i] = (TYPE)0; \
} \
\
/* Add 42 to all elements of the I/O field */ \
for (i = 0; i < n_io_elements; i++) { \
io_field[i] = io_field[i] + (TYPE)42; \
} \
\
/* Transfer the modified field from I/O tasks to compute tasks */ \
transfer_field(decomp, SMIOL_IO_TO_COMP, sizeof(TYPE), \
(const void *)io_field, (void *)comp_field); \
\
free(io_field); \
\
/* Verify values in the compute field */ \
for (i = 0; i < n_compute_elements; i++) { \
if (comp_field[i] != (TYPE)i + (TYPE)42) { \
free(comp_field); \
return 1; \
} \
} \
\
free(comp_field); \
\
return 0; \
}
transfer_type(transfer_float, float)
transfer_type(transfer_int, int)
transfer_type(transfer_double, double)
transfer_type(transfer_char, char)
int main(int argc, char **argv)
{
int ierr;
int i;
float f;
int my_proc_id;
SMIOL_Offset dimsize;
size_t n_compute_elements;
SMIOL_Offset *compute_elements;
int num_io_tasks, io_stride;
struct SMIOL_decomp *decomp = NULL;
struct SMIOL_context *context = NULL;
struct SMIOL_file *file = NULL;
char log_fname[17];
FILE *test_log = NULL;
char **dimnames;
float *buf;
if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
fprintf(stderr, "Error: MPI_Init failed.\n");
return 1;
}
if (MPI_Comm_rank(MPI_COMM_WORLD, &my_proc_id) != MPI_SUCCESS) {
fprintf(stderr, "Error: MPI_Comm_rank failed.\n");
}
sprintf(log_fname, "smiol.%04d.test", my_proc_id);
test_log = fopen(log_fname, "w");
if (test_log == NULL) {
fprintf(stderr, "Error: Could not open test log file\n");
return 1;
}
/*
* Unit tests for SMIOL_init and SMIOL_finalize
*/
ierr = test_init_finalize(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for SMIOL_open_file and SMIOL_close_file
*/
ierr = test_open_close(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for dimensions
*/
ierr = test_dimensions(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for variables
*/
ierr = test_variables(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for attributes
*/
ierr = test_attributes(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for SMIOL_create_decomp and SMIOL_free_decomp
*/
ierr = test_decomp(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for build_exchange
*/
ierr = test_build_exch(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests transfer_field
*/
ierr = test_transfer(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for SMIOL_sync_file
*/
ierr = test_file_sync(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for SMIOL utilities
*/
ierr = test_utils(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for decomposition of I/O elements
*/
ierr = test_io_decomp(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for list aggregation utility function
*/
ierr = test_aggregate_list(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for set, get frame
*/
ierr = test_set_get_frame(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Unit tests for writing and reading variables
*/
ierr = test_put_get_vars(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Test I/O aggregation
*/
ierr = test_io_aggregation(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
/*
* Test buffered I/O
*/
ierr = test_buffered_io(test_log);
if (ierr == 0) {
fprintf(test_log, "All tests PASSED!\n\n");
}
else {
fprintf(test_log, "%i tests FAILED!\n\n", ierr);
}
num_io_tasks = 16;
io_stride = 4;
if ((ierr = SMIOL_init(MPI_COMM_WORLD, num_io_tasks, io_stride, &context)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_init: %s ", SMIOL_error_string(ierr));
return 1;
}
if (context == NULL) {
fprintf(test_log, "SMIOL_init returned a NULL context\n");
return 1;
}
/* Create elements */
n_compute_elements = 100;
compute_elements = malloc(sizeof(SMIOL_Offset) * n_compute_elements);
memset((void *)compute_elements, 0, sizeof(SMIOL_Offset) * n_compute_elements);
ierr = SMIOL_create_decomp(context, n_compute_elements, compute_elements, 1,
&decomp);
if (ierr != SMIOL_SUCCESS) {
printf("ERROR: SMIOL_create_decomp - SMIOL_SUCCESS was not returned\n");
return 1;
}
/* Free local copy */
free(compute_elements);
if ((ierr = SMIOL_free_decomp(&decomp)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_free_decomp: %s ",
SMIOL_error_string(ierr));
return 1;
}
if (decomp != NULL) {
fprintf(test_log, "ERROR: SMIOL_free_decomp - Decomp not 'NULL' after free\n");
return 1;
}
if ((ierr = SMIOL_inquire()) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_inquire: %s ",
SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_open_file(context, "blah.nc", SMIOL_FILE_CREATE, &file,
(size_t)64000000, SMIOL_FORMAT_CDF5)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_open_file: %s ",
SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_define_dim(file, "Time", -1)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_define_dim: %s ", SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_sync_file(file)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_sync_file: %s ",
SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_define_dim(file, "nCells", 40962)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_define_dim: %s ", SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_inquire_dim(file, "nCells", &dimsize, NULL)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_inquire_dim: %s ", SMIOL_error_string(ierr));
return 1;
}
fprintf(test_log, "Size of nCells dimension is %li\n", (long int)dimsize);
if ((ierr = SMIOL_sync_file(file)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_sync_file: %s ",
SMIOL_error_string(ierr));
return 1;
}
dimnames = (char **)malloc((size_t)2 * sizeof(char *));
dimnames[0] = (char *)malloc((size_t)64 * sizeof(char));
dimnames[1] = (char *)malloc((size_t)64 * sizeof(char));
snprintf(dimnames[0], 64, "Time");
snprintf(dimnames[1], 64, "nCells");
if ((ierr = SMIOL_define_var(file, "theta", SMIOL_REAL32, 2, (const char**)dimnames)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_define_var: %s ", SMIOL_error_string(ierr));
return 1;
}
free(dimnames[0]);
free(dimnames[1]);
free(dimnames);
i = 2;
if ((ierr = SMIOL_define_att(file, "theta", "time_levels", SMIOL_INT32,
(const void *)&i)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_define_att: %s ",
SMIOL_error_string(ierr));
return 1;
}
f = (float)3.14159265;
if ((ierr = SMIOL_define_att(file, NULL, "pi", SMIOL_REAL32,
(const void *)&f)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_define_att: %s ",
SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_define_att(file, NULL, "title", SMIOL_CHAR,
"MPAS-Atmosphere v7.0")) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_define_att: %s ",
SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_inquire_att(file, NULL, "title", NULL, NULL, NULL)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_inquire_att: %s ",
SMIOL_error_string(ierr));
return 1;
}
buf = malloc(sizeof(float) * (size_t)40962);
memset((void *)buf, 0, sizeof(float) * (size_t)40962);
if ((ierr = SMIOL_put_var(file, "theta", NULL, buf)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_put_var: %s ",
SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_get_var(file, "theta", NULL, buf)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_get_var: %s ",
SMIOL_error_string(ierr));
return 1;
}
free(buf);
if ((ierr = SMIOL_close_file(&file)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_close_file: %s ", SMIOL_error_string(ierr));
return 1;
}
if ((ierr = SMIOL_set_option()) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_set_option: %s ",
SMIOL_error_string(ierr));
return 1;
}
fprintf(test_log, "SMIOL_error_string test 'Unknown error': %s\n",
SMIOL_error_string(-1));
fprintf(test_log, "SMIOL_error_string test 'Success!': %s\n",
SMIOL_error_string(SMIOL_SUCCESS));
fprintf(test_log, "SMIOL_error_string test 'malloc returned a null pointer': %s\n",
SMIOL_error_string(SMIOL_MALLOC_FAILURE));
fprintf(test_log, "SMIOL_error_string test 'invalid subroutine argument': %s\n",
SMIOL_error_string(SMIOL_INVALID_ARGUMENT));
fprintf(test_log, "SMIOL_error_string test 'internal MPI call failed': %s\n",
SMIOL_error_string(SMIOL_MPI_ERROR));
fprintf(test_log, "SMIOL_error_string test 'Fortran wrapper detected an inconsistency in C return values': %s\n",
SMIOL_error_string(SMIOL_FORTRAN_ERROR));
fprintf(test_log, "SMIOL_error_string test 'bad return code from a library call': %s\n",
SMIOL_error_string(SMIOL_LIBRARY_ERROR));
fprintf(test_log, "SMIOL_lib_error_string 'Could not find matching library for the source of the error': %s\n",
SMIOL_lib_error_string(context));
fprintf(test_log, "SMIOL_error_string test 'argument is of the wrong type': %s\n",
SMIOL_error_string(SMIOL_WRONG_ARG_TYPE));
fprintf(test_log, "SMIOL_error_string test 'argument is of insufficient size': %s\n",
SMIOL_error_string(SMIOL_INSUFFICIENT_ARG));
fprintf(test_log, "SMIOL_error_string test 'invalid format for file creation': %s\n",
SMIOL_error_string(SMIOL_INVALID_FORMAT));
if ((ierr = SMIOL_finalize(&context)) != SMIOL_SUCCESS) {
fprintf(test_log, "ERROR: SMIOL_finalize: %s ", SMIOL_error_string(ierr));
return 1;
}
if (context != NULL) {
fprintf(test_log, "SMIOL_finalize returned a non-NULL context\n");
return 1;
}
if (fclose(test_log) != 0) {
fprintf(stderr, "Error: Could not close test_log\n");
return 1;
}
fprintf(stderr, "Called all SMIOL functions successfully!\n");
if (MPI_Finalize() != MPI_SUCCESS) {
fprintf(stderr, "Error: MPI_Finalize failed.\n");
return 1;
}
return 0;
}
int test_init_finalize(FILE *test_log)
{
int ierr;
int errcount;
struct SMIOL_context *context;
int num_io_tasks;
int io_stride;
fprintf(test_log, "********************************************************************************\n");
fprintf(test_log, "************ SMIOL_init / SMIOL_finalize unit tests ****************************\n");
fprintf(test_log, "\n");
errcount = 0;
MPI_Comm_size(MPI_COMM_WORLD, &num_io_tasks);
io_stride = 1;
/* Null context pointer */
fprintf(test_log, "Null pointer to context pointer (SMIOL_init): ");
ierr = SMIOL_init(MPI_COMM_WORLD, num_io_tasks, io_stride, NULL);
if (ierr == SMIOL_SUCCESS) {
fprintf(test_log, "FAIL - SMIOL_SUCCESS was returned, when an error was expected\n");
errcount++;
}
else {
fprintf(test_log, "PASS\n");
}
/* Invalid MPI communicator, and with a non-NULL context that should be NULL on return */
fprintf(test_log, "Invalid MPI communicator (SMIOL_init): ");
context = (struct SMIOL_context *)NULL + 42; /* Any non-NULL value should be fine... */
ierr = SMIOL_init(MPI_COMM_NULL, num_io_tasks, io_stride, &context);
if (ierr == SMIOL_SUCCESS) {
fprintf(test_log, "FAIL - SMIOL_SUCCESS was returned, when an error was expected\n");
errcount++;
}
else if (context != NULL) {
fprintf(test_log, "FAIL - an error code was returned, but context was not NULL\n");
errcount++;
}
else {
fprintf(test_log, "PASS\n");
}
/* Handle NULL context in SMIOL_finalize */
fprintf(test_log, "Handle NULL context (SMIOL_finalize): ");
context = NULL;
ierr = SMIOL_finalize(&context);
if (ierr == SMIOL_SUCCESS && context == NULL) {
fprintf(test_log, "PASS\n");
}
else if (context != NULL) {
fprintf(test_log, "FAIL - context is not NULL\n");
errcount++;
}
else {
fprintf(test_log, "FAIL - context is NULL as expected, but SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Handle NULL pointer to context pointer in SMIOL_finalize */
fprintf(test_log, "Handle NULL pointer to context pointer (SMIOL_finalize): ");
ierr = SMIOL_finalize(NULL);
if (ierr == SMIOL_SUCCESS) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Everything OK for SMIOL_init */
fprintf(test_log, "Everything OK (SMIOL_init): ");
context = NULL;
ierr = SMIOL_init(MPI_COMM_WORLD, num_io_tasks, io_stride, &context);
if (ierr == SMIOL_SUCCESS && context != NULL) {
fprintf(test_log, "PASS\n");
}
else if (ierr == SMIOL_SUCCESS && context == NULL) {
fprintf(test_log, "FAIL - context is NULL, although SMIOL_SUCCESS was returned\n");
errcount++;
}
else if (ierr != SMIOL_SUCCESS && context != NULL) {
fprintf(test_log, "FAIL - context is not NULL as expected, but SMIOL_SUCCESS was not returned\n");
errcount++;
}
else {
fprintf(test_log, "FAIL - context is NULL, and SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Everything OK for SMIOL_finalize */
fprintf(test_log, "Everything OK (SMIOL_finalize): ");
ierr = SMIOL_finalize(&context);
if (ierr == SMIOL_SUCCESS && context == NULL) {
fprintf(test_log, "PASS\n");
}
else if (context != NULL) {
fprintf(test_log, "FAIL - context is not NULL\n");
errcount++;
}
else {
fprintf(test_log, "FAIL - context is NULL as expected, but SMIOL_SUCCESS was not returned\n");
errcount++;
}
fflush(test_log);
ierr = MPI_Barrier(MPI_COMM_WORLD);
fprintf(test_log, "\n");
return errcount;
}
int test_open_close(FILE *test_log)
{
int ierr;
int errcount;
struct SMIOL_context *context;
struct SMIOL_file *file;
int num_io_tasks;
int io_stride;
fprintf(test_log, "********************************************************************************\n");
fprintf(test_log, "************ SMIOL_open_file / SMIOL_close_file unit tests *********************\n");
fprintf(test_log, "\n");
errcount = 0;
MPI_Comm_size(MPI_COMM_WORLD, &num_io_tasks);
io_stride = 1;
/* Create a SMIOL context for testing file open/close routines */
ierr = SMIOL_init(MPI_COMM_WORLD, num_io_tasks, io_stride, &context);
if (ierr != SMIOL_SUCCESS || context == NULL) {
fprintf(test_log, "Failed to create SMIOL context...\n");
return -1;
}
/* Try to open a file with an invalid mode */
fprintf(test_log, "Try to open a file with an invalid mode: ");
file = NULL;
ierr = SMIOL_open_file(context, "smiol_invalid.nc", ~(SMIOL_FILE_CREATE | SMIOL_FILE_WRITE | SMIOL_FILE_READ), &file, (size_t)64000000, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_INVALID_ARGUMENT && file == NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - expected error code of SMIOL_INVALID_ARGUMENT not returned, or file not NULL\n");
errcount++;
}
/* Try to create a file with an invalid file format */
fprintf(test_log, "Try to create a file with an invalid file format: ");
file = NULL;
ierr = SMIOL_open_file(context, "smiol_invalid_fformat.nc", SMIOL_FILE_CREATE, &file, (size_t)64000000, ~(SMIOL_FORMAT_CDF2 | SMIOL_FORMAT_CDF5));
if (ierr == SMIOL_INVALID_FORMAT && file == NULL){
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - expected error code of SMIOL_INVALID_FORMAT not returned or file not NULL\n");
errcount++;
}
/* Try to create a file with the so far unused file format */
fprintf(test_log, "Try to create a file with CDF2 file format: ");
file = NULL;
ierr = SMIOL_open_file(context, "smiol_cdf2.nc", SMIOL_FILE_CREATE, &file, (size_t)64000000, SMIOL_FORMAT_CDF2);
if (ierr == SMIOL_SUCCESS && file != NULL){
ierr = SMIOL_close_file(&file);
if (ierr == SMIOL_SUCCESS){
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - unable to close newly created CDF2 format file\n");
errcount++;
}
}
else {
fprintf(test_log, "FAIL - unable to create file of CDF2 format\n");
errcount++;
}
/* Try open a file with an invalid file format (ensure fformat is ignored for opening files) */
fprintf(test_log,"Try to open a file with an ignored invalid file format: ");
file = NULL;
ierr = SMIOL_open_file(context, "smiol_cdf2.nc", SMIOL_FILE_READ, &file, (size_t)64000000, ~(SMIOL_FORMAT_CDF2 | SMIOL_FORMAT_CDF5));
if (ierr == SMIOL_SUCCESS && file != NULL){
ierr = SMIOL_close_file(&file);
if (ierr == SMIOL_SUCCESS){
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - unable to close file opened with an ignored invalid file format\n");
errcount++;
}
}
else {
fprintf(test_log, "FAIL - unable to open file with an ignored invalid file format\n");
errcount++;
}
#ifdef SMIOL_PNETCDF
/* Try to create a file for which we should not have sufficient permissions */
fprintf(test_log, "Try to create a file with insufficient permissions: ");
file = NULL;
ierr = SMIOL_open_file(context, "/smiol_test.nc", SMIOL_FILE_CREATE, &file, (size_t)64000000, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_LIBRARY_ERROR && file == NULL) {
fprintf(test_log, "PASS (%s)\n", SMIOL_lib_error_string(context));
}
else {
fprintf(test_log, "FAIL - expected error code of SMIOL_LIBRARY_ERROR not returned, or file not NULL\n");
errcount++;
}
/* Try to open a file that does not exist */
fprintf(test_log, "Try to open a non-existent file: ");
file = NULL;
ierr = SMIOL_open_file(context, "/smiol_foobar.nc", SMIOL_FILE_READ, &file, (size_t)0, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_LIBRARY_ERROR && file == NULL) {
fprintf(test_log, "PASS (%s)\n", SMIOL_lib_error_string(context));
}
else {
fprintf(test_log, "FAIL - expected error code of SMIOL_LIBRARY_ERROR not returned, or file not NULL\n");
errcount++;
}
#if 0
/*
* Since members of the SMIOL_file struct may be used in various ways within
* SMIOL_close_file, and without being able to verify that the contents of the
* SMIOL_file are valid, this test may lead to segfaults or other crashes.
* Until we have a way of verifying the validity of a SMIOL_file before using
* its contents, this test should probably just be omitted.
*/
/* Try to close a file that was never opened */
fprintf(test_log, "Try to close a file that was never opened: ");
file = (struct SMIOL_file *)malloc(sizeof(struct SMIOL_file));
file->context = context;
ierr = SMIOL_close_file(&file);
free(file);
if (ierr == SMIOL_LIBRARY_ERROR) {
fprintf(test_log, "PASS (%s)\n", SMIOL_lib_error_string(context));
}
else {
fprintf(test_log, "FAIL - expected error code of SMIOL_LIBRARY_ERROR not returned\n");
errcount++;
}
#endif
/* Create a file to be closed and opened again */
fprintf(test_log, "Create a file to be closed and later re-opened: ");
file = NULL;
ierr = SMIOL_open_file(context, "pnetcdf_test_c.nc", SMIOL_FILE_CREATE, &file, (size_t)64000000, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_SUCCESS && file != NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL (%s) - failed to create a new file\n", SMIOL_lib_error_string(context));
errcount++;
}
/* Close the file that was just created */
fprintf(test_log, "Close the file that was just created: ");
ierr = SMIOL_close_file(&file);
if (ierr == SMIOL_SUCCESS && file == NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - context is not NULL or SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Re-open the file with read access */
fprintf(test_log, "Re-open file with read access: ");
file = NULL;
ierr = SMIOL_open_file(context, "pnetcdf_test_c.nc", SMIOL_FILE_READ, &file, (size_t)0, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_SUCCESS && file != NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL (%s) - failed to re-open existing file\n", SMIOL_lib_error_string(context));
errcount++;
}
/* Close the file */
fprintf(test_log, "Close the file: ");
ierr = SMIOL_close_file(&file);
if (ierr == SMIOL_SUCCESS && file == NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - context is not NULL or SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Re-open the file with write access */
fprintf(test_log, "Re-open file with write access: ");
file = NULL;
ierr = SMIOL_open_file(context, "pnetcdf_test_c.nc", SMIOL_FILE_WRITE, &file, (size_t)64000000, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_SUCCESS && file != NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL (%s) - failed to re-open existing file\n", SMIOL_lib_error_string(context));
errcount++;
}
/* Close the file */
fprintf(test_log, "Close the file: ");
ierr = SMIOL_close_file(&file);
if (ierr == SMIOL_SUCCESS && file == NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - context is not NULL or SMIOL_SUCCESS was not returned\n");
errcount++;
}
#endif
/* Everything OK (SMIOL_open_file) */
fprintf(test_log, "Everything OK (SMIOL_open_file): ");
file = NULL;
ierr = SMIOL_open_file(context, "test.nc", SMIOL_FILE_CREATE, &file, (size_t)64000000, SMIOL_FORMAT_CDF5);
if (ierr == SMIOL_SUCCESS && file != NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - context is NULL or SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Everything OK (SMIOL_close_file) */
fprintf(test_log, "Everything OK (SMIOL_close_file): ");
ierr = SMIOL_close_file(&file);
if (ierr == SMIOL_SUCCESS && file == NULL) {
fprintf(test_log, "PASS\n");
}
else {
fprintf(test_log, "FAIL - context is not NULL or SMIOL_SUCCESS was not returned\n");
errcount++;
}
/* Free the SMIOL context */
ierr = SMIOL_finalize(&context);
if (ierr != SMIOL_SUCCESS || context != NULL) {
fprintf(test_log, "Failed to free SMIOL context...\n");
return -1;
}
fflush(test_log);
ierr = MPI_Barrier(MPI_COMM_WORLD);
fprintf(test_log, "\n");
return errcount;
}
int test_decomp(FILE *test_log)
{
int ierr;
int errcount = 0;
int comm_rank;
int comm_size;
size_t i;
size_t n_compute_elements;
SMIOL_Offset *compute_elements = NULL;
struct SMIOL_context *context = NULL;
struct SMIOL_decomp *decomp = NULL;
int num_io_tasks;
int io_stride;
fprintf(test_log, "********************************************************************************\n");
fprintf(test_log, "************ SMIOL_create_decomp / SMIOL_free_decomp unit tests ****************\n");
fprintf(test_log, "\n");
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
if (ierr != MPI_SUCCESS) {
fprintf(test_log, "Failed to get MPI rank...\n");
return -1;
}
ierr = MPI_Comm_size(MPI_COMM_WORLD, &comm_size);
if (ierr != MPI_SUCCESS) {
fprintf(test_log, "Failed to get MPI size...\n");
return -1;
}
num_io_tasks = comm_size;
io_stride = 1;
/* Create a SMIOL context for testing decomp routines */
ierr = SMIOL_init(MPI_COMM_WORLD, num_io_tasks, io_stride, &context);
if (ierr != SMIOL_SUCCESS || context == NULL) {
fprintf(test_log, "Failed to create SMIOL context...\n");
return -1;
}
/* Test create decomp with compute_elements == NULL and n_compute != 0 */
fprintf(test_log, "Testing SMIOL_create_decomp with NULL elements and n_elements != 0: ");
n_compute_elements = 1;
ierr = SMIOL_create_decomp(context, n_compute_elements, compute_elements, 1, &decomp);
if (ierr == SMIOL_INVALID_ARGUMENT && decomp == NULL) {
fprintf(test_log, "PASS\n");
} else {
fprintf(test_log, "FAIL - Either SMIOL_INVALID_ARGUMENT was not returned or decomp was not NULL\n");
errcount++;
}
fprintf(test_log, "Everything OK (SMIOL_free_decomp) with NULL elements: ");
ierr = SMIOL_free_decomp(&decomp);
if (ierr == SMIOL_SUCCESS && decomp == NULL) {
fprintf(test_log, "PASS\n");
} else if (ierr == SMIOL_SUCCESS && decomp != NULL) {
fprintf(test_log, "FAIL - Returned SMIOL_SUCCESS but, decomp was not NULL\n");
errcount++;
} else if (ierr != SMIOL_SUCCESS && decomp == NULL) {
fprintf(test_log, "FAIL - decomp was NULL but did not returned SMIOL_SUCCESS\n");
errcount++;
}
/* Create and Free Decomp with elements == 0 */
fprintf(test_log, "Everything OK (SMIOL_create_decomp) with 0 elements: ");
n_compute_elements = 0;
compute_elements = malloc(sizeof(SMIOL_Offset) * n_compute_elements);
ierr = SMIOL_create_decomp(context, n_compute_elements, compute_elements, 1, &decomp);
if (ierr == SMIOL_SUCCESS && decomp != NULL) {
fprintf(test_log, "PASS\n");
} else {
fprintf(test_log, "FAIL - SMIOL_SUCCESS was not returned or decomp was NULL\n");
errcount++;
}
free(compute_elements);
fprintf(test_log, "Everything OK (SMIOL_free_decomp) with 0 elements: ");
ierr = SMIOL_free_decomp(&decomp);
if (ierr == SMIOL_SUCCESS && decomp == NULL) {
fprintf(test_log, "PASS\n");
} else if (ierr == SMIOL_SUCCESS && decomp != NULL) {
fprintf(test_log, "FAIL - Returned SMIOL_SUCCESS but, decomp was not NULL\n");
errcount++;
} else if (ierr != SMIOL_SUCCESS && decomp == NULL) {
fprintf(test_log, "FAIL - decomp was NULL but did not returned SMIOL_SUCCESS\n");
errcount++;
}
/* In case an error occured above, free the decomp to continue testing */
if (decomp != NULL) {
free(decomp);
decomp = NULL;
}
/* Check for error with negative aggregation factor */
fprintf(test_log, "Check for error with negative aggregation factor: ");
n_compute_elements = 0;
compute_elements = malloc(sizeof(SMIOL_Offset) * n_compute_elements);
ierr = SMIOL_create_decomp(context, n_compute_elements, compute_elements, -1, &decomp);
if (ierr == SMIOL_INVALID_ARGUMENT && decomp == NULL) {
fprintf(test_log, "PASS\n");
} else if (ierr != SMIOL_INVALID_ARGUMENT) {
fprintf(test_log, "FAIL - SMIOL_INVALID_ARGUMENT was not returned\n");
errcount++;
} else if (decomp != NULL) {
fprintf(test_log, "FAIL - decomp was not NULL on return\n");
errcount++;
}
free(compute_elements);
if (decomp != NULL) {
/*
* Since we do not expect decomp to be non-NULL, we have no way
* to know what state decomp is in, so just leak memory and nullify
* rather than calling SMIOL_free_decomp with unknown memory.
*/
decomp = NULL;
}
/* Create and Free Decomp with elements == 1 */