PPL Unit-4
PPL Unit-4
Concurrent ML:
Concurrent ML (CML) is an extension to ML that includes a form of
threads and a form of synchronous message passing to support
concurrency.
A thread is created in CML with the spawn primitive, which takes the
function as its parameter. In many cases, the function is specified as an
anonymous function.
As soon as the thread is created, the function begins its execution in
the new thread. The return value of the function is discarded. The
effects of the function are either output produced or through
communications with other threads.
Channels provide the means of communicating between threads. A
channel is created with the channel constructor. For example, the
following statement creates a channel of arbitrary type
named mychannel:
let val mychannel = channel()
The two primary operations (functions) on channels are for sending
(send) and receiving (recv) messages.
The type of the message is inferred from the send operation. The send
function that sends the integer value 7, can be written as
send(mychannel, 7)
The recv function names the channel as its parameter. Its return value
is the value it received.
In the Concurrent ML communications are synchronous. That means
the message is both sent and received only if both the sender and the
receiver are ready.
If a thread sends a message on a channel and no other thread is ready
to receive on that channel, the sender is blocked and waits for another
thread to execute a recv on the channel.
if a recv is executed on a channel by a thread but no other thread has
sent a message on that channel, the thread that ran the recv is
blocked and waits for a message on that channel.
F#:
F# support for concurrency is based on the same .NET classes that are
used by C#, specifically System.Threading.Thread. For example,
suppose we want to run the function myConMethod in its own thread.
The following function, when called, will create the thread and start the
execution of the function in the new thread:
STATEMENT-LEVEL CONCURRENCY
The statement level concurrency provides a mechanism that the
programmer can use to inform the compiler of ways it can map the program
onto a multiprocessor architecture.
High-Performance Fortran:
High-Performance Fortran (HPF) is a collection of extensions to Fortran
90 that are meant to allow programmers to specify information to the
compiler to help it optimize the execution of programs on
multiprocessor computers.
The primary specification statements of HPF are for specifying the
number of processors, the distribution of data over the memories of
those processors, and the alignment of data with other data in terms of
memory placement.
The HPF specification statements appear as special comments in a
Fortran program. Each of them is introduced by the prefix !HPF$,
where ! is the character used to begin lines of comments in Fortran 90.
This prefix makes them invisible to Fortran 90 compilers but easy for
HPF compilers to recognize.
The PROCESSORS specification has the following form:
!HPF$ PROCESSORS procs (n)
This statement is used to specify to the compiler the number of
processors that can be used by the code generated for this program.
The DISTRIBUTE statement specifies what data are to be distributed
and the kind of distribution that is to be used. Its form is as follows:
!HPF$ DISTRIBUTE (kind) ONTO
procs :: identifier_list
The FORALL statement specifies a sequence of assignment statements
that may be executed concurrently. For example,
FORALL (index = 1:1000)
list_1(index) = list_2(index)
END FORALL
C# 4.0 (and the other .NET languages) include two methods that
behave somewhat like FORALL. They are loop control statements in
which the iterations can be unrolled and the bodies executed
concurrently. These are Parallel.For and Parallel.ForEach.
EVENT HANDLING IN C#
In event handler, in C#, is a method that contains the code that gets
executed in response to a specific event that occurs in an application.
Event handlers are used in Graphical User interface. (GUI) applications
to handle events such as button clicks and menu selections, raised by
controls in the user interface.
The C# event model is based on a "publish- subscribe" pattern in
which a class (publisher) triggers an event, while another class
(subscriber) receives that event.
Delegate: An event is nothing but an encapsulated delegate. We can
declare the delegate as shown below: