23
Scala LASER Summer School 2012 Martin Odersky EPFL and Typesafe

Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Scala

LASER Summer School 2012

Martin Odersky

EPFL and Typesafe

Page 2: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

The Challenge

The world of mainstream software is changing:

– Moore’s law now achieved

by increasing # of cores

not clock cycles

– Huge volume workloads

that require horizontal

scaling

– “PPP” Grand Challenge

2

Data from Kunle Olukotun, Lance Hammond, Herb

Sutter, Burton Smith, Chris Batten, and Krste

Asanovic

Page 3: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Concurrency and Parallelism

Parallel programming Execute programs faster on

parallel hardware.

Concurrent programming Manage concurrent execution

threads explicitly.

Both are too hard!

3

Page 4: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

The Root of The Problem

• Non-determinism caused by

concurrent threads accessing

shared mutable state.

• It helps to encapsulate state in actors

or transactions, but the fundamental

problem stays the same.

• So,

non-determinism = parallel processing + mutable state

• To get deterministic processing, avoid the mutable state!

• Avoiding mutable state means programming functionally.

4

var x = 0

async { x = x + 1 }

async { x = x * 2 }

// can give 0, 1, 2

Page 5: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Space vs Time

5

Time (imperative/concurrent)

Space (functional/parallel)

Page 6: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

6

Scala is a Unifier

Agile, with lightweight syntax

Object-Oriented Scala Functional

Safe and performant, with strong static tpying

Page 7: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

7

Scala is a Unifier

Agile, with lightweight syntax

Parallel

Object-Oriented Scala Functional

Sequential

Safe and performant, with strong static tpying

Page 8: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Some Commercial Users

Page 9: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

9

Some adoption vectors:

• Web platforms

• Trading platforms

• Networking

• Simulation

Fast to first product, scalable

afterwards

Page 10: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Scala’s Toolbox

10

Page 11: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

11

Different Tools for Different Purposes

Parallelism:

Parallel Collections

Collections

Distributed Collections

Parallel DSLs

Concurrency:

Actors

Software transactional memory Akka

Futures

Page 12: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

12

Let’s see an example:

Page 13: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

13

A class ... public class Person {

public final String name;

public final int age;

Person(String name, int age) {

this.name = name;

this.age = age;

}

}

class Person(val name: String, val age: Int)

... in Java:

... in Scala:

Page 14: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

14

... and its usage

import java.util.ArrayList;

...

Person[] people;

Person[] minors;

Person[] adults;

{ ArrayList<Person> minorsList = new ArrayList<Person>();

ArrayList<Person> adultsList = new ArrayList<Person>();

for (int i = 0; i < people.length; i++)

(people[i].age < 18 ? minorsList : adultsList)

.add(people[i]);

minors = minorsList.toArray(people);

adults = adultsList.toArray(people);

}

... in Java:

... in Scala: val people: Array[Person] val (minors, adults) = people partition (_.age < 18)

A simple pattern match

An infix method call

A function value

Page 15: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

15

Going Parallel

?

... in Java:

... in Scala: val people: Array[Person] val (minors, adults) = people.par partition (_.age < 18)

Page 16: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Parallel Collections

• Split work by number of Processors

• Each Thread has a work queue that is split

exponentially. Largest on end of queue

• Granularity balance against scheduling overhead

• On completion threads “work steals” from end of other

thread queues

16

Page 17: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Going Distributed

• Can we get the power of parallel collections to work on

10’000s of computers?

• Hot technologies: MapReduce (Google’s and Hadoop)

• But not everything is easy to fit into that mold

• Sometimes 100’s of map-reduce steps are needed.

• Distributed collections retain most operations, provide a

powerful frontend for MapReduce computations.

• Scala’s uniform collection model is designed to also

accommodate parallel and distributed.

17

Page 18: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

18

The Future

Scala’s persistent collections are

• easy to use:

• concise:

• safe:

• fast:

• scalable:

We see them play a rapidly increasing role in software

development.

few steps to do the job

one word replaces a whole loop

type checker is really good at catching errors

collection ops are tuned, can be parallelized

one vocabulary to work on all kinds of

collections: sequential, parallel, or distributed.

Page 19: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Going further: Parallel DSLs

But how do we keep a bunch of Fermi’s happy?

– How to find and deal with 10000+ threads in an

application?

– Parallel collections and actors are necessary but not

sufficient for this.

Our bet for the mid term future: parallel embedded DSLs.

– Find parallelism in domains: physics simulation, machine

learning, statistics, ...

Joint work with Kunle Olukuton, Pat Hanrahan @ Stanford.

EPFL side funded by ERC.

19

Page 20: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

EPFL / Stanford Research

Domain Embedding Language (Scala)

Virtual

Worlds

Personal

Robotics

Data

informatics

Scientific

Engineering

Physics

(Liszt) Scripting

Probabilistic

(RandomT)

Machine Learning (OptiML)

Rendering

Parallel Runtime (Delite, Sequoia, GRAMPS)

Dynamic Domain Spec. Opt. Locality Aware Scheduling

Staging Polymorphic Embedding

Applications

Domain

Specific

Languages

Heterogeneous

Hardware

DSL

Infrastructure

Task & Data Parallelism

Hardware Architecture

OOO Cores SIMD Cores Threaded Cores Specialized Cores

Static Domain Specific Opt.

Programmable

Hierarchies

Scalable

Coherence

Isolation &

Atomicity

On-chip

Networks

Pervasive

Monitoring

20

Page 21: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Fuel injection

Transition Thermal

Turbulence

Turbulence

Combustion

Example: Liszt - A DSL for Physics

Simulation

• Mesh-based

• Numeric Simulation

• Huge domains

– millions of cells

• Example: Unstructured Reynolds-averaged Navier

Stokes (RANS) solver

21

Page 22: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

Liszt as Virtualized Scala

val // calculating scalar convection (Liszt)

val Flux = new Field[Cell,Float]

val Phi = new Field[Cell,Float]

val cell_volume = new Field[Cell,Float]

val deltat = .001

...

untilconverged {

for(f <- interior_faces) {

val flux = calc_flux(f)

Flux(inside(f)) -= flux

Flux(outside(f)) += flux

}

for(f <- inlet_faces) {

Flux(outside(f)) += calc_boundary_flux(f)

}

for(c <- cells(mesh)) {

Phi(c) += deltat * Flux(c) /cell_volume(c)

}

for(f <- faces(mesh))

Flux(f) = 0.f

}

AST

Hardware

DSL Library

Optimisers Generators

Schedulers

GPU, Multi-Core, etc

22

Page 23: Scalalaser.inf.ethz.ch/2012/slides/Odersky/laser-intro.pdf · 2012-09-03 · • Distributed collections retain most operations, provide a powerful frontend for MapReduce computations

scala-lang.org

akka.io

23

To find out more

typesafe.com

playframework.org