From 03b47b3e34823b774c2648282ac94085eec3deb5 Mon Sep 17 00:00:00 2001 From: Krzysztof Blaszkowski Date: Thu, 7 Jan 2016 08:54:14 +0100 Subject: [PATCH] src/frontend/outif.c, plotAddRealValue() etc, rallocate more coarsely reallocate in chunks, instead of once per time step. --- src/frontend/dvec.c | 14 ++++++++++++++ src/frontend/outitf.c | 28 ++++++++++++++++++++++++---- src/include/ngspice/dvec.h | 2 ++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/frontend/dvec.c b/src/frontend/dvec.c index 54f4efab8..6da1d44a3 100644 --- a/src/frontend/dvec.c +++ b/src/frontend/dvec.c @@ -16,6 +16,7 @@ dvec_alloc(char *name, int type, short flags, int length, void *storage) rv->v_type = type; rv->v_flags = flags; rv->v_length = length; + rv->v_alloc_length = length; if (!length) { rv->v_realdata = NULL; @@ -60,6 +61,19 @@ dvec_realloc(struct dvec *v, int length, void *storage) } v->v_length = length; + v->v_alloc_length = length; +} + + +void +dvec_extend(struct dvec *v, int length) +{ + if (isreal(v)) + v->v_realdata = TREALLOC(double, v->v_realdata, length); + else + v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, length); + + v->v_alloc_length = length; } diff --git a/src/frontend/outitf.c b/src/frontend/outitf.c index ba8372b91..f965a252c 100644 --- a/src/frontend/outitf.c +++ b/src/frontend/outitf.c @@ -1,7 +1,7 @@ /********** Copyright 1990 Regents of the University of California. All rights reserved. Author: 1988 Wayne A. Christopher, U. C. Berkeley CAD Group -Modified: 2000 AlansFixes +Modified: 2000 AlansFixes, 2013/2015 patch by Krzysztof Blaszkowski **********/ /* @@ -1057,17 +1057,35 @@ plotInit(runDesc *run) } +static inline int +vlength2delta(int l) +{ + if (l < 50000) + return 512; + if (l < 200000) + return 256; + if (l < 500000) + return 128; + /* larger memory allocations may exhaust memory easily + * this function may use better estimation depending on + * available memory and number of vectors (run->numData) + */ + return 64; +} + + static void plotAddRealValue(dataDesc *desc, double value) { struct dvec *v = desc->vec; + if (v->v_length >= v->v_alloc_length) + dvec_extend(v, v->v_length + vlength2delta(v->v_length)); + if (isreal(v)) { - v->v_realdata = TREALLOC(double, v->v_realdata, v->v_length + 1); v->v_realdata[v->v_length] = value; } else { /* a real parading as a VF_COMPLEX */ - v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, v->v_length + 1); v->v_compdata[v->v_length].cx_real = value; v->v_compdata[v->v_length].cx_imag = 0.0; } @@ -1082,7 +1100,9 @@ plotAddComplexValue(dataDesc *desc, IFcomplex value) { struct dvec *v = desc->vec; - v->v_compdata = TREALLOC(ngcomplex_t, v->v_compdata, v->v_length + 1); + if (v->v_length >= v->v_alloc_length) + dvec_extend(v, v->v_length + vlength2delta(v->v_length)); + v->v_compdata[v->v_length].cx_real = value.real; v->v_compdata[v->v_length].cx_imag = value.imag; diff --git a/src/include/ngspice/dvec.h b/src/include/ngspice/dvec.h index eb5a9a9ae..77eb6f179 100644 --- a/src/include/ngspice/dvec.h +++ b/src/include/ngspice/dvec.h @@ -45,6 +45,7 @@ struct dvec { GRIDTYPE v_gridtype; /* One of GRID_*. */ PLOTTYPE v_plottype; /* One of PLOT_*. */ int v_length; /* Length of the vector. */ + int v_alloc_length; /* How much has been actually allocated. */ int v_rlength; /* How much space we really have. Used as binary flag */ int v_outindex; /* Index if writedata is building the vector. */ @@ -70,6 +71,7 @@ struct dveclist { struct dvec *dvec_alloc(char *name, int type, short flags, int length, void *storage); void dvec_realloc(struct dvec *v, int length, void *storage); +void dvec_extend(struct dvec *v, int length); void dvec_trunc(struct dvec *v, int length); void dvec_free(struct dvec *);