From 7dd23b6eb6913ef691f6fb081c555e340d2248e2 Mon Sep 17 00:00:00 2001 From: Ed Hartnett Date: Sat, 27 Jul 2019 06:03:51 -0600 Subject: [PATCH] added C test to guide fortran test building --- src/clib/pio_darray_int.c | 43 +++++++------ src/flib/ncint_mod.F90 | 2 +- tests/fncint/Makefile.am | 5 +- tests/fncint/ftst_pio.f90 | 8 +-- tests/fncint/ftst_pio_orig.f90 | 15 +++-- tests/fncint/run_tests.sh | 3 +- tests/fncint/tst_c_pio.c | 114 +++++++++++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 tests/fncint/tst_c_pio.c diff --git a/src/clib/pio_darray_int.c b/src/clib/pio_darray_int.c index 0a8d2b31016..20ed259fa41 100644 --- a/src/clib/pio_darray_int.c +++ b/src/clib/pio_darray_int.c @@ -1785,33 +1785,36 @@ flush_output_buffer(file_desc_t *file, bool force, PIO_Offset addsize) int request[reqcnt]; int status[reqcnt]; - for (int i = 0; i <= maxreq; i++) + if (file->varlist) { - if ((ierr = get_var_desc(i, &file->varlist, &vdesc))) - return pio_err(NULL, file, ierr, __FILE__, __LINE__); -#ifdef MPIO_ONESIDED - /*onesided optimization requires that all of the requests in a wait_all call represent - a contiguous block of data in the file */ - if (rcnt > 0 && (prev_record != vdesc->record || vdesc->nreqs == 0)) + for (int i = 0; i <= maxreq; i++) { - ierr = ncmpi_wait_all(file->fh, rcnt, request, status); - rcnt = 0; - } - prev_record = vdesc->record; + if ((ierr = get_var_desc(i, &file->varlist, &vdesc))) + return pio_err(NULL, file, ierr, __FILE__, __LINE__); +#ifdef MPIO_ONESIDED + /*onesided optimization requires that all of the requests in a wait_all call represent + a contiguous block of data in the file */ + if (rcnt > 0 && (prev_record != vdesc->record || vdesc->nreqs == 0)) + { + ierr = ncmpi_wait_all(file->fh, rcnt, request, status); + rcnt = 0; + } + prev_record = vdesc->record; #endif - for (reqcnt = 0; reqcnt < vdesc->nreqs; reqcnt++) - request[rcnt++] = max(vdesc->request[reqcnt], NC_REQ_NULL); - PLOG((3,"flush_output_buffer rcnt=%d",rcnt)); + for (reqcnt = 0; reqcnt < vdesc->nreqs; reqcnt++) + request[rcnt++] = max(vdesc->request[reqcnt], NC_REQ_NULL); + PLOG((3,"flush_output_buffer rcnt=%d",rcnt)); - if (vdesc->request != NULL) - free(vdesc->request); - vdesc->request = NULL; - vdesc->nreqs = 0; + if (vdesc->request != NULL) + free(vdesc->request); + vdesc->request = NULL; + vdesc->nreqs = 0; #ifdef FLUSH_EVERY_VAR - ierr = ncmpi_wait_all(file->fh, rcnt, request, status); - rcnt = 0; + ierr = ncmpi_wait_all(file->fh, rcnt, request, status); + rcnt = 0; #endif + } } if (rcnt > 0) diff --git a/src/flib/ncint_mod.F90 b/src/flib/ncint_mod.F90 index b94e5ade0dc..483633a2ebc 100644 --- a/src/flib/ncint_mod.F90 +++ b/src/flib/ncint_mod.F90 @@ -180,9 +180,9 @@ function nf_def_decomp(iosysid, basepiotype, dims, compdof, & integer(i4), intent(in) :: basepiotype integer(i4), intent(in) :: dims(:) integer (PIO_OFFSET_KIND), intent(in) :: compdof(:) + integer(i4), intent(out) :: decompid integer, optional, target :: rearr integer (PIO_OFFSET_KIND), optional :: iostart(:), iocount(:) - integer(i4), intent(inout) :: decompid type (io_desc_t) :: iodesc type (iosystem_desc_t) :: iosystem integer :: status diff --git a/tests/fncint/Makefile.am b/tests/fncint/Makefile.am index 98783787800..14d32577d00 100644 --- a/tests/fncint/Makefile.am +++ b/tests/fncint/Makefile.am @@ -12,8 +12,11 @@ LDADD += -lnetcdff # Find the pio.mod file. AM_FCFLAGS = -I${top_builddir}/src/flib ${CPPFLAGS} +# Find the pio.h and pio_tests.h file for the C test. +AM_CPPFLAGS = -I${top_srcdir}/src/clib -I${top_srcdir}/tests/cunit + # Build the test for make check. -check_PROGRAMS = ftst_pio ftst_pio_orig +check_PROGRAMS = ftst_pio ftst_pio_orig tst_c_pio ftst_pio_SOURCES = ftst_pio.f90 ftst_pio_orig_SOURCES = ftst_pio_orig.f90 diff --git a/tests/fncint/ftst_pio.f90 b/tests/fncint/ftst_pio.f90 index 2406a4d2cf0..ea47fd24e0d 100644 --- a/tests/fncint/ftst_pio.f90 +++ b/tests/fncint/ftst_pio.f90 @@ -39,13 +39,13 @@ program ftst_pio ! Define an IOSystem. ierr = nf_def_iosystem(my_rank, MPI_COMM_WORLD, niotasks, numAggregator, & - stride, PIO_rearr_subset, iosysid, base) + stride, PIO_rearr_box, iosysid, base) if (ierr .ne. nf_noerr) call handle_err(ierr) ! Define a 2D decomposition. - dims(1) = NLAT * 2 / ntasks - dims(2) = NLON * 2 / ntasks - maplen = dims(1) * dims(2) + dims(1) = 1 + dims(2) = 4 + maplen = 4 print *, 'dims: ', dims print *, 'maplen: ', maplen print *, 'my_rank: ', my_rank diff --git a/tests/fncint/ftst_pio_orig.f90 b/tests/fncint/ftst_pio_orig.f90 index c6b780b8122..cbf41787ea6 100644 --- a/tests/fncint/ftst_pio_orig.f90 +++ b/tests/fncint/ftst_pio_orig.f90 @@ -10,7 +10,7 @@ program ftst_pio include 'netcdf.inc' character*(*) FILE_NAME - parameter (FILE_NAME = 'ftst_pio.nc') + parameter (FILE_NAME = 'ftst_pio_orig.nc') integer :: NDIM3 = 3, NRECS = 2, NLAT = 4, NLON = 4 character*(*) LAT_NAME, LON_NAME, REC_NAME, VAR_NAME parameter (LAT_NAME = 'latitude', LON_NAME = 'longitude', & @@ -23,6 +23,7 @@ program ftst_pio integer, dimension(2) :: dims integer, dimension(3) :: var_dim type(iosystem_desc_t) :: ioSystem + type(file_desc_t) :: pioFileDesc type(io_desc_t) :: iodesc integer :: maplen integer :: decompid, iosysid @@ -61,13 +62,14 @@ program ftst_pio end do print *, 'compdof', my_rank, compdof -! call PIO_initdecomp(ioSystem, PIO_int, maplen, compdof, iodesc) + call PIO_initdecomp(ioSystem, PIO_int, dims, compdof, iodesc) ! ierr = nf_def_decomp(iosysid, PIO_int, dims, compdof, decompid) ! if (ierr .ne. nf_noerr) call handle_err(ierr) - ! ! Create a file. + ! Create a file. + ierr = PIO_createfile(ioSystem, pioFileDesc, PIO_IOTYPE_PNETCDF, FILE_NAME, PIO_clobber) ! ierr = nf_create(FILE_NAME, 64, ncid) ! if (ierr .ne. nf_noerr) call handle_err(ierr) @@ -89,11 +91,12 @@ program ftst_pio ! ierr = nf_put_vard_int(ncid, varid, decompid, 1, data_buffer) ! if (ierr .ne. nf_noerr) call handle_err(ierr) - ! ! Close the file. + ! Close the file. + call PIO_closefile(pioFileDesc) ! ierr = nf_close(ncid) - ! if (ierr .ne. nf_noerr) call handle_err(ierr) + if (ierr .ne. nf_noerr) call handle_err(ierr) - ! ! Free resources. + ! Free resources. ! ierr = nf_free_decomp(decompid) ! if (ierr .ne. nf_noerr) call handle_err(ierr) deallocate(compdof) diff --git a/tests/fncint/run_tests.sh b/tests/fncint/run_tests.sh index 774fdcd7aa2..081d09b51a9 100755 --- a/tests/fncint/run_tests.sh +++ b/tests/fncint/run_tests.sh @@ -11,7 +11,8 @@ trap exit INT TERM printf 'running Fortran tests for PIO netCDF integration...\n' #PIO_TESTS='ftst_pio_orig ftst_pio' -PIO_TESTS='ftst_pio_orig' +#PIO_TESTS='tst_c_pio ftst_pio_orig' +PIO_TESTS='tst_c_pio' success1=true for TEST in $PIO_TESTS diff --git a/tests/fncint/tst_c_pio.c b/tests/fncint/tst_c_pio.c new file mode 100644 index 00000000000..e020bb5ec2c --- /dev/null +++ b/tests/fncint/tst_c_pio.c @@ -0,0 +1,114 @@ +/* This program does a very simple I/O system and decomposition, and + * writes a simple file. + + Ed Hartnett, 7/27/19 +*/ + +#include "config.h" +#include +#include +#include +#include + +#define FILE_NAME "tst_c_pio.nc" +#define VAR_NAME "data_var" +#define DIM_NAME_UNLIMITED "dim_unlimited" +#define DIM_NAME_X "dim_x" +#define DIM_NAME_Y "dim_y" +#define DIM_LEN_X 4 +#define DIM_LEN_Y 4 +#define NDIM2 2 +#define NDIM3 3 +#define LLEN 4 +#define MPI_ERR 999 + +int +main(int argc, char **argv) +{ + int my_rank; + int ntasks; + + /* Initialize MPI. */ + if (MPI_Init(&argc, &argv)) + return MPI_ERR; + + /* Learn my rank and the total number of processors. */ + if (MPI_Comm_rank(MPI_COMM_WORLD, &my_rank)) + return MPI_ERR; + if (MPI_Comm_size(MPI_COMM_WORLD, &ntasks)) + return MPI_ERR; + + if (!my_rank) + { + printf("\n*** Testing simple use of PIO.\n"); + printf("*** testing creating of simple file..."); + } + { + int iosysid; + int ncid; + int dimid[NDIM3]; + int varid; + int ioid; + int dimlen[NDIM3] = {NC_UNLIMITED, DIM_LEN_X, DIM_LEN_Y}; + char dimname[NDIM3][NC_MAX_NAME + 1] = {DIM_NAME_UNLIMITED, DIM_NAME_X, DIM_NAME_Y}; + int iotype = PIO_IOTYPE_NETCDF; + int my_data[LLEN]; + PIO_Offset compmap[LLEN]; + int i; + int ret; + + PIOc_set_log_level(3); + + /* Initialize local data. */ + for (i = 0; i < LLEN; i++) + my_data[i] = my_rank * 10 + i; + + /* Initialize the IOSystem. */ + if ((ret = PIOc_Init_Intracomm(MPI_COMM_WORLD, 1, 1, 0, 0, &iosysid))) + return ret; + + /* Create a file. */ + if ((ret = PIOc_createfile(iosysid, &ncid, &iotype, FILE_NAME, 0))) + return ret; + + /* Define metadata. */ + for (i = 0; i < NDIM3; i++) + if ((ret = PIOc_def_dim(ncid, dimname[i], dimlen[i], &dimid[i]))) + return ret; + if ((ret = PIOc_def_var(ncid, VAR_NAME, PIO_INT, NDIM3, dimid, &varid))) + return ret; + if ((ret = PIOc_enddef(ncid))) + return ret; + + /* Create the decomposition. */ + for (i = 0; i < LLEN; i++) + compmap[i] = i + my_rank * LLEN; + if ((ret = PIOc_init_decomp(iosysid, PIO_INT, NDIM2, &dimlen[1], LLEN, compmap, + &ioid, PIO_REARR_BOX, NULL, NULL))) + return ret; + + /* Write data. */ + if ((ret = PIOc_setframe(ncid, varid, 0))) + return ret; + if ((ret = PIOc_write_darray(ncid, varid, ioid, LLEN, my_data, NULL))) + return ret; + + /* Close the file. */ + if ((ret = PIOc_closefile(ncid))) + return ret; + + /* Free the decomposition. */ + if ((ret = PIOc_freedecomp(iosysid, ioid))) + return ret; + + /* Free the IOSystem. */ + if ((ret = PIOc_free_iosystem(iosysid))) + return ret; + } + + if (!my_rank) + printf("\nSUCCESS!\n"); + /* Finalize MPI. */ + MPI_Finalize(); + return 0; +}