From cd9841c6ae84ba7bc08044d90da300c15052ef27 Mon Sep 17 00:00:00 2001 From: Ed Hartnett Date: Mon, 15 Aug 2011 13:27:10 +0000 Subject: [PATCH] added test for netCDF-4 performance --- nc_test4/Makefile.am | 4 +- nc_test4/bm_many_objs.c | 41 ++--------------- nc_test4/bm_netcdf4_recs.c | 94 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 37 deletions(-) create mode 100644 nc_test4/bm_netcdf4_recs.c diff --git a/nc_test4/Makefile.am b/nc_test4/Makefile.am index 2d4bf4c82..e55da9906 100644 --- a/nc_test4/Makefile.am +++ b/nc_test4/Makefile.am @@ -46,8 +46,10 @@ endif # LARGE_FILE_TESTS if BUILD_BENCHMARKS check_PROGRAMS += tst_create_files bm_file tst_chunks3 tst_ar4 \ tst_ar4_3d tst_ar4_4d bm_many_objs tst_h_many_atts bm_many_atts \ -tst_files2 tst_files3 tst_ar5 tst_h_files3 tst_mem tst_knmi +tst_files2 tst_files3 tst_ar5 tst_h_files3 tst_mem tst_knmi \ +bm_netcdf4_recs +bm_netcdf4_recs_SOURCES = bm_netcdf4_recs.c tst_utils.c bm_many_atts_SOURCES = bm_many_atts.c tst_utils.c bm_many_objs_SOURCES = bm_many_objs.c tst_utils.c tst_ar4_3d_SOURCES = tst_ar4_3d.c tst_utils.c diff --git a/nc_test4/bm_many_objs.c b/nc_test4/bm_many_objs.c index ba60a775f..887d3a695 100644 --- a/nc_test4/bm_many_objs.c +++ b/nc_test4/bm_many_objs.c @@ -1,10 +1,8 @@ -/* -Copyright 2010, UCAR/Unidata -See COPYRIGHT file for copying and redistribution conditions. - +/** \file This program benchmarks creating a netCDF file with many objects. -$Id $ +Copyright 2010, UCAR/Unidata See COPYRIGHT file for copying and +redistribution conditions. */ #include @@ -18,35 +16,6 @@ $Id $ /* We will create this file. */ #define FILE_NAME "bm_many_objs.nc" -/* Subtract the `struct timeval' values X and Y, storing the result in - RESULT. Return 1 if the difference is negative, otherwise 0. This - function from the GNU documentation. */ -int -timeval_subtract (result, x, y) - struct timeval *result, *x, *y; -{ - /* Perform the carry for the later subtraction by updating Y. */ - if (x->tv_usec < y->tv_usec) { - int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; - y->tv_usec -= 1000000 * nsec; - y->tv_sec += nsec; - } - if (x->tv_usec - y->tv_usec > 1000000) { - int nsec = (x->tv_usec - y->tv_usec) / 1000000; - y->tv_usec += 1000000 * nsec; - y->tv_sec -= nsec; - } - - /* Compute the time remaining to wait. - `tv_usec' is certainly positive. */ - result->tv_sec = x->tv_sec - y->tv_sec; - result->tv_usec = x->tv_usec - y->tv_usec; - - /* Return 1 if result is negative. */ - return x->tv_sec < y->tv_sec; -} - - int main(int argc, char **argv) { struct timeval start_time, end_time, diff_time; @@ -83,7 +52,7 @@ int main(int argc, char **argv) if(nc_put_var(grp, var, data)) ERR; if(g%1000 == 0) { /* only print every 1000th group name */ if (gettimeofday(&end_time, NULL)) ERR; - if (timeval_subtract(&diff_time, &end_time, &start_time)) ERR; + if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR; sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec; printf("%s\t%.3g sec\n", gname, sec); } @@ -114,7 +83,7 @@ int main(int argc, char **argv) if(nc_put_var(grp, var, data)) ERR; if(v%1000 == 0) { /* only print every 1000th variable name */ if (gettimeofday(&end_time, NULL)) ERR; - if (timeval_subtract(&diff_time, &end_time, &start_time)) ERR; + if (nc4_timeval_subtract(&diff_time, &end_time, &start_time)) ERR; sec = diff_time.tv_sec + 1.0e-6 * diff_time.tv_usec; printf("%s/%s\t%.3g sec\n", gname, vname, sec); } diff --git a/nc_test4/bm_netcdf4_recs.c b/nc_test4/bm_netcdf4_recs.c new file mode 100644 index 000000000..b5a3cefcf --- /dev/null +++ b/nc_test4/bm_netcdf4_recs.c @@ -0,0 +1,94 @@ +/** \file + +This program benchmarks creating a netCDF file and reading records. + +Copyright 2011, UCAR/Unidata See COPYRIGHT file for copying and +redistribution conditions. +*/ + +#include +#include +#include +#include +#include +#include +#include /* Extra high precision time info. */ + +/* We will create this file. */ +#define FILE_NAME "bm_netcdf4_recs.nc" + +int main(int argc, char **argv) +{ + struct timeval start_time, end_time, diff_time; + double sec; + int nitem = 10000; /* default number of objects of each type */ + int i; + int ncid; + int data[] = {42}; + int g, grp, numgrp; + char gname[16]; + int v, var, numvar, vn, vleft, nvars; + + int stat; /* return status */ + + /* dimension ids */ + int basetime_dim; + int forecast_dim; + int bounds_dim; + int latitude_dim; + int longitude_dim; + + /* dimension lengths */ + size_t basetime_len = NC_UNLIMITED; + size_t forecast_len = 32; + size_t bounds_len = 2; + size_t latitude_len = 121; + size_t longitude_len = 101; + + /* variable ids */ + int temperature_2m_id; + + /* rank (number of dimensions) for each variable */ +# define RANK_temperature_2m 4 + + /* variable shapes */ + int temperature_2m_dims[RANK_temperature_2m]; + static const float temperature_2m_FillValue_att[1] = {9.96921e+36} ; + static const float temperature_2m_missing_value_att[1] = {9.96921e+36} ; + static const float temperature_2m_valid_min_att[1] = {180} ; + static const float temperature_2m_valid_max_att[1] = {330} ; + + /* enter define mode */ + if (nc_create(FILE_NAME, NC_CLOBBER, &ncid)) ERR; + + /* define dimensions */ + if (nc_def_dim(ncid, "basetime", basetime_len, &basetime_dim)) ERR; + if (nc_def_dim(ncid, "forecast", forecast_len, &forecast_dim)) ERR; + if (nc_def_dim(ncid, "bounds", bounds_len, &bounds_dim)) ERR; + if (nc_def_dim(ncid, "latitude", latitude_len, &latitude_dim)) ERR; + if (nc_def_dim(ncid, "longitude", longitude_len, &longitude_dim)) ERR; + + /* define variables */ + temperature_2m_dims[0] = basetime_dim; + temperature_2m_dims[1] = forecast_dim; + temperature_2m_dims[2] = latitude_dim; + temperature_2m_dims[3] = longitude_dim; + if (nc_def_var(ncid, "temperature_2m", NC_FLOAT, RANK_temperature_2m, + temperature_2m_dims, &temperature_2m_id)) ERR; + + /* assign per-variable attributes */ + if (nc_put_att_text(ncid, temperature_2m_id, "long_name", 36, "Air temperature 2m above the surface")) ERR; + if (nc_put_att_text(ncid, temperature_2m_id, "units", 1, "K")) ERR; + if (nc_put_att_float(ncid, temperature_2m_id, "_FillValue", NC_FLOAT, 1, temperature_2m_FillValue_att)) ERR; + if (nc_put_att_float(ncid, temperature_2m_id, "missing_value", NC_FLOAT, 1, temperature_2m_missing_value_att)) ERR; + if (nc_put_att_float(ncid, temperature_2m_id, "valid_min", NC_FLOAT, 1, temperature_2m_valid_min_att)) ERR; + if (nc_put_att_float(ncid, temperature_2m_id, "valid_max", NC_FLOAT, 1, temperature_2m_valid_max_att)) ERR; + if (nc_put_att_text(ncid, temperature_2m_id, "standard_name", 15, "air_temperature")) ERR; + if (nc_put_att_text(ncid, temperature_2m_id, "cell_methods", 10, "area: mean")) ERR; + if (nc_put_att_text(ncid, temperature_2m_id, "coordinates", 5, "level")) ERR; + if (nc_close(ncid)) ERR; + + if (gettimeofday(&start_time, NULL)) ERR; + + return(0); +}