parallel programming exercise session 2 - spcl · parallel programming exercise session 2 spring...

27
Parallel Programming Exercise Session 2 Spring 2020

Upload: others

Post on 01-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Parallel ProgrammingExercise Session 2Spring 2020

Page 2: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Preparations

1. Import assignment2.zip in Eclipse

2. Run the projects unit-tests in Eclipse

3. Understand output of unit-tests• Did the test fail or succeed?• Why did the test fail?

4. Start coding and keep checking if tests pass

2

Page 3: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: import project

3

Page 4: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: import project

4

Page 5: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: import project

5

Page 6: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: add to git

6

Team -> Share Project ...

Page 7: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: add to git

7Important: Select same directory as for assignment 1

Page 8: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: running JUnit tests (1)

8

Page 9: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Eclipse: running JUnit tests (2)

9

Page 10: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Code Style

• Try to make your code as readable as possible(Use Eclipse formatter <CTRL>+<SHIFT>+F)

• Include high-level comments that explain why you are doing something (much better than a line-by-line commentary of your code)

10

Page 11: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Code Style / Errors

Keep attention what Eclipse reports:

11

Page 12: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

12

Java Doc (http://docs.oracle.com/javase/7/docs/api/)

Page 13: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

13

Java Doc (http://docs.oracle.com/javase/7/docs/api/)

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

Packages

Classes

Page 14: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

03.03.17 Parallel Programming – SS 2016 14

Method Signature Semantic description what the method does

Parameter description

Possible occurring errors

Page 15: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task A

To start with, print to the console "Hello Thread!" from a new thread. How do you check that the statement was indeed printed from a thread that is different to the main thread of your application? Furthermore, ensure that you program (i.e., the execution of main thread) finishes only after the thread execution finishes.

15

Page 16: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task A: How to create and start a new thread?

16

option 1: Extend class Thread

option 2: Implement Runnable

Page 17: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task B

Run the method computePrimeFactors in a single thread other than the main thread. Measure the execution time of sequential execution (on the main thread) and execution using a single thread. Is there any noticeable difference?

17

Page 18: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task C

Design and run an experiment that would measure the overhead of creating and executing a thread.

18

Page 19: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task C

19

option 1: Measures real time elapsed including time when the thread is not running.

option 2: Measures thread cpu time excluding time when the thread is not running.

Page 20: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task D

Before you parallelize the loop in Task E, design how the work should be split between the threads by implementing method PartitionData. Each thread should process roughly equal amount of elements. Briefly describe you solution and discuss alternative ways to split the work?

20

Page 21: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task D: Split the work between the threads

21

PartitionData(int length, int numPartitions) { … }

Input

length (20)

a) PartitionData(20,1)

b) PartitionData(20,2)

c) PartitionData(20,3)

?

?

?

Page 22: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task D: Split the work between the threads

22

PartitionData(int length, int numPartitions) { … }

Input

length (20)

a) PartitionData(20,1)

b) PartitionData(20,2)

c) PartitionData(20,3)

d) PartitionData(20,3)

both c) and d) are correct solutions for this exercise

Page 23: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task D

• What about (length>0 and numPartitions>0) and length<numPartitions?• ??• ??

• And (length<=0 or numPartitions<=0)?• ??• ??

23

PartitionData(int length, int numPartitions) { … }

Page 24: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task D

• What about (length>0 and numPartitions>0) and length<numPartitions?• Throw an exception?• Return m = min(m,n) splits?

• And (length<=0 or numPartitions<=0)?• Throw an exception?• Create a default return value (e.g. new ArraySplit[0])?

• In any case, write your assumptions in JavaDoc

24

PartitionData(int length, int numPartitions) { … }

Page 25: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task E

Parallelize the loop execution in computePrimeFactors using a configurable amount of threads.

25

Page 26: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task F

Think of how would a plot that shows the execution speed-up of your implementation, for n = 1, 2, 4, 8, 16, 32, 64, 128 threads and the input array size of 100, 1000, 10000, 100000 look like

26

Page 27: Parallel Programming Exercise Session 2 - SPCL · Parallel Programming Exercise Session 2 Spring 2020 Preparations 1. Import assignment2.zip in Eclipse 2. Run the projects unit-tests

Task G

Measure the execution time of your parallel implementation for n = 1, 2, 4, 8, 16, 32, 64, 128 threads and the input array size of input.length = 100, 1000, 10000, 100000. Discuss the differences in the two plots from task F and G.

27