Object-Oriented Models ====================== Object = Encapsulation of data and procedures Class = Type of an object Object = Instance of a class Arbitrary many instances of a class may be generated "Object-Based Programming" = programming based on objects Inheritance: derivation of a "subclass" from a "superclass" Subtyping: an instance of a subclass may appear in any context where an instance of a superclass is expected "Object-Oriented Programming" = "Object-Based Programming" + Inheritance Parallelism ----------- Parallel variants of Object-Based Models - basic idea: objects correspond to processes - inheritance or not is an orthagonal issue Basic distinction [ - parallelism within an object ] - passive objects - active objects Parallelism within an object ---------------------------- class C { ... f(...) { par { ... ... } } } Like in imperative programming Passive objects --------------- Object is a "communication space" in which multiple concurrent threads may interact un a coordinated way CC++: class Object { Data data; public: Object() { init(data) } access(args) { update(data, args) } } main() { global Object *o = new Object // communication space par { process(o) // thread 1 process(o) // thread 2 } } process(Object *o) { ... o->access(args) ... } Active objects -------------- Object is a process that becomes active when the object is created and that lives its live interacting with other objects - when object created, a thread is started exectuing its body - different objects execute in parallel and may interact by explicit sending and answering of messages Simulation by passive objects (CC++-like syntax): class Object { Data data; Condition full, empty; Object *prev; run() { while (!prev.term()) { data = prev.get(); signal full; wait empty; } } public: Object(Object *o): prev(o), full(false), empty(true) { spawn run() } virtual get() { wait full; Data d = data; signal empty; return(d); } virtual term() { return(prev.term()); } } class Input: Object { ... } extern Input *input; class Main { run() { Object *o = input; for (int i = 0; i < N; i++) { o = new Object(o) } while (!o.term) { result = o.get() cout << result; } } public: Main() { spawn run() } } - Just simulation - More convenient syntax/programming style later Passive Object Languages ======================== CC++ COOL Concurrent Smalltalk COOL ---- Concurrent Object Oriented Language Stanford Extension of C++ - passive objects approach class C { .... [parallel] [mutex] T f(...) { ... } } Parallel functions ------------------ When invoked immediately returns a result ("future", "promise") Synchronization takes implicitly place when value is accessed [ cf. C++ "sync" Type] - Concurrency across objects Functions on different objects may execute concurrently - Concurrency within an object, across functions Several functions may execute simultaneously on the same object - Concurrency within a function A function executing on an object may invoke parallel functions Mutex functions --------------- Function has exclusive access to object [cf. Monitors, C++ "atomic"] Future operations ----------------- [ cf. C++ "sync" Type] class C { ... parallel f() {...} ... } C *o = new C; int$ x = o->f(); y = x+1; // implicit synchronization waitset(x); // explicit synchronization clear(x); // unresolve x Future variables can be unresolved and set again "Multiple single assignments" Locks ----- lock(lockvar) unlock(lockvar) Example: merge sort ------- SLIDE: Languages and Compilers for Parallel Computing page 138, Figure 4 Concurrent Smalltalk -------------------- Keio University, Japan PASSIVE objects approach - Asynchronous method call db update& method update is invoked asynchronously on object db - CBOX objects communication box, "future" t1 <- db lookup: #key& ... t2 <- t1 receive - Atomic objects SuperClassName atomicSubclass: SubClassName .... Methods are executed serially (and thus without interference) Active Object Languages ======================= Actors ------ A model for concurrent computation in distributed systems Agha, MIT Program = set of actors - computational agents with "behavior" Behavior accepts a certain set of messages - send messages to other actors - create new actors - becomes new behavior Analogy - behavior ~ class - actor ~ object Formal basis of programming languages with active objects - behavior definition def behavior(acquaintances) [communications] command defines new behavior - acquaintances: arguments provided on actor creation - communications: bound by communication - communications [ params ] - accept values binding params [ case exp of label: (params) ... end ] - if communication is [label, vals] accept communication with values binding vals to params - create actor new behavior(args) create actor with certain behavior binding the acquaintances - send communication to actor send comm to expression - replace behavior become expression Example: ------- def main let p = new stack-node(NIL) popper = new pop(p, popper) in become push(p, n) def push(p, n) send [push, n] to p become push(p, n+1) def pop(p, self) send [pop, self] to p become receive(p, self) def receive(p, self) [ result ] ... become pop(p, self) def stack-node (content) [ case operation of pop: (customer) push: (elem) end case ] if operation = pop and not(isnil(content)) then send first(content) to customer become stack-node(rest(content)) fi if operation = push then become stack-node(cons(elem, content)) fi end def POOL ---- Parallel Object Oriented Language Philips, ESPRIT II Programming language of DOOM - Decentralized Object-Oriented Machine ACTIVE objects approach - body of a class executes sort of a "SELECT" (cf. OCCAM) statement describing which messages iit accepts in a sertain state SLIDE "Parallel Computers: Object-Oriented, Functional, Logic" page 54, Figure 3.2 page 56, Figure 3.3 uC++ ---- University of Waterloo uTask T { ... void main(); // starting member ... public: T(params) { ... } } creates a task type T T *t = new T(args); creates new task t executing t->main() features - uTask corresponds to monitor public member routines provide mutual exclusion - condition variables uWait cond, uSignal cond - uAccept (method) accept next only calls of particular method all other access attempts are blocked selective accepting of communications [ uWhen(exp) ] uAccept(dname) statement uOr [ uWhen(exp) ] uAccept(dname) statement uOr ... [ uWhen(exp) ] uAccept(dname) statement uElse statement Allows to get rid of condition variables!!! SLIDE uC++ Annotated Reference Manual page 24, Figure 2.6 Examples -------- SLIDE uC++ Annotated Reference Manual page 73, C.3.3 Task Migration -------------- old_cluster = uMigrate(new_cluster); migrates current task to other processor cluster Autonomous objects, intelligent messages, messengers, mobile agents.