From 6736d2f72045a0913ad2c1d4f969ec07284c526a Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Tue, 30 Jun 2015 13:51:10 +0300 Subject: [PATCH] Reorder code within gmxfio Move all XDR writing code, including helper functions only used by it, into gmxfio_xdr.c and rename it into gmxfio-xdr.c. Remove these from gmxfio-impl.h and make them static. No changes to the actual code, except for some handling of the itmp variables in gmx_bool functions to satisfy gcc maybe-uninitialized warnings. Change-Id: If2190f9429585ac0e2ee86b24dd41969f4ae8d49 --- src/gromacs/fileio/gmxfio-impl.h | 14 - .../fileio/{gmxfio_rw.c => gmxfio-xdr.c} | 308 +++++++++++++++++- src/gromacs/fileio/gmxfio.c | 31 -- src/gromacs/fileio/gmxfio_xdr.c | 296 ----------------- 4 files changed, 296 insertions(+), 353 deletions(-) rename src/gromacs/fileio/{gmxfio_rw.c => gmxfio-xdr.c} (53%) delete mode 100644 src/gromacs/fileio/gmxfio_xdr.c diff --git a/src/gromacs/fileio/gmxfio-impl.h b/src/gromacs/fileio/gmxfio-impl.h index e91745a52d..f42facdc52 100644 --- a/src/gromacs/fileio/gmxfio-impl.h +++ b/src/gromacs/fileio/gmxfio-impl.h @@ -79,23 +79,9 @@ struct t_fileio a lock */ }; -/** Names for different items that can be read/written with gmx_fio_do_*() */ -extern const char *eioNames[eioNR]; - -/** check the number of items against the allowed number of items */ -void gmx_fio_check_nitem(int eio, int nitem, const char *file, - int line); -/** check the output type against allowed values */ -void gmx_fio_fe(t_fileio *fio, int eio, const char *desc, const char *srcfile, - int line); - /** lock the mutex associated with a fio */ void gmx_fio_lock(t_fileio *fio); /** unlock the mutex associated with a fio */ void gmx_fio_unlock(t_fileio *fio); -/** xdr read/write routine */ -gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio, - const char *desc, const char *srcfile, int line); - #endif diff --git a/src/gromacs/fileio/gmxfio_rw.c b/src/gromacs/fileio/gmxfio-xdr.c similarity index 53% rename from src/gromacs/fileio/gmxfio_rw.c rename to src/gromacs/fileio/gmxfio-xdr.c index 3e180847cb..6b9b5eb562 100644 --- a/src/gromacs/fileio/gmxfio_rw.c +++ b/src/gromacs/fileio/gmxfio-xdr.c @@ -36,10 +36,295 @@ */ #include "gmxpre.h" +#include +#include + #include "gromacs/fileio/gmxfio.h" +#include "gromacs/fileio/xdrf.h" +#include "gromacs/utility/fatalerror.h" +#include "gromacs/utility/smalloc.h" #include "gmxfio-impl.h" +static const char *eioNames[eioNR] = +{ + "REAL", "INT", "GMX_STE_T", "UCHAR", "NUCHAR", "USHORT", "RVEC", "NRVEC", + "IVEC", "STRING" +}; + +/* check the number of items given against the type */ +static void gmx_fio_check_nitem(int eio, int nitem, const char *file, int line) +{ + if ((nitem != 1) && !((eio == eioNRVEC) || (eio == eioNUCHAR))) + { + gmx_fatal(FARGS, + "nitem (%d) may differ from 1 only for %s or %s, not for %s" + "(%s, %d)", nitem, eioNames[eioNUCHAR], eioNames[eioNRVEC], + eioNames[eio], file, line); + } +} + + +/* output a data type error. */ +static void gmx_fio_fe(t_fileio *fio, int eio, const char *desc, + const char *srcfile, int line) +{ + + gmx_fatal(FARGS, "Trying to %s %s type %d (%s), src %s, line %d", + fio->bRead ? "read" : "write", desc, eio, + ((eio >= 0) && (eio < eioNR)) ? eioNames[eio] : "unknown", + srcfile, line); +} + +/* This is the part that reads xdr files. */ + +static gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio, + const char *desc, const char *srcfile, int line) +{ + unsigned char ucdum, *ucptr; + bool_t res = 0; + float fvec[DIM]; + double dvec[DIM]; + int j, m, *iptr, idum; + gmx_int64_t sdum; + real *ptr; + unsigned short us; + double d = 0; + float f = 0; + + gmx_fio_check_nitem(eio, nitem, srcfile, line); + switch (eio) + { + case eioREAL: + if (fio->bDouble) + { + if (item && !fio->bRead) + { + d = *((real *) item); + } + res = xdr_double(fio->xdr, &d); + if (item) + { + *((real *) item) = d; + } + } + else + { + if (item && !fio->bRead) + { + f = *((real *) item); + } + res = xdr_float(fio->xdr, &f); + if (item) + { + *((real *) item) = f; + } + } + break; + case eioFLOAT: + if (item && !fio->bRead) + { + f = *((float *) item); + } + res = xdr_float(fio->xdr, &f); + if (item) + { + *((float *) item) = f; + } + break; + case eioDOUBLE: + if (item && !fio->bRead) + { + d = *((double *) item); + } + res = xdr_double(fio->xdr, &d); + if (item) + { + *((double *) item) = d; + } + break; + case eioINT: + if (item && !fio->bRead) + { + idum = *(int *) item; + } + res = xdr_int(fio->xdr, &idum); + if (item) + { + *(int *) item = idum; + } + break; + case eioINT64: + if (item && !fio->bRead) + { + sdum = *(gmx_int64_t *) item; + } + res = xdr_int64(fio->xdr, &sdum); + if (item) + { + *(gmx_int64_t *) item = sdum; + } + break; + case eioUCHAR: + if (item && !fio->bRead) + { + ucdum = *(unsigned char *) item; + } + res = xdr_u_char(fio->xdr, &ucdum); + if (item) + { + *(unsigned char *) item = ucdum; + } + break; + case eioNUCHAR: + ucptr = (unsigned char *) item; + res = 1; + for (j = 0; (j < nitem) && res; j++) + { + res = xdr_u_char(fio->xdr, &(ucptr[j])); + } + break; + case eioUSHORT: + if (item && !fio->bRead) + { + us = *(unsigned short *) item; + } + res = xdr_u_short(fio->xdr, (unsigned short *) &us); + if (item) + { + *(unsigned short *) item = us; + } + break; + case eioRVEC: + if (fio->bDouble) + { + if (item && !fio->bRead) + { + for (m = 0; (m < DIM); m++) + { + dvec[m] = ((real *) item)[m]; + } + } + res = xdr_vector(fio->xdr, (char *) dvec, DIM, + (unsigned int) sizeof(double), + (xdrproc_t) xdr_double); + if (item) + { + for (m = 0; (m < DIM); m++) + { + ((real *) item)[m] = dvec[m]; + } + } + } + else + { + if (item && !fio->bRead) + { + for (m = 0; (m < DIM); m++) + { + fvec[m] = ((real *) item)[m]; + } + } + res = xdr_vector(fio->xdr, (char *) fvec, DIM, + (unsigned int) sizeof(float), + (xdrproc_t) xdr_float); + if (item) + { + for (m = 0; (m < DIM); m++) + { + ((real *) item)[m] = fvec[m]; + } + } + } + break; + case eioNRVEC: + ptr = NULL; + res = 1; + for (j = 0; (j < nitem) && res; j++) + { + if (item) + { + ptr = ((rvec *) item)[j]; + } + res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line); + } + break; + case eioIVEC: + iptr = (int *) item; + res = 1; + for (m = 0; (m < DIM) && res; m++) + { + if (item && !fio->bRead) + { + idum = iptr[m]; + } + res = xdr_int(fio->xdr, &idum); + if (item) + { + iptr[m] = idum; + } + } + break; + case eioSTRING: + { + char *cptr; + int slen; + + if (item) + { + if (!fio->bRead) + { + slen = strlen((char *) item) + 1; + } + else + { + slen = 0; + } + } + else + { + slen = 0; + } + + if (xdr_int(fio->xdr, &slen) <= 0) + { + gmx_fatal(FARGS, "wrong string length %d for string %s" + " (source %s, line %d)", slen, desc, srcfile, line); + } + if (!item && fio->bRead) + { + snew(cptr, slen); + } + else + { + cptr = (char *)item; + } + if (cptr) + { + res = xdr_string(fio->xdr, &cptr, slen); + } + else + { + res = 1; + } + if (!item && fio->bRead) + { + sfree(cptr); + } + break; + } + default: + gmx_fio_fe(fio, eio, desc, srcfile, line); + } + if ((res == 0) && (fio->bDebug)) + { + fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n", + eioNames[eio], desc, fio->fn, srcfile, line); + } + + return (res != 0); +} + /******************************************************************* * * READ/WRITE FUNCTIONS @@ -93,18 +378,18 @@ gmx_bool gmx_fio_doe_gmx_bool(t_fileio *fio, gmx_bool *item, const char *desc, const char *srcfile, int line) { gmx_bool ret; - int itmp; gmx_fio_lock(fio); if (fio->bRead) { - ret = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line); - *item = itmp; + int itmp = 0; + ret = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line); + *item = itmp; } else { - itmp = *item; - ret = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line); + int itmp = *item; + ret = do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line); } gmx_fio_unlock(fio); return ret; @@ -238,22 +523,21 @@ gmx_bool gmx_fio_ndoe_gmx_bool(t_fileio *fio, gmx_bool *item, int n, const char *desc, const char *srcfile, int line) { gmx_bool ret = TRUE; - int i, itmp; + int i; gmx_fio_lock(fio); for (i = 0; i < n; i++) { if (fio->bRead) { - ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc, - srcfile, line); - item[i] = itmp; + int itmp = 0; + ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line); + item[i] = itmp; } else { - itmp = item[i]; - ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc, - srcfile, line); + int itmp = item[i]; + ret = ret && do_xdr(fio, &itmp, 1, eioINT, desc, srcfile, line); } } gmx_fio_unlock(fio); diff --git a/src/gromacs/fileio/gmxfio.c b/src/gromacs/fileio/gmxfio.c index cd487b1b5c..6b00014243 100644 --- a/src/gromacs/fileio/gmxfio.c +++ b/src/gromacs/fileio/gmxfio.c @@ -81,12 +81,6 @@ static t_fileio *open_files = NULL; during the simulation. */ static tMPI_Thread_mutex_t open_file_mutex = TMPI_THREAD_MUTEX_INITIALIZER; -const char *eioNames[eioNR] = -{ - "REAL", "INT", "GMX_STE_T", "UCHAR", "NUCHAR", "USHORT", "RVEC", "NRVEC", - "IVEC", "STRING" -}; - /****************************************************************** * * Internal functions: @@ -109,31 +103,6 @@ static int gmx_fio_int_flush(t_fileio* fio) return rc; } - -/* check the number of items given against the type */ -void gmx_fio_check_nitem(int eio, int nitem, const char *file, int line) -{ - if ((nitem != 1) && !((eio == eioNRVEC) || (eio == eioNUCHAR))) - { - gmx_fatal(FARGS, - "nitem (%d) may differ from 1 only for %s or %s, not for %s" - "(%s, %d)", nitem, eioNames[eioNUCHAR], eioNames[eioNRVEC], - eioNames[eio], file, line); - } -} - - -/* output a data type error. */ -void gmx_fio_fe(t_fileio *fio, int eio, const char *desc, - const char *srcfile, int line) -{ - - gmx_fatal(FARGS, "Trying to %s %s type %d (%s), src %s, line %d", - fio->bRead ? "read" : "write", desc, eio, - ((eio >= 0) && (eio < eioNR)) ? eioNames[eio] : "unknown", - srcfile, line); -} - /* lock the mutex associated with this fio. This needs to be done for every type of access to the fio's elements. */ void gmx_fio_lock(t_fileio *fio) diff --git a/src/gromacs/fileio/gmxfio_xdr.c b/src/gromacs/fileio/gmxfio_xdr.c deleted file mode 100644 index 3823670a79..0000000000 --- a/src/gromacs/fileio/gmxfio_xdr.c +++ /dev/null @@ -1,296 +0,0 @@ -/* - * This file is part of the GROMACS molecular simulation package. - * - * Copyright (c) 1991-2000, University of Groningen, The Netherlands. - * Copyright (c) 2001-2004, The GROMACS development team. - * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by - * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, - * and including many others, as listed in the AUTHORS file in the - * top-level source directory and at http://www.gromacs.org. - * - * GROMACS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 - * of the License, or (at your option) any later version. - * - * GROMACS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with GROMACS; if not, see - * http://www.gnu.org/licenses, or write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * If you want to redistribute modifications to GROMACS, please - * consider that scientific software is very special. Version - * control is crucial - bugs must be traceable. We will be happy to - * consider code for inclusion in the official distribution, but - * derived work must not be called official GROMACS. Details are found - * in the README & COPYING files - if they are missing, get the - * official version at http://www.gromacs.org. - * - * To help us fund GROMACS development, we humbly ask that you cite - * the research papers on the package. Check out http://www.gromacs.org. - */ -#include "gmxpre.h" - -#include -#include - -#include "gromacs/fileio/gmxfio.h" -#include "gromacs/fileio/xdrf.h" -#include "gromacs/utility/fatalerror.h" -#include "gromacs/utility/smalloc.h" - -#include "gmxfio-impl.h" - -/* This is the part that reads xdr files. */ - -gmx_bool do_xdr(t_fileio *fio, void *item, int nitem, int eio, - const char *desc, const char *srcfile, int line) -{ - unsigned char ucdum, *ucptr; - bool_t res = 0; - float fvec[DIM]; - double dvec[DIM]; - int j, m, *iptr, idum; - gmx_int64_t sdum; - real *ptr; - unsigned short us; - double d = 0; - float f = 0; - - gmx_fio_check_nitem(eio, nitem, srcfile, line); - switch (eio) - { - case eioREAL: - if (fio->bDouble) - { - if (item && !fio->bRead) - { - d = *((real *) item); - } - res = xdr_double(fio->xdr, &d); - if (item) - { - *((real *) item) = d; - } - } - else - { - if (item && !fio->bRead) - { - f = *((real *) item); - } - res = xdr_float(fio->xdr, &f); - if (item) - { - *((real *) item) = f; - } - } - break; - case eioFLOAT: - if (item && !fio->bRead) - { - f = *((float *) item); - } - res = xdr_float(fio->xdr, &f); - if (item) - { - *((float *) item) = f; - } - break; - case eioDOUBLE: - if (item && !fio->bRead) - { - d = *((double *) item); - } - res = xdr_double(fio->xdr, &d); - if (item) - { - *((double *) item) = d; - } - break; - case eioINT: - if (item && !fio->bRead) - { - idum = *(int *) item; - } - res = xdr_int(fio->xdr, &idum); - if (item) - { - *(int *) item = idum; - } - break; - case eioINT64: - if (item && !fio->bRead) - { - sdum = *(gmx_int64_t *) item; - } - res = xdr_int64(fio->xdr, &sdum); - if (item) - { - *(gmx_int64_t *) item = sdum; - } - break; - case eioUCHAR: - if (item && !fio->bRead) - { - ucdum = *(unsigned char *) item; - } - res = xdr_u_char(fio->xdr, &ucdum); - if (item) - { - *(unsigned char *) item = ucdum; - } - break; - case eioNUCHAR: - ucptr = (unsigned char *) item; - res = 1; - for (j = 0; (j < nitem) && res; j++) - { - res = xdr_u_char(fio->xdr, &(ucptr[j])); - } - break; - case eioUSHORT: - if (item && !fio->bRead) - { - us = *(unsigned short *) item; - } - res = xdr_u_short(fio->xdr, (unsigned short *) &us); - if (item) - { - *(unsigned short *) item = us; - } - break; - case eioRVEC: - if (fio->bDouble) - { - if (item && !fio->bRead) - { - for (m = 0; (m < DIM); m++) - { - dvec[m] = ((real *) item)[m]; - } - } - res = xdr_vector(fio->xdr, (char *) dvec, DIM, - (unsigned int) sizeof(double), - (xdrproc_t) xdr_double); - if (item) - { - for (m = 0; (m < DIM); m++) - { - ((real *) item)[m] = dvec[m]; - } - } - } - else - { - if (item && !fio->bRead) - { - for (m = 0; (m < DIM); m++) - { - fvec[m] = ((real *) item)[m]; - } - } - res = xdr_vector(fio->xdr, (char *) fvec, DIM, - (unsigned int) sizeof(float), - (xdrproc_t) xdr_float); - if (item) - { - for (m = 0; (m < DIM); m++) - { - ((real *) item)[m] = fvec[m]; - } - } - } - break; - case eioNRVEC: - ptr = NULL; - res = 1; - for (j = 0; (j < nitem) && res; j++) - { - if (item) - { - ptr = ((rvec *) item)[j]; - } - res = do_xdr(fio, ptr, 1, eioRVEC, desc, srcfile, line); - } - break; - case eioIVEC: - iptr = (int *) item; - res = 1; - for (m = 0; (m < DIM) && res; m++) - { - if (item && !fio->bRead) - { - idum = iptr[m]; - } - res = xdr_int(fio->xdr, &idum); - if (item) - { - iptr[m] = idum; - } - } - break; - case eioSTRING: - { - char *cptr; - int slen; - - if (item) - { - if (!fio->bRead) - { - slen = strlen((char *) item) + 1; - } - else - { - slen = 0; - } - } - else - { - slen = 0; - } - - if (xdr_int(fio->xdr, &slen) <= 0) - { - gmx_fatal(FARGS, "wrong string length %d for string %s" - " (source %s, line %d)", slen, desc, srcfile, line); - } - if (!item && fio->bRead) - { - snew(cptr, slen); - } - else - { - cptr = (char *)item; - } - if (cptr) - { - res = xdr_string(fio->xdr, &cptr, slen); - } - else - { - res = 1; - } - if (!item && fio->bRead) - { - sfree(cptr); - } - break; - } - default: - gmx_fio_fe(fio, eio, desc, srcfile, line); - } - if ((res == 0) && (fio->bDebug)) - { - fprintf(stderr, "Error in xdr I/O %s %s to file %s (source %s, line %d)\n", - eioNames[eio], desc, fio->fn, srcfile, line); - } - - return (res != 0); -} -- 2.22.0