350 likes | 534 Views
Matt Defenthaler John Maskasky Vinash Seenath. Programming Language: Concurrent ML. 7/15/2009. Presentation Outline. CML Origins/History ML in CaML Shell Features of CML TraceCML CML vs. SML Dynamic Semantics Definitions Classes Grammar Evaluation Context Event Matching
E N D
Matt Defenthaler John Maskasky Vinash Seenath Programming Language: Concurrent ML 7/15/2009
Presentation Outline • CML Origins/History • ML in CaML Shell • Features of CML • TraceCML • CML vs. SML • Dynamic Semantics • Definitions • Classes • Grammar • Evaluation Context • Event Matching • Syntax of CML • Concurrency • Synchronization • Producer/Consumer in CML • File I/O • Conclusions 7/15/2009
ML originally released in the 1980’s specifically purposed as a meta language First CaML implementations came about in the late 1980's. O-CaML is an object-oriented, concurrent variation of the ML programming language CML was created by Dr. John H. Reppy CML Origins http://tunes.org/cliki/ocaml.html 7/15/2009
Objective Caml version 3.11.0 # 1+2*3;; - : int = 7 # let rec fib n = if n < 2 then n else fib(n-1) + fib(n-2) ;; val fib : int -> int = <fun> # fib 10;; - : int = 55 # fib 2;; - : int = 1 ML in CaML Shell These are some examples of code run in the Shell of the CaML compiler. • The first function is a simple addition multiplication problem in which it returned 7 • The second is defined as a function fib. to determine the Fibonacci number. (sequence starting with 1, 1) 7/15/2009
Inherited from SML Functions as first-class values Strong static typing Polymorphism Datatypes and pattern matching Lexical scoping Exception handling State of the art module facility Written in SML: should run on anything running SML Features of CML 7/15/2009
Features of CML • Those added with CML • Concurrency with dynamic thread creation • Preemptive scheduling (to prevent processor monopolization) • Automatic reclamation of threads and channels • Synchronous I/O operations • TraceCML 7/15/2009
Features of CML: TraceCML • Provides good debugging support for CML • 3 facilities • Trace modules: for controlling debugging output • Thread watching: for detecting thread termination • Reporting of uncaught exceptions on 'per thread' basis • Trace modules have been implemented in such a way that invocation of them can occur regardless of the current running state of CML. 7/15/2009
Features of CML: TraceCML TraceCML Interface type trace_moduledatatype trace_to = TraceToOut | TraceToErr | TraceToNull | TraceToFile of string | TraceToStream of TextIO.outstreamval setTraceFile : trace_to -> unitval traceRoot : trace_moduleexception NoSuchModuleval traceModule : trace_module * string -> trace_moduleval nameOf : trace_module -> stringval moduleOf : string -> trace_moduleval traceOn : trace_module -> unitval traceOff : trace_module -> unitval traceOnly : trace_module -> unitval amTracing : trace_module -> boolval status : trace_module -> (trace_module * bool) listval trace : trace_module * (unit -> string list) -> unitval watcher : trace_moduleval watch : string * CML.thread_id -> unitval unwatch : CML.thread_id -> unitval setUncaughtFn : (CML.thread_id * exn -> unit) -> unitval setHandleFn : (CML.thread_id * exn -> bool) -> unitval resetUncaughtFn : unit -> unit http://cml.cs.uchicago.edu/pages/trace-cml.html 7/15/2009
CML vs. SML • Concurrent ML is embedded in the standard ML language for programming concurrent systems. • CML supports explicit thread creation. • CML possesses event values for communications between threads. These event values are an abstraction for synchronous communications. 7/15/2009
Concurrency in ML is achieved mostly via libraries. Programs in CML are more likely to spawn processes when they are needed, instead of at the beginning of the program. CML like SML supports polymorphism, has user defined variables, and an automatic garbage collector. CML vs. SML 7/15/2009
Dynamic Semantics of λcv • λcv is a concurrent extension of λv–calculus • λcv is useful as it does posses synchronous operations 7/15/2009
Dynamic Semantics - Definitions • Here we have the foundation of λcv • Channel names are needed to send and receive information 7/15/2009
Dynamic Semantics - Classes • 3 Syntactic classes: • Expressions • Values • Event values 7/15/2009
Dynamic Semantics - Classes • e can be: 7/15/2009
Dynamic Semantics - Classes • v can be: 7/15/2009
Dynamic Semantics - Grammar • Of note are • Channel output • Channel input • Wrapper • Choice 7/15/2009
Dynamic Semantics – Evaluation Context • Similar to the production rules of denotational semantics • Spawn E creates a new process • Sync E synchronizes a channel after a non-deterministic choice is offered 7/15/2009
Dynamic Semantics – Event Matching • Key concept in semantics of concurrent evaluation • The semantic of rendezvous • By synchronizing on matching events, values can be exchanged 7/15/2009
Dynamic Semantics – Event Matching • Read as: ev1 matches ev2 on channel k with results e1 and e2 7/15/2009
Dynamic Semantics – Event Matching • Recall that k is the channel name 7/15/2009
Dynamic Semantics – Event Matching • ev1 and ev2 can be 7/15/2009
Dynamic Semantics – Event Matching • And e1 • e1 are : 7/15/2009
Dynamic Semantics – Event Matching • An example is show to illustrate an event matching situation 7/15/2009
Syntax of CML Bold text denotes elements found in CML and not in SML http://www.cs.cornell.edu/Courses/cs312/2005sp/lectures/lec02.html 7/15/2009
To achieve concurrency in ML we need to create and run multiple threads simultaneously. We may have to force the threads to synchronize in order to protect the integrity of the data. Concurrency 7/15/2009
CML has various ways in which threads can synchronize with each other. Events Threads can Synchronize on an event Communication channels Send/Recv functions -- Blocking Events SendPoll/RecvPoll functions -- Non-Blocking Events SendEvt/RecvEvt – will create an event associated with sending or receiving a message. Multicasting -- this is where a message can be transmitted to multiple threads simultaneously. SyncVars – variables that can be filled or emptied Processes will synchronize on reading from an emptied variable Synchronization 7/15/2009
MailBoxes are a combination of SyncVars and Communication channels. A producer may put objects in a mailbox where a consumer will retrieve them. Events Associated with a MailBox Send - non-blocking Recv - blocking RecvPoll – non-blocking Synchronization 7/15/2009
Buffered Producer/Consumer in CML datatype 'a buffer = BUF of {insCh : 'a chan,remCh : 'a chan}fun buffer () = let val insCh = channel() and remCh = channel()fun loop [] = loop [recv inCh]| loop buf = if (length buf > maxlen)then (send (remCh, hd buf); loop (tl, buf))else (select remCh!(hd buf) => loop (tl buf)or insCh?x => loop (buf @ [x]))inspawn loop;BUF{insCh = insCh,remCh = remCh}endfun insert (BUF{insCh, ...}, v) = send (insCh, v)fun remove (BUF{remCh, ...}) = recv remCh http://www.archub.org/arcsug.txt 7/15/2009
Buffered Producer/Consumer in CML creates buffer datatype containing two channels (insCh-for inserting elements into the buffer and remCh-for removing elements from the buffer datatype 'a buffer = BUF of {insCh : 'a chan,remCh : 'a chan}fun buffer () = let val insCh = channel() and remCh = channel()fun loop [] = loop [recv inCh]| loop buf = if (length buf > maxlen)then (send (remCh, hd buf); loop (tl, buf))else (select remCh!(hd buf) => loop (tl buf)or insCh?x => loop (buf @ [x])) creates two synchronous channels checks to see if the buffer is full ! is an attempt to send, select blocks all channels on a list of send/recv calls and executes the associated code with whichever call returns first (and drops the rest) sends the head of the buffer on the remCh channel, then callsloop with the tail of the buffer and the buffer as arguments ? is an attempt to receive, => connects the associated code to execute
Buffered Producer/Consumer in CML creates new thread of control to evaluate body of loop function. A unique ID for thread is returned. inspawn loop;BUF{insCh = insCh,remCh = remCh}endfun insert (BUF{insCh, ...}, v) = send (insCh, v)fun remove (BUF{remCh, ...}) = recv remCh inserts item into buffer by sending item v on insCh removes item from buffer by receiving on remCh 7/15/2009
Because CML is based in SML, file I/O is similar between the two. The functions needed to perform file I/O are included in the IMPERATIVE_IO file. This file includes functions such as openIn : name openOut : name These open the file for reading and writing. inputLine : strm This function will take in one line of the file File I/O in CML 7/15/2009
TextIO in CML is accessed in a similar manner to TextIO in SML The implementation was changed because “two different threads should not access the same queue without synchronization” File I/O in CML http://www.cs.princeton.edu/courses/archive/fall00/cs510/crxw/ 7/15/2009
Example: function to copy one text file to another. Include IMPERATIVE_IOfun copyFile(infile: string, outfile: string) let (* Opening files for input and output *) val ins = TextIO.openIn infile val outs = TextIO.openOut outfile (* Recursive statement, copying one character *) (* from the input file to the output file *) fun recurs(copt: char option) = case copt of NONE => (TextIO.closeIn ins; TextIO.closeOut outs | SOME(c) => (TextIO.output1(outs, c)); recurs(TextIO.intput1 ins)) in recurs(TextIO.input1 ins) end File I/O in CML 7/15/2009
Conclusions • Concurrent ML is a well-formed language due to its basis on a pre-established language, SML. • CML can provide an introduction to the concepts and challenges inherent to concurrent programming for students and hobbyists. • Unfortunately, there seems to be a lack of example code and sufficient documentation. These facts may prevent many from becoming familiar with the language. 7/15/2009
Matt Defenthaler John Maskasky Vinash Seenath Programming Language: Concurrent ML 7/15/2009