--- a/src/include/ngspice/inpptree.h +++ b/src/include/ngspice/inpptree.h @@ -11,6 +11,7 @@ */ #include "ngspice/ifsim.h" +#include #ifndef ngspice_INPPTREE_H #define ngspice_INPPTREE_H @@ -31,6 +32,9 @@ IFparseTree p; struct INPparseNode *tree; /* The real stuff. */ struct INPparseNode **derivs; /* The derivative parse trees. */ + void *tft_plan; + int tft_plan_attempted; + int tft_cache_mode; /* 0 unknown, 1 stateless, -1 stateful fallback. */ } INPparseTree; /* This is what is passed as the actual parameter value. The fields will all @@ -53,7 +57,11 @@ void (*function)(void); /* ... and pointer to the function. */ void *data; /* private data for certain functions, currently PTF_PWL */ int usecnt; + uint64_t tft_eval_epoch; + double tft_eval_value; } INPparseNode; + +void TFTfreePlan(void *opaque); /* A debugging function */ void INPptPrint(char *str, IFparseTree * ptree); --- a/src/spicelib/parser/ifeval.c +++ b/src/spicelib/parser/ifeval.c @@ -19,8 +19,42 @@ extern double PTfudge_factor; +#ifndef TFT_EVAL_MEMO +#define TFT_EVAL_MEMO 1 +#endif +#ifndef TFT_EVAL_DIRECT +#define TFT_EVAL_DIRECT 1 +#endif + +/* Research fast path: the function and its derivative trees share subtrees. + * Reuse a shared node only during ONE IFeval invocation. Input values, time, + * temperature and gmin are fixed for that invocation. No Newton/timepoint + * cache survives it. tmalloc() zero-initializes the fields on each new node. + * ngspice's parser already uses process-global state; this patch is scoped to + * the existing single-thread build. Stateful/private operators fall back. + */ +#if TFT_EVAL_MEMO +static uint64_t tft_epoch; + +static int +PTstateful(const INPparseNode *tree) +{ + if (!tree) + return 0; + if (tree->type == PT_FUNCTION && + (tree->funcnum == PTF_DDT || + (tree->data && tree->funcnum != PTF_PWL && + tree->funcnum != PTF_PWL_DERIVATIVE))) + return 1; + return PTstateful(tree->left) || PTstateful(tree->right); +} +#endif + + +#include "tft_eval_plan.inc" + static int PTeval(INPparseNode * tree, double gmin, double *res, - double *vals); + double *vals, uint64_t epoch); @@ -30,11 +64,39 @@ { int i, err; INPparseTree *myTree = (INPparseTree *) tree; + uint64_t epoch = 0; if (!myTree) { fprintf(stderr, "\nInternal error: No tree to evaluate.\n"); controlled_exit(EXIT_BAD); } + +#if TFT_EVAL_MEMO + if (!myTree->tft_cache_mode) + myTree->tft_cache_mode = PTstateful(myTree->tree) ? -1 : 1; + if (myTree->tft_cache_mode == 1) { + epoch = ++tft_epoch; + if (!epoch) + controlled_exit(EXIT_BAD); /* Never reuse an epoch after wrap. */ + } +#endif +#if TFT_EVAL_DIRECT + PTfudge_factor = gmin * 1.0e-20; +#endif + +#if TFT_EVAL_PLAN + if (epoch) { + if (!myTree->tft_plan_attempted) { + myTree->tft_plan = TFTcompile(myTree); + myTree->tft_plan_attempted = 1; + } + if (myTree->tft_plan) { + err = TFTrun(myTree->tft_plan, epoch, result, vals, derivs); + if (err != OK && ft_stricterror) controlled_exit(EXIT_BAD); + return err; + } + } +#endif #ifdef TRACE INPptPrint("calling PTeval, tree = ", tree); @@ -43,7 +105,7 @@ printf("\tvar%d = %lg\n", i, vals[i]); #endif - if ((err = PTeval(myTree->tree, gmin, result, vals)) != OK) { + if ((err = PTeval(myTree->tree, gmin, result, vals, epoch)) != OK) { if (ft_ngdebug) { INPptPrint("calling PTeval, tree = ", tree); printf("values:"); @@ -56,7 +118,7 @@ } for (i = 0; i < myTree->p.numVars; i++) - if ((err = PTeval(myTree->derivs[i], gmin, &derivs[i], vals)) != OK) { + if ((err = PTeval(myTree->derivs[i], gmin, &derivs[i], vals, epoch)) != OK) { if (ft_ngdebug) { INPptPrint("calling PTeval, tree = ", tree); printf("results: function = %lg\n", *result); @@ -78,12 +140,20 @@ } static int -PTeval(INPparseNode * tree, double gmin, double *res, double *vals) +PTeval(INPparseNode * tree, double gmin, double *res, double *vals, uint64_t epoch) { double r1, r2; int err; - + const int memo = epoch && tree->usecnt > 1 && + tree->type != PT_CONSTANT && tree->type != PT_VAR; + + if (memo && tree->tft_eval_epoch == epoch) { + *res = tree->tft_eval_value; + return OK; + } +#if !TFT_EVAL_DIRECT PTfudge_factor = gmin * 1.0e-20; /* defaults to 1e-32, should be small enough */ +#endif switch (tree->type) { case PT_CONSTANT: *res = tree->constant; @@ -100,10 +170,10 @@ case PTF_PWR: case PTF_MIN: case PTF_MAX: - err = PTeval(tree->left->left, gmin, &r1, vals); + err = PTeval(tree->left->left, gmin, &r1, vals, epoch); if (err != OK) return (err); - err = PTeval(tree->left->right, gmin, &r2, vals); + err = PTeval(tree->left->right, gmin, &r2, vals, epoch); if (err != OK) return (err); *res = PTbinary(tree -> function) (r1, r2); @@ -115,7 +185,7 @@ break; /* fcns with single argument */ default: - err = PTeval(tree->left, gmin, &r1, vals); + err = PTeval(tree->left, gmin, &r1, vals, epoch); if (err != OK) return (err); if(tree->data == NULL) @@ -137,12 +207,12 @@ INPparseNode *arg2 = tree->right->left; INPparseNode *arg3 = tree->right->right; - err = PTeval(arg1, gmin, &r1, vals); + err = PTeval(arg1, gmin, &r1, vals, epoch); if (err != OK) return (err); /*FIXME > 0.0, >= 0.5, != 0.0 or what ? */ - err = PTeval((r1 != 0.0) ? arg2 : arg3, gmin, &r2, vals); + err = PTeval((r1 != 0.0) ? arg2 : arg3, gmin, &r2, vals, epoch); if (err != OK) return (err); @@ -155,13 +225,22 @@ case PT_TIMES: case PT_DIVIDE: case PT_POWER: - err = PTeval(tree->left, gmin, &r1, vals); + err = PTeval(tree->left, gmin, &r1, vals, epoch); if (err != OK) return (err); - err = PTeval(tree->right, gmin, &r2, vals); + err = PTeval(tree->right, gmin, &r2, vals, epoch); if (err != OK) return (err); +#if TFT_EVAL_DIRECT + switch (tree->type) { + case PT_PLUS: *res = r1 + r2; break; + case PT_MINUS: *res = r1 - r2; break; + case PT_TIMES: *res = r1 * r2; break; + default: *res = PTbinary(tree->function)(r1, r2); break; + } +#else *res = PTbinary(tree -> function) (r1, r2); +#endif if (*res == HUGE) { fprintf(stderr, "\nError: %g, %g out of range for %s\n", r1, r2, tree->funcname); @@ -186,5 +265,9 @@ return (E_PANIC); } + if (memo) { + tree->tft_eval_epoch = epoch; + tree->tft_eval_value = *res; + } return (OK); } --- a/src/spicelib/parser/inpptree.c +++ b/src/spicelib/parser/inpptree.c @@ -1622,6 +1622,7 @@ for (i = 0; i < pt->p.numVars; i++) dec_usage(pt->derivs[i]); + TFTfreePlan(pt->tft_plan); dec_usage(pt->tree); txfree(pt->derivs); --- /dev/null +++ b/src/spicelib/parser/tft_eval_plan.inc @@ -0,0 +1,207 @@ +/* Prepared evaluator experiment. Same operations, operand order and branches + * as PTeval; no arithmetic reassociation or relaxed floating point. A program + * belongs to one parse tree and is released by INPfreeTree. Stateful/private + * expressions use the original evaluator. Single-threaded build only. + */ +#ifndef TFT_EVAL_PLAN +#define TFT_EVAL_PLAN 1 +#endif + +enum { + TP_CHECK = 100, TP_UNARY, TP_PRIVATE, TP_BINARY, TP_BRANCH, + TP_JUMP, TP_COPY, TP_OUTPUT +}; +typedef struct { + int op, dst, a, b, target; + INPparseNode *node; +} TFTinstruction; +typedef struct { + TFTinstruction *code; + INPparseNode **nodes; + double *values; + uint64_t *stamps; + int length, capacity, count, node_capacity; +} TFTplan; + +void +TFTfreePlan(void *opaque) +{ + TFTplan *p = opaque; + if (!p) return; + txfree(p->code); + txfree(p->nodes); + txfree(p->values); + txfree(p->stamps); + txfree(p); +} + +static int +TFTslot(TFTplan *p, INPparseNode *n) +{ + int i; + for (i = 0; i < p->count; ++i) + if (p->nodes[i] == n) return i; + if (p->count >= 8192) return -1; + if (p->count == p->node_capacity) { + p->node_capacity = p->node_capacity ? p->node_capacity * 2 : 32; + p->nodes = TREALLOC(INPparseNode *, p->nodes, p->node_capacity); + } + p->nodes[p->count] = n; + return p->count++; +} + +static int +TFTemit(TFTplan *p, int op, int dst, int a, int b, INPparseNode *n) +{ + TFTinstruction *instruction; + int i; + if (p->length >= 65536) return -1; + if (p->length == p->capacity) { + p->capacity = p->capacity ? p->capacity * 2 : 64; + p->code = TREALLOC(TFTinstruction, p->code, p->capacity); + } + i = p->length++; + instruction = &p->code[i]; + instruction->op = op; instruction->dst = dst; + instruction->a = a; instruction->b = b; + instruction->node = n; instruction->target = 0; + return i; +} + +static int +TFTcompileNode(TFTplan *p, INPparseNode *n, int depth) +{ + int dst, a = -1, b = -1, check = -1, branch, jump, op; + if (!n || depth > 256 || (dst = TFTslot(p, n)) < 0) return -1; + if (n->usecnt > 1 && n->type != PT_CONSTANT && n->type != PT_VAR) { + check = TFTemit(p, TP_CHECK, dst, 0, 0, n); + if (check < 0) return -1; + } + op = n->type; + switch (n->type) { + case PT_CONSTANT: case PT_VAR: case PT_TIME: + case PT_TEMPERATURE: case PT_FREQUENCY: + break; + case PT_PLUS: case PT_MINUS: case PT_TIMES: + case PT_DIVIDE: case PT_POWER: + a = TFTcompileNode(p, n->left, depth + 1); + b = TFTcompileNode(p, n->right, depth + 1); + if (a < 0 || b < 0) return -1; + break; + case PT_FUNCTION: + switch (n->funcnum) { + case PTF_POW: case PTF_PWR: case PTF_MIN: case PTF_MAX: + a = TFTcompileNode(p, n->left->left, depth + 1); + b = TFTcompileNode(p, n->left->right, depth + 1); + if (a < 0 || b < 0) return -1; + op = TP_BINARY; + break; + default: + if (n->funcnum == PTF_DDT || + (n->data && n->funcnum != PTF_PWL && + n->funcnum != PTF_PWL_DERIVATIVE)) return -1; + a = TFTcompileNode(p, n->left, depth + 1); + if (a < 0) return -1; + op = n->data ? TP_PRIVATE : TP_UNARY; + break; + } + break; + case PT_TERN: + a = TFTcompileNode(p, n->left, depth + 1); + if (a < 0) return -1; + branch = TFTemit(p, TP_BRANCH, 0, a, 0, n); + if (branch < 0) return -1; + b = TFTcompileNode(p, n->right->left, depth + 1); + if (b < 0 || TFTemit(p, TP_COPY, dst, b, 0, n) < 0) return -1; + jump = TFTemit(p, TP_JUMP, 0, 0, 0, n); + if (jump < 0) return -1; + p->code[branch].target = p->length; + b = TFTcompileNode(p, n->right->right, depth + 1); + if (b < 0 || TFTemit(p, TP_COPY, dst, b, 0, n) < 0) return -1; + p->code[jump].target = p->length; + if (check >= 0) p->code[check].target = p->length; + return dst; + default: + return -1; + } + if (TFTemit(p, op, dst, a, b, n) < 0) return -1; + if (check >= 0) p->code[check].target = p->length; + return dst; +} + +static TFTplan * +TFTcompile(INPparseTree *tree) +{ + TFTplan *p = TMALLOC(TFTplan, 1); + int i, slot; + for (i = -1; i < tree->p.numVars; ++i) { + slot = TFTcompileNode(p, i < 0 ? tree->tree : tree->derivs[i], 0); + if (slot < 0 || TFTemit(p, TP_OUTPUT, i, slot, 0, NULL) < 0) { + TFTfreePlan(p); + return NULL; + } + } + p->values = TMALLOC(double, p->count); + p->stamps = TMALLOC(uint64_t, p->count); +#ifdef TFT_PLAN_STATS + fprintf(stderr, "TFT prepared: %d nodes, %d instructions\n", p->count, p->length); +#endif + return p; +} + +static int +TFTrun(TFTplan *p, uint64_t epoch, double *result, double *vals, double *derivs) +{ + int pc = 0, type; + double value, a, b; + while (pc < p->length) { + TFTinstruction *i = &p->code[pc++]; + INPparseNode *n = i->node; + type = i->op; + switch (type) { + case TP_CHECK: + if (p->stamps[i->dst] == epoch) pc = i->target; + continue; + case TP_BRANCH: + if (p->values[i->a] == 0.0) pc = i->target; + continue; + case TP_JUMP: pc = i->target; continue; + case TP_OUTPUT: + if (i->dst < 0) *result = p->values[i->a]; + else derivs[i->dst] = p->values[i->a]; + continue; + case PT_CONSTANT: value = n->constant; break; + case PT_VAR: value = vals[n->valueIndex]; break; + case PT_TIME: value = ((CKTcircuit *) n->data)->CKTtime; break; + case PT_TEMPERATURE: value = ((CKTcircuit *) n->data)->CKTtemp - CONSTCtoK; break; + case PT_FREQUENCY: value = ((CKTcircuit *) n->data)->CKTomega / 2. / M_PI; break; + case TP_COPY: value = p->values[i->a]; break; + case TP_UNARY: case TP_PRIVATE: + a = p->values[i->a]; + value = type == TP_PRIVATE ? PTunary_with_private(n->function)(a, n->data) : PTunary(n->function)(a); + if (value == HUGE) { + fprintf(stderr, "Error: %g out of range for %s\n", a, n->funcname); + return E_PARMVAL; + } + break; + case PT_PLUS: case PT_MINUS: case PT_TIMES: + case PT_DIVIDE: case PT_POWER: case TP_BINARY: + a = p->values[i->a]; b = p->values[i->b]; + switch (type) { + case PT_PLUS: value = a + b; break; + case PT_MINUS: value = a - b; break; + case PT_TIMES: value = a * b; break; + default: value = PTbinary(n->function)(a, b); break; + } + if (value == HUGE) { + fprintf(stderr, "Error: %g, %g out of range for %s\n", a, b, n->funcname); + return E_PARMVAL; + } + break; + default: return E_PANIC; + } + p->values[i->dst] = value; + p->stamps[i->dst] = epoch; + } + return OK; +}