Skip to content

Commit db67cf3

Browse files
committed
Added logic to handle PIO data/define mode attribute writing issues.
1 parent 66d1ab8 commit db67cf3

File tree

1 file changed

+115
-41
lines changed

1 file changed

+115
-41
lines changed

src/framework/mpas_io.F

Lines changed: 115 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ module mpas_io
105105
module procedure MPAS_io_get_var_char1d
106106
end interface MPAS_io_get_var
107107

108+
interface put_att_pio
109+
module procedure put_att_generic_pio
110+
end interface put_att_pio
111+
108112
interface MPAS_io_put_var
109113
module procedure MPAS_io_put_var_int0d
110114
module procedure MPAS_io_put_var_int1d
@@ -5338,12 +5342,29 @@ subroutine MPAS_io_put_att_int0d(handle, attName, attValue, fieldname, syncVal,
53385342
end if
53395343

53405344
#ifdef MPAS_PIO_SUPPORT
5341-
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValueLocal)
5345+
! if (handle % preexisting_file) then
5346+
! pio_ierr = PIO_redef(handle % pio_file)
5347+
! if (pio_ierr /= PIO_noerr) then
5348+
! io_global_err = pio_ierr
5349+
! return
5350+
! end if
5351+
! end if
5352+
5353+
call put_att_pio(handle, varid, attName, attValueLocal, ierr=ierr)
5354+
!pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValueLocal)
53425355
if (pio_ierr /= PIO_noerr) then
53435356
io_global_err = pio_ierr
53445357
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
53455358
return
53465359
end if
5360+
5361+
! if (handle % preexisting_file) then
5362+
! pio_ierr = PIO_enddef(handle % pio_file)
5363+
! if (pio_ierr /= PIO_noerr) then
5364+
! io_global_err = pio_ierr
5365+
! return
5366+
! end if
5367+
! end if
53475368
#endif
53485369

53495370
#ifdef MPAS_SMIOL_SUPPORT
@@ -5685,11 +5706,22 @@ subroutine MPAS_io_put_att_real0d(handle, attName, attValue, fieldname, syncVal,
56855706
end if
56865707
end if
56875708

5709+
#ifdef MPAS_PIO_SUPPORT
5710+
! if (handle % preexisting_file) then
5711+
! pio_ierr = PIO_redef(handle % pio_file)
5712+
! if (pio_ierr /= PIO_noerr) then
5713+
! io_global_err = pio_ierr
5714+
! return
5715+
! end if
5716+
! end if
5717+
#endif
5718+
56885719
if ((new_attlist_node % attHandle % precision == MPAS_IO_SINGLE_PRECISION) .and. &
56895720
(MPAS_IO_NATIVE_PRECISION /= MPAS_IO_SINGLE_PRECISION)) then
56905721
singleVal = real(attValueLocal,R4KIND)
56915722
#ifdef MPAS_PIO_SUPPORT
5692-
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, singleVal)
5723+
call put_att_pio(handle, varid, attName, singleVal, ierr=ierr)
5724+
!pio_ierr = PIO_put_att(handle % pio_file, varid, attName, singleVal)
56935725
#endif
56945726

56955727
#ifdef MPAS_SMIOL_SUPPORT
@@ -5703,7 +5735,8 @@ subroutine MPAS_io_put_att_real0d(handle, attName, attValue, fieldname, syncVal,
57035735
(MPAS_IO_NATIVE_PRECISION /= MPAS_IO_DOUBLE_PRECISION)) then
57045736
doubleVal = real(attValueLocal,R8KIND)
57055737
#ifdef MPAS_PIO_SUPPORT
5706-
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, doubleVal)
5738+
call put_att_pio(handle, varid, attName, doubleVal, ierr=ierr)
5739+
!pio_ierr = PIO_put_att(handle % pio_file, varid, attName, doubleVal)
57075740
#endif
57085741

57095742
#ifdef MPAS_SMIOL_SUPPORT
@@ -5715,7 +5748,8 @@ subroutine MPAS_io_put_att_real0d(handle, attName, attValue, fieldname, syncVal,
57155748
#endif
57165749
else
57175750
#ifdef MPAS_PIO_SUPPORT
5718-
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValueLocal)
5751+
call put_att_pio(handle, varid, attName, attValueLocal, ierr=ierr)
5752+
!pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValueLocal)
57195753
#endif
57205754

57215755
#ifdef MPAS_SMIOL_SUPPORT
@@ -5733,6 +5767,14 @@ subroutine MPAS_io_put_att_real0d(handle, attName, attValue, fieldname, syncVal,
57335767
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
57345768
return
57355769
end if
5770+
5771+
! if (handle % preexisting_file) then
5772+
! pio_ierr = PIO_enddef(handle % pio_file)
5773+
! if (pio_ierr /= PIO_noerr) then
5774+
! io_global_err = pio_ierr
5775+
! return
5776+
! end if
5777+
! end if
57365778
#endif
57375779
#ifdef MPAS_SMIOL_SUPPORT
57385780
if (local_ierr /= SMIOL_SUCCESS) then
@@ -5949,6 +5991,74 @@ subroutine MPAS_io_put_att_real1d(handle, attName, attValue, fieldname, syncVal,
59495991

59505992
end subroutine MPAS_io_put_att_real1d
59515993

5994+
subroutine put_att_generic_pio(handle, varid, attName, attValue, ierr)
5995+
type (MPAS_IO_Handle_type), intent(inout) :: handle
5996+
integer, intent(in) :: varid
5997+
character (len=*), intent(in) :: attName
5998+
class(*), intent(in) :: attValue
5999+
integer, optional :: ierr
6000+
6001+
select type(attValue)
6002+
type is (integer)
6003+
call mpas_log_write('Calling PIO_put_att for integer attribute '//trim(attname))
6004+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6005+
type is (real(kind=R4KIND))
6006+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6007+
call mpas_log_write('Calling PIO_put_att for real(kind=R4KIND attribute '//trim(attname))
6008+
type is (real(kind=R8KIND))
6009+
call mpas_log_write('Calling PIO_put_att for real(kind=R8KIND attribute '//trim(attname))
6010+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6011+
type is (character(len=*))
6012+
call mpas_log_write('Calling PIO_put_att for text attribute '//trim(attname))
6013+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6014+
end select
6015+
if (pio_ierr /= PIO_noerr) then
6016+
6017+
io_global_err = pio_ierr
6018+
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
6019+
6020+
if (handle % preexisting_file .and. .not. handle % data_mode) then
6021+
call mpas_log_write('Calling PIO_redef')
6022+
pio_ierr = PIO_redef(handle % pio_file)
6023+
if (pio_ierr /= PIO_noerr) then
6024+
io_global_err = pio_ierr
6025+
return
6026+
end if
6027+
call mpas_log_write('Successfully called PIO_redef')
6028+
select type(attValue)
6029+
type is (integer)
6030+
call mpas_log_write('Calling PIO_put_att for integer attribute '//trim(attname))
6031+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6032+
type is (real(kind=R4KIND))
6033+
call mpas_log_write('Calling PIO_put_att for real(kind=R4KIND attribute '//trim(attname))
6034+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6035+
type is (real(kind=R8KIND))
6036+
call mpas_log_write('Calling PIO_put_att for real(kind=R8KIND attribute '//trim(attname))
6037+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6038+
type is (character(len=*))
6039+
call mpas_log_write('Calling PIO_put_att for text attribute '//trim(attname))
6040+
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, attValue)
6041+
end select
6042+
if (pio_ierr /= PIO_noerr) then
6043+
io_global_err = pio_ierr
6044+
return
6045+
end if
6046+
6047+
call mpas_log_write('Calling PIO_enddef')
6048+
pio_ierr = PIO_enddef(handle % pio_file)
6049+
if (pio_ierr /= PIO_noerr) then
6050+
io_global_err = pio_ierr
6051+
return
6052+
end if
6053+
call mpas_log_write('Successfully called PIO_enddef')
6054+
6055+
if (present(ierr)) ierr = MPAS_IO_NOERR
6056+
end if
6057+
return
6058+
end if
6059+
end subroutine
6060+
6061+
59526062

59536063
subroutine MPAS_io_put_att_text(handle, attName, attValue, fieldname, syncVal, ierr)
59546064

@@ -6100,43 +6210,7 @@ subroutine MPAS_io_put_att_text(handle, attName, attValue, fieldname, syncVal, i
61006210
end if
61016211

61026212
#ifdef MPAS_PIO_SUPPORT
6103-
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, trim(attValueLocal))
6104-
if (pio_ierr /= PIO_noerr) then
6105-
6106-
io_global_err = pio_ierr
6107-
if (present(ierr)) ierr = MPAS_IO_ERR_BACKEND
6108-
6109-
!
6110-
! If we are working with a pre-existing file and the text attribute is larger than in the file, we need
6111-
! to enter define mode before writing the attribute. Note the PIO_redef documentation:
6112-
! 'Entering and leaving netcdf define mode causes a file sync operation to occur,
6113-
! these operations can be very expensive in parallel systems.'
6114-
!
6115-
if (handle % preexisting_file .and. .not. handle % data_mode) then
6116-
pio_ierr = PIO_redef(handle % pio_file)
6117-
if (pio_ierr /= PIO_noerr) then
6118-
io_global_err = pio_ierr
6119-
return
6120-
end if
6121-
6122-
pio_ierr = PIO_put_att(handle % pio_file, varid, attName, trim(attValueLocal))
6123-
if (pio_ierr /= PIO_noerr) then
6124-
io_global_err = pio_ierr
6125-
return
6126-
end if
6127-
6128-
pio_ierr = PIO_enddef(handle % pio_file)
6129-
if (pio_ierr /= PIO_noerr) then
6130-
io_global_err = pio_ierr
6131-
return
6132-
end if
6133-
6134-
if (present(ierr)) ierr = MPAS_IO_NOERR
6135-
6136-
end if
6137-
6138-
return
6139-
end if
6213+
call put_att_pio(handle, varid, attName, attValueLocal, ierr=ierr)
61406214
#endif
61416215

61426216
#ifdef MPAS_SMIOL_SUPPORT

0 commit comments

Comments
 (0)