Programming Language: Concurrent ML

35
Programming Language: Concurrent ML Matt Defenthaler John Maskasky Vinash Seenath 7/15/2009

description

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 - PowerPoint PPT Presentation

Transcript of Programming Language: Concurrent ML

Page 1: Programming Language: Concurrent ML

Programming Language: Concurrent ML

Matt Defenthaler John Maskasky Vinash Seenath

7/15/2009

Page 2: Programming Language: Concurrent ML

Presentation Outline• CML Origins/History• ML in CaML Shell• Features of CML

o TraceCML• CML vs. SML• Dynamic Semantics

o Definitions o Classeso Grammaro Evaluation Contexto Event Matching

• Syntax of CML• Concurrency• Synchronization• Producer/Consumer in CML • File I/O• Conclusions

7/15/2009

Page 3: Programming Language: Concurrent ML

7/15/2009

CML Origins

• 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

http://tunes.org/cliki/ocaml.html

Page 4: Programming Language: Concurrent ML

7/15/2009

ML in CaML Shell• 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

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)

Page 5: Programming Language: Concurrent ML

7/15/2009

Features of CML

• Inherited from SMLo Functions as first-class valueso Strong static typingo Polymorphismo Datatypes and pattern matchingo Lexical scopingo Exception handlingo State of the art module facility

• Written in SML: should run on anything running SML

Page 6: Programming Language: Concurrent ML

7/15/2009

Features of CML

• Those added with CMLo Concurrency with dynamic thread creationo Preemptive scheduling (to prevent processor monopolization)o Automatic reclamation of threads and channelso Synchronous I/O operations o TraceCML

Page 7: Programming Language: Concurrent ML

Features of CML: TraceCML

• Provides good debugging support for CML• 3 facilities

o Trace modules: for controlling debugging outputo Thread watching: for detecting thread terminationo 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

Page 8: Programming Language: Concurrent ML

Features of CML: TraceCML

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

TraceCML Interface

7/15/2009http://cml.cs.uchicago.edu/pages/trace-cml.html

Page 9: Programming Language: Concurrent ML

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.

Page 10: Programming Language: Concurrent ML

7/15/2009

CML vs. SML

• 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.

Page 11: Programming Language: Concurrent ML

Dynamic Semantics of λcv

• λcv is a concurrent extension of λv–calculus

• λcv is useful as it does posses synchronous operations

7/15/2009

Page 12: Programming Language: Concurrent ML

Dynamic Semantics - Definitions

• Here we have the foundation of λcv • Channel names are needed to send and

receive information

7/15/2009

Page 13: Programming Language: Concurrent ML

Dynamic Semantics - Classes

• 3 Syntactic classes:o Expressionso Values o Event values

7/15/2009

Page 14: Programming Language: Concurrent ML

Dynamic Semantics - Classes

• e can be:

7/15/2009

Page 15: Programming Language: Concurrent ML

Dynamic Semantics - Classes

• v can be:

7/15/2009

Page 16: Programming Language: Concurrent ML

Dynamic Semantics - Grammar

• Of note are o Channel outputo Channel inputo Wrapper o Choice

7/15/2009

Page 17: Programming Language: Concurrent ML

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

Page 18: Programming Language: Concurrent ML

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

Page 19: Programming Language: Concurrent ML

Dynamic Semantics – Event Matching

• Read as:ev1 matches ev2 on channel k with results e1 and e2

7/15/2009

Page 20: Programming Language: Concurrent ML

Dynamic Semantics – Event Matching

• Recall that k is the channel name

7/15/2009

Page 21: Programming Language: Concurrent ML

Dynamic Semantics – Event Matching

• ev1 and ev2 can be

7/15/2009

Page 22: Programming Language: Concurrent ML

Dynamic Semantics – Event Matching

• And e1 e1 are :

7/15/2009

Page 23: Programming Language: Concurrent ML

Dynamic Semantics – Event Matching

• An example is show to illustrate an event matching situation

7/15/2009

Page 24: Programming Language: Concurrent ML

Syntax of CMLBold text denotes elements found in CML and not in SML

http://www.cs.cornell.edu/Courses/cs312/2005sp/lectures/lec02.html7/15/2009

Page 25: Programming Language: Concurrent ML

7/15/2009

Concurrency

• 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.

Page 26: Programming Language: Concurrent ML

7/15/2009

Synchronization

• CML has various ways in which threads can synchronize with each other.o Events

Threads can Synchronize on an evento 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.o SyncVars – variables that can be filled or emptied

Processes will synchronize on reading from an emptied variable

Page 27: Programming Language: Concurrent ML

7/15/2009

Synchronization

• MailBoxes are a combination of SyncVars and Communication channels. o A producer may put objects in a mailbox where a

consumer will retrieve them. o Events Associated with a MailBox

Send - non-blocking Recv - blocking RecvPoll – non-blocking

Page 28: Programming Language: Concurrent ML

Buffered Producer/Consumer in CMLdatatype '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

7/15/2009 http://www.archub.org/arcsug.txt

Page 29: Programming Language: Concurrent ML

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]))

creates buffer datatype containing two channels (insCh-for inserting elements into the buffer and remCh-for removing elements from the buffer

creates two synchronous channels

checks to see if the buffer is full

sends the head of the buffer on the remCh channel, then calls loop with the tail of the buffer and the buffer as arguments

! 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)

? is an attempt to receive,=> connects the associated code to execute

Page 30: Programming Language: Concurrent ML

Buffered Producer/Consumer in CML

inspawn loop;BUF{insCh = insCh,remCh = remCh}endfun insert (BUF{insCh, ...}, v) = send (insCh, v)

fun remove (BUF{remCh, ...}) = recv remCh

7/15/2009

creates new thread of control to evaluate body of loop function. A unique ID for thread is returned.

inserts item into buffer by sending item v on insCh

removes item from buffer by receiving on remCh

Page 31: Programming Language: Concurrent ML

7/15/2009

File I/O in CML

• 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 aso openIn : nameo openOut : name

These open the file for reading and writing.o inputLine : strm

This function will take in one line of the file

Page 32: Programming Language: Concurrent ML

7/15/2009

File I/O in CML

• 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”

http://www.cs.princeton.edu/courses/archive/fall00/cs510/crxw/

Page 33: Programming Language: Concurrent ML

7/15/2009

File I/O in CML

• 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

Page 34: Programming Language: Concurrent ML

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

Page 35: Programming Language: Concurrent ML

Programming Language: Concurrent ML

Matt Defenthaler John Maskasky Vinash Seenath

7/15/2009