Move setting up OMP from CKTsetup() to inp_readall().

Add a check for abvailable logical CPU cores,
and use half of them as default.
This commit is contained in:
Holger Vogt 2026-02-11 20:15:57 +01:00
parent a4d3b9dc5d
commit c093f985e1
2 changed files with 25 additions and 18 deletions

View File

@ -58,6 +58,11 @@ Author: 1985 Wayne A. Christopher
/* gtri - end - 12/12/90 */
#endif
#ifdef USE_OMP
#include <omp.h>
#include "ngspice/cpextern.h"
#endif
/* SJB - Uncomment this line for debug tracing */
/*#define TRACE*/
@ -73,6 +78,10 @@ Author: 1985 Wayne A. Christopher
#define VALIDCHARS "!$%_#?@.[]&"
#ifdef USE_OMP
int nthreads = 4;
#endif
static struct library {
char *realpath;
char *habitat;
@ -1088,6 +1097,21 @@ struct card *inp_readall(FILE *fp, const char *dir_name, const char* file_name,
struct card *tmp_ptr1;
struct names *subckt_w_params = new_names();
/* set the number of threads used by ngspice via OpenMP */
#ifdef USE_OMP
if (!cp_getvar("num_threads", CP_NUM, &nthreads, 0)) {
nthreads = omp_get_num_procs();
fprintf(stdout, "\nNote: %d logical cores detected, ", nthreads);
nthreads = (int)(nthreads / 2);
fprintf(stdout, "max %d threads are used by ngspice.\n", nthreads);
// fprintf(stdout, " (May be overriden by 'set num_threads = xx in .spiceinit.)\n\n", nthreads);
}
else
fprintf(stdout, "\nNote: Number of threads used by ngspice set to %d.\n\n", nthreads);
omp_set_num_threads(nthreads);
#endif
/* skip title line */
struct card *working = cc->nextcard;

View File

@ -20,11 +20,6 @@ Author: 1985 Thomas L. Quarles
#include "ngspice/enh.h"
#endif
#ifdef USE_OMP
#include <omp.h>
#include "ngspice/cpextern.h"
#endif
#define CKALLOC(var,size,type) \
if(size && ((var = TMALLOC(type, size)) == NULL)){\
return(E_NOMEM);\
@ -35,9 +30,7 @@ CKTsetup(CKTcircuit *ckt)
{
int i;
int error;
#ifdef USE_OMP
int nthreads = 2;
#endif
#ifdef XSPICE
/* gtri - begin - Setup for adding rshunt option resistors */
CKTnode *node;
@ -86,16 +79,6 @@ CKTsetup(CKTcircuit *ckt)
matrix = ckt->CKTmatrix;
#ifdef USE_OMP
if (!cp_getvar("num_threads", CP_NUM, &nthreads, 0))
nthreads = 2;
omp_set_num_threads(nthreads);
/* if (nthreads == 1)
printf("OpenMP: %d thread is requested in ngspice\n", nthreads);
else
printf("OpenMP: %d threads are requested in ngspice\n", nthreads);*/
#endif
#ifdef HAS_PROGREP
SetAnalyse("Device Setup", 0);