Logtalk + JPL : Concurrent call to logtalk_load

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
Victor NOEL
Posts: 48
Joined: Tue May 15, 2007 4:53 pm

Logtalk + JPL : Concurrent call to logtalk_load

Post by Victor NOEL » Wed May 28, 2008 3:04 pm

Hello,

As you may know, I am doing nasty things with logtalk, JPL (swi-prolog java library using java native interface) and agents running concurrently.

With JPL, when doing prolog queries, if there are done in different threads, they are run in different engines (I guess it is a swi-prolog thing for concurrence or multi-threading, but you know better than me), and can be queried concurrently. What I did was to put each query in a thread to do it.

And while I was doing this kind of thing, a problem appeared :

Two logtalk_load predicates were called at the same time by two thread (so two threads were each calling one thread calling the query), and it happened that the loading failed, and I got each time a different error (source_sink, or missing :-end_object, and things like that).
When I understood that the problem was that, I made the java method that call logtalk_load synchronized and then they were not run at the same time anymore and all is working fine.

After that I verified with another query calling a predicate I wrote : each thread was calling 15 query sequentially (so one thread by query sequentially, but since there was two thread, there was some concurrence) and it didn't raise any exception.

So my question is : Is it normal ? What can be causing that in my code ? Is it fixable in logtalk ? Is there other predicates that have this kind of problem ?

Thanks you,

Victor

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Logtalk + JPL : Concurrent call to logtalk_load

Post by Paulo Moura » Wed May 28, 2008 10:33 pm

Victor NOEL wrote: As you may know, I am doing nasty things with logtalk, JPL (swi-prolog java library using java native interface) and agents running concurrently.
I suspect that using Logtalk+SWI-Prolog multi-threading features would be a simpler and more robust solution, at least from your posts on the SWI-Prolog mailing list.
Victor NOEL wrote: With JPL, when doing prolog queries, if there are done in different threads, they are run in different engines (I guess it is a swi-prolog thing for concurrence or multi-threading, but you know better than me), and can be queried concurrently. What I did was to put each query in a thread to do it.
I've no experience in using JPL.
Victor NOEL wrote: And while I was doing this kind of thing, a problem appeared :

Two logtalk_load predicates were called at the same time by two thread (so two threads were each calling one thread calling the query), and it happened that the loading failed, and I got each time a different error (source_sink, or missing :-end_object, and things like that).
The Logtalk compiler uses a set of dynamic predicates for temporary data. This leads to the problems you observed when running concurrent logtalk_load/1 queries (in SWI-Prolog, by default, all dynamic predicates are shared between threads).
Victor NOEL wrote: When I understood that the problem was that, I made the java method that call logtalk_load synchronized and then they were not run at the same time anymore and all is working fine.
That's the easiest solution.
Victor NOEL wrote: After that I verified with another query calling a predicate I wrote : each thread was calling 15 query sequentially (so one thread by query sequentially, but since there was two thread, there was some concurrence) and it didn't raise any exception.

So my question is : Is it normal ? What can be causing that in my code ? Is it fixable in logtalk ? Is there other predicates that have this kind of problem ?
Sorry, I don't understand the problem from your description. Could you provide some details?

Best regards,

Paulo
Paulo Moura
Logtalk developer

Victor NOEL
Posts: 48
Joined: Tue May 15, 2007 4:53 pm

Re: Logtalk + JPL : Concurrent call to logtalk_load

Post by Victor NOEL » Thu May 29, 2008 7:32 am

Paulo Moura wrote: I suspect that using Logtalk+SWI-Prolog multi-threading features would be a simpler and more robust solution, at least from your posts on the SWI-Prolog mailing list.
Do you mean by defining my agents in prolog ? So to not use java and JPL ?
Paulo Moura wrote: The Logtalk compiler uses a set of dynamic predicates for temporary data. This leads to the problems you observed when running concurrent logtalk_load/1 queries (in SWI-Prolog, by default, all dynamic predicates are shared between threads).
Victor NOEL wrote: When I understood that the problem was that, I made the java method that call logtalk_load synchronized and then they were not run at the same time anymore and all is working fine.
That's the easiest solution.
Victor NOEL wrote: After that I verified with another query calling a predicate I wrote : each thread was calling 15 query sequentially (so one thread by query sequentially, but since there was two thread, there was some concurrence) and it didn't raise any exception.

So my question is : Is it normal ? What can be causing that in my code ? Is it fixable in logtalk ? Is there other predicates that have this kind of problem ?
Sorry, I don't understand the problem from your description. Could you provide some details?
The problem is exactly what I described with logtalk_load, what I described with 15 query was to show that I didn't had this problem with every predicate, but only logtalk_load.

And I am wondering if there is predicate like logtalk_load that uses dynamic predicates ?

Thanks you.

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Logtalk + JPL : Concurrent call to logtalk_load

Post by Paulo Moura » Thu May 29, 2008 8:49 am

Victor NOEL wrote:
Paulo Moura wrote: I suspect that using Logtalk+SWI-Prolog multi-threading features would be a simpler and more robust solution, at least from your posts on the SWI-Prolog mailing list.
Do you mean by defining my agents in prolog ? So to not use java and JPL ?
Possibly. Why exactly you're using Java? For giving your agents a graphical user interface?
Victor NOEL wrote: The problem is exactly what I described with logtalk_load, what I described with 15 query was to show that I didn't had this problem with every predicate, but only logtalk_load.

And I am wondering if there is predicate like logtalk_load that uses dynamic predicates ?
Logtalk uses a set of dynamic predicates to represent runtime tables. These tables can be accessed using a set of built-in objects. In addition, the three built-in methods that create new, dynamic entities call internally the Logtalk compiler, similar to the logtalk_load/1-2 and logtalk_compile/1-2 built-in predicates. These predicates would also need to be synchronized.

Best regards,

Paulo
Paulo Moura
Logtalk developer

Victor NOEL
Posts: 48
Joined: Tue May 15, 2007 4:53 pm

Re: Logtalk + JPL : Concurrent call to logtalk_load

Post by Victor NOEL » Thu May 29, 2008 9:21 am

Paulo Moura wrote:
Victor NOEL wrote: Do you mean by defining my agents in prolog ? So to not use java and JPL ?
Possibly. Why exactly you're using Java? For giving your agents a graphical user interface?
In fact I am not using java, but scala, which is : (from its homage scala-lang.org)
"Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages. It is also fully interoperable with Java."

And I do use it because I like it and because I know that my work will be used with java.
Furthermore, I need my agents to communicate, to be on different machines, and I don't know which platform will be used, and scala provide some interesting features like actors, that could be distributed, and pattern matching.

It is true that I could write my agents with prolog, but I never thought about it, and I don't feel like doing it since my internship is ending in 3-4 weeks and I am late :>
Paulo Moura wrote: Logtalk uses a set of dynamic predicates to represent runtime tables. These tables can be accessed using a set of built-in objects. In addition, the three built-in methods that create new, dynamic entities call internally the Logtalk compiler, similar to the logtalk_load/1-2 and logtalk_compile/1-2 built-in predicates. These predicates would also need to be synchronized.
So, technically, I need to synchronize logtalk_load/1-2, logtalk_compile/1-2, and create_{object, category, protocole} ?
What about the abolish_{object, category, protocole} predicates ?

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Logtalk + JPL : Concurrent call to logtalk_load

Post by Paulo Moura » Thu May 29, 2008 9:41 am

Victor NOEL wrote:
Paulo Moura wrote: Logtalk uses a set of dynamic predicates to represent runtime tables. These tables can be accessed using a set of built-in objects. In addition, the three built-in methods that create new, dynamic entities call internally the Logtalk compiler, similar to the logtalk_load/1-2 and logtalk_compile/1-2 built-in predicates. These predicates would also need to be synchronized.
So, technically, I need to synchronize logtalk_load/1-2, logtalk_compile/1-2, and create_{object, category, protocole} ?
What about the abolish_{object, category, protocole} predicates ?
You should also synchronize the abolish entity predicates. There is always, at least theoretically, a possibility that one thread tries to use an object while another thread is trying to abolish it.

Best regards,

Paulo
Paulo Moura
Logtalk developer

Victor NOEL
Posts: 48
Joined: Tue May 15, 2007 4:53 pm

Re: Logtalk + JPL : Concurrent call to logtalk_load

Post by Victor NOEL » Thu May 29, 2008 9:42 am

Ok, thanks you for all your answers :)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests