An Introduction to the Kent C++CSP Library. Authors Neil Brown Peter Welch

Similar documents
An Introduction to the Kent C++CSP Library

Design Of JCSP Language Classes

Integrating and Extending JCSP

ProXC - A CSP-Inspired. Concurrency Library for the C. Programming Language

COM2007 Week 9 Java Concurrency and Alting

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Concurrent Object Oriented Languages

Plan for rest of course

Communicating Haskell Processes: Composable Explicit Concurrency Using Monads

Towards Millions of Processes on the JVM

Shared Channels etc.

The JVMCSP Runtime and Code Generator for ProcessJ in Java

Native JCSP the CSP for Java Library with a Low-Overhead CSP Kernel

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Any of the descriptors in the set {1, 4} have an exception condition pending

CSP.NET. A Software library for concurrent and distributed programming

Automatic Translation from Circus to Java

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Introduction to Programming Using Java (98-388)

Process Oriented Design for Java - Concurrency for All

CS 162, Lecture 25: Exam II Review. 30 May 2018

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Input from Files. Buffered Reader

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Alting barriers: synchronisation with choice in Java using JCSP

A Deterministic Concurrent Language for Embedded Systems

Communicating Haskell Processes tutorial

VALLIAMMAI ENGINEERING COLLEGE

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

PROCEEDINGS OF THE 5TH PROGRESS SYMPOSIUM ON EMBEDDED SYSTEMS

SpiNNaker Application Programming Interface (API)

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

C++\CLI. Jim Fawcett CSE687-OnLine Object Oriented Design Summer 2017

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CS365 Midterm -- Spring 2019

Communicating Mobile Processes

Self-test Java Programming

Conditional Communication in the Presence of Priority

Performance Throughput Utilization of system resources

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

The Java Programming Language

Object Oriented Programming

1 Shyam sir JAVA Notes

Structured bindings with polymorphic lambas

Exercise 6.2 A generic container class

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CO Java SE 8: Fundamentals

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Selected Java Topics

C++ (Non for C Programmer) (BT307) 40 Hours

A Deterministic Concurrent Language for Embedded Systems

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

This is a talk given by me to the Northwest C++ Users Group on May 19, 2010.

Lecture 3. Lecture

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Sri Vidya College of Engineering & Technology Question Bank

Advanced Systems Programming

AIMS Embedded Systems Programming MT 2017

Object-Oriented Design. March 2005 Object Oriented Design 1

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

C Pointers. 6th April 2017 Giulio Picierro

Introducing C++ to Java Programmers

G52CON: Concepts of Concurrency

The Java Collections Framework. Chapters 7.5

C and C++ 7. Exceptions Templates. Alan Mycroft

ECE 2400 Computer Systems Programming, Fall 2017 Prelim 2 Prep

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Choice and Non-Determinism

Lecture 15a Persistent Memory & Shared Pointers

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Vector and Free Store (Pointers and Memory Allocation)

Short Notes of CS201

Introduction to Java

main read Identical static threads Dynamic threads with Concurrent and

CS201 - Introduction to Programming Glossary By

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Midterm Review. PIC 10B Spring 2018

LUNA: Hard Real-Time, Multi-Threaded, CSP-Capable Execution Framework M. M. Bezemer R. J. W. Wilterdink J. F. Broenink

User Space Multithreading. Computer Science, University of Warwick

Concurrency. Johan Montelius KTH

Lecture 1: Overview of Java

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

What did we talk about last time? Course overview Policies Schedule

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

C++ - Lesson 2 This is a function prototype. a' is a function that takes an integer array argument and returns an integer pointer.

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

05-01 Discussion Notes

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Sampling and Timing: A Task for the Environmental Process

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }

CSP Networking for Java (JCSP.net)

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Multiple Inheritance, Abstract Classes, Interfaces

Transcription:

An Introduction to the Kent C++CSP Library Authors Neil Brown (neil@twistedsquare.com) Peter Welch (P.H.Welch@kent.ac.uk)

C++ Overview Object-oriented but not pure objectoriented like Smalltalk C++ provides elegant solutions to complex problems C with classes bolted on Almost as fast as C, but with better structure

CSP (Quick!) Overview Processes, channels, concurrency etc. Very scaleable without becoming overcomplex Easy to visualise and understand

CSP (Quick!) Overview Processes, channels, concurrency etc. Very scaleable without becoming overcomplex Easy to visualise and understand δ???

Library Overview A process is a subclass of CSProcess, implementing the run method Channels are templated, so they can communicate any type Communication is done using channel end objects, not the channel itself

Our First Processes Writer Reader

Our First Processes class Writer : public CSProcess { private: Chanout<int> out; protected: void run(); public: Writer( const Chanout<int>& o ); ; Chanout<int> is the writing end of a channel of integers

Our First Processes class Reader : public CSProcess { private: Chanin<int> in; protected: void run(); public: Reader( const Chanin<int>& i ); ; Chanin<int> is the reading end of a channel of integers

Our First Processes void Writer::run() { int i; for (i = 0;i < 100;i++) { out << i; void Reader::run() { int i,n; for (i = 0;i < 100;i++) { in >> n; Input and output are done with simple operators, as in occam

Our First Processes

Our First Processes void function() { One2OneChannel<int> channel; Where appropriate, JCSP s API is copied Parallel( new Writer(channel.writer()), new Reader(channel.reader()), NULL); void main() { Start_CSP(); function(); End_CSP(); Use writer/reader calls to get channel ends Parallel takes a NULLterminated list of process pointers The Start_CSP/End_CSP functions must be called before/after the library is used

Our First Processes void Writer::run() { int i; for (i = 0;i < 100;i++) { out << i; void Reader::run() { int i,n; for (i = 0;i < 100;i++) { in >> n; The for loop is inflexible - maybe there is another way?.

Poisoning

Poisoning Writer Reader

Poisoning Writer Reader

Poisoning void Writer::run() { int i; for (i = 0;i < 100;i++) { out << i; void Writer::run() { int i; for (i = 0;i < 100;i++) { out << i; out.poison(); This channel will now be poisoned (forever - no antidote available!). Any attempts to use the channel will cause a PoisonException to be thrown. Except: poisoning an already-poisoned channel has no effect (no exception is thrown).

Poisoning void Reader::run() { int i,n; for (i = 0;i < 100;i++) { in >> n; void Reader::run() { try { int n; while (true) { in >> n; catch (PoisonException e) { When the channel The forhas loop been can poisoned, now become the next a input will while (true) cause loop a PoisonException to be thrown, and it will be caught here

Shared Channels

Shared Channels Writer Reader

Shared Channels Reader Writer Reader

Shared Channels One2OneChannel<int> channel; Parallel( new Writer(channel.writer()), new Reader(channel.reader()), NULL); One2AnyChannel<int> channel; Change the channel type and add an extra reader - simple as that! Parallel( new Writer(channel.writer()), new Reader(channel.reader()), new Reader(channel.reader()), NULL);

Buffered Channels

Buffered Channels Writer Reader

Buffered Channels Writer Reader

Buffered Channels One2OneChannel<int> channel; Parallel( new Writer(channel.writer()), new Reader(channel.reader()), NULL); One2OneChannelX<int> channel( Buffer<int>(4) ); Parallel( new Writer(channel.writer()), new Reader(channel.reader()), NULL); Note the added X! Pass in the buffer object you want to use for buffering - like JCSP, C++CSP takes a copy of it rather than use the original

Parallel Communications void Delta::run() { int n; ParallelComm pc( outa.parout(&n), outb.parout(&n), NULL); while (true) { in >> n; pc.communicate(); Like other library calls, the ParallelComm constructor takes This communicate a Chanin NULL-terminated and Chanout call list performs have of ParCommItems parin all the and parout communications calls respectively associated to get with the ParCommItems objects. passed to the They constructor take the of the destination/source ParallelComm of the communication respectively as an argument

Alternatives (ALTing) void Alter::run() { /*declarations*/ int n; Time t = Seconds(1); Alternative alt( ina.inputguard(&n), inb.inputguard(&n), new RelTimeoutGuard(t), NULL); /*.. main body..*/ The inputguard Simple platform method of Chanin independent returns functions a Guard object and to classes be used for with dealing the Alternative with time class inputguard takes the destination of the input as an argument RelTimeoutGuard is a library class to provide a timeout guard relative to the start of the ALT. An absolute timeout guard is also provided

Alternatives (ALTing) void Alter::run() { /*declarations*/ int n; Time t = Seconds(1); Alternative alt( ina.inputguard(&n), inb.inputguard(&n), new RelTimeoutGuard(t), NULL); /*.. main body..*/

Alternatives (ALTing) void Alter::run() { /*.. declarations..*/ /*main body*/ while (true) { switch (alt.priselect()){ case 0: /*input on A*/ case 1: /*input on B*/ case 2: /*timeout*/ The actions associated with an ALT (i.e. inputs) take place during the priselect method, and are not done separately afterwards like in JCSP So by the time this line gets executed, the input will have been successfully completed

Extra Features Extended rendezvous available for both normal input operations and input guards in ALTs Templated smart pointer class Mobile<T> that has the same semantics as KRoC s MOBILEs KRoC Interoperability using KRoC s UDC mechanism -- both the KRoC and C++CSP program use their native channels

Technical Details The library uses cooperatively multitasked threading (in a single operating-system thread), similar to KRoC The scheduler kernel is highly portable - the library currently functions under Windows and most Unixes Unfortunately, processes must specify the size of the stack they want

CommsTime 0 δ + Recorder

CommsTime CSP Framework Time per iteration

CommsTime CSP Framework Time per iteration occam (KRoC 1.3.3) 1.3 microseconds

CommsTime CSP Framework Time per iteration occam (KRoC 1.3.3) 1.3 microseconds C++CSP 5 microseconds

CommsTime CSP Framework Time per iteration occam (KRoC 1.3.3) 1.3 microseconds C++CSP 5 microseconds JCSP (JDK 1.4) 230 microseconds

Stressed ALTer Stressed ALT

Stressed ALT CSP Framework 10 writers * 10 channels 100 writers * 20 channels 200 writers * 100 channels

Stressed ALT CSP Framework 10 writers * 10 channels 100 writers * 20 channels 200 writers * 100 channels occam (KRoC 1.3.3) 0.6 0.7 1 (All times are in microseconds)

Stressed ALT CSP Framework 10 writers * 10 channels 100 writers * 20 channels 200 writers * 100 channels occam (KRoC 1.3.3) 0.6 0.7 1 C++CSP 3 7 10 (All times are in microseconds)

Stressed ALT CSP Framework 10 writers * 10 channels 100 writers * 20 channels 200 writers * 100 channels occam (KRoC 1.3.3) 0.6 0.7 1 C++CSP 3 7 10 JCSP 130 200 - (JDK 1.4) (All times are in microseconds)

An Equivalence One2OneChannel<int> channel; Parallel( new Writer(channel.writer()), new Reader(channel.reader()), NULL); Equivalent One2OneChannel<int> channel; Barrier barrier(1); spawnprocess(new Writer(channel.writer()),&barrier); spawnprocess(new Reader(channel.reader()),&barrier); barrier.sync();

Going Multi-Threaded One2OneChannel<int> channel; Barrier barrier(1); spawnprocess(new Writer(channel.writer()),&barrier); spawnprocess(new Reader(channel.reader()),&barrier); barrier.sync(); InterThreadChannel<int> channel; InterThreadBarrier barrier(1); spawnasnewthread(new Writer(channel.writer()),&barrier); spawnasnewthread(new Reader(channel.reader()),&barrier); barrier.sync();

Conclusion C++CSP is a new but stable library in the mould of JCSP It has many new useful features, such as stateful poisoning and templated channels It is very portable, and offers both single-threaded and multi-threaded concurrency