ngspice compiles as a shared library

2013-04-14
2013-10-11
  • Holger Vogt

    Holger Vogt - 2013-04-14

    Dear all,

    ngspice from git may be compiled as a shared library or dll,
    offering control over the simulator by a calling process.
    Please have a look at http://ngspice.sourceforge.net/shared.html
    for a short introduction and links to example files.

    More documentation still has to come.

    Holger

     
  • jb22

    jb22 - 2013-05-24

    Hello all,

    I am currently trying to integrate the shared library in a larger framework. Here is what I am struggling with:

    The framework I am using is object oriented and written in C++. My plan was to create a class for ngspice and integrate the callback functions in that class. First I use dlopen, dlsym to load the library and register the interface functions via the function pointers.

      const char *libpath = NG_LIB_PATH; 
      nglibhandle = dlopen(libpath, RTLD_NOW);
    
      ngSpice_Init_handle = dlsym(nglibhandle, "ngSpice_Init");
    

    When calling ngSpice_Init I need to pass function pointers for the callback functions. This is where I have trouble fitting it into the class structure. ngSpice_Init expects function pointers which are either stand alone or static member functions. But once I am using static member functions I do not have access to my class object anymore.

      ret = ((int * (*)(SendChar*, SendStat*, ControlledExit*, SendData*, SendInitData*,
          BGThreadRunning*, void*)) ngSpice_Init_handle)(
          ng_send_char_static, 
          ng_send_stat_static,
          ng_exit_static, 
          ng_send_data_static, 
          ng_send_initdata_static, 
          ng_bgthread_running_static, 
          nullptr);
    

    In other words: The callbacks expects function pointers. Pointers to my class member functions would member function pointers. They are incompatible with each other and I can only use static functions. But when instantiating multiple objects of the ngspice class, the static members are shared among all the objects.

    Did you think or do you know of a way to elegantly integrate the shared library in C++ class without using static functions/variables.

    Thanks a lot
    Jens

     
  • Holger Vogt

    Holger Vogt - 2013-05-24

    Jens,

    when you use the last paramenter of the ngspice_init function to send the this-pointer of your class object, the callback will return it without change as a parameter to your callback function, and you may use it to access your class object.

    I used this in the MS Windows Delphi example to have accecc to the form class.

    I got this idea from
    http://bcbjournal.org/articles/vol3/9902/Using_callbacks_in_DLLs.htm
    chapter "Adding a user data parameter".

    Holger

     
  • jb22

    jb22 - 2013-05-24

    Holger,

    thanks for the swift reply.
    This is exactly what I was looking for. I didn't get that the userdata of the callbacks is the same as handed over to the init function. So, I already tried to pass the this pointer in some other ways, without success though.

    Thanks for your support.
    Jens

    (btw. I really appreciate all your efforts you put into ngspice)

     
  • Robert Larice

    Robert Larice - 2013-05-25

    Hello Jens,

      if I understand correctly you intend to use
         more than one ngspice in your program.

      unfortunatly we still have quite some global
         variables around in ngspice,
      thus prohibiting your plan.

      we are slowly reducing them, but IMHO this
         will still need quite some time untill
          the last one is gone.

      if you need more than one ngspice,
         your best bet currently is to rely on fork()/exit()
         and some sort of inter process comunication,
       (shared memory, sockets and their like …)

    Regards,
      Robert

     
  • Robert Larice

    Robert Larice - 2013-05-25

    Hello,

       Holger, I think thats correct, if the globals are not exported,
          these might pose not problem at all.

    Robert

     
  • Robert Larice

    Robert Larice - 2013-05-25

    Sorry, too fast,

    > Holger, I think thats correct, if the globals are not exported, these might pose not problem at all.

    thats not correct.
    you see, I'd have to think more about it, and can't.

    nevertheless,  be warnded, and have a thorough
       look at those links Holger provided.

    Sorry,
       Robert
      

     
  • jb22

    jb22 - 2013-06-13

    Just a update on what I finally did...

    I have wrapped the ngspice API in a class and I am dynamically creating objects. As was said by Holger and Robert, running multiple instances with the same library does not work that easily.

    I took the easy route and am creating local copies of libngspice for every object of ngspice. Therefore every object has its own library.

    This is definitely not a very neat way of doing it, but fast and it works. I have tested it using threads for each ngspice simulation and can run an arbitrary number of simulations in parallel.

    Thanks again for the feedback.
    Jens

     
  • Samir Sabri

    Samir Sabri - 2013-10-11

    Hello Holger,

    I came across this thread, and I am interested in compiling a shared library targeting android, is this possible thing? can I compile from windows?

    At http://ngspice.sourceforge.net/shared.html :

    As a new option ngspice may be compiled as a shared library (.so in LINUX, .dll in MS Windows)

    So, is this only possible for LINUX& MS Windows?

    Would the generated shared library for android be totally independent from dependencies?

    We need a documentation about how to compile the shared library please.
    Any help would be highly appreciated.

    Also, from legal point of view, can I use the shared library in a proprietary project? what's the conditions?

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks