Menu

#661 sharedspice - gradual slowdown of ngSpice_Command() despite using destroy all

v1.0 (example)
open
None
5
2024-02-27
2024-02-27
simpletk
No

I have following program.

// gcc -o slowdown slowdown.c -lngspice -lpthread -Wall -ggdb

#include <stdio.h>
#include <stdbool.h>
#include <ngspice/sharedspice.h>
#include <sys/time.h>
#include <unistd.h>

int myControlledExit(int exitstatus, bool immediate, bool quitexit, int ident, void *userdata) {
    printf("exit: es=%d im=%d qe=%d\n", exitstatus, immediate, quitexit);
    return exitstatus;
}

long long millis() {
    struct timeval te;
    gettimeofday(&te, NULL);
    long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000;
    return milliseconds;
}

int main() {
    ngSpice_Init(NULL, NULL, myControlledExit, NULL, NULL, NULL, NULL);

    long long t1 = millis(), t2;

    char *a[] = {
        (char*)"* first line is ignored",
        (char*)"V1 1 0 9",
        (char*)"RR1 1 3 100",
        (char*)"RR2 3 0 100",
        (char*)".end",
        NULL
    };

    for (int i = 0; i < 100000; i++) {
        ngSpice_Circ(a);
        ngSpice_Command((char*)"tran 100u 20m 0");
        ngSpice_Command((char*)"destroy all");

        if (i % 1000 == 999) {
            t2 = millis();
            printf("iter %d, duration %lld\n", i, t2 - t1);
            t1 = t2;
        }
    }

    return 0;
}

When I ran it it gradually slows down:

iter 999, duration 2262
iter 1999, duration 2423
iter 2999, duration 2715
iter 3999, duration 3096

How can I make it not slow down?

Discussion

  • Holger Vogt

    Holger Vogt - 2024-02-27
    • assigned_to: Holger Vogt
     
  • Holger Vogt

    Holger Vogt - 2024-02-27

    I don't think that this is a bug.

    When you run ngSpice_Circ(a);, you are creating a new circuit. When you run tran 100u 20m 0, you are creating a new plot storing the simlation results. When you run destroy all, you are removing the simulation results.

    The circuits stay stored in ngspice.

    So after the loop has finished, you have 100000 circuits loaded. If you need all of them, you will have to live with the slowdown. If not, make use of the command remcirc.

    Only when this does not help, we should profile ngspice to find the source of the then still occuring slowdown.

     
  • simpletk

    simpletk - 2024-02-27

    remcirc solved the issue, thanks.

     

Log in to post a comment.