40
Parallel Programming Exercise Session 3 10.03.2014 – 14.03.2014

Parallel Programming Exercise Session 3

Embed Size (px)

Citation preview

Page 1: Parallel Programming Exercise Session 3

Parallel Programming Exercise Session 3 10.03.2014 – 14.03.2014

Page 2: Parallel Programming Exercise Session 3

Lab Overview

Feedback from the last exercise

Discussion of exercise 3

Long Multiplication

Sieve of Eratosthenes

Java Tutorial

13.03.2014 Parallel Programming – SS 2014 2

Page 3: Parallel Programming Exercise Session 3

Feedback: Exercise 2

• Pipelining

• Amdahl’s Law

• Task Graphs

• Identify potential parallelism

13.03.2014 Parallel Programming – SS 2014 3

Page 4: Parallel Programming Exercise Session 3

Lab Overview

Feedback from the last exercise

Discussion of exercise 3

Long Multiplication

Sieve of Eratosthenes

Java Tutorial

13.03.2014 Parallel Programming – SS 2014 4

Page 5: Parallel Programming Exercise Session 3

Exercise 3: Goals

What is an algorithm?

• Formal description Input, output, operations

• Correctness and exceptions Does it work? If not, in which cases?

• Complexity analysis Is it asymptotically better than X? If so

when?

What is a program?

• Familiarize yourself with key

concepts and practices of Java

• Syntax How do I write the instructions down?

• Topics: types, variables, control

flow, loops and arrays

13.03.2014 Parallel Programming – SS 2014 5

Page 6: Parallel Programming Exercise Session 3

Lab Overview

Feedback from the last exercise

Discussion of exercise 3

Long Multiplication

Sieve of Eratosthenes

Java Tutorial

13.03.2014 Parallel Programming – SS 2014 6

Page 7: Parallel Programming Exercise Session 3

Multiplication

13.03.2014 Parallel Programming – SS 2014 7

Input: 𝑥, 𝑦 ∈ ℕ>0

Output: 𝑥 × 𝑦 (multiplication)

Approaches:

• Russian Peasant method (Lecture last week)

• Long multiplication (Primary school)

• Karatsuba multiplication (Data Structures and Algorithms course)

• …

Page 8: Parallel Programming Exercise Session 3

Long Multiplication

13.03.2014 Parallel Programming – SS 2014 8

Input: 𝑥, 𝑦 ∈ ℕ>0

Output: 𝑥 × 𝑦 (multiplication)

Example:

Page 9: Parallel Programming Exercise Session 3

Long Multiplication: Implementation

• We provide a skeleton class and (failing) unit tests

• Solution should support arbitrary precision

Helper routines to convert strings to their digit representation and back

• Use of the multiplication operator (*) is allowed

but only for multiplication by constants or single digit values

13.03.2014 Parallel Programming – SS 2014 9

Page 10: Parallel Programming Exercise Session 3

Russian Peasant Method: Efficiency of the Algorithm

Question: How long does it take to multiply a and b?

Efficiency metric: Total number of basic operations • Multiplication, division, additions, test for even/odd, tests equal to “1” • Per call to 𝑓(𝑎, 𝑏) not more than 5 basic operations

13.03.2014 10

static int f(int a, int b){

if (b == 1) return a;

if (b%2 == 0) return f(a+a, b/2);

else return a + f(a+a, b/2);

}

Parallel Programming – SS 2014

Page 11: Parallel Programming Exercise Session 3

Efficiency estimate

More precise analysis needs to take costs of individual operations into account • Multiplication (by two) is cheaper than adding

• Large values are more expensive than small values

• We assume that all operations are equally expensive

Number of recursions is decisive: • This only depends on 𝑏 not on 𝑎

How many recursions do we need? • Conservatively estimated via rounding:

•𝑏

2𝑥 ≤ 1 => 𝑥 ≥ 𝑙𝑜𝑔2𝑏

We only need 5 log2 𝑏 operations or less

13.03.2014 Parallel Programming – SS 2014 11

Cost ≈ Time

static int f(int a, int b){ if (b == 1) return a; if (b%2 == 0) return f(a+a, b/2); else return a + f(a+a, b/2); }

Page 12: Parallel Programming Exercise Session 3

Comparison with Long multiplication

Why do we learn long multiplication in school?

Is this better (than the old-egyptian method)?

13.03.2014 Parallel Programming – SS 2014 12

3 1 2 X 4 1 5

1 2 4 8 +

3 1 2 +

1 5 6 0 =

1 2 9 4 8 0

3 1 2 X 4 1 5 3 1 2 X 4 1 5

1 2 4 8 +

3 1 2 +

1 5 6 0 =

1 2 9 4 8 0

Page 13: Parallel Programming Exercise Session 3

Challenge: Karatsuba multiplication

• Analyzed at Data Structures and Algorithms course

• At the time of its discovery (1960), first algorithm known to be

asymptotically faster than long multiplication

• Applies divide-and-conquer approach and requires only three

rather than the usual four multiplications

13.03.2014 Parallel Programming – SS 2014 14

Page 14: Parallel Programming Exercise Session 3

Lab Overview

Feedback from the last exercise

Discussion of exercise 3

Long Multiplication

Sieve of Eratosthenes

Java Tutorial

13.03.2014 Parallel Programming – SS 2014 15

Page 15: Parallel Programming Exercise Session 3

Algorithm

13.03.2014 Parallel Programming – SS 2014 16

Input: 𝑚𝑎𝑥 ∈ ℕ>0

Output: all the prime numbers less than or equal to 𝑚𝑎𝑥

Operations:

for each … do

end

Page 16: Parallel Programming Exercise Session 3

Prime Numbers

Prime: integer 𝑝 > 1 with no divisors other than 1 and 𝑝 itself

Example:

• 13 is prime since its only divisors are 1 and 13

• 24 is not prime. It has divisors 1, 2, 3, 4, 6, 8, 12, and 24. It can be factorized as 24 = 23 · 3

Prime sieve: algorithm for finding all primes up to a given limit

13.03.2014 Parallel Programming – SS 2014 17

Page 17: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

• Demonstrate the algorithm on the blackboard.

• This week: sequential, next week: parallelise.

13.03.2014 Parallel Programming – SS 2014 18

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 18: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 19

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 19: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 20

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 20: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 21

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 21: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 22

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 22: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 23

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 23: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 24

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Page 24: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Example

13.03.2014 Parallel Programming – SS 2014 25

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

… and so on

Page 25: Parallel Programming Exercise Session 3

Challenge: Iterative Prime Sieve

• Alternative formulation of the sieve in lazy functional style

• Allows to generate prime numbers indefinitely

• Implement in Java using iterators

• For details and example code see paper:

“The Genuine Sieve of Eratosthenes” by Melissa E. O’Neill

http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf

13.03.2014 Parallel Programming – SS 2014 26

Page 26: Parallel Programming Exercise Session 3

Lab Overview

Feedback from the last exercise

Discussion of exercise 3

Long Multiplication

Sieve of Eratosthenes

Java Tutorial

13.03.2014 Parallel Programming – SS 2014 27

Page 27: Parallel Programming Exercise Session 3

Java Programing

“Java in a Nutshell for Eiffel Programmers”, Stephanie Balzer, February 2010

http://www.lst.inf.ethz.ch/teaching/lectures/ss10/24/slides/2010-02-25-etj.pdf

http://www.lst.inf.ethz.ch/teaching/lectures/ss10/24/slides/2010-02-25-code_ex.zip

[@TAs: discuss the slides referenced above]

13.03.2014 Parallel Programming – SS 2014 28

Page 28: Parallel Programming Exercise Session 3

Eclipse: importing a project (1)

• Download ZIP from course page

• Menu: File -> Import

13.03.2014 Parallel Programming – SS 2014 29

Page 29: Parallel Programming Exercise Session 3

Eclipse: importing a project (2)

• Enter ZIP file path

• Hit <Enter> to confirm

• Make sure assignment3 project is

selected

• Click “Finish”

• Repeat the steps to add project to

SVN

13.03.2014 Parallel Programming – SS 2014 30

Page 30: Parallel Programming Exercise Session 3

Eclipse: running JUnit tests (1)

13.03.2014 Parallel Programming – SS 2014 31

Page 31: Parallel Programming Exercise Session 3

Eclipse: running JUnit tests (2)

13.03.2014 Parallel Programming – SS 2014 32

Page 32: Parallel Programming Exercise Session 3

JavaDoc

• API Specification for Java

http://docs.oracle.com/javase/7/docs/api/

• Also available directly within Eclipse / IntelliJ / …

• Explain: how to read a method signature

13.03.2014 Parallel Programming – SS 2014 33

Page 33: Parallel Programming Exercise Session 3

13.03.2014 Parallel Programming – SS 2014 34

Page 34: Parallel Programming Exercise Session 3

13.03.2014 Parallel Programming – SS 2014 35

Page 35: Parallel Programming Exercise Session 3

13.03.2014 Parallel Programming – SS 2014 36

Detailed Documentation: • Class Description • Inheritance Hierarchy • Method Summary

Packages

Classes

Page 36: Parallel Programming Exercise Session 3

13.03.2014 Parallel Programming – SS 2014 37

Page 37: Parallel Programming Exercise Session 3

More Resources

Stanford CS 108: Object Oriented Systems Design

Handouts on Java, Eclipse, Debugging, Generics, Threading, …

http://www.stanford.edu/class/cs108/

13.03.2014 Parallel Programming – SS 2014 38

Page 38: Parallel Programming Exercise Session 3

BACKUP

13.03.2014 Parallel Programming – SS 2014 39

Page 39: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Algorithm

Input: a non-negative integer 𝑚𝑎𝑥

Output: all the prime numbers less than or equal to 𝑚𝑎𝑥

13.03.2014 Parallel Programming – SS 2014 40

Page 40: Parallel Programming Exercise Session 3

Sieve of Eratosthenes: Algorithm

Input: a non-negative integer 𝑚𝑎𝑥

Output: all the prime numbers less than or equal to 𝑚𝑎𝑥

Algorithm:

• Create a list of natural numbers 2, 3, 4, 5, …, 𝑚𝑎𝑥. (None of which is marked.)

• Set k to 2, the first unmarked number on the list.

• Repeat until 𝑘2 > 𝑚𝑎𝑥: • Mark all multiples of 𝑘 between 𝑘2 and 𝑚𝑎𝑥. • Find the smallest number greater than 𝑘 that is unmarked. Set 𝑘 to this new value.

• All the unmarked numbers are primes.

13.03.2014 Parallel Programming – SS 2014 41