Skip to content
Snippets Groups Projects
Commit bca97099 authored by Olivier Blanvillain's avatar Olivier Blanvillain
Browse files

Update recitation-session-3.md

parent 24001442
No related branches found
No related tags found
No related merge requests found
...@@ -50,17 +50,11 @@ class Node[A](val value: A) { ...@@ -50,17 +50,11 @@ class Node[A](val value: A) {
} }
``` ```
## Question 1 **Question 1:** What computational complexity do your methods have? Are the actual complexities of your methods acceptable according to the `Combiner` requirements?
What computational complexity do your methods have? Are the actual complexities of your methods acceptable according to the `Combiner` requirements? **Question 2:** One of the three methods you have implemented, `result`, should work in parallel according to the `Combiner` contract. Can you think of a way to implement this method efficiently using 2 parallel tasks?
## Question 2 **Question 3:** Can you, given the current internal representation of your combiner, implement `result` so that it executes efficiently using 4 parallel tasks? If not, can you think of a way to make it possible?
One of the three methods you have implemented, `result`, should work in parallel according to the `Combiner` contract. Can you think of a way to implement this method efficiently using 2 parallel tasks?
## Question 3
Can you, given the current internal representation of your combiner, implement `result` so that it executes efficiently using 4 parallel tasks? If not, can you think of a way to make it possible?
*Hint:* This is an open-ended question, there might be multiple solutions. In your solution, you may want to add extra information to the class Node and/or the class DLLCombiner. *Hint:* This is an open-ended question, there might be multiple solutions. In your solution, you may want to add extra information to the class Node and/or the class DLLCombiner.
...@@ -83,9 +77,7 @@ p(x) = (((x + 1) Application of first function ...@@ -83,9 +77,7 @@ p(x) = (((x + 1) Application of first function
In this exercise, we will investigate the possibility to process such pipelines in parallel. In this exercise, we will investigate the possibility to process such pipelines in parallel.
## Question 1 **Question 1:** Implement the following `toPipeline` function, which turns a parallel sequence of functions into a pipeline. You may use any of the parallel combinators available on `ParSeq`, such as the parallel `fold` or the parallel `reduce` methods.
Implement the following `toPipeline` function, which turns a parallel sequence of functions into a pipeline. You may use any of the parallel combinators available on `ParSeq`, such as the parallel `fold` or the parallel `reduce` methods.
```scala ```scala
def toPipeline(fs: ParSeq[A => A]): A => A = ??? def toPipeline(fs: ParSeq[A => A]): A => A = ???
...@@ -93,15 +85,11 @@ def toPipeline(fs: ParSeq[A => A]): A => A = ??? ...@@ -93,15 +85,11 @@ def toPipeline(fs: ParSeq[A => A]): A => A = ???
*Hint:* Functions have a method called andThen, which implements function composition: it takes as argument another function and also returns a function. The returned function first applies the first function, and then applies the function passed as argument to that result. You may find it useful in your implementation of pipeline. *Hint:* Functions have a method called andThen, which implements function composition: it takes as argument another function and also returns a function. The returned function first applies the first function, and then applies the function passed as argument to that result. You may find it useful in your implementation of pipeline.
## Question 2 **Question 2:** Given that your `toPipeline` function works in parallel, would the pipelines it returns also work in parallel? Would you expect pipelines returned by a sequential implementation of toPipeline to execute any slower? If so, why?
Given that your `toPipeline` function works in parallel, would the pipelines it returns also work in parallel? Would you expect pipelines returned by a sequential implementation of toPipeline to execute any slower? If so, why?
Discuss those questions with your group and try to get a good understanding of what is happening. Discuss those questions with your group and try to get a good understanding of what is happening.
## Question 3 **Question 3:** Instead of arbitrary functions, we will now consider functions that are constant everywhere except on a finite domain. We represent such functions in the following way:
Instead of arbitrary functions, we will now consider functions that are constant everywhere except on a finite domain. We represent such functions in the following way:
```scala ```scala
class FiniteFun[A](mappings: immutable.Map[A, A], default: A) { class FiniteFun[A](mappings: immutable.Map[A, A], default: A) {
...@@ -117,9 +105,7 @@ class FiniteFun[A](mappings: immutable.Map[A, A], default: A) { ...@@ -117,9 +105,7 @@ class FiniteFun[A](mappings: immutable.Map[A, A], default: A) {
Implement the andThen method. Can pipelines of such finite functions be efficiently constructed in parallel using the appropriately modified `toPipeline` method? Can the resulting pipelines be efficiently executed? Implement the andThen method. Can pipelines of such finite functions be efficiently constructed in parallel using the appropriately modified `toPipeline` method? Can the resulting pipelines be efficiently executed?
## Question 4 **Question 4:** Compare the *work* and *depth* of the following two functions, assuming infinite parallelism. For which kind of input would the parallel version be asymptotically faster?
Compare the *work* and *depth* of the following two functions, assuming infinite parallelism. For which kind of input would the parallel version be asymptotically faster?
```scala ```scala
def applyAllSeq[A](x: A, fs: Seq[FiniteFun[A]]): A = { def applyAllSeq[A](x: A, fs: Seq[FiniteFun[A]]): A = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment