-
Notifications
You must be signed in to change notification settings - Fork 2
/
smiol_runner.F90
3925 lines (3400 loc) · 158 KB
/
smiol_runner.F90
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 "smiol_codes.inc"
program smiol_runner
use iso_c_binding, only : c_size_t, c_float
use SMIOLf
use mpi
implicit none
integer :: ierr
integer :: i
real :: f
integer :: my_proc_id
integer :: test_log = 42
integer(kind=c_size_t) :: n_compute_elements = 1
integer :: num_io_tasks, io_stride
integer(kind=SMIOL_offset_kind), dimension(:), pointer :: compute_elements
type (SMIOLf_decomp), pointer :: decomp => null()
type (SMIOLf_context), pointer :: context => null()
type (SMIOLf_file), pointer :: file => null()
character(len=16) :: log_fname
character(len=32), dimension(2) :: dimnames
integer(kind=SMIOL_offset_kind) :: dimsize
integer :: ndims
character(len=32) :: tempstr
real(kind=c_float), dimension(:), pointer :: real32_buf
call MPI_Init(ierr)
if (ierr /= MPI_SUCCESS) then
write(0,'(a)') 'Error: MPI_Init failed'
stop 1
end if
call MPI_Comm_rank(MPI_COMM_WORLD, my_proc_id, ierr)
if (ierr /= MPI_SUCCESS) then
write(0,'(a)') 'Error: MPI_Comm_rank failed'
stop 1
end if
write(log_fname, '(a,i4.4,a)') "smiolf.", my_proc_id, ".test"
open(unit=test_log, file=log_fname, status='replace')
!
! Unit tests for SMIOL_init and SMIOL_finalize
!
ierr = test_init_finalize(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
end if
!
! Unit tests for SMIOL_open_file and SMIOL_close_file
!
ierr = test_open_close(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
end if
!
! Unit tests for dimensions
!
ierr = test_dimensions(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
end if
!
! Unit tests for variables
!
ierr = test_variables(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
end if
!
! Unit tests for attributes
!
ierr = test_attributes(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
end if
!
! Unit tests for SMIOL_create_decomp and SMIOL_free_decomp
!
ierr = test_decomp(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
!
! Unit tests for SMIOL_sync_file
!
ierr = test_file_sync(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
!
! Unit tests for SMIOL_set/get_Frame
!
ierr = test_set_get_frame(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
!
! Unit tests for SMIOLf_f_to_c_string
!
ierr = test_f_to_c_string(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
!
! Unit tests for SMIOL_put/get_var
!
ierr = test_put_get_var(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
!
! Unit tests for I/O aggregation
!
ierr = test_io_aggregation(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
!
! Unit tests for buffered I/O
!
ierr = test_buffered_io(test_log)
if (ierr == 0) then
write(test_log,'(a)') 'All tests PASSED!'
write(test_log,'(a)') ''
else
write(test_log,'(i3,a)') ierr, ' tests FAILED!'
write(test_log,'(a)') ''
endif
num_io_tasks = 16
io_stride = 4
if (SMIOLf_init(MPI_COMM_WORLD, num_io_tasks, io_stride, context) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_init' was not called successfully"
stop 1
endif
if (.not. associated(context)) then
write(test_log,'(a)') 'Error: SMIOLf_init returned an unassociated context'
stop 1
end if
n_compute_elements = 100
allocate(compute_elements(n_compute_elements))
compute_elements(:) = 0
if (SMIOLf_create_decomp(context, n_compute_elements, compute_elements, &
decomp, aggregation_factor=1) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "Error: SMIOLf_create_decomp was not called successfully"
stop 1
endif
deallocate(compute_elements)
if (SMIOLf_free_decomp(decomp) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "Error: SMIOLf_free_decomp was not called successfully"
stop 1
endif
if (SMIOLf_inquire() /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire' was not called successfully"
stop 1
endif
if (SMIOLf_open_file(context, "blahf.nc", SMIOL_FILE_CREATE, file) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_open_file' was not called successfully"
stop 1
endif
if (SMIOLf_define_dim(file, 'Time', -1_SMIOL_offset_kind) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_define_dim' was not called successfully"
stop 1
endif
if (SMIOLf_sync_file(file) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_sync_file' was not called successfully"
stop 1
endif
if (SMIOLf_define_dim(file, 'nCells', 40962_SMIOL_offset_kind) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_define_dim' was not called successfully"
stop 1
endif
if (SMIOLf_inquire_dim(file, 'nCells', dimsize) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire_dim' was not called successfully"
stop 1
endif
write(test_log,'(a,i6)') 'Size of nCells dimension is ', dimsize
if (SMIOLf_sync_file(file) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_sync_file' was not called successfully"
stop 1
endif
dimnames(1) = 'nCells'
dimnames(2) = 'Time'
if (SMIOLf_define_var(file, 'theta', SMIOL_REAL32, 2, dimnames) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_define_var' was not called successfully"
stop 1
endif
if (SMIOLf_inquire_var(file, 'theta', ndims=ndims) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire_var' was not called successfully"
stop 1
endif
i = 2
if (SMIOLf_define_att(file, 'theta', 'time_levels', i) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_define_att' was not called successfully"
stop 1
endif
f = 3.14159265
if (SMIOLf_define_att(file, '', 'pi', f) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_define_att' was not called successfully"
stop 1
endif
if (SMIOLf_define_att(file, '', 'title', 'MPAS-Atmosphere v7.0') /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_define_att' was not called successfully"
stop 1
endif
#ifdef SMIOL_PNETCDF
if (SMIOLf_inquire_att(file, 'theta', 'time_levels', i) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire_att' was not called successfully"
stop 1
endif
#else
if (SMIOLf_inquire_att(file, '', 'title', tempstr) /= SMIOL_WRONG_ARG_TYPE) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire_att' was not called successfully"
stop 1
endif
#endif
#ifdef SMIOL_PNETCDF
if (SMIOLf_inquire_att(file, '', 'title', tempstr) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire_att' was not called successfully"
stop 1
endif
#else
if (SMIOLf_inquire_att(file, '', 'title', tempstr) /= SMIOL_WRONG_ARG_TYPE) then
write(test_log,'(a)') "ERROR: 'SMIOLf_inquire_att' was not called successfully"
stop 1
endif
#endif
allocate(real32_buf(40962))
real32_buf(:) = 0.0
if (SMIOLf_put_var(file, 'theta', decomp, real32_buf) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_put_var' was not called successfully"
stop 1
endif
if (SMIOLf_get_var(file, 'theta', decomp, real32_buf) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_get_var' was not called successfully"
stop 1
endif
deallocate(real32_buf)
if (SMIOLf_close_file(file) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_close_file' was not called successfully"
stop 1
endif
if (SMIOLf_set_option() /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_set_option' was not called successfully"
stop 1
endif
write(test_log,'(a)') "Testing SMIOLf_error_string success: ", trim(SMIOLf_error_string(SMIOL_SUCCESS))
write(test_log,'(a)') "Testing SMIOLf_error_string unkown error: ", trim(SMIOLf_error_string(1))
write(test_log,'(a)') "Testing SMIOLf_error_string malloc returned a null pointer: ", &
trim(SMIOLf_error_string(SMIOL_MALLOC_FAILURE))
write(test_log,'(a)') "Testing SMIOL_error_string test invalid subroutine argument: ", &
trim(SMIOLf_error_string(SMIOL_INVALID_ARGUMENT))
write(test_log,'(a)') "Testing SMIOL_error_string test internal MPI call failed: ", &
trim(SMIOLf_error_string(SMIOL_MPI_ERROR))
write(test_log,'(a)') "Testing SMIOL_error_string test Fortran wrapper detected an inconsistency in C return values: ", &
trim(SMIOLf_error_string(SMIOL_FORTRAN_ERROR))
write(test_log,'(a)') "Testing SMIOL_error_string: bad return code from a library call: ", &
trim(SMIOLf_error_string(SMIOL_LIBRARY_ERROR))
write(test_log,'(a)') "Testing SMIOL_lib_error_string: Could not find matching library for the source of the error: ", &
trim(SMIOLf_lib_error_string(context))
write(test_log,'(a)') "Testing SMIOL_error_string 'argument is of the wrong type': ", &
trim(SMIOLf_error_string(SMIOL_WRONG_ARG_TYPE))
write(test_log,'(a)') "Testing SMIOL_error_string 'argument is of insufficient size': ", &
trim(SMIOLf_error_string(SMIOL_INSUFFICIENT_ARG))
write(test_log,'(a)') "Testing SMIOL_error_string 'invalid format for file creation': ", &
trim(SMIOLf_error_string(SMIOL_INVALID_FORMAT))
if (SMIOLf_finalize(context) /= SMIOL_SUCCESS) then
write(test_log,'(a)') "ERROR: 'SMIOLf_finalize' was not called successfully"
stop 1
endif
if (associated(context)) then
write(test_log,'(a)') 'Error: SMIOLf_finalize returned an associated context'
stop 1
end if
close(test_log)
call MPI_Finalize(ierr)
if (ierr /= MPI_SUCCESS) then
write(0,'(a)') 'Error: MPI_Finalize failed'
stop 1
end if
stop 0
contains
function test_init_finalize(test_log) result(ierrcount)
implicit none
integer, intent(in) :: test_log
integer :: ierrcount
type (SMIOLf_context), pointer :: context
type (SMIOLf_context), pointer :: context_temp
integer :: num_io_tasks
integer :: io_stride
write(test_log,'(a)') '********************************************************************************'
write(test_log,'(a)') '************ SMIOLf_init / SMIOLf_finalize unit tests **************************'
write(test_log,'(a)') ''
ierrcount = 0
call MPI_Comm_size(MPI_COMM_WORLD, num_io_tasks, ierr)
io_stride = 1
! Invalid MPI communicator, and with an associated context that should be nullified
write(test_log,'(a)',advance='no') 'Invalid MPI communicator (SMIOLf_init): '
allocate(context_temp)
context => context_temp
ierr = SMIOLf_init(MPI_COMM_NULL, num_io_tasks, io_stride, context)
deallocate(context_temp)
if (ierr == SMIOL_SUCCESS) then
write(test_log,'(a)') 'FAIL - SMIOL_SUCCESS was returned, when an error was expected'
ierrcount = ierrcount + 1
else if (associated(context)) then
write(test_log,'(a)') 'FAIL - an error code was returned, but context was not nullified'
ierrcount = ierrcount + 1
else
write(test_log,'(a)') 'PASS'
end if
! Handle unassociated context in SMIOL_finalize
write(test_log,'(a)',advance='no') 'Handle unassociated context (SMIOLf_finalize): '
nullify(context)
ierr = SMIOLf_finalize(context)
if (ierr == SMIOL_SUCCESS .and. .not. associated(context)) then
write(test_log,'(a)') 'PASS'
else if (associated(context)) then
write(test_log,'(a)') 'FAIL - context is associated'
ierrcount = ierrcount + 1
else
write(test_log,'(a)') 'FAIL - context is unassociated as expected, but SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
! Everything OK for SMIOLf_init
write(test_log,'(a)',advance='no') 'Everything OK (SMIOLf_init): '
nullify(context)
ierr = SMIOLf_init(MPI_COMM_WORLD, num_io_tasks, io_stride, context)
if (ierr == SMIOL_SUCCESS .and. associated(context)) then
write(test_log,'(a)') 'PASS'
else if (ierr == SMIOL_SUCCESS .and. .not. associated(context)) then ! May not be possible at present...
write(test_log,'(a)') 'FAIL - context is not associated, although SMIOL_SUCCESS was returned'
ierrcount = ierrcount + 1
else if (ierr /= SMIOL_SUCCESS .and. associated(context)) then
write(test_log,'(a)') 'FAIL - context is associated as expected, but SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
else
write(test_log,'(a)') 'FAIL - context is not associated, and SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
! Everything OK for SMIOLf_finalize
write(test_log,'(a)',advance='no') 'Everything OK (SMIOLf_finalize): '
ierr = SMIOLf_finalize(context)
if (ierr == SMIOL_SUCCESS .and. .not. associated(context)) then
write(test_log,'(a)') 'PASS'
else if (associated(context)) then
write(test_log,'(a)') 'FAIL - context is associated'
ierrcount = ierrcount + 1
else
write(test_log,'(a)') 'FAIL - context is not associated as expected, but SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
write(test_log,'(a)') ''
end function test_init_finalize
function test_open_close(test_log) result(ierrcount)
#if 0
use iso_c_binding, only : c_loc
#endif
implicit none
integer, intent(in) :: test_log
integer :: ierrcount
type (SMIOLf_context), pointer :: context
type (SMIOLf_file), pointer :: file
integer :: num_io_tasks
integer :: io_stride
write(test_log,'(a)') '********************************************************************************'
write(test_log,'(a)') '************ SMIOL_open_file / SMIOL_close_file unit tests *********************'
write(test_log,'(a)') ''
ierrcount = 0
call MPI_Comm_size(MPI_COMM_WORLD, num_io_tasks, ierr)
io_stride = 1
! Create a SMIOL context for testing file open/close routines
nullify(context)
ierr = SMIOLf_init(MPI_COMM_WORLD, num_io_tasks, io_stride, context)
if (ierr /= SMIOL_SUCCESS .or. .not. associated(context)) then
ierrcount = -1
return
end if
! Try to open a file with an invalid mode
write(test_log,'(a)',advance='no') 'Try to open a file with an invalid mode: '
nullify(file)
ierr = SMIOLf_open_file(context, 'smiol_invalid.nc', &
not(ior(ior(SMIOL_FILE_CREATE, SMIOL_FILE_WRITE), SMIOL_FILE_READ)), file)
if (ierr == SMIOL_INVALID_ARGUMENT .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - expected error code of SMIOL_INVALID_ARGUMENT not returned, or file handle is associated'
ierrcount = ierrcount + 1
end if
! Try to create a file with an invalid format
write(test_log,'(a)', advance='no') 'Try to create a file with an invalid file format: '
nullify(file)
ierr = SMIOLf_open_file(context, 'smiolf_invalid_frmt.nc', &
SMIOL_FILE_CREATE, file, &
fformat=not(ior(SMIOL_FORMAT_CDF2, SMIOL_FORMAT_CDF5)))
if (ierr == SMIOL_INVALID_FORMAT .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log, '(a)') 'FAIL - expected error code of SMIOL_INVALID_FORMT not returned, or file handle is associated'
ierrcount = ierrcount + 1
end if
! Try to create the file with the other, optional file format that needs to be requested
write(test_log,'(a)', advance='no') 'Try to create a file with CDF2 file format: '
nullify(file)
ierr = SMIOLf_open_file(context, 'smiolf_cdf2.nc', &
SMIOL_FILE_CREATE, file, &
fformat=SMIOL_FORMAT_CDF2)
if (ierr == SMIOL_SUCCESS .and. associated(file)) then
ierr = SMIOLf_close_file(file)
if (ierr == SMIOL_SUCCESS) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - unable to close newly created CDF2 format file'
ierrcount = ierrcount + 1
end if
else
write(test_log, '(a)') 'FAIL - unable to create file of CDF2 format'
ierrcount = ierrcount + 1
end if
! Try to open a file with an invalid file format (ensure fformat is ignored for opening files)
write(test_log,'(a)', advance='no') 'Try to open a file with an ignored invalid file format: '
nullify(file)
ierr = SMIOLf_open_file(context, 'smiolf_cdf2.nc', &
SMIOL_FILE_READ, file, &
fformat=not(ior(SMIOL_FORMAT_CDF2, SMIOL_FORMAT_CDF5)))
if (ierr == SMIOL_SUCCESS .and. associated(file)) then
ierr = SMIOLf_close_file(file)
if (ierr == SMIOL_SUCCESS) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - unable to close file opened with an ignored invalid file format'
ierrcount = ierrcount + 1
end if
else
write(test_log,'(a)') 'FAIL - unable to open file with an ignored invalid file format'
ierrcount = ierrcount + 1
end if
#ifdef SMIOL_PNETCDF
! Try to create a file for which we should not have sufficient permissions
write(test_log,'(a)',advance='no') 'Try to create a file with insufficient permissions: '
nullify(file)
ierr = SMIOLf_open_file(context, '/smiol_test.nc', SMIOL_FILE_CREATE, file)
if (ierr == SMIOL_LIBRARY_ERROR .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS ('//trim(SMIOLf_lib_error_string(context))//')'
else
write(test_log,'(a)') 'FAIL - expected error code of SMIOL_LIBRARY_ERROR not returned, or file handle is associated'
ierrcount = ierrcount + 1
end if
! Try to open a file that does not exist
write(test_log,'(a)',advance='no') 'Try to open a non-existent file: '
nullify(file)
ierr = SMIOLf_open_file(context, '/smiol_foobar.nc', SMIOL_FILE_READ, file)
if (ierr == SMIOL_LIBRARY_ERROR .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS ('//trim(SMIOLf_lib_error_string(context))//')'
else
write(test_log,'(a)') 'FAIL - expected error code of SMIOL_LIBRARY_ERROR not returned, or file handle is associated'
ierrcount = ierrcount + 1
end if
#if 0
!
! This test will not work under most compilers:
! * The flang compiler complains that file % context requires a reference to a TYPE(C_PTR)
! * The Intel compiler generates a double-free error when SMIOL_close_file tries to free
! the 'file' allocated by Fortran
!
! Try to close a file that was never opened
write(test_log,'(a)',advance='no') 'Try to close a file that was never opened: '
allocate(file)
file % context = c_loc(context)
ierr = SMIOLf_close_file(file)
if (associated(file)) deallocate(file)
if (ierr == SMIOL_LIBRARY_ERROR) then
write(test_log,'(a)') 'PASS ('//trim(SMIOLf_lib_error_string(context))//')'
else
write(test_log,'(a)') 'FAIL - expected error code of SMIOL_LIBRARY_ERROR not returned'
ierrcount = ierrcount + 1
end if
#endif
! Create a file to be closed and opened again
write(test_log,'(a)',advance='no') 'Create a file to be closed and later re-opened: '
nullify(file)
ierr = SMIOLf_open_file(context, 'pnetcdf_test_f.nc', SMIOL_FILE_CREATE, file)
if (ierr == SMIOL_SUCCESS .and. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL ('//trim(SMIOLf_lib_error_string(context))//') - failed to create a new file'
ierrcount = ierrcount + 1
end if
! Close the file that was just created
write(test_log,'(a)',advance='no') 'Close the file that was just created: '
ierr = SMIOLf_close_file(file)
if (ierr == SMIOL_SUCCESS .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - file handle is associated or SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
! Re-open the file with read access
write(test_log,'(a)',advance='no') 'Re-open file with read access: '
nullify(file)
ierr = SMIOLf_open_file(context, 'pnetcdf_test_f.nc', SMIOL_FILE_READ, file)
if (ierr == SMIOL_SUCCESS .and. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL ('//trim(SMIOLf_lib_error_string(context))//') - failed to re-open existing file'
ierrcount = ierrcount + 1
end if
! Close the file
write(test_log,'(a)',advance='no') 'Close the file: '
ierr = SMIOLf_close_file(file)
if (ierr == SMIOL_SUCCESS .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - file handle is associated or SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
! Re-open the file with write access
write(test_log,'(a)',advance='no') 'Re-open file with write access: '
nullify(file)
ierr = SMIOLf_open_file(context, 'pnetcdf_test_f.nc', SMIOL_FILE_WRITE, file)
if (ierr == SMIOL_SUCCESS .and. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL ('//trim(SMIOLf_lib_error_string(context))//') - failed to re-open existing file'
ierrcount = ierrcount + 1
end if
! Close the file
write(test_log,'(a)',advance='no') 'Close the file: '
ierr = SMIOLf_close_file(file)
if (ierr == SMIOL_SUCCESS .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - file handle is associated or SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
#endif
! Everything OK (SMIOLf_open_file)
write(test_log,'(a)',advance='no') 'Everything OK (SMIOLf_open_file): '
nullify(file)
ierr = SMIOLf_open_file(context, 'test_fortran.nc', SMIOL_FILE_CREATE, file)
if (ierr == SMIOL_SUCCESS .and. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - file handle is not associated or SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
! Everything OK (SMIOLf_close_file)
write(test_log,'(a)',advance='no') 'Everything OK (SMIOLf_close_file): '
ierr = SMIOLf_close_file(file)
if (ierr == SMIOL_SUCCESS .and. .not. associated(file)) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - file handle is associated or SMIOL_SUCCESS was not returned'
ierrcount = ierrcount + 1
end if
! Free the SMIOL context
ierr = SMIOLf_finalize(context)
if (ierr /= SMIOL_SUCCESS .or. associated(context)) then
ierrcount = -1
return
end if
write(test_log,'(a)') ''
end function test_open_close
function test_decomp(test_log) result(ierrcount)
use iso_c_binding, only : c_size_t
implicit none
integer, intent(in) :: test_log
integer :: ierrcount
integer :: comm_size
integer :: comm_rank
integer(kind=c_size_t) :: n_compute_elements
integer(kind=c_size_t) :: i
integer(kind=SMIOL_offset_kind), dimension(:), pointer :: compute_elements
type (SMIOLf_context), pointer :: context
type (SMIOLf_decomp), pointer :: decomp => null()
logical :: matched
integer :: num_io_tasks
integer :: io_stride
write(test_log,'(a)') '********************************************************************************'
write(test_log,'(a)') '************ SMIOLf_create_decomp / SMIOLf_free_decomp tests *******************'
write(test_log,'(a)') ''
ierrcount = 0
call MPI_Comm_rank(MPI_COMM_WORLD, comm_rank, ierr)
if (ierr /= MPI_SUCCESS) then
write(test_log, '(a)') 'Failed to get MPI rank...'
ierrcount = -1
return
end if
call MPI_Comm_size(MPI_COMM_WORLD, comm_size, ierr)
if (ierr /= MPI_SUCCESS) then
write(test_log, '(a)') 'Failed to get MPI size...'
ierrcount = -1
return
end if
num_io_tasks = comm_size
io_stride = 1
! Create a SMIOL context for testing decomp routines
nullify(context)
ierr = SMIOLf_init(MPI_COMM_WORLD, num_io_tasks, io_stride, context)
if (ierr /= SMIOL_SUCCESS .or. .not. associated(context)) then
ierrcount = -1
return
end if
! Test with 0 elements
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_create_decomp with 0 elements: '
n_compute_elements = 0
allocate(compute_elements(n_compute_elements))
ierr = SMIOLf_create_decomp(context, n_compute_elements, compute_elements, decomp, aggregation_factor=1)
if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "PASS"
else
write(test_log, '(a)') "FAIL - Either SMIOL_SUCCESS was not returned or decomp was not associated"
ierrcount = ierrcount + 1
endif
deallocate(compute_elements)
! Free Decomp
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_free_decomp with 0 elements: '
ierr = SMIOLf_free_decomp(decomp)
if (ierr == SMIOL_SUCCESS .and. .not. associated(decomp)) then
write(test_log,'(a)') "PASS"
else if (ierr /= SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr did not return SMIOL_SUCCESS, and decomp was still associated"
ierrcount = ierrcount + 1
else if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr returned SMIOL_SUCCESS but the decomp was associated"
ierrcount = ierrcount + 1
endif
! Check for error with negative aggregation factor
write(test_log,'(a)',advance='no') 'Check for error with negative aggregation factor: '
n_compute_elements = 0
allocate(compute_elements(n_compute_elements))
ierr = SMIOLf_create_decomp(context, n_compute_elements, compute_elements, decomp, aggregation_factor=-1)
if (ierr == SMIOL_INVALID_ARGUMENT .and. .not. associated(decomp)) then
write(test_log,'(a)') 'PASS'
else if (ierr /= SMIOL_INVALID_ARGUMENT) then
write(test_log, '(a)') 'FAIL - SMIOL_INVALID_ARGUMENT was not returned'
ierrcount = ierrcount + 1
else if (associated(decomp)) then
write(test_log, '(a)') 'FAIL - decomp was associated on return'
ierrcount = ierrcount + 1
end if
deallocate(compute_elements)
if (associated(decomp)) then
!
! Since we do not expect decomp to be associated, we have no way
! to know what state decomp is in, so just leak memory and nullify
! rather than calling SMIOLf_free_decomp with unknown memory.
!
nullify(decomp)
end if
! Small number of Compute and IO Elements
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_create_decomp 1 element: '
n_compute_elements = 1
allocate(compute_elements(n_compute_elements))
compute_elements(:) = 0
ierr = SMIOLf_create_decomp(context, n_compute_elements, compute_elements, decomp, aggregation_factor=1)
if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "PASS"
else
write(test_log, '(a)') "FAIL - Either SMIOL_SUCCESS was not returned or decomp was not associated"
ierrcount = ierrcount + 1
endif
deallocate(compute_elements)
! Free Decomp
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_free_decomp with 1 element: '
ierr = SMIOLf_free_decomp(decomp)
if (ierr == SMIOL_SUCCESS .and. .not. associated(decomp)) then
write(test_log,'(a)') "PASS"
else if (ierr /= SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr did not return SMIOL_SUCCESS, and decomp was still associated"
ierrcount = ierrcount + 1
else if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr returned SMIOL_SUCCESS but the decomp was associated"
ierrcount = ierrcount + 1
endif
! Large number of Compute and IO Elements
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_create_decomp large number of elements: '
n_compute_elements = 1000000
allocate(compute_elements(n_compute_elements))
compute_elements(:) = 0
ierr = SMIOLf_create_decomp(context, n_compute_elements, compute_elements, decomp, aggregation_factor=1)
if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "PASS"
else
write(test_log, '(a)') "FAIL - Either SMIOL_SUCCESS was not returned or decomp was not associated"
ierrcount = ierrcount + 1
endif
deallocate(compute_elements)
! Free Decomp
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_free_decomp large number of elements: '
ierr = SMIOLf_free_decomp(decomp)
if (ierr == SMIOL_SUCCESS .and. .not. associated(decomp)) then
write(test_log,'(a)') "PASS"
else if (ierr /= SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr did not return SMIOL_SUCCESS, and decomp was still associated"
ierrcount = ierrcount + 1
else if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr returned SMIOL_SUCCESS but the decomp was associated"
ierrcount = ierrcount + 1
endif
! Pass SMIOLf_free_decomp a decomp that has already been freed
write(test_log,'(a)',advance='no') 'Everything OK for SMIOLf_free_decomp on an unassociated decomp: '
ierr = SMIOLf_free_decomp(decomp)
if (ierr == SMIOL_SUCCESS .and. .not. associated(decomp)) then
write(test_log,'(a)') "PASS"
else if (ierr /= SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr did not return SMIOL_SUCCESS, and decomp became associated..."
ierrcount = ierrcount + 1
else if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
write(test_log,'(a)') "FAIL - ierr returned SMIOL_SUCCESS, but the decomp became associated..."
ierrcount = ierrcount + 1
endif
! Free the SMIOL context
ierr = SMIOLf_finalize(context)
if (ierr /= SMIOL_SUCCESS .or. associated(context)) then
ierrcount = -1
return
end if
!
! The following tests will only be run if there are exactly two MPI tasks.
! In principle, as long as there are at least two MPI ranks in MPI_COMM_WORLD,
! an intracommunicator with exactly two ranks could be created for these tests.
!
if (comm_size == 2) then
! Create a SMIOL context for testing decomp routines
nullify(context)
ierr = SMIOLf_init(MPI_COMM_WORLD, 2, 1, context)
if (ierr /= SMIOL_SUCCESS .or. .not. associated(context)) then
ierrcount = -1
return
end if
! Odd/even compute, half/half I/O
write(test_log,'(a)',advance='no') 'Odd/even compute, half/half I/O: '
n_compute_elements = 4
allocate(compute_elements(n_compute_elements))
if (comm_rank == 0) then
do i = 1, n_compute_elements
compute_elements(i) = 2 * (i-1) + 1 ! Odd elements
end do
else
do i = 1, n_compute_elements
compute_elements(i) = 2 * (i-1) ! Even elements
end do
end if
ierr = SMIOLf_create_decomp(context, n_compute_elements, compute_elements, decomp, aggregation_factor=1)
if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
! The correct comp_list and io_list arrays, below, were verified manually
if (comm_rank == 0) then
matched = compare_decomps(decomp, &
[ integer(kind=SMIOL_offset_kind) :: 2, 0, 2, 0, 1, 1, 2, 2, 3 ], &
[ integer(kind=SMIOL_offset_kind) :: 2, 0, 2, 1, 3, 1, 2, 0, 2 ])
else
matched = compare_decomps(decomp, &
[ integer(kind=SMIOL_offset_kind) :: 2, 0, 2, 0, 1, 1, 2, 2, 3 ], &
[ integer(kind=SMIOL_offset_kind) :: 2, 0, 2, 1, 3, 1, 2, 0, 2 ])
end if
if (matched) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - the decomp did not contain the expected values'
ierrcount = ierrcount + 1
end if
else
write(test_log, '(a)') 'FAIL - Either SMIOL_SUCCESS was not returned or decomp was not associated'
ierrcount = ierrcount + 1
end if
deallocate(compute_elements)
ierr = SMIOLf_free_decomp(decomp)
if (ierr /= SMIOL_SUCCESS .or. associated(decomp)) then
write(test_log,'(a)') 'After previous unit test, SMIOL_free_decomp was unsuccessful: FAIL'
ierrcount = ierrcount + 1
end if
! Free the SMIOL context
ierr = SMIOLf_finalize(context)
if (ierr /= SMIOL_SUCCESS .or. associated(context)) then
ierrcount = -1
return
end if
! Create a SMIOL context for testing decomp routines
nullify(context)
ierr = SMIOLf_init(MPI_COMM_WORLD, 1, 2, context)
if (ierr /= SMIOL_SUCCESS .or. .not. associated(context)) then
ierrcount = -1
return
end if
! Even/odd compute, all/nothing I/O
write(test_log,'(a)',advance='no') 'Even/odd compute, all/nothing I/O: '
n_compute_elements = 4
allocate(compute_elements(n_compute_elements))
if (comm_rank == 0) then
do i = 1, n_compute_elements
compute_elements(i) = 2 * (i-1) ! Even elements
end do
else
do i = 1, n_compute_elements
compute_elements(i) = 2 * (i-1) + 1 ! Odd elements
end do
end if
ierr = SMIOLf_create_decomp(context, n_compute_elements, compute_elements, decomp, aggregation_factor=1)
if (ierr == SMIOL_SUCCESS .and. associated(decomp)) then
! The correct comp_list and io_list arrays, below, were verified manually
if (comm_rank == 0) then
matched = compare_decomps(decomp, &
[ integer(kind=SMIOL_offset_kind) :: 1, 0, 4, 0, 1, 2, 3 ], &
[ integer(kind=SMIOL_offset_kind) :: 2, 0, 4, 0, 2, 4, 6, 1, 4, 1, 3, 5, 7 ])
else
matched = compare_decomps(decomp, &
[ integer(kind=SMIOL_offset_kind) :: 1, 0, 4, 0, 1, 2, 3 ], &
[ integer(kind=SMIOL_offset_kind) :: 0 ])
end if
if (matched) then
write(test_log,'(a)') 'PASS'
else
write(test_log,'(a)') 'FAIL - the decomp did not contain the expected values'
ierrcount = ierrcount + 1
end if
else
write(test_log, '(a)') 'FAIL - Either SMIOL_SUCCESS was not returned or decomp was not associated'
ierrcount = ierrcount + 1
end if
deallocate(compute_elements)