Programming Language: Concurrent ML

Post on 31-Dec-2015

31 views 2 download

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

Programming Language: Concurrent ML

Matt Defenthaler John Maskasky Vinash Seenath

7/15/2009

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

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

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)

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

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

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

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

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

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.

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:o Expressionso Values o 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 o Channel outputo Channel inputo Wrapper o 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 CMLBold text denotes elements found in CML and not in SML

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

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.

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

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

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

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

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

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

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/

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

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

Programming Language: Concurrent ML

Matt Defenthaler John Maskasky Vinash Seenath

7/15/2009