Skip to content

Commit

Permalink
Merge pull request ESMCI#1566 from NCAR/ejh_ncint_fortran_decomp
Browse files Browse the repository at this point in the history
Fixing a memory leak in the netCDF integration layer
  • Loading branch information
edhartnett authored Jul 17, 2019
2 parents 9566912 + b657f85 commit 91c0564
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 46 deletions.
34 changes: 33 additions & 1 deletion src/flib/ncint_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,36 @@ end function PIOc_finalize
status = ierr
end function nf_free_iosystem

end module ncint_mod
! !>
! !! @public
! !! @ingroup PIO_initdecomp
! !! Implements the block-cyclic decomposition for PIO_initdecomp.
! !! This provides the ability to describe a computational
! !! decomposition in PIO that has a block-cyclic form. That is
! !! something that can be described using start and count arrays.
! !! Optional parameters for this subroutine allows for the
! !! specification of io decomposition using iostart and iocount
! !! arrays. If iostart and iocount arrays are not specified by the
! !! user, and rearrangement is turned on then PIO will calculate a
! !! suitable IO decomposition
! !!
! !! @param iosystem @copydoc iosystem_desc_t
! !! @param basepiotype @copydoc use_PIO_kinds
! !! @param dims An array of the global length of each dimesion of the
! !! variable(s)
! !! @param compstart The start index into the block-cyclic
! !! computational decomposition
! !! @param compcount The count for the block-cyclic computational
! !! decomposition
! !! @param iodesc @copydoc iodesc_generate
! !! @author Jim Edwards
! !<
! function nf_init_decomp(iosystem,basepiotype,dims,compstart,compcount,iodesc)
! type (iosystem_desc_t), intent(inout) :: iosystem
! integer(i4), intent(in) :: basepiotype
! integer(i4), intent(in) :: dims(:)
! integer (kind=PIO_OFFSET_KIND) :: compstart(:)
! integer (kind=PIO_OFFSET_KIND) :: compcount(:)
! type (IO_desc_t), intent(out) :: iodesc

end module ncint_mod
27 changes: 26 additions & 1 deletion src/ncint/ncintdispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,35 @@ PIO_NCINT_abort(int ncid)
return TEST_VAL_42;
}

/**
* Close a file opened with PIO.
*
* @param ncid the ncid for the PIO file.
* @param v ignored, use NULL.
*
* @return PIO_NOERR for success, error code otherwise.
* @author Ed Hartnett
*/
int
PIO_NCINT_close(int ncid, void *v)
{
return PIOc_closefile(ncid);
NC_FILE_INFO_T *h5;
int retval;

/* Tell PIO to close the file. */
if ((retval = PIOc_closefile(ncid)))
return retval;

/* Find our metadata for this file. */
if ((retval = nc4_find_grp_h5(ncid, NULL, &h5)))
return retval;
assert(h5);

/* Delete the group name. */
if ((retval = nc4_nc4f_list_del(h5)))
return retval;

return retval;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/fncint/ftst_pio.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,31 @@ program ftst_pio
integer :: ncid
character*(*) FILE_NAME
parameter (FILE_NAME='ftst_pio.nc')
integer, dimension(3) :: data_buffer, compdof
integer, dimension(1) :: dims
type(io_desc_t) :: iodesc_nCells
integer :: ierr

call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, myRank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, ntasks, ierr)


ierr = pio_set_log_level(2)
ierr = nf_set_log_level(2)
ierr = nf_init_intracom(myRank, MPI_COMM_WORLD, niotasks, numAggregator, &
stride, PIO_rearr_subset, ioSystem, base)


dims(1) = 3 * ntasks
compdof = 3 * myRank + (/1, 2, 3/) ! Where in the global array each task writes
data_buffer = myRank
call PIO_initdecomp(ioSystem, PIO_int, dims, compdof, iodesc_nCells)

ierr = nf_create(FILE_NAME, 64, ncid)
ierr = nf_close(ncid)

call PIO_freedecomp(ioSystem, iodesc_nCells)
ierr = nf_free_iosystem()
call MPI_Finalize(ierr)
if (myRank .eq. 0) then
Expand Down
88 changes: 44 additions & 44 deletions tests/ncint/tst_pio_udf.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,54 +68,54 @@ main(int argc, char **argv)

/* Create a file with a 3D record var. */
if (nc_create(FILE_NAME, NC_UDF0, &ncid)) ERR;
if (nc_def_dim(ncid, DIM_NAME_UNLIMITED, dimlen[0], &dimid[0])) ERR;
if (nc_def_dim(ncid, DIM_NAME_X, dimlen[1], &dimid[1])) ERR;
if (nc_def_dim(ncid, DIM_NAME_Y, dimlen[2], &dimid[2])) ERR;
if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIM3, dimid, &varid)) ERR;

/* Calculate a decomposition for distributed arrays. */
elements_per_pe = DIM_LEN_X * DIM_LEN_Y / ntasks;
if (!(compdof = malloc(elements_per_pe * sizeof(size_t))))
ERR;
for (i = 0; i < elements_per_pe; i++)
compdof[i] = my_rank * elements_per_pe + i;

/* Create the PIO decomposition for this test. */
if (nc_init_decomp(iosysid, PIO_INT, NDIM2, &dimlen[1], elements_per_pe,
compdof, &ioid, 1, NULL, NULL)) ERR;
free(compdof);

/* Create some data on this processor. */
if (!(my_data = malloc(elements_per_pe * sizeof(int)))) ERR;
for (i = 0; i < elements_per_pe; i++)
my_data[i] = my_rank * 10 + i;

/* Write some data with distributed arrays. */
if (nc_put_vard_int(ncid, varid, ioid, 0, my_data)) ERR;
if (nc_close(ncid)) ERR;

/* Check that our user-defined format has been added. */
if (nc_inq_user_format(NC_UDF0, &disp_in, NULL)) ERR;
if (disp_in != &NCINT_dispatcher) ERR;

/* Open the file. */
if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR;

/* Read distributed arrays. */
if (!(data_in = malloc(elements_per_pe * sizeof(int)))) ERR;
if (nc_get_vard_int(ncid, varid, ioid, 0, data_in)) ERR;

/* Check results. */
for (i = 0; i < elements_per_pe; i++)
if (data_in[i] != my_data[i]) ERR;
/* if (nc_def_dim(ncid, DIM_NAME_UNLIMITED, dimlen[0], &dimid[0])) ERR; */
/* if (nc_def_dim(ncid, DIM_NAME_X, dimlen[1], &dimid[1])) ERR; */
/* if (nc_def_dim(ncid, DIM_NAME_Y, dimlen[2], &dimid[2])) ERR; */
/* if (nc_def_var(ncid, VAR_NAME, NC_INT, NDIM3, dimid, &varid)) ERR; */

/* /\* Calculate a decomposition for distributed arrays. *\/ */
/* elements_per_pe = DIM_LEN_X * DIM_LEN_Y / ntasks; */
/* if (!(compdof = malloc(elements_per_pe * sizeof(size_t)))) */
/* ERR; */
/* for (i = 0; i < elements_per_pe; i++) */
/* compdof[i] = my_rank * elements_per_pe + i; */

/* /\* Create the PIO decomposition for this test. *\/ */
/* if (nc_init_decomp(iosysid, PIO_INT, NDIM2, &dimlen[1], elements_per_pe, */
/* compdof, &ioid, 1, NULL, NULL)) ERR; */
/* free(compdof); */

/* /\* Create some data on this processor. *\/ */
/* if (!(my_data = malloc(elements_per_pe * sizeof(int)))) ERR; */
/* for (i = 0; i < elements_per_pe; i++) */
/* my_data[i] = my_rank * 10 + i; */

/* /\* Write some data with distributed arrays. *\/ */
/* if (nc_put_vard_int(ncid, varid, ioid, 0, my_data)) ERR; */
/* if (nc_close(ncid)) ERR; */

/* /\* Check that our user-defined format has been added. *\/ */
/* if (nc_inq_user_format(NC_UDF0, &disp_in, NULL)) ERR; */
/* if (disp_in != &NCINT_dispatcher) ERR; */

/* /\* Open the file. *\/ */
/* if (nc_open(FILE_NAME, NC_UDF0, &ncid)) ERR; */

/* /\* Read distributed arrays. *\/ */
/* if (!(data_in = malloc(elements_per_pe * sizeof(int)))) ERR; */
/* if (nc_get_vard_int(ncid, varid, ioid, 0, data_in)) ERR; */

/* /\* Check results. *\/ */
/* for (i = 0; i < elements_per_pe; i++) */
/* if (data_in[i] != my_data[i]) ERR; */

/* Close file. */
if (nc_close(ncid)) ERR;

/* Free resources. */
free(data_in);
free(my_data);
if (nc_free_decomp(ioid)) ERR;
/* /\* Free resources. *\/ */
/* free(data_in); */
/* free(my_data); */
/* if (nc_free_decomp(ioid)) ERR; */
if (nc_free_iosystem(iosysid)) ERR;
}
SUMMARIZE_ERR;
Expand Down

0 comments on commit 91c0564

Please sign in to comment.