CA341 - Comparative Programming Languages Memory Models
Total Page:16
File Type:pdf, Size:1020Kb
Memory Models CA341 - Comparative Programming Languages Memory Models David Sinclair Memory Models Memory Models There are 3 common memory models: RAM Random Access Memory is the most common memory model. In programming language terms, this means that variables are represented by an identifier that maps to the physical address of the variable in RAM. Stack All variables are held in the stack. Operators take their operands from the stack and push their result back onto the stack. For example, the ADD operator takes the top 2 items from the stack, adds them together and pushes the result into the stack. This model is used in the JVM. Memory Models Memory Models (2) Associative This is also known as Content Addressable Memory. Typically data is stored in tuples (an ordered set of data items) and memory is “addressed” by the contents of the tuple. This form of memory is used in specific environments where memory is either outside the processor’s address space or where the data is constantly being moved for optimisation purposes. Classic examples of this are databases and distributed processing. Associative Memory is also used in caches, artificial neural networks and data compression hardware. There are standards and hardware implementations of Content Addressable Memory. Memory Models SQL An example of Associative Memory in Databases and the implications in terms of programming languages is SQL. In SQL, data is stored as tuples (rows) in relations (tables). For example, consider 2 relations, Student and Apply. The tuples in the Student relation contain the fields sName and sID. The tuples in the Apply relation contain the fields sID, degree and university. An SQL query to find the ID and name of the students who have applied for Computer Science (‘CS’) but not Electronic Engineering (‘EE’) is: select sID , sName from Student where sID in ( select sID from Apply where degree=’CS’) and sID not in ( select sID from Apply where degree=’EE’ ); Memory Models SQL (2) The 2 subqueries retrieve the contents of the sID field based on the contents of the degree field in the Apply relation. The main query retrieves the sID and sName fields from the Student relation based on a subset of sID values from the Apply relation. By using Associative Memory, this allows the database engine to optimise the execution of a query by moving where data is stored. The order of the tuples in a relation, and the order of fields in the tuples can be modified at runtime without requiring the query to be changed. Memory Models Tuple Spaces A tuple space is a logically shared, associatively addressed memory space into which tuples are placed and removed from. A tuple is simply an ordered collection of data elements. Tuple Spaces have been implemented in many programming languages. One of the most common implementation is Javaspaces. JavaSpaces is derived from the Linda system developed by David Gelernter. Linda is a machine independent concurrent programming concept. It, in itself, is not a language but an extension to existing sequential languages, which provides for concurrent processing. JavaSpaces extends the main ideas of Linda to the world of Java. The tuple space is replaced by a JavaSpace and the tuples are replaced by Java objects in the form of Entrys. One of the advantages of this is that the JavaSpace is aware of Java?s strong type system while retaining the associative addressing properties of Linda. Memory Models JavaSpaces Every object stored in a JavaSpace must implement the Entry interface and is a strongly typed collection of public, nonstatic, nonfinal, nontransient objects. An Entry is searched for using the same techniques and matching rules as used in the Jini lookup service. JavaSpaces is distributed with Jini development kit and is implemented by a fully-fledged service called outrigger. Since JavaSpaces uses transactions so we also have to run the transaction service mahalo. Memory Models The Javaspace Interface The programming model for JavaSpaces follows a passive, type-aware version of the Linda programming model. package net. jini .space; public interface JavaSpace { public final long NO WAIT = 0 ; Lease write (Entry e, Transaction txn, long lease) throws RemoteException , TransactionException ; Entry read (Entry tmpl, Transaction txn, long timeout) throws RemoteException , TransactionException , UnusableEntryException , InterruptedException ; Entry readIfExists (Entry tmpl, Transaction txn, long timeout) throws RemoteException , TransactionException , UnusableEntryException , InterruptedException ; Entry take (Entry tmpl, Transaction txn, long timeout) throws RemoteException , TransactionException , UnusableEntryException , InterruptedException ; Memory Models The Javaspace Interface (2) The programming model for JavaSpaces follows a passive, type-aware version of the Linda programming model. Entry takeIfExists (Entry tmpl, Transaction txn, long timeout) throws RemoteException , TransactionException , UnusableEntryException , InterruptedException ; EventRegistration notify (Entry tmpl, Transaction txn, RemoteEventListener l , long lease , MarshalledObject obj) throws RemoteException , TransactionException ; Entry snapshot (Entry e) throws RemoteException; } The write() method adds an Entry to the JavaSpace. It takes a parameter that specifies which Transaction the write() method is part of (this can be null if you do not wish the write() method to be part of any operation). The final argument specifies the requested lease duration (in milliseconds). Memory Models The Javaspace Interface (3) The read() method is used to read an Entry from the JavaSpace. The read() method takes a template as an argument and returns an Entry in the JavaSpace that matches the template. If there is more than one Entry in the JavaSpace that matches the template then the JavaSpace is free to pick any matching Entry. If there is no Entry in the JavaSpace that matches the template then the read() method will wait for timeout milliseconds before returning. The readIfExists() method is like the read() method except that it will block for timeout milliseconds if the only possible matching Entry is involved in a transaction, otherwise it returns immediately. The read() and readIfExists() methods can also be part of a transaction. Memory Models The Javaspace Interface (4) The take() and takeIfExists() methods are similar to the read() and readIfExists() methods except that they remove the returned Entry from the JavaSpace. The notify() method is used to register an interest in an Entry that matches a specified template being entered into the JavaSpace. This method takes a template, a transaction, a RemoteEventListener, a requested lease duration and a MarshalledObject as arguments and returns an EventRegistration. If an Entry that matched the template is added to the JavaSpace while the lease is active then the notify() method in the RemoteEventListener is invoked. Memory Models The Javaspace Interface (5) The snapshot() method provides an optimisation to improve the performance of JavaSpaces. Every time you pass an Entry as an argument of a JavaSpace method the Entry needs to be serialised. This can be a time consuming process. The snapshot() method takes an Entry and returns an Entry that can be used on future calls to the same JavaSpace. The advantage is that the returned Entry no longer needs to be serialised. This can result is a huge performance boost. Memory Models JavaSpace Diagram (c) Sun MicroSystems Memory Models JavaSpaces Example import net. jini .core.entry. ∗ ; public class Message implements Entry { public String content; public Message () { } public Message(String content) { this .content = content; } public String toString() { return (”Message is : ” + content); } } Memory Models JavaSpaces Example (2) import net. jini .core. discovery.LookupLocator; import net. jini .core.lookup . ∗ ; import net. jini .core.entry.Entry; import net. jini .space.JavaSpace; import net. jini .lookup.entry . ∗ ; public class HelloWorld { public static void main(String [] args) { t r y { System.out. println(”Create Message entry”); Message msg = new Message(”Hello World”); Class [] types = new Class [] { JavaSpace. class } ; ServiceTemplate template = new ServiceTemplate ( null , types , null ); JavaSpace space = (JavaSpace) registrar.lookup (template ); Memory Models JavaSpaces Example (3) space. write(msg, null , Lease .FOREVER) ; Message template = new Message (); Message result = (Message) space.read(template , null , Long . MAX VALUE ) ; System.out. println(result ); } catch (Exception e) { e.printStackTrace (); } System. exit (0); } } Memory Models Linda The original Linda programming model has operations equivalent to the JavaSpaces methods. JavaSpaces Linda write() out() read() rd() readIfExists() rdp() take() in() takeIfExists() inp() But Linda also has the concept of active tuples which are placed into a tuple space using the eval() operation. The eval() operation creates a thread in the tuple space that resolves the active tuple into a normal tuple. eval (find name(), calc score(), "CA341") Linda can be added to many languages; for example C-Linda is Linda implemented in a C language environment. Memory Models Linda (2).