+ All Categories
Home > Technology > Cnc scala-presentation

Cnc scala-presentation

Date post: 20-May-2015
Category:
Upload: skills-matter-talks
View: 748 times
Download: 4 times
Share this document with a friend
Popular Tags:
37
CnC-Scala: A Declarative Approach to Multicore Parallelism Scala Days April 17, 2012 Shams Imam and Vivek Sarkar Rice University 1
Transcript
Page 1: Cnc scala-presentation

CnC-Scala: A Declarative Approach to

Multicore Parallelism

Scala Days April 17, 2012

Shams Imam and Vivek Sarkar Rice University

1

Page 2: Cnc scala-presentation

Acknowledgments

•  Habanero Group –  Sagnak Tasirlar – Dragos Sbirlea –  Alina Sbirlea

•  “Concurrent Collections” Zoran Budimlic, Michael Burke, Vincent Cave, Kathleen Knobe, Geoff Lowney, Ryan Newton, Jens Palsberg, David Peixotto, Vivek Sarkar, Frank Schlimbach, Sagnak Tasirlar. Scientific Programming.

•  “Concurrent Collections: Easy and effective distributed computing” Frank Schlimbach, Kath Knobe, James Brodman.

2

Page 3: Cnc scala-presentation

Inverted Pyramid of Parallel Programming Skills

Parallelism oblivious developers

Parallelism aware devs

Concurrency Experts

Java threads, locks, etc.

Focus of Rice Habanero Project

Focus of this talk

Habanero-Scala,

presented earlier today

3 http://habanero.rice.edu

Page 4: Cnc scala-presentation

The Issue

Parallel programs are notoriously difficult for

mortals to write, debug, maintain and port.

4

Page 5: Cnc scala-presentation

The Big Idea

•  Don’t specify what operations run in parallel

•  Difficult and depends on target

•  Specify the semantic ordering constraints only •  Easier and depends only on application

5

Page 6: Cnc scala-presentation

•  Producer must execute before consumer

•  Controller must execute before controllee

Exactly Two Sources of Ordering Requirements

6

Page 7: Cnc scala-presentation

•  The Concurrent Collections (CnC) Model •  Features •  Constructs

•  CnC Scala – Example •  Implementation •  Results

7

Outline

Page 8: Cnc scala-presentation

The CnC model

•  Programmer defines semantic dependences •  Ordering constraints between computational units •  Code in computation unit is standard serial code

•  Runtime takes care of •  Executing the computational units •  Extracting parallelism in the application

8

Page 9: Cnc scala-presentation

CnC Constructs - Steps

•  Computational unit of a CnC program •  Stateless •  Side-effect free •  Functional with respect to inputs

(step)

9

Page 10: Cnc scala-presentation

CnC Constructs – Data Items

•  Data units are called Item Collection •  Means of communication between steps •  Mapping of data tags to items

•  Items can only be retrieved by their tags

•  Dynamic single assignment

[in_item] [out_item](step)

10

Page 11: Cnc scala-presentation

CnC Constructs - Control

•  Tag Collections are control units •  Used to execute (prescribe) steps •  A CnC step will not launch until its tag has been put

<tag>

[in_item] [out_item](step)

11

Page 12: Cnc scala-presentation

CnC Constructs - Environment

•  A driver which encapsulates the CnC Graph •  Provides inputs to and consumes outputs

from the CnC Graph

env

env env

<tag>

[in_item] [out_item](step)

12

Page 13: Cnc scala-presentation

Controller - Controllee Producer - Consumer

•  Producer must execute before consumer

•  Controller must execute before controllee

Exactly Two Sources of Ordering Requirements

(cons)[C1](prod) (C-ee)(C-er)

<tag1>

13

Page 14: Cnc scala-presentation

•  The CnC Model •  CnC-Scala

•  Build Model •  Capitalize-Words Example •  Step Code

•  Implementation •  Results

14

Outline

Page 15: Cnc scala-presentation

CnC-Scala

•  Written in pure Scala •  uses continuations compiler plugin

•  Only dependency – jsr-166y.jar •  comes bundled with java 7

•  Distribution with examples available at: •  http://cnc-scala.rice.edu/

15

Page 16: Cnc scala-presentation

CnC-Scala Build Model

CnC Graph Spec

Generated Stub code

- Step code - main() method

Compiled bytecode

cnc_scala_compile

cnc_scala_run

Output

cnc_scala_translate

User Code

Generated Code 16

Page 17: Cnc scala-presentation

Capitalize-Words example

17

1.  import edu.rice.cnc.translator.GraphDescription._2.  object SplitCapitalizeCncDefinition extends App {3.  // Item Collections4.  ItemCollection[String, String]("input")5.  ItemCollection[String, String]("words")6.  ItemCollection[String, String]("result")7.  // Tag Collections8.  TagCollection[String]("data")9.  TagCollection[String]("token")10.  // Step Prescriptions11.  Presription("data", List("split"))12.  Presription("token", List("capitalize"))13.  // Step Dependences14.  StepDependence("split", List ("input"), List("token", "words"))15.  StepDependence("capitalize", List("words"), List("result"))16.  // Environment Collections17.  EnvironmentDependence(List("input", "data"), List("result"))18.  }

Page 18: Cnc scala-presentation

Generated Code Example

18

1.  trait SplitStep extends Step {2.  3.  // Optional to provide an implementation4.  def createDependences(5.  tag: java.lang.String, 6.  inInput: InputCollection[java.lang.String, java.lang.String],7.  dependenceManager: DependenceManager8.  ): Unit = {}9.  10.  // User must provide an implementation for this method11.  def compute(12.  tag: java.lang.String, 13.  inInput: InputCollection[java.lang.String, java.lang.String], 14.  outToken: TagCollection[java.lang.String], 15.  outWords: OutputCollection[java.lang.String, java.lang.String]16.  ): Unit@cpsParam[Any, Any]17.  }

Page 19: Cnc scala-presentation

User Step Code

19

1.  class UserSplitStep extends SplitStep {2.  3.  // User must provide an implementation for this method4.  def compute(5.  tag: java.lang.String, 6.  inInput: InputCollection[java.lang.String, java.lang.String], 7.  outToken: TagCollection[java.lang.String], 8.  outWords: OutputCollection[java.lang.String, java.lang.String]9.  ): Unit@cpsParam[Any, Any] = {

10.  val inString = inInput.get(tag)11.  for ((token, index) <- inString.split(" +").view.zipWithIndex) {12.  val newTag = tag + ":" + index13.  outWords.put(newTag, token)14.  outToken.put(newTag)15.  } } }

Page 20: Cnc scala-presentation

User Main

20

1.  object SplitCapitalizeMain extends CncScalaApp {

2.  // Instantiate Steps3.  val splitStep = new UserSplitStep()4.  val capitalizeStep = new UserCapitalizeStep()5.  val graph = new SplitCapitalizeGraph(splitStep, capitalizeStep)6.  7.  graph.run(new Runnable() {8.  def run(): Unit = {9.  // populate data from environment to collections10.  val tag = "1"11.  graph.input.put(tag, "hello world")12.  graph.data.put(tag)13.  }14.  })15.  // read results16.  graph.result.dump(System.out)17.  }

Page 21: Cnc scala-presentation

•  The CnC Model •  CnC-Scala •  Implementation

•  Data-driven futures •  Continuations

•  Results

21

Outline

Page 22: Cnc scala-presentation

Implementation

•  Tag Collections •  Spawn new tasks run step code

•  Item Collections •  Use concurrent maps with user defined tag types as keys

and data-driven futures (DDF) as values •  What to do when get() has unavailable items?

•  Create continuations

•  When value becomes available… •  DDFs resume continuations

22

Page 23: Cnc scala-presentation

Data-driven futures

•  Separation of classical “futures” into data and control parts

•  Consumers can eagerly register on the DDF even before we know about the producer •  i.e., lazily attaches a producer to an item

•  When item is produced, all previously registered consumers are notified •  Subsequent consumers always get the value

immediately

23

Page 24: Cnc scala-presentation

Continuations

•  Represents rest of the computation from a given point in the program

•  Allows •  suspending current execution state •  resume from that point later

•  We need only delimited one-shot continuations •  Scala has shift-reset!

24

Page 25: Cnc scala-presentation

Benefits of using continuations

•  No re-execution of code •  Allow arbitrary (data-dependent) gets •  Threads never block

•  No extra threads created

25

Page 26: Cnc scala-presentation

Scala Continuation example

1.  object Main {2.  def main(args: Array[String]) {3.  println("A. Main starts")4.  var continuation: (Unit) => Any = null5.  reset { // delimit boundary start6.  println("B. Entered reset")7.  shift[Unit, Any, Unit] {8.  delimCont =>9.  println("C. Entered shift")10.  continuation = delimCont11.  println("D. Inside shift")12.  } 13.  println("E. After shift")14.  } // delimit boundary end15.  println("F. Outside reset")16.  println("G. Calling cont.")17.  continuation()18.  println("H. Main ends")19.  }20.  }

Output:A. Main startsB. Entered resetC. Entered shiftD. Inside shiftF. Outside resetG. Calling cont.E. After shiftH. Main ends

26

Page 27: Cnc scala-presentation

CnC-Scala Runtime - step

•  Tag Collection step prescription •  Wrap each step.compute() in a reset

// some preparation reset { step.compute(tag, inputs, outputs) } // book-keeping logic based on // whether continuation was stored // or execution completed normally

27

Page 28: Cnc scala-presentation

CnC-Scala Runtime - get

•  If item is available return it •  Else store continuation get(tag: TagType): ItemType = { if (itemAvailable) return item else shift { continuation => // store continuation } // when cont resumes, item is available return item }

28

Page 29: Cnc scala-presentation

CnC-Scala Runtime - put

•  Store item •  Resume waiting continuations put(tag: TagType, item: ItemType) { // store item into DDF // resume ALL waiting continuations }

29

Page 30: Cnc scala-presentation

•  The CnC Model •  CnC-Scala •  Implementation •  Results

Outline

30

Page 31: Cnc scala-presentation

CnC-Scala Results

•  12-core (two hex-cores) 2.8 GHz Intel Westmere SMP

•  48 GB memory, running Red Hat Linux (RHEL 6.0) •  Hotspot JDK 1.7 •  Scala version 2.9.1-1 •  CnC-Scala 0.1.2 •  Arithmetic mean of last thirty iterations from

hundred iterations on ten separate JVM invocations

31

Page 32: Cnc scala-presentation

Successive Over-Relaxation

•  1 Item Collection, 1 Tag Collection, and 1 Step. 32

Page 33: Cnc scala-presentation

NQueens

•  3 Item Collections, 1 Tag Collection, and 1 Step. 33

Page 34: Cnc scala-presentation

LU Decomposition

•  3 Item Collections, 5 Tag Collections, and 8 Steps. 34

Page 35: Cnc scala-presentation

CnC Features

•  Coordination language •  Dynamic light-weight task based •  Single assignment •  Deterministic •  Race free

35

Page 36: Cnc scala-presentation

Summary

•  CnC exposes more potential parallelism •  Development is productive

•  User declaratively defines dependences and writes only serial code

•  runtime hides all the difficulties with using low-level techniques

•  result is deterministic and independent of number of threads

36

Page 37: Cnc scala-presentation

Thank you!

[image source: http://www.jerryzeinfeld.com/tag/question-of-the-day/] 37


Recommended