#142 Parallelize BSIM4 transient truncation checks with OpenMP
BSIM4 model evaluation already uses OpenMP, while its transient truncation checks still walk the instance lists serially. On sufficiently large BSIM4 circuits this leaves a parallelizable part of each timestep on the critical path.
This patch parallelizes the existing BSIM4 truncation calculation for versions 4.5, 4.6.5, 4.7 and 4.8.3. It reuses the instance array built by model setup, including instances belonging to later models. Each thread starts with the incoming timestep limit, calls the existing CKTterr routine on its assigned devices, and merges one minimum at the end. CKTterr reads the fixed circuit history; no device equations, error tolerances or integration formulas change.
The parallel path requires at least 256 instances per requested thread. Smaller circuits, non-OpenMP builds and STEPDEBUG builds retain the serial traversal. The grain rule avoids the overhead observed when parallelizing a small device population.
The patch applies directly to current SourceForge pre-master-48, commit 8bf480386586af6bf6a5736000769bfa0225e99c. The four modified source files are identical to the already tested experimental patch.
Validation completed on ngspice-47, whose original truncation routines and CKTterr are unchanged in this development branch:
- 33,792 direct serial-versus-patched comparisons per build, repeated with OpenMP enabled, ASan/UBSan, and the production routines compiled without OpenMP; no mismatches or sanitizer diagnostics.
- An additional 36,864 comparisons covering actual 1/8/16/32-thread teams and the 4,096/8,192-instance activation boundaries; all bit-identical. Both direct harnesses split instances across multiple models and exercise NQS, body resistance and gate resistance charge states, trapezoidal orders 1/2, Gear orders 1-6, and incoming limits that do and do not change.
- 54 complete high-thread-count transient runs, including 8,192-MOS trapezoidal/Gear cases and the charge-state options for all four model versions. The 27 matched original/patched pairs had byte-identical saved time, voltage and supply-current arrays, identical timestep/iteration counters, and identical derived average power. These are observations for the tested cases, not a claim of zero absolute integration error.
- 198 existing BSIM4 NMOS/PMOS QA output files were byte-identical.
For the 8,192-MOS synthetic case, three-run median transient analysis time at 16 threads decreased from 6.029 s to 5.388 s; the truncation phase decreased from 0.875 s to 0.273 s. At 32 threads the corresponding times were 5.717 s to 5.085 s and 0.880 s to 0.328 s. These figures describe one circuit and host, rather than a general speedup guarantee. A 915-MOS RC-heavy example remains on the serial truncation path and showed no material overall gain; its runtime is dominated by factorization.
Sparse matrix factorization and solve are unchanged. Reproduction sources and an independent serial reference runner accompany the patch.
Final validation on pre-master-48 itself: original and patched builds both completed successfully; 33,792 direct comparisons were bit-identical, and all 198 NMOS/PMOS QA output files were byte-identical between the matched original/patched builds. The legacy CMC tolerance diagnostics were also identical between the two builds.
The attached review archive contains the git-format patch, portable serial-versus-parallel regression sources and runner, and validation summary. The patch commit is bc1b726b899e2ea3c02479acdbe30daf9d3ddbe5.
Patch SHA-256: 5dd578a56d31ce1ee85333e431cf576ccc1e9c7bd20c80a7b0f84c3cb4391589
The complete git-format patch is also included below:
From bc1b726b899e2ea3c02479acdbe30daf9d3ddbe5 Mon Sep 17 00:00:00 2001
From: Terry <125424658+Touka20@users.noreply.github.com>
Date: Tue, 17 Nov 2026 20:07:10 +0800
Subject: [PATCH] Parallelize BSIM4 transient truncation checks with OpenMP
Reuse the existing model instance arrays and compute a thread-local timestep limit with CKTterr before merging the minimum. Cover BSIM4 4.5, 4.6.5, 4.7 and 4.8.3.
Require at least 256 instances per requested thread to amortize parallel overhead. Keep the original serial traversal for smaller populations, non-OpenMP builds and STEPDEBUG diagnostics.
---
src/spicelib/devices/bsim4/b4trunc.c | 92 ++++++++++++++++--------
src/spicelib/devices/bsim4v5/b4v5trunc.c | 91 +++++++++++++++--------
src/spicelib/devices/bsim4v6/b4v6trunc.c | 91 +++++++++++++++--------
src/spicelib/devices/bsim4v7/b4v7trunc.c | 92 ++++++++++++++++--------
4 files changed, 246 insertions(+), 120 deletions(-)
diff --git a/src/spicelib/devices/bsim4/b4trunc.c b/src/spicelib/devices/bsim4/b4trunc.c
index b9533ed..49d8776 100644
--- a/src/spicelib/devices/bsim4/b4trunc.c
+++ b/src/spicelib/devices/bsim4/b4trunc.c
@@ -28,44 +28,76 @@ standard can be found at: http://www.si2.org/cmc
#include "ngspice/sperror.h"
#include "ngspice/suffix.h"
-int
-BSIM4trunc(
-GENmodel *inModel,
-CKTcircuit *ckt,
-double *timeStep)
+#ifdef USE_OMP
+#include <omp.h>
+#endif
+
+static void
+BSIM4truncInstance(BSIM4instance *here, CKTcircuit *ckt,
+ double *timeStep)
{
-BSIM4model *model = (BSIM4model*)inModel;
-BSIM4instance *here;
+ CKTterr(here->BSIM4qb, ckt, timeStep);
+ CKTterr(here->BSIM4qg, ckt, timeStep);
+ CKTterr(here->BSIM4qd, ckt, timeStep);
+ if (here->BSIM4trnqsMod)
+ CKTterr(here->BSIM4qcdump, ckt, timeStep);
+ if (here->BSIM4rbodyMod) {
+ CKTterr(here->BSIM4qbs, ckt, timeStep);
+ CKTterr(here->BSIM4qbd, ckt, timeStep);
+ }
+ if (here->BSIM4rgateMod == 3)
+ CKTterr(here->BSIM4qgmid, ckt, timeStep);
+}
+int
+BSIM4trunc(GENmodel *inModel, CKTcircuit *ckt, double *timeStep)
+{
+ BSIM4model *model = (BSIM4model *) inModel;
+ BSIM4instance *here;
#ifdef STEPDEBUG
double debugtemp;
-#endif /* STEPDEBUG */
+#endif
- for (; model != NULL; model = BSIM4nextModel(model))
- { for (here = BSIM4instances(model); here != NULL;
- here = BSIM4nextInstance(here))
- {
+#if defined(USE_OMP) && !defined(STEPDEBUG)
+ /* CKTterr reads the fixed state history and only limits its argument.
+ * Reuse the instance array of the parallel model evaluation and merge
+ * one timestep per thread, keeping a minimum grain of 256 instances per requested thread. */
+ const int threads = omp_get_max_threads();
+ if (model && threads > 1 && model->BSIM4InstCount / threads >= 256) {
+ const double initialStep = *timeStep;
+ double limitedStep = initialStep;
+ int index;
+#pragma omp parallel
+ {
+ double localStep = initialStep;
+#pragma omp for schedule(static)
+ for (index = 0; index < model->BSIM4InstCount; index++)
+ BSIM4truncInstance(model->BSIM4InstanceArray[index],
+ ckt, &localStep);
+#pragma omp critical
+ {
+ if (localStep < limitedStep)
+ limitedStep = localStep;
+ }
+ }
+ *timeStep = limitedStep;
+ return OK;
+ }
+#endif
+
+ for (; model != NULL; model = BSIM4nextModel(model)) {
+ for (here = BSIM4instances(model); here != NULL;
+ here = BSIM4nextInstance(here)) {
#ifdef STEPDEBUG
debugtemp = *timeStep;
-#endif /* STEPDEBUG */
- CKTterr(here->BSIM4qb,ckt,timeStep);
- CKTterr(here->BSIM4qg,ckt,timeStep);
- CKTterr(here->BSIM4qd,ckt,timeStep);
- if (here->BSIM4trnqsMod)
- CKTterr(here->BSIM4qcdump,ckt,timeStep);
- if (here->BSIM4rbodyMod)
- { CKTterr(here->BSIM4qbs,ckt,timeStep);
- CKTterr(here->BSIM4qbd,ckt,timeStep);
- }
- if (here->BSIM4rgateMod == 3)
- CKTterr(here->BSIM4qgmid,ckt,timeStep);
+#endif
+ BSIM4truncInstance(here, ckt, timeStep);
#ifdef STEPDEBUG
- if(debugtemp != *timeStep)
- { printf("device %s reduces step from %g to %g\n",
- here->BSIM4name,debugtemp,*timeStep);
- }
-#endif /* STEPDEBUG */
+ if (debugtemp != *timeStep)
+ printf("device %s reduces step from %g to %g\n",
+ here->BSIM4name, debugtemp, *timeStep);
+#endif
}
}
- return(OK);
+ return OK;
}
diff --git a/src/spicelib/devices/bsim4v5/b4v5trunc.c b/src/spicelib/devices/bsim4v5/b4v5trunc.c
index 38fd894..4260b99 100644
--- a/src/spicelib/devices/bsim4v5/b4v5trunc.c
+++ b/src/spicelib/devices/bsim4v5/b4v5trunc.c
@@ -15,45 +15,76 @@
#include "ngspice/suffix.h"
-int
-BSIM4v5trunc(
-GENmodel *inModel,
-CKTcircuit *ckt,
-double *timeStep)
+#ifdef USE_OMP
+#include <omp.h>
+#endif
+
+static void
+BSIM4v5truncInstance(BSIM4v5instance *here, CKTcircuit *ckt,
+ double *timeStep)
{
-BSIM4v5model *model = (BSIM4v5model*)inModel;
-BSIM4v5instance *here;
+ CKTterr(here->BSIM4v5qb, ckt, timeStep);
+ CKTterr(here->BSIM4v5qg, ckt, timeStep);
+ CKTterr(here->BSIM4v5qd, ckt, timeStep);
+ if (here->BSIM4v5trnqsMod)
+ CKTterr(here->BSIM4v5qcdump, ckt, timeStep);
+ if (here->BSIM4v5rbodyMod) {
+ CKTterr(here->BSIM4v5qbs, ckt, timeStep);
+ CKTterr(here->BSIM4v5qbd, ckt, timeStep);
+ }
+ if (here->BSIM4v5rgateMod == 3)
+ CKTterr(here->BSIM4v5qgmid, ckt, timeStep);
+}
+int
+BSIM4v5trunc(GENmodel *inModel, CKTcircuit *ckt, double *timeStep)
+{
+ BSIM4v5model *model = (BSIM4v5model *) inModel;
+ BSIM4v5instance *here;
#ifdef STEPDEBUG
double debugtemp;
-#endif /* STEPDEBUG */
+#endif
- for (; model != NULL; model = BSIM4v5nextModel(model))
- { for (here = BSIM4v5instances(model); here != NULL;
- here = BSIM4v5nextInstance(here))
- {
+#if defined(USE_OMP) && !defined(STEPDEBUG)
+ /* CKTterr reads the fixed state history and only limits its argument.
+ * Reuse the instance array of the parallel model evaluation and merge
+ * one timestep per thread, keeping a minimum grain of 256 instances per requested thread. */
+ const int threads = omp_get_max_threads();
+ if (model && threads > 1 && model->BSIM4v5InstCount / threads >= 256) {
+ const double initialStep = *timeStep;
+ double limitedStep = initialStep;
+ int index;
+#pragma omp parallel
+ {
+ double localStep = initialStep;
+#pragma omp for schedule(static)
+ for (index = 0; index < model->BSIM4v5InstCount; index++)
+ BSIM4v5truncInstance(model->BSIM4v5InstanceArray[index],
+ ckt, &localStep);
+#pragma omp critical
+ {
+ if (localStep < limitedStep)
+ limitedStep = localStep;
+ }
+ }
+ *timeStep = limitedStep;
+ return OK;
+ }
+#endif
+ for (; model != NULL; model = BSIM4v5nextModel(model)) {
+ for (here = BSIM4v5instances(model); here != NULL;
+ here = BSIM4v5nextInstance(here)) {
#ifdef STEPDEBUG
debugtemp = *timeStep;
-#endif /* STEPDEBUG */
- CKTterr(here->BSIM4v5qb,ckt,timeStep);
- CKTterr(here->BSIM4v5qg,ckt,timeStep);
- CKTterr(here->BSIM4v5qd,ckt,timeStep);
- if (here->BSIM4v5trnqsMod)
- CKTterr(here->BSIM4v5qcdump,ckt,timeStep);
- if (here->BSIM4v5rbodyMod)
- { CKTterr(here->BSIM4v5qbs,ckt,timeStep);
- CKTterr(here->BSIM4v5qbd,ckt,timeStep);
- }
- if (here->BSIM4v5rgateMod == 3)
- CKTterr(here->BSIM4v5qgmid,ckt,timeStep);
+#endif
+ BSIM4v5truncInstance(here, ckt, timeStep);
#ifdef STEPDEBUG
- if(debugtemp != *timeStep)
- { printf("device %s reduces step from %g to %g\n",
- here->BSIM4v5name,debugtemp,*timeStep);
- }
-#endif /* STEPDEBUG */
+ if (debugtemp != *timeStep)
+ printf("device %s reduces step from %g to %g\n",
+ here->BSIM4v5name, debugtemp, *timeStep);
+#endif
}
}
- return(OK);
+ return OK;
}
diff --git a/src/spicelib/devices/bsim4v6/b4v6trunc.c b/src/spicelib/devices/bsim4v6/b4v6trunc.c
index 8951c19..18b05ab 100644
--- a/src/spicelib/devices/bsim4v6/b4v6trunc.c
+++ b/src/spicelib/devices/bsim4v6/b4v6trunc.c
@@ -17,45 +17,76 @@
#include "ngspice/suffix.h"
-int
-BSIM4v6trunc(
-GENmodel *inModel,
-CKTcircuit *ckt,
-double *timeStep)
+#ifdef USE_OMP
+#include <omp.h>
+#endif
+
+static void
+BSIM4v6truncInstance(BSIM4v6instance *here, CKTcircuit *ckt,
+ double *timeStep)
{
-BSIM4v6model *model = (BSIM4v6model*)inModel;
-BSIM4v6instance *here;
+ CKTterr(here->BSIM4v6qb, ckt, timeStep);
+ CKTterr(here->BSIM4v6qg, ckt, timeStep);
+ CKTterr(here->BSIM4v6qd, ckt, timeStep);
+ if (here->BSIM4v6trnqsMod)
+ CKTterr(here->BSIM4v6qcdump, ckt, timeStep);
+ if (here->BSIM4v6rbodyMod) {
+ CKTterr(here->BSIM4v6qbs, ckt, timeStep);
+ CKTterr(here->BSIM4v6qbd, ckt, timeStep);
+ }
+ if (here->BSIM4v6rgateMod == 3)
+ CKTterr(here->BSIM4v6qgmid, ckt, timeStep);
+}
+int
+BSIM4v6trunc(GENmodel *inModel, CKTcircuit *ckt, double *timeStep)
+{
+ BSIM4v6model *model = (BSIM4v6model *) inModel;
+ BSIM4v6instance *here;
#ifdef STEPDEBUG
double debugtemp;
-#endif /* STEPDEBUG */
+#endif
- for (; model != NULL; model = BSIM4v6nextModel(model))
- { for (here = BSIM4v6instances(model); here != NULL;
- here = BSIM4v6nextInstance(here))
- {
+#if defined(USE_OMP) && !defined(STEPDEBUG)
+ /* CKTterr reads the fixed state history and only limits its argument.
+ * Reuse the instance array of the parallel model evaluation and merge
+ * one timestep per thread, keeping a minimum grain of 256 instances per requested thread. */
+ const int threads = omp_get_max_threads();
+ if (model && threads > 1 && model->BSIM4v6InstCount / threads >= 256) {
+ const double initialStep = *timeStep;
+ double limitedStep = initialStep;
+ int index;
+#pragma omp parallel
+ {
+ double localStep = initialStep;
+#pragma omp for schedule(static)
+ for (index = 0; index < model->BSIM4v6InstCount; index++)
+ BSIM4v6truncInstance(model->BSIM4v6InstanceArray[index],
+ ckt, &localStep);
+#pragma omp critical
+ {
+ if (localStep < limitedStep)
+ limitedStep = localStep;
+ }
+ }
+ *timeStep = limitedStep;
+ return OK;
+ }
+#endif
+ for (; model != NULL; model = BSIM4v6nextModel(model)) {
+ for (here = BSIM4v6instances(model); here != NULL;
+ here = BSIM4v6nextInstance(here)) {
#ifdef STEPDEBUG
debugtemp = *timeStep;
-#endif /* STEPDEBUG */
- CKTterr(here->BSIM4v6qb,ckt,timeStep);
- CKTterr(here->BSIM4v6qg,ckt,timeStep);
- CKTterr(here->BSIM4v6qd,ckt,timeStep);
- if (here->BSIM4v6trnqsMod)
- CKTterr(here->BSIM4v6qcdump,ckt,timeStep);
- if (here->BSIM4v6rbodyMod)
- { CKTterr(here->BSIM4v6qbs,ckt,timeStep);
- CKTterr(here->BSIM4v6qbd,ckt,timeStep);
- }
- if (here->BSIM4v6rgateMod == 3)
- CKTterr(here->BSIM4v6qgmid,ckt,timeStep);
+#endif
+ BSIM4v6truncInstance(here, ckt, timeStep);
#ifdef STEPDEBUG
- if(debugtemp != *timeStep)
- { printf("device %s reduces step from %g to %g\n",
- here->BSIM4v6name,debugtemp,*timeStep);
- }
-#endif /* STEPDEBUG */
+ if (debugtemp != *timeStep)
+ printf("device %s reduces step from %g to %g\n",
+ here->BSIM4v6name, debugtemp, *timeStep);
+#endif
}
}
- return(OK);
+ return OK;
}
diff --git a/src/spicelib/devices/bsim4v7/b4v7trunc.c b/src/spicelib/devices/bsim4v7/b4v7trunc.c
index 9aea006..c4e44cf 100644
--- a/src/spicelib/devices/bsim4v7/b4v7trunc.c
+++ b/src/spicelib/devices/bsim4v7/b4v7trunc.c
@@ -17,44 +17,76 @@
#include "ngspice/suffix.h"
-int
-BSIM4v7trunc(
-GENmodel *inModel,
-CKTcircuit *ckt,
-double *timeStep)
+#ifdef USE_OMP
+#include <omp.h>
+#endif
+
+static void
+BSIM4v7truncInstance(BSIM4v7instance *here, CKTcircuit *ckt,
+ double *timeStep)
{
-BSIM4v7model *model = (BSIM4v7model*)inModel;
-BSIM4v7instance *here;
+ CKTterr(here->BSIM4v7qb, ckt, timeStep);
+ CKTterr(here->BSIM4v7qg, ckt, timeStep);
+ CKTterr(here->BSIM4v7qd, ckt, timeStep);
+ if (here->BSIM4v7trnqsMod)
+ CKTterr(here->BSIM4v7qcdump, ckt, timeStep);
+ if (here->BSIM4v7rbodyMod) {
+ CKTterr(here->BSIM4v7qbs, ckt, timeStep);
+ CKTterr(here->BSIM4v7qbd, ckt, timeStep);
+ }
+ if (here->BSIM4v7rgateMod == 3)
+ CKTterr(here->BSIM4v7qgmid, ckt, timeStep);
+}
+int
+BSIM4v7trunc(GENmodel *inModel, CKTcircuit *ckt, double *timeStep)
+{
+ BSIM4v7model *model = (BSIM4v7model *) inModel;
+ BSIM4v7instance *here;
#ifdef STEPDEBUG
double debugtemp;
-#endif /* STEPDEBUG */
+#endif
- for (; model != NULL; model = BSIM4v7nextModel(model))
- { for (here = BSIM4v7instances(model); here != NULL;
- here = BSIM4v7nextInstance(here))
- {
+#if defined(USE_OMP) && !defined(STEPDEBUG)
+ /* CKTterr reads the fixed state history and only limits its argument.
+ * Reuse the instance array of the parallel model evaluation and merge
+ * one timestep per thread, keeping a minimum grain of 256 instances per requested thread. */
+ const int threads = omp_get_max_threads();
+ if (model && threads > 1 && model->BSIM4v7InstCount / threads >= 256) {
+ const double initialStep = *timeStep;
+ double limitedStep = initialStep;
+ int index;
+#pragma omp parallel
+ {
+ double localStep = initialStep;
+#pragma omp for schedule(static)
+ for (index = 0; index < model->BSIM4v7InstCount; index++)
+ BSIM4v7truncInstance(model->BSIM4v7InstanceArray[index],
+ ckt, &localStep);
+#pragma omp critical
+ {
+ if (localStep < limitedStep)
+ limitedStep = localStep;
+ }
+ }
+ *timeStep = limitedStep;
+ return OK;
+ }
+#endif
+
+ for (; model != NULL; model = BSIM4v7nextModel(model)) {
+ for (here = BSIM4v7instances(model); here != NULL;
+ here = BSIM4v7nextInstance(here)) {
#ifdef STEPDEBUG
debugtemp = *timeStep;
-#endif /* STEPDEBUG */
- CKTterr(here->BSIM4v7qb,ckt,timeStep);
- CKTterr(here->BSIM4v7qg,ckt,timeStep);
- CKTterr(here->BSIM4v7qd,ckt,timeStep);
- if (here->BSIM4v7trnqsMod)
- CKTterr(here->BSIM4v7qcdump,ckt,timeStep);
- if (here->BSIM4v7rbodyMod)
- { CKTterr(here->BSIM4v7qbs,ckt,timeStep);
- CKTterr(here->BSIM4v7qbd,ckt,timeStep);
- }
- if (here->BSIM4v7rgateMod == 3)
- CKTterr(here->BSIM4v7qgmid,ckt,timeStep);
+#endif
+ BSIM4v7truncInstance(here, ckt, timeStep);
#ifdef STEPDEBUG
- if(debugtemp != *timeStep)
- { printf("device %s reduces step from %g to %g\n",
- here->BSIM4v7name,debugtemp,*timeStep);
- }
-#endif /* STEPDEBUG */
+ if (debugtemp != *timeStep)
+ printf("device %s reduces step from %g to %g\n",
+ here->BSIM4v7name, debugtemp, *timeStep);
+#endif
}
}
- return(OK);
+ return OK;
}
Reproduction material for this patch is available at https://gist.github.com/Touka20/e6ee89a4458660b1bc3fbcfb7f0acb88 .
It includes the exact git-format patch, the basic 1/2/4/8-thread and extended 1/8/16/32-thread serial-versus-parallel regression sources, a portable runner and instructions, and the validation summary.
On current pre-master-48 (8bf480386586af6bf6a5736000769bfa0225e99c), both original and patched builds completed; 33,792 direct comparisons were bit-identical, and all 198 BSIM4 NMOS/PMOS QA output files were byte-identical. The existing CMC diagnostics were identical in the two builds.
Patch SHA-256: 5dd578a56d31ce1ee85333e431cf576ccc1e9c7bd20c80a7b0f84c3cb4391589.
Reproduction material for this patch is available at https://gist.github.com/Touka20/e6ee89a4458660b1bc3fbcfb7f0acb88 .
This page is not available - 404.
Can you provide the patch here as a attachement, please.
I have made some tests with Skywater PDK applied to circuits 7552_ann (BSIM4 14942, Cap 48376, Res 33847), and with a SRAM block containing 200k BSIM4.5 devices.
My setup is Windows 11, CPU is Intel i9 9900K with 8 physical cores.
Indeed I see a speedup of the truncation check by a factor of 2 - 5.
Transient trunc time = 5.10684instead ofTransient trunc time = 26.2933for the SRAM, however with aTotal analysis time (seconds) = 546.277, but still worthwhile.With AVX512 I do not see any improvement, even some slowdown, as my processor does not support AVX512, but only AVX2. In addition Intel obviously has dropped support of AVX512 for their current consumer processors. So for testing any acceleration the access to a server or workstation CPU is inevitable.
Even if these tests are successful, we need to make sure that compiling in AVX512 support into ngspice will not harm the average user. It has to allow being switched off completely.
OK, I see that compiling for AVX512 or AVX2 on MS VIsual Studio is not supported by the patch. I will make some tests on Windows with a MSYS2/MINGW executable.