Skip to content

Commit 39e5584

Browse files
authored
Merge pull request #2016 from NCAR/fix/netcdf4_var_par_access
Collective access when extending a file
2 parents 88769ea + 559bf63 commit 39e5584

File tree

5 files changed

+114
-10
lines changed

5 files changed

+114
-10
lines changed

src/clib/pio_getput_int.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,6 @@ PIOc_put_vars_tc(int ncid, int varid, const PIO_Offset *start, const PIO_Offset
10811081
else
10821082
fake_stride = (PIO_Offset *)stride;
10831083
}
1084-
10851084
#ifdef _PNETCDF
10861085
if (file->iotype == PIO_IOTYPE_PNETCDF)
10871086
{
@@ -1137,6 +1136,15 @@ PIOc_put_vars_tc(int ncid, int varid, const PIO_Offset *start, const PIO_Offset
11371136
{
11381137
PLOG((2, "PIOc_put_vars_tc calling netcdf function file->iotype = %d",
11391138
file->iotype));
1139+
#ifdef _NETCDF4
1140+
if (file->iotype == PIO_IOTYPE_NETCDF4P)
1141+
{
1142+
PLOG((2, "Setting NC_COLLECTIVE mode"));
1143+
if ((ierr = nc_var_par_access(file->fh, varid, NC_COLLECTIVE)))
1144+
return pio_err(ios, file, ierr, __FILE__, __LINE__);
1145+
}
1146+
1147+
#endif
11401148
switch(xtype)
11411149
{
11421150
case NC_BYTE:

src/clib/pioc_support.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,7 @@ PIOc_openfile_retry(int iosysid, int *ncidp, int *iotype, const char *filename,
27202720
#ifdef _MPISERIAL
27212721
ierr = nc_open(filename, mode, &file->fh);
27222722
#else
2723-
imode = mode | NC_MPIIO;
2723+
imode = mode | NC_MPIIO | NC_NETCDF4;
27242724
if ((ierr = nc_open_par(filename, imode, ios->io_comm, ios->info,
27252725
&file->fh))){
27262726
PLOG((2, "%d: PIOc_openfile_retry nc_open_par ierr=%d",__LINE__,ierr));
@@ -2736,6 +2736,7 @@ PIOc_openfile_retry(int iosysid, int *ncidp, int *iotype, const char *filename,
27362736
&pio_type_size, &mpi_type,
27372737
&mpi_type_size, &ndims)))
27382738
break;
2739+
27392740
PLOG((2, "PIOc_openfile_retry:nc_open_par filename = %s mode = %d "
27402741
"imode = %d ierr = %d", filename, mode, imode, ierr));
27412742
#endif

tests/cunit/test_pioc_putget.c

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ int putget_read_vars_nt(int ncid, int *varid, PIO_Offset *start, PIO_Offset *cou
17671767
* @returns 0 for success, error code otherwise.
17681768
*/
17691769
int create_putget_file(int iosysid, int access, int unlim, int flavor, int *dim_len,
1770-
int *varid, const char *filename, int *ncidp)
1770+
int *varid, const char *filename, int *ncidp, int *tsvarid)
17711771
{
17721772
int dimids[NDIM]; /* The dimension IDs. */
17731773
int num_vars = NUM_CLASSIC_TYPES + 1;
@@ -1786,7 +1786,9 @@ int create_putget_file(int iosysid, int access, int unlim, int flavor, int *dim_
17861786
return ret;
17871787

17881788
/* Are we using unlimited dimension? */
1789-
if (!unlim)
1789+
if (unlim)
1790+
dim_len[0] = NC_UNLIMITED;
1791+
else
17901792
dim_len[0] = NUM_TIMESTEPS;
17911793

17921794
/* Define netCDF dimensions and variable. */
@@ -1812,7 +1814,14 @@ int create_putget_file(int iosysid, int access, int unlim, int flavor, int *dim_
18121814
if ((ret = PIOc_def_var(ncid, var_name, my_type, NDIM, dimids, &varid[v])))
18131815
return ret;
18141816
}
1815-
1817+
if(unlim){
1818+
/* create an additional variable with only the unlimdim dimension */
1819+
if ((ret = PIOc_def_var(ncid, "timestep", PIO_INT, 1, dimids, tsvarid)))
1820+
{
1821+
printf("Defined a timestep variable %d\n", ret);
1822+
return ret;
1823+
}
1824+
}
18161825
/* For the first access, also test attributes. */
18171826
if (access == 0)
18181827
if ((ret = test_write_atts(ncid, varid, flavor)))
@@ -1908,7 +1917,8 @@ int test_putget(int iosysid, int num_flavors, int *flavor, int my_rank,
19081917
MPI_Comm test_comm)
19091918
{
19101919
int dim_len[NDIM] = {NC_UNLIMITED, X_DIM_LEN, Y_DIM_LEN};
1911-
1920+
int tsvarid;
1921+
19121922
#define NUM_ACCESS 8
19131923
for (int unlim = 0; unlim < 2; unlim++)
19141924
for (int access = 0; access < NUM_ACCESS; access++)
@@ -1931,15 +1941,15 @@ int test_putget(int iosysid, int num_flavors, int *flavor, int my_rank,
19311941

19321942
/* Create test file with dims and vars defined. */
19331943
if ((ret = create_putget_file(iosysid, access, unlim, flavor[fmt], dim_len, varid,
1934-
filename, &ncid)))
1944+
filename, &ncid, &tsvarid)))
19351945
return ret;
19361946

19371947
/* Write some data. */
19381948
PIO_Offset index[NDIM] = {0, 0, 0};
19391949
PIO_Offset start[NDIM] = {0, 0, 0};
19401950
PIO_Offset count[NDIM] = {1, X_DIM_LEN, Y_DIM_LEN};
19411951
PIO_Offset stride[NDIM] = {1, 1, 1};
1942-
1952+
PIO_Offset index1d = 0;
19431953
switch (access)
19441954
{
19451955
case 0:
@@ -2003,7 +2013,21 @@ int test_putget(int iosysid, int num_flavors, int *flavor, int my_rank,
20032013
/* Check contents of the file. */
20042014
if ((ret = check_file(access, ncid, varid, flavor[fmt], index, start, count, stride, unlim)))
20052015
return ret;
2006-
2016+
if(unlim > 0) {
2017+
/* try advancing the file */
2018+
index1d = 0;
2019+
if ((ret = PIOc_put_var1_int(ncid, tsvarid, &index1d, dim_len)))
2020+
{
2021+
printf("extend file time test %d\n",ret);
2022+
return ret;
2023+
}
2024+
index1d = 1;
2025+
if ((ret = PIOc_put_var1_int(ncid, tsvarid, &index1d, dim_len+1)))
2026+
{
2027+
printf("extend file time test %d\n",ret);
2028+
return ret;
2029+
}
2030+
}
20072031
/* Close the netCDF file. */
20082032
if ((ret = PIOc_closefile(ncid)))
20092033
ERR(ret);

tests/fncint/ftst_pio.f90

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ program ftst_pio
2424
integer, dimension(3) :: var_dim
2525
integer :: maplen
2626
integer :: decompid, iosysid
27-
integer :: varid, i
27+
integer :: varid, i, tsvarid
2828
integer :: ierr
2929

3030
! Set up MPI.
@@ -75,13 +75,19 @@ program ftst_pio
7575
! Define a data variable.
7676
ierr = nf_def_var(ncid, VAR_NAME, NF_INT, NDIM3, var_dim, varid)
7777
if (ierr .ne. nf_noerr) call handle_err(ierr)
78+
79+
ierr = nf_def_var(ncid, 'timestep', NF_INT, 1, var_dim(3), tsvarid)
80+
7881
ierr = nf_enddef(ncid)
7982
if (ierr .ne. nf_noerr) call handle_err(ierr)
8083

8184
! Write 1st record with distributed arrays.
8285
ierr = nf_put_vard_int(ncid, varid, decompid, 1, data_buffer)
8386
if (ierr .ne. nf_noerr) call handle_err(ierr)
8487

88+
ierr = nf_put_var(ncid, tsvarid, 2, 2)
89+
if (ierr .ne. nf_noerr) call handle_err(ierr)
90+
8591
! Close the file.
8692
ierr = nf_close(ncid)
8793
if (ierr .ne. nf_noerr) call handle_err(ierr)

tests/general/ncdf_simple_tests.F90.in

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,69 @@ PIO_TF_AUTO_TEST_SUB_BEGIN test_def_var
100100

101101
PIO_TF_AUTO_TEST_SUB_END test_def_var
102102

103+
PIO_TF_AUTO_TEST_SUB_BEGIN test_extend_unlimited_var
104+
use ncdf_simple_tests_tgv
105+
Implicit none
106+
type(file_desc_t) :: pio_file
107+
type(var_desc_t) :: pio_var
108+
integer :: pio_dim
109+
integer :: ret, i
110+
integer, parameter :: initial_len = 5, extend_len = 3
111+
integer, dimension(initial_len + extend_len) :: data_out, data_in
112+
character(len=PIO_TF_MAX_STR_LEN), parameter :: fname = "pio_test_extend_unlim.nc"
113+
114+
! Create file with unlimited dimension and write initial data
115+
ret = PIO_createfile(pio_tf_iosystem_, pio_file, tgv_iotype, fname, PIO_CLOBBER)
116+
PIO_TF_CHECK_ERR(ret, "Failed to create:" // trim(fname))
117+
118+
ret = PIO_def_dim(pio_file, 'unlim_dim', PIO_UNLIMITED, pio_dim) ! -1 for unlimited
119+
PIO_TF_CHECK_ERR(ret, "Failed to define unlimited dim:" // trim(fname))
120+
121+
ret = PIO_def_var(pio_file, 'unlim_var', PIO_int, (/pio_dim/), pio_var)
122+
PIO_TF_CHECK_ERR(ret, "Failed to define var:" // trim(fname))
123+
124+
ret = PIO_enddef(pio_file)
125+
PIO_TF_CHECK_ERR(ret, "Failed to enddef:" // trim(fname))
126+
127+
do i = 1, initial_len
128+
data_out(i) = i
129+
end do
130+
ret = PIO_put_var(pio_file, pio_var, data_out(1:initial_len))
131+
PIO_TF_CHECK_ERR(ret, "Failed to write initial data:" // trim(fname))
132+
133+
call PIO_closefile(pio_file)
134+
135+
! Reopen file and extend variable
136+
ret = PIO_openfile(pio_tf_iosystem_, pio_file, tgv_iotype, fname, PIO_write)
137+
PIO_TF_CHECK_ERR(ret, "Failed to reopen:" // trim(fname))
138+
139+
do i = 1, extend_len
140+
data_out(initial_len + i) = 100 + i
141+
end do
142+
ret = PIO_put_var(pio_file, pio_var, start=(/initial_len/), count=(/extend_len/), &
143+
ival=data_out(initial_len+1:initial_len+extend_len))
144+
PIO_TF_CHECK_ERR(ret, "Failed to extend data:" // trim(fname))
145+
146+
call PIO_syncfile(pio_file)
147+
148+
! Optionally, read back and verify
149+
ret = PIO_get_var(pio_file, pio_var, data_in)
150+
PIO_TF_CHECK_ERR(ret, "Failed to read back data:" // trim(fname))
151+
do i = 1, initial_len
152+
if (data_in(i) /= i) then
153+
print *, "ERROR: Initial data mismatch at ", i, ":", data_in(i), "!=", i
154+
end if
155+
end do
156+
do i = 1, extend_len
157+
if (data_in(initial_len + i) /= 100 + i) then
158+
print *, "ERROR: Extended data mismatch at ", initial_len + i, ":", data_in(initial_len + i), "!=", 100 + i
159+
end if
160+
end do
161+
162+
call PIO_closefile(pio_file)
163+
call PIO_deletefile(pio_tf_iosystem_, fname)
164+
165+
PIO_TF_AUTO_TEST_SUB_END test_extend_unlimited_var
103166
PIO_TF_TEMPLATE<PIO_TF_PREDEF_TYPENAME PIO_TF_DATA_TYPE, PIO_TF_PREDEF_TYPENAME PIO_TF_FC_DATA_TYPE>
104167
PIO_TF_AUTO_TEST_SUB_BEGIN test_data_conversion
105168
use ncdf_simple_tests_tgv
@@ -190,6 +253,8 @@ PIO_TF_TEST_DRIVER_BEGIN
190253

191254
! Make sure that global variables are set correctly before running the tests
192255
PIO_TF_AUTO_TESTS_RUN(trim(iotype_descs(i)))
256+
PRINT *, "PIO_TF: Starting test_extend_unlimited_var for ", trim(iotype_descs(i))
257+
CALL test_extend_unlimited_var()
193258

194259
call PIO_deletefile(pio_tf_iosystem_, tgv_fname)
195260
end do

0 commit comments

Comments
 (0)