UNIT 2.ppt

advertisement
MPI Java
• Basically designed for C/C++
•
(i) Fully featured
(ii) Object oriented API
(iii) Comprehensive test suite
(iv) Availability on almost all operating systems
The COMM class :
• MPI communicator
– A group of processes—the participants in some
kind of parallel task or subtask, and
– a communication context.
• A process group in MPI is defined as a fixed set
of processes
• Size()
• Rank
• void Send(Object buf, int offset, int count,
Datatype type, int dst, int tag) ;
• Status Recv(Object buf, int offset, int count,
Datatype type, int src, int tag) ;
• MPI.ANY_SOURCE
• MPI.ANY_TAG
• Int -- status.source holds the rank of the process
• Int -- status.tag holds the message tag specified by
the sender
• Int -- status.Get_count(type) returns number of
items
• int -- status.Get_elements(type) returns number of
basic elements received in the message.
• Int -- status.index is set by methods like
Request.Waitany()
Installing mpiJava :
• Install native MPI ed.
• Download the mpiJava sources from
http://www.hpjava.org/mpiJava.html
• Extract the archive, then go through the
conventional GNU-like installation process like
/configure, make, …
• Put the mpiJava/src/scripts/ directory on your
PATH
• put mpiJava/lib/classes/ directory on the
CLASSPATH.
• mpi.MPI.Init()
• mpi.MPI.Finalize()
import mpi. * ;
class first _ Java _ MPI{
public static void main (string s []){
MPI.Init (s) ;
Int myrank = MPI. COMM_WORLD. Rank () :
if (myrank = = 0){
char [] message = “Hello”.tochartrray () ;
MPI.COMM_WORLD. Send ( message, O, message. length, MPI.
CHAR,1,99); }
else{
char [] message = new char [2o] ;
MPI.COMM_WORLD.Recv (message, 0, 20, MPI.CHAR,0,99);
System.out.println( “ received” + new string (message) );
}
MPI.finalize ( )
}
}
•
•
•
•
•
MPI.COMM_WORLD.Bcast ()
MPI.COMM_WORLD.Send ()
MPI.COMM_WORLD.Recv ()
MPI.COMM_WORLD.Rank ()
MPI.COMM_WORLD.Size()
• A petrinet is a 5 tuple
•
PN =
(P, T, F, W, Mo.) where,
•
• A petrinet is a 5 tuple
•
PN
=
(P, T, F, W, Mo.) where,
•
P
=
{p1, p2, p3, …, pm} is a finite set of places.
•
T
=
{t1, t2, t3, …, tn} is a finite set of
transitions.
•
F

{P  T}  {T  P} is a set of arcs.
•
W
: F  N is weight function. Where, N is set of
natural numbers
•
Mo
:
P  I+ is the initial marking where I+ is
set of positive integers including zero.
•
P T =
 and P  T  
• A transition t is said to be enabled if each
input place p of t is marked with at least
w (p, t) tokens, where w(p, t) is weight of the
arc from p to t.
• An enabled transition may or may not fire.
• A firing of an enabled transition t1 remove.
w(p, t) tokens from each input place p of t and
adds w (t, p) tokens to each output place p of
t.
• A pair of p, t is called self loop if p is both input and
output place of t.
• A petrinet is called pure if there is non self loop.
• A petrinet is ordinary, if weight on each arc is 1.
• A petrinet is called marked graph, every place in the
petrinet has exactly one incoming and one outgoing
arc.
•
•
•
•
•
•
•
G
=
(V, , S, , N, , ) where
G’ =
(V, E, S, ) is a CFG
V
=
Set of none terminals
E
=
Set of terminals
S
=
Start symbol

=
Set of production rules.
N
=
(P, T, F, W) is a petrinet

:     {} is a transition labeling
function and  is a set of final markings.
Concurrent Java :
• A Runnable object can be provided.
• Subclass Thread can be used.
• The Runnable interface defines a single
method run. This method is meant to contain
the code executed in the thread. The object is
passed to the Thread constructor for
execution.
• public class Mythreadpgm implements Runnable
• {
•
public void run ( )
•
{
•

}
•
•
•
•
•
•
public static void main (string s [ ] )
{ 
(new Thread (new Mythread pgm ( )))
start ( );

}}
• public class Mythreadpgm extends Thread
• {
•
public void run ( )
{
•
•
•
•
•
•
•
•

}
public static void main (string s [ ] )
{ 
(new Mythread pgm ( )). start();

}
}
• subclass thread cannot be a subclass of any
other class.
sleep
•
•
•
•
•
•
•
•
•
•
public class Mysleeppgm
{
public static void main (string s[ ]) throws Interrupted
Execution
{
string messages [ ] ={ “msg1”, “msg2”, “msg3”, “msg4” };
for (int i = 0; i  messages.length; i + +)
{
Thread.sleep (5000);
System.out.println (messages [i]);
}} }
Thread Synchronization :
• Thread interference error
• Memory consistency error
• Join()
class multi extends Thread{
int n;
multi(int n)//constructor{
this.n=n;}
public void run(){
for(int i=0;i<5;i++)
System.out.println("running thread "+n+" ....."+i);
}
public static void main(String args[]){
multi[] t=new multi[5];
for(int i=0;i<5;i++){
t[i]=new multi(i);
t[i].start();
} } }
Communication and Synchronization
of Concurrent Tasks :
• Process
• Thread
Process’ address space
•
•
•
•
Instructions
Data present in the process
Stacks for function calls
Local variables
dependencies
•
•
•
•
A  B : A depends on B
A  B : B depends on A
A  B : Both depend on each other.
A NULL B : A and B one independent of
each other.
Communication Dependencies
• Intermediate task
• Posix queue
• Global variable
• FIFO
Co-operation Dependencies
Counting Task Dependencies
• Number of threads = n
• Threads involved in dependency = k
• Combinations of threads involved in
dependency = nCk
• total number of dependency types is given by
nC  2k.
k
• If n = 3 (A, B and C) and k = 2, then number of
combinations = 3C2 = 3.
• total number of possible
dependencies = 3C2  22 = 12.
Persistence of variables
•
•
•
•
•
•
Automatic
Static
Dynamic
File system
Kernel
Process
Synchronization
• Data
• Hardware
• task
Critical section
• entry point (synchronization starts here)
• {
• Body of critical section
• access file, variable or other resource
• }exit point (synchronization ends here)
• Mutual exclusion
• Progress
• Bounded wait
•
•
•
•
Exclusive Read and Exclusive Write (EREW)
Concurrent Read and Exclusive Write (CREW)
Exclusive Read and Concurrent Write (ERCW)
Concurrent Read and Concurrent Write
(CRCW)
Coordinating the Order of Execution
of Concurrent Tasks :
• semaphore
Process migration
• Locate a remote host
• Transfer the code image to remote host
• Initialize the remote operation.
• The state information of a process has 2 parts
– Computation state
– Communication state
Link Redirection and Message
Forwarding :
LISP
• progn
• (progn (* 2 6) (+ 5 6))
• Lambda expression
• (lambda (x) (* x 5))
Concurrent LISP
SB-THREAD package.
• sb-thread:thread
• sb-thread:current-thread
• sb-thread:make-thread function &key name
• (sb-thread:make-thread (lambda () (print
"Hello, World")))
• "Hello, World"
• #<SB-THREAD:THREAD FINISHED values:
"Hello, World" {1002CAF013}>
• sb-thread:thread-alive-p thread
• sb-thread:list-all-threads
• sb-thread:terminate-thread thread
• Implement a calculator (64 bit binary
multiplication) application using concurrent
lisp.
• Bignum
•
•
•
•
•
•
•
•
•
(defvar a) (defvar b) (defvar c) (defvar d)
(write-line " Enter two numbers : ")
(setf a(read)) (setf b(read))
(sb-thread:make-thread(lambda()(progn(sleep
0)
(setf c(+ a b))
(print "ADDITION : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep
0)
(setf c(- a b))
•
•
•
•
•
•
•
•
•
•
(print "SUBTRACTION : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 0)
(setf c(* a b))
(print "MULTIPLICATION : ")
(print c))))
(sb-thread:make-thread(lambda()(progn(sleep 0)
(setf c(/ a b))
(print "DIVISION - first/second : ")
(print c))))
•
•
•
•
•
•
* (load "calculator.lisp")
Enter two numbers :
18446744073709551616
18446744073709551616
"MULTIPLICATION : "
3402823669209384634633746074317682114
56
Multilisp
• Pcall
• (Pcall + ( a b) ( c d))
Concurrent yacc
•
•
•
•
Binpac is a compiler. It requires :
flex (fast lexical analyzer),
bison (a GNU parser generator)
cmake 2.6.3 or greater
• PAC (proxy auto-config) file
• FindProxyForURL(url, host)
• The input for the compiler is one or more .pac
files. These files are compiled to generate a .cc
and .h file.
• PAC grammar can be in the form of a record. The
data types or the primitives used are
• int8
• int16
• int32
• uint8
• uint16
• uint32
• Regular expression (As in case of YACC file)
• Bytestring.
• type myType = record
• {
• data:uint8;
• };
PAC header file
• class myType
• {
•
public:
•
myType();
•
~myType();
•
int Parse(const_byteptr beg_of_data,
const_byteptr end_of_data);
•
uint8 data() const { return data_; }
•
protected:
•
uint8 data_;
• };
• type HTTP_Message(expect_body: ExpectBody) =
record
• {
•
headers : HTTP_Headers;
•
body_or_not : case expect_body of
•
{
•
BODY_NOT_EXPECTED -> none: empty;
•
default -> body: HTTP_Body(expect_body);
•
};
• };
• # This will match for 10 element only
• type HTTP_Headers = HTTP_Header [10];
•
• # This will match until the condition is met
• type HTTP_Headers = HTTP_Header []
&until(/*Some condition*/);
• Array can also be used directly inside of
“record”.
• For example:
• type DNS_message = record
• {
•
header: DNS_header;
•
question: DNS_question(this)[header.qdcount];
•
answer: DNS_rr(this,
DNS_ANSWER)[header.ancount];
•
authority: DNS_rr(this,
DNS_AUTHORITY)[header.nscount];
•
additional: DNS_rr(this,
DNS_ADDITIONAL)[header.arcount];
• }&byteorder = bigendian, &exportsourcedata
• flow <Flow name>(<optional attribute>)
• {
•
<flowunit|datagram> = <top level data
unit> withcontext (<context constructor
parameter>);
• };
User defined functions
• PAC with embedded body :
• PAC with PAC-case body :
• Inlined by %code :
• function print_stuff(value
:const_bytestring):bool
• %{
• printf("Value [%s]\n", std_str(value).c_str());
• %}
• function RPC_Service(prog: uint32, vers:
uint32): EnumRPCService =
• case prog of {
•
default -> RPC_SERVICE_UNKNOWN;
• };
• %code{
• EnumRPCService RPC_Service(const RPC_Call*
call)
• {
•
return call ? call->service() :
RPC_SERVICE_UNKNOWN;
• }
• %}
Embedding into .pac File :
•
•
•
•
%header{...%}
%code{...%}
%member{...%}
%init{...%}
Download