Agents and Actors

Agents and Actors

Agents and actors April 16, 2014 Scope Scope ■ Agent programming is a term used in many contexts Clojure agents Agents/actors ◆ concurrent entities elsewhere ◆ intelligent agents ◆ agents with goals ◆ cooperating agents ◆ etc We are primarly interested in properties arising from concurrency. 2 / 19 Scope Clojure agents General Agent mechanics Diagram Semantics of agents Errors in agents IO/STM/nested sends Example: log the Clojure agents game Agents/actors elsewhere 3 / 19 General Scope ■ Agent in Clojure is a reference type (mutable holding Clojure agents General state) that can be changed by sending it an action Agent mechanics (function) Diagram Semantics of agents ◆ uncoordinated – independent of changes of other Errors in agents IO/STM/nested agents sends ◆ Example: log the asynchronous – changes are away from the thread game that schedules an action Agents/actors elsewhere ■ immediate return: no wait for the completion of an action ■ 2 characteristics of Clojure agents ◆ I/O may be used safely with agents unlike in STM ◆ agents are STM aware 4 / 19 Agent mechanics Scope ■ reading agent’s state deref/@ – non-blocking Clojure agents ■ General (send a action-fn & args) – send agent a an action Agent mechanics Diagram ◆ action-fn takes agent state, args and returns new Semantics of agents agent state Errors in agents ◆ IO/STM/nested as with sends (apply action-fn state-of-agent args) Example: log the game Agents/actors ■ actions are stored into the queue for each agent elsewhere ◆ processed sequentially by threads, mutating its state ◆ thread from one of two thread pools ■ thread pools ◆ fixed size – actions delivered from send ◆ unlimited size – actions delivered from send-off ■ needed for blocking IO 5 / 19 Diagram Scope Clojure agents General Agent mechanics Diagram Semantics of agents Errors in agents IO/STM/nested sends Example: log the game Agents/actors elsewhere ■ black actions are CPU bound (send) ◆ threads t2 and t3 are from fixed size pool ◆ threads t18 and t9 are from unbounded pool ■ for single agent all actions still are sequential, although may be running in different threads 6 / 19 Semantics of agents Scope ■ agents != threads Clojure agents General ◆ usually number of threads << number of agents Agent mechanics Diagram ◆ agents still run concurrently -> green threads Semantics of agents ■ Errors in agents Remote Procedure Call (RPC) IO/STM/nested sends ◆ Example: log the allow to enter multiple threads instead of one game ◆ state is not declarative Agents/actors elsewhere ■ reduce – value from a list + operation = action (fun + args) ◆ intermediate reduction values = states ■ await/await-for – wait for all actions from current thread to complete 7 / 19 Errors in agents Scope ■ agent on exception goes to error state Clojure agents General ◆ agent-error returns exception or nil Agent mechanics Diagram ◆ sending actions to agent throw the exception Semantics of agents ■ Errors in agents (restart-agent agent new-state & options) – IO/STM/nested clears error state sends Example: log the game ◆ :clear-actions also clears the action queue Agents/actors elsewhere ■ when creating an agent ◆ :error-mode, either :fail (default) or :continue ◆ agent is not failed on ’continue’ mode ◆ :error-handler – provide function that is called in ’continue’ mode ■ set-error-mode!, set-error-handler! 8 / 19 IO/STM/nested sends Scope ■ synchronization point for IO Clojure agents General ◆ exclusive access to file,socket,stream Agent mechanics Diagram ◆ others schedule actions on this agent Semantics of agents ■ Errors in agents unlike refs/atoms that should avoid side-effects because IO/STM/nested sends of possible transaction restarts Example: log the ■ game STM aware: any actions sent within a transaction are Agents/actors held until it commits elsewhere ■ nested send: any actions sent within an agent are withheld until current action is finished 9 / 19 Example: log the game Scope ■ Add watchers to game atoms, refs Clojure agents ■ General watchers send states to logging agent(s) Agent mechanics Diagram ◆ watcher callback is called in the transaction thread, Semantics of agents presumably before releasing the ref Errors in agents ◆ IO/STM/nested correct order of ref states are queued to the agent(s) sends Example: log the ■ game logging game changes and not states requires Agents/actors interception of game methods elsewhere ◆ must be done (!) in the transaction because of possible transaction restarts ■ log action is delayed until commit 10 / 19 Scope Clojure agents Agents/actors elsewhere Oz, Port object (Agent) Oz Example: counter agent Oz Multiagent dataflow programming Agents/actors elsewhere Actors in general (Oz) Active vs. passive objects Active objects in Oz Erlang agents Scala/akka actors 11 / 19 Oz, Port object (Agent) Scope fun {NewPortObject Init Fun} Clojure agents Sin Sout in Agents/actors thread {FoldL Sin Fun Init Sout} end elsewhere Oz, Port object {NewPort Sin} (Agent) Oz Example: end counter agent fun {NewPortObject2 Proc} Oz Multiagent dataflow Sin in programming Actors in general thread for Msg in Sin do {Proc Msg} end end (Oz) {NewPort Sin} Active vs. passive objects end Active objects in Oz Erlang agents ■ 1 thread! Scala/akka An agent is looping recursive procedure ( ) actors ◆ with input stream (inbox) ◆ maybe state ■ with state – NewPortObject ■ without state – NewPortObject2 12 / 19 Oz Example: counter agent Scope fun {Counter Sin X} Clojure agents case X Agents/actors of add(Z) then elsewhere Oz, Port object Sin+Z % state changes (Agent) Oz Example: [] get(R) then counter agent R=Sin Oz Multiagent dataflow Sin % state does not change programming Actors in general end (Oz) end Active vs. passive objects A = {NewPortObject 0 Counter} Active objects in {Send A add(3)} Oz Erlang agents {Send A add(˜4)} Scala/akka {Browse {Send A get($)}} actors ■ maintains internal counter ◆ access through messages add and get 13 / 19 Oz Multiagent dataflow programming Scope The author of CTM defines multiagent dataflow programming Clojure agents Agents/actors 1. declarative model with dataflow variables elsewhere Oz, Port object (Agent) ■ race condition free Oz Example: counter agent Oz Multiagent 2. + ports dataflow programming Actors in general ■ enhances type of algorithms (Oz) Active vs. ◆ i.e. to those with non-determinism passive objects Active objects in ■ Oz adds race condition possibility Erlang agents Scala/akka actors 14 / 19 Actors in general (Oz) Scope Actors are basicaly the same as agents (port objects). Clojure agents Agents/actors ■ like a port object elsewhere Oz, Port object (Agent) ◆ one thread with a message stream Oz Example: counter agent ◆ process messages sequentially Oz Multiagent dataflow programming ■ unlike a port object Actors in general (Oz) Active vs. ◆ behavior is defined by class passive objects Active objects in ■ i.e. it is OOP object Oz Erlang agents ◆ every message in the stream is one method call Scala/akka actors ◆ has explicit state in object attributes ■ state is encapsulated (confined) 15 / 19 Active vs. passive objects Scope In passive (typical) object Clojure agents Agents/actors ■ no dedicated thread elsewhere Oz, Port object ■ if a thread calls a method it “runs the method through” (Agent) Oz Example: counter agent ◆ synchronous – waits until the method is finished Oz Multiagent dataflow programming ■ several threads may execute inside the same object Actors in general (Oz) Active vs. In active object passive objects Active objects in Oz ■ one dedicated thread Erlang agents ■ Scala/akka asynchronous – adds method call into the stream actors ■ only one thread executes inside the same object 16 / 19 Active objects in Oz Scope fun {NewActive Class Init} Clojure agents Obj={New Class Init} Agents/actors P elsewhere Oz, Port object in (Agent) Oz Example: thread S in counter agent {NewPort S P} Oz Multiagent dataflow for M in S do {Obj M} end programming Actors in general end (Oz) proc {$ M} {Send P M} end Active vs. passive objects end Active objects in Oz Erlang agents ■ Scala/akka much like an agent actors ■ Send is abstracted with the help of procedure ■ same syntax if we want to call a method of either ◆ an active or a passive object 17 / 19 Erlang agents Scope See Erlang-1 slides. Clojure agents Agents/actors elsewhere Oz, Port object (Agent) Oz Example: counter agent Oz Multiagent dataflow programming Actors in general (Oz) Active vs. passive objects Active objects in Oz Erlang agents Scala/akka actors 18 / 19 Scala/akka actors Scope Clojure agents Agents/actors elsewhere Oz, Port object (Agent) Oz Example: counter agent Oz Multiagent dataflow programming Actors in general (Oz) Active vs. passive objects Active objects in Oz Erlang agents Scala/akka actors 19 / 19.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    19 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us