Recent

Author Topic: PASCAL shared library are getting shared in multiple instances  (Read 2867 times)

selvavm

  • Newbie
  • Posts: 4
PASCAL shared library are getting shared in multiple instances
« on: February 09, 2019, 09:43:46 am »
Hi Guys,

I am really new to PASCAL and fpc. I am working on a project where I have to make the PASCAL code as a shared library and then load into FORTRAN.

I have successfully made the .so and using dlopen command I am able to load the .so in my FORTRAN. In PASCAL, I used exports command to expose some functions and they return results correctly in FORTRAN.

Now, I have to load multiple instances of this .so in my FORTRAN in a loop with different initial conditions. Also, these are loaded using openmp thread.

Here comes my problem. I could see that variables are getting shared between threads in FORTRAN and hence my results are wrong.

I tried many things in FORTRAN and PASCAL. In PASCAL, I tried threadvar, -Cg compiler flag but still no luck.

I need to implement this immediately and I am completely lost  %)

Can anyone please let me know what I can do? Thanks in advance

Note: My PASCAL code is huge and hence I can’t make any big code change or refactoring.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: PASCAL shared library are getting shared in multiple instances
« Reply #1 on: February 09, 2019, 11:00:39 am »
This is not an FPC issue, but an architectural issue: you will run into the same issue with any programming language including FORTRAN itself.
It is not really clear to me what you actually want to achieve, maybe maintaining state on a per thread basis? in that case you need to load the dynamic library instances in the threads and not in the main program and indeed use threadvars.
« Last Edit: February 09, 2019, 11:02:22 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

balazsszekely

  • Guest
Re: PASCAL shared library are getting shared in multiple instances
« Reply #2 on: February 09, 2019, 11:16:00 am »
You can also dynamically load the library when needed, like this:
- load library
- call the function, get the values you need
- free library
I can provide a pascal example if needed. I never worked with Fortran, but I'm almost certain you can achieve the same thing.

selvavm

  • Newbie
  • Posts: 4
Re: PASCAL shared library are getting shared in multiple instances
« Reply #3 on: February 09, 2019, 12:52:16 pm »
This is not an FPC issue, but an architectural issue: you will run into the same issue with any programming language including FORTRAN itself.
It is not really clear to me what you actually want to achieve, maybe maintaining state on a per thread basis? in that case you need to load the dynamic library instances in the threads and not in the main program and indeed use threadvars.

My pascal code is a huge application and it reads the input from a txt file. From the txt file, it computes some calculation and along with that build some folder paths. I can see that the thread 2 logs has the folder path from the thread 1. I do load the applications in a thread and also I tried threadvar as mentioned earlier.

In other languages, it looks like there is an option called fvisibility to serve this purpose but in fpc I am not able to find a similar option.

You can also dynamically load the library when needed, like this:
- load library
- call the function, get the values you need
- free library
I can provide a pascal example if needed. I never worked with Fortran, but I'm almost certain you can achieve the same thing.

Thanks for the suggestion. But, I can’t free one instance and then load other. For example, I run a function in all instances at once using thread and then do some calculations in FORTRAN. Then I invoke a new function in all instances at once using thread and with the input from the calculation in FORTRAN.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: PASCAL shared library are getting shared in multiple instances
« Reply #4 on: February 09, 2019, 07:05:49 pm »
I don't know how fortran handles the loading of a lib file like that but if there Is a reference handle
being returned you should compare it with other loads..

 Its possible only one lib per process space is getting loaded because it see's that it is already loaded..

 Personally I think you are taking the wrong approach to this..  It should be a single lib file with arrays of
data.

 To keep it simple for the time being, you could use a INDEX parameter that gets set and thus adding
data to the arrays are simply because the index is known already..

 Then when these items need to work, they can do so by specifying the array indexes …

 as for threads, this INDEX could be the thread index using the single instance of the .so lib..

 Something to think about I am sure.

The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: PASCAL shared library are getting shared in multiple instances
« Reply #5 on: February 09, 2019, 09:15:43 pm »
To use Threadvar, you need to have a thread manager. Would adding cthreads unit to your uses section solve this problem?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: PASCAL shared library are getting shared in multiple instances
« Reply #6 on: February 09, 2019, 09:23:25 pm »
Threadvar is not what you need, afaik its instancing is narrowly tied to thread apis.


sash

  • Sr. Member
  • ****
  • Posts: 366
Re: PASCAL shared library are getting shared in multiple instances
« Reply #7 on: February 09, 2019, 09:27:56 pm »
Not sure I'm getting your explanation...
Now, I have to load multiple instances of this .so in my FORTRAN in a loop with different initial conditions.
... I could see that variables are getting shared between threads

Why do you ever need to load multiple instances of shared lib?
Instead, why don't you to create a multiple instances of your data and call your processing procedure (no matter where it is located or whether it called in threads or serially).
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

selvavm

  • Newbie
  • Posts: 4
Re: PASCAL shared library are getting shared in multiple instances
« Reply #8 on: February 09, 2019, 11:04:54 pm »
Thanks everyone for the response. I tried to remove the shared variable and implemented some logic to calculate it.

Anyway, Now I face a different issue. In my pascal shared library there is a below code,
Code: Pascal  [Select][+][-]
  1. try
  2.   //Handle: tHandle;  is a private variable in a class
  3.   Write('Loading so');
  4.   Handle := LoadLibrary(PChar(INPUTSpath + DLLFilename)); //INPUTSpath is different for each call
  5.   Write('Loaded so');
  6. except
  7.    //
  8. end;

Now, when invoking two instances in OpenMP thread, I get the 'Loading so' printed twice, 'Loaded so' printed once and then it throws RangeCheckError at runtime. However, If I don't run in multiple threads (synchronised), this error does not occur and I get 'Loaded so' printed twice. Any idea why? Could this be a bug?


To all about the shared library should not be running as multiple instances, I am also aware but unfortunately my PASCAL code is a legacy codebase and huge. Hence, I don't have control on it.
« Last Edit: February 10, 2019, 04:48:19 am by selvavm »

selvavm

  • Newbie
  • Posts: 4
Re: PASCAL shared library are getting shared in multiple instances
« Reply #9 on: February 13, 2019, 10:49:15 am »
I came across simpleIPC and looks like I can use this to avoid the thread sharing issue. I can make each instance as a server with unique id and my FORTRAN as a client.
 
But I have below doubts
  • Is FORTRAN supported as Client?
  • Is it possible to call a function/procedure from client?
  • Is it possible to send/receive an array?

 

TinyPortal © 2005-2018