Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • lara/cs206-demos
  • gcharles/cs206-demos
  • gambhir/cs206-demos
3 results
Show changes
Showing
with 955 additions and 0 deletions
package lecture6
import java.util.concurrent.ForkJoinPool
// Run using `running lecture6.ExecutorsCreate`.
@main def ExecutorsCreate =
val executor = new ForkJoinPool
executor.execute(new Runnable:
override def run() = log("This task is run asynchronously.")
)
Thread.sleep(500)
package lecture6
import scala.concurrent.ExecutionContext
@main def ExecutionContextCreate =
val ectx = ExecutionContext.global
ectx.execute(new Runnable:
override def run() = log("This task is run asynchronously.")
)
Thread.sleep(500)
package lecture6
import scala.concurrent.ExecutionContext
def execute(body: => Unit) =
ExecutionContext.global.execute(new Runnable:
override def run() = body
)
@main def SimpleExecute =
execute { log("This task is run asynchronously.") }
Thread.sleep(500)
package lecture6
import scala.concurrent.ExecutionContext
import lecture1.threadStart
class OnePlaceBuffer[Elem]:
var elem: Elem = _
var full = false
def put(e: Elem): Unit = this.synchronized {
while full do wait()
elem = e
full = true
log(f"Put $e in the buffer")
notifyAll()
}
def get(): Elem = this.synchronized {
while !full do wait()
full = false
notifyAll()
log(f"Got $elem from the buffer")
elem
}
@main def OnePlaceBufferDemo =
val buffer = OnePlaceBuffer[Int]()
val getThreads = for i <- 0 until 10 yield threadStart { buffer.get() }
val putThreads = for i <- 0 until 10 yield threadStart { buffer.put(i) }
putThreads.foreach(_.join())
getThreads.foreach(_.join())
package lecture6
class Fork(var index: Int)
def philosopherTurn(i: Int, l: Fork, r: Fork): Unit =
Thread.sleep(10)
var (left, right) =
if i % 2 == 0 then (l, r)
else (r, l)
// if l.index < r.index then (l, r) else (r, l)
left.synchronized {
log(f"Philosopher $i picks up left fork ${left.index}")
right.synchronized {
log(f"Philosopher $i picks up right fork ${right.index} - eating")
Thread.sleep(100)
log(f"Philosopher $i puts down right fork ${right.index}")
}
log(f"Philosopher $i puts down left fork ${left.index}")
}
@main def run() =
val n = 5
val k: Int = 3
val forks = new Array[Fork](n)
val philosophers = new Array[Thread](n)
for p <- 0 to n - 1 do
forks(p) = Fork(p)
for p <- 0 to n - 1 do
philosophers(p) = new Thread:
override def run() =
for _ <- 0 until k do
philosopherTurn(p, forks(p % n), forks((p + 1) % n))
philosophers(p).start
for p <- 0 to n - 1 do
philosophers(p).join()
package lecture6
val log = println(_)
package midterm22
import scala.collection.mutable.Set
@main def mock1() =
val values = Set[Int]()
for _ <- 1 to 100000 do
var sum = 0
val t1 = task { sum += 1 }
val t2 = task { sum += 1 }
t1.join()
t2.join()
values += sum
println(values)
package midterm22
import instrumentation.Monitor
class Account(private var amount: Int = 0) extends Monitor:
def transfer(target: Account, n: Int) =
this.synchronized {
target.synchronized {
this.amount -= n
target.amount += n
}
}
@main def mock2() =
val a = new Account(50)
val b = new Account(70)
val t1 = task { a.transfer(b, 10) }
val t2 = task { b.transfer(a, 10) }
t1.join()
t2.join()
package midterm22
import scala.collection.parallel.Task
import scala.collection.parallel.CollectionConverters.*
// Questions 1-3
// See tests in midterm22.Part1Test.
// Run with `sbt "testOnly midterm22.Part1Test2"`.
def parallel3[A, B, C](op1: => A, op2: => B, op3: => C): (A, B, C) =
val res1 = task { op1 }
val res2 = task { op2 }
val res3 = op3
(res1.join(), res2.join(), res3)
def find(arr: Array[Int], value: Int, threshold: Int): Option[Int] =
def findHelper(start: Int, end: Int): Option[Int] =
if end - start <= threshold then
var i = start
while i < end do
if arr(i) == value then return Some(value)
i += 1
None
else
val inc = (end - start) / 3
val (res1, res2, res3) = parallel3(
findHelper(start, start + inc),
findHelper(start + inc, start + 2 * inc),
findHelper(start + 2 * inc, end)
)
res1.orElse(res2).orElse(res3)
findHelper(0, arr.size)
def findAggregated(arr: Array[Int], value: Int): Option[Int] =
val no: Option[Int] = None
val yes: Option[Int] = Some(value)
def f = (x1: Option[Int], x2: Int) => if x2 == value then Some(x2) else x1
def g = (x1: Option[Int], x2: Option[Int]) => if x1 != None then x1 else x2
arr.par.aggregate(no)(f, g)
@main def part1() =
println(find(Array(1, 2, 3), 2, 1))
// See tests in Part1Test
package midterm22
import scala.annotation.nowarn
// Questions 4-7
// See tests in midterm22.Part2Test.
// Run with `sbt testOnly midterm22.Part2Test`.
/*
Answers to the exam questions:
When called with a Vector:
The total amount of work is O(n), as it is dominated by the time needed to
read the array. More precisely W(n) = c + 2*W(n/2) = O(n).
The depth is O(log(n)), because every recursion takes constant time
and we divide the size of the input by 2 every time, i.e. D(n) = c + D(n/2) = O(log(n)).
Note however that in practice it is often still faster to manipulate
start and end indices rather than using take and drop.
When called with a List:
Every recursion takes up to time O(n) rather than constant time.
The total amount of work is O(n) times the number of recursion, because
take and drop takes time O(n) on lists. Precisely, W(n) = n + 2*W(n/2) = O(log(n)*n)
The depth is computed similarly: D(n) = n + D(n/2) = O(n), i.e.
Note: these are theoretical results. In practice, you should always double-check
that kind of conclusions with benchmarks. We did so in
`midterm-code/src/main/scala/bench`. Results are available in `bench-results`.
From these results, we can conclude that
1. Vectors are indeed faster in this case, and
2. parallelization of `contains` yields a 2x speedup.
*/
@nowarn
def contains[A](l: Iterable[A], elem: A): Boolean =
val n = l.size
if n <= 5 then
for i <- l do
if i == elem then
return true
false
else
val (p0, p1) = parallel(
contains(l.take(n / 2), elem),
contains(l.drop(n / 2), elem)
)
p0 || p1
package midterm22
// Question 8
// Run with `sbt runMain midterm22.part3`
@main def part3() =
def thread(b: => Unit) =
val t = new Thread:
override def run() = b
t
val t = thread { println(s"Hello World") }
t.join()
println(s"Hello")
package midterm22
import java.util.concurrent.atomic.AtomicInteger
import instrumentation.*
import instrumentation.Monitor
// Questions 9-15
// See tests in midterm22.Part4Test.
// Run with `sbt testOnly midterm22.Part4Test`.
class Node(
// Globally unique identifier. Different for each instance.
val guid: Int
) extends Monitor
// This function might be called concurrently.
def lockFun(nodes: List[Node], fn: (e: Int) => Unit): Unit =
if nodes.size > 0 then
nodes.head.synchronized {
fn(nodes(0).guid)
lockFun(nodes.tail, fn)
}
package midterm22
import instrumentation.Monitor
// Question 21
// See tests in midterm22.Part6Test.
// Run with `sbt testOnly midterm22.Part6Test`.
class TicketsManager(totalTickets: Int) extends Monitor:
var remainingTickets = totalTickets
// This method might be called concurrently
def getTicket(): Boolean =
if remainingTickets > 0 then
this.synchronized {
remainingTickets -= 1
}
true
else false
package midterm22
import instrumentation.Monitor
// Questions 22-24
// See tests in midterm22.Part7Test.
// Run with `sbt testOnly midterm22.Part7Test`.
class NIC(private val _index: Int, private var _assigned: Boolean)
extends Monitor:
def index = _index
def assigned = _assigned
def assigned_=(v: Boolean) = _assigned = v
class NICManager(n: Int):
// Creates a list with n NICs
val nics = (for i <- 0 until n yield NIC(i, false)).toList
// This method might be called concurrently
def assignNICs(limitRecvNICs: Boolean = false): (Int, Int) =
var recvNIC: Int = 0
var sendNIC: Int = 0
var gotRecvNIC: Boolean = false
var gotSendNIC: Boolean = false
/// Obtaining receiving NIC...
while !gotRecvNIC do
nics(recvNIC).synchronized {
if !nics(recvNIC).assigned then
nics(recvNIC).assigned = true
gotRecvNIC = true
else recvNIC = (recvNIC + 1) % (if limitRecvNICs then n - 1 else n)
}
// Successfully obtained receiving NIC
// Obtaining sending NIC...
while !gotSendNIC do
nics(sendNIC).synchronized {
if !nics(sendNIC).assigned then
nics(sendNIC).assigned = true
gotSendNIC = true
else sendNIC = (sendNIC + 1) % n
}
// Successfully obtained sending NIC
return (recvNIC, sendNIC)
package midterm22
import scala.collection.concurrent.{TrieMap, Map}
import java.util.concurrent.atomic.AtomicInteger
import scala.annotation.tailrec
// Question 25
// See tests in midterm22.Part8Test.
// Run with `sbt testOnly midterm22.Part8Test`.
// Represent a social network where user can follow each other. Each user is
// represented by an id that is an `Int`.
abstract class AbstractInstagram:
// The map storing the "following" relation of our social network.
// `graph(a)` contains the list of user ids that user `a` follows.
val graph: Map[Int, List[Int]] = new TrieMap[Int, List[Int]]()
// The maximum user id allocated until now. This value should be incremented
// by one each time a new user is added.
val maxId = new AtomicInteger(0)
// Allocates a new user and returns its unique id. Internally, this should
// also create an empty list at the corresponding id in `graph`. The
// implementation must be thread-safe.
def add(): Int
// Make `a` follow `b`. The implementation must be thread-safe.
def follow(a: Int, b: Int): Unit
// Makes `a` unfollow `b`. The implementation must be thread-safe.
def unfollow(a: Int, b: Int): Unit
// Removes user with id `a`. This should also remove all references to `a`
// in `graph`. The implementation must be thread-safe.
def remove(a: Int): Unit
class Instagram extends AbstractInstagram:
// This method is worth 6 points.
def add(): Int =
// It is important to increment and read the value in one atomic step. See
// test `testParallelWrongAdd` for an alternative wrong implementation.
val id = maxId.incrementAndGet
// Note: it is also correct to use `graph.putIfAbsent`, but not needed as
// `id` is always new and therefore absent from the map at this point.
graph.update(id, Nil)
id
// This method is worth 8 points.
def remove(a: Int): Unit =
graph.remove(a)
// Iterate through all keys to make sure that nobody follows `a` anymore.
// For each key, we need to unfollow a in a thread-safe manner. Calling
// `unfollow` is the simplest way to so, as it is already guaranteed to be
// thread-safe. See test `testParallelWrongRemove` for an example of wrong
// implementation.
for b <- graph.keys do unfollow(b, a)
// This method is worth 10 points.
def unfollow(a: Int, b: Int) =
// Here, it is important to read the value only once. First calling
// `.contains(a)` and then `graph(a)` does not work, as `a` might be removed
// between the two calls. See `testParallelWrongUnfollow` for an example of
// this wrong implementation.
val prev = graph.get(a)
// Returns silently if `a` does not exist.
if prev.isEmpty then return
// We replace the list of users that `a` follows in an atomic manner. If the
// list of followed users changed concurrently, we start over.
if !graph.replace(a, prev.get, prev.get.filter(_ != b)) then unfollow(a, b)
// This method is worth 12 points.
def follow(a: Int, b: Int) =
val prev = graph.get(a)
// Returns silently if `a` does not exist.
if prev.isEmpty then return
// We replace the list of users that `a` follows in an atomic manner. If the
// list of followed users changed concurrently, we start over.
if !graph.replace(a, prev.get, b :: prev.get) then follow(a, b)
// Difficult: this handles the case where `b` is concurrently removed by
// another thread. To detect this case, we must check if `b` still exists
// after we have followed it, and unfollow it if it is not the case. See
// test `testParallelFollowABRemoveB`. This last step is worth 4 points.
else if !graph.contains(b) then unfollow(a, b)
package midterm22.appendix
// Represents optional values. Instances of Option are either an instance of
// scala.Some or the object None.
sealed abstract class Option[+A]:
// Returns the option's value if the option is an instance of scala.Some, or
// throws an exception if the option is None.
def get: A
// Returns true if the option is an instance of scala.Some, false otherwise.
// This is equivalent to:
// option match
// case Some(v) => true
// case None => false
def isDefined: Boolean
// Returns this scala.Option if it is nonempty, otherwise return the result of
// evaluating alternative.
def orElse[B >: A](alternative: => Option[B]): Option[B]
abstract class Iterable[+A]:
// Selects all elements except first n ones.
def drop(n: Int): Iterable[A]
// The size of this collection.
def size: Int
// Selects the first n elements.
def take(n: Int): Iterable[A]
abstract class List[+A] extends Iterable[A]:
// Adds an element at the beginning of this list.
def ::[B >: A](elem: B): List[B]
// A copy of this sequence with an element appended.
def appended[B >: A](elem: B): List[B]
// Get the element at the specified index.
def apply(n: Int): A
// Selects all elements of this list which satisfy a predicate.
def filter(pred: (A) => Boolean): List[A]
// Selects the first element of this list.
def head: A
// Sorts this sequence according to a comparison function.
def sortWith(lt: (A, A) => Boolean): List[A]
// Selects all elements except the first.
def tail: List[A]
abstract class Array[+A] extends Iterable[A]:
// Get the element at the specified index.
def apply(n: Int): A
abstract class Thread:
// Subclasses should override this method.
def run(): Unit
// Causes this thread to begin execution; the Java Virtual Machine calls the
// run method of this thread.
def start(): Unit
// Waits for this thread to die.
def join(): Unit
// Creates and starts a new task ran concurrently.
def task[T](body: => T): ForkJoinTask[T] = ???
abstract class ForkJoinTask[T]:
// Returns the result of the computation when it is done.
def join(): T
// A concurrent hash-trie or TrieMap is a concurrent thread-safe lock-free
// implementation of a hash array mapped trie.
abstract class TrieMap[K, V]:
// Retrieves the value which is associated with the given key. Throws a
// NoSuchElementException if there is no mapping from the given key to a
// value.
def apply(key: K): V
// Tests whether this map contains a binding for a key.
def contains(key: K): Boolean
// Applies a function f to all elements of this concurrent map. This function
// iterates over a snapshot of the map.
def foreach[U](f: ((K, V)) => U): Unit
// Optionally returns the value associated with a key.
def get(key: K): Option[V]
// Collects all key of this map in an iterable collection. The result is a
// snapshot of the values at a specific point in time.
def keys: Iterator[K]
// Transforms this map by applying a function to every retrieved value. This
// returns a new map.
def mapValues[W](f: V => W): TrieMap[K, W]
// Associates the given key with a given value, unless the key was already
// associated with some other value. This is an atomic operation.
def putIfAbsent(k: K, v: V): Option[V]
// Removes a key from this map, returning the value associated previously with
// that key as an option.
def remove(k: K): Option[V]
// Removes the entry for the specified key if it's currently mapped to the
// specified value. This is an atomic operation.
def remove(k: K, v: V): Boolean
// Replaces the entry for the given key only if it was previously mapped to a
// given value. Returns true if the change is successful, or false otherwise.
// This is an atomic operation.
def replace(k: K, oldvalue: V, newvalue: V): Boolean
// Adds a new key/value pair to this map.
def update(k: K, v: V): Unit
// Collects all values of this map in an iterable collection. The result is a
// snapshot of the values at a specific point in time.
def values: Iterator[V]
// An int value that may be updated atomically.
// The constructor takes the initial value at its only argument. For example,
// this create an `AtomicInteger` with an initial value of `42`:
// val myAtomicInteger = new AtomicInteger(42)
abstract class AtomicInteger:
// Atomically adds the given value to the current value and returns the
// updated value.
def addAndGet(delta: Int): Int
// Atomically sets the value to the given updated value if the current value
// == the expected value. Returns true if the change is successful, or false
// otherwise. This is an atomic operation.
def compareAndSet(oldvalue: Int, newvalue: Int): Boolean
// Gets the current value. This is an atomic operation.
def get(): Int
// Atomically increments by one the current value. This is an atomic operation.
def incrementAndGet(): Int
// ---------------------------------------------
// Needed so that we can compile successfully, but not included for students.
// See Option class doc instead.
abstract class Some[+A](value: A) extends Option[A]
object Some:
def apply[A](value: A): Some[A] = ???
val None: Option[Nothing] = ???
object List:
def apply[A](values: A*): List[A] = ???
val Nil: List[Nothing] = ???
object Array:
def apply[A](values: A*): Array[A] = ???
package midterm22
import java.util.concurrent.ForkJoinTask
import java.util.concurrent.RecursiveTask
import java.util.concurrent.ForkJoinWorkerThread
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.atomic.AtomicInteger
val forkJoinPool = ForkJoinPool()
var parallelismEnabled = true
var tasksCreated: AtomicInteger = AtomicInteger(0)
def schedule[T](body: => T): ForkJoinTask[T] =
val t = new RecursiveTask[T]:
def compute = body
Thread.currentThread match
case wt: ForkJoinWorkerThread => t.fork()
case _ => forkJoinPool.execute(t)
t
def task[T](body: => T): ForkJoinTask[T] =
tasksCreated.incrementAndGet
schedule(body)
def parallel[A, B](op1: => A, op2: => B): (A, B) =
if parallelismEnabled then
val res1 = task { op1 }
val res2 = op2
(res1.join(), res2)
else (op1, op2)
package midterm23
import instrumentation.Monitor
import scala.annotation.tailrec
/** Runs one of the three provided solutions to the barber shop problem.
*
* Run using `sbt "runMain midterm23.barberSolution 2 3"` for example.
*
* @param solutionNumber
* One of the 3 provided solutions to run: 1, 2, or 3.
* @param nCustomers
* The number of customers to simulate.
*/
@main def barberSolution(solutionNumber: Int, nCustomers: Int): Unit =
val barberShop: AbstractBarberShopSolution =
solutionNumber match
case 1 => BarberShopSolution1(nCustomers)
case 2 => BarberShopSolution2(nCustomers)
case 3 => BarberShopSolution3(nCustomers)
barberSolutionMain(barberShop, solutionNumber, nCustomers)
def barberSolutionMain(
barberShop: AbstractBarberShopSolution,
solutionNumber: Int,
nCustomers: Int
): Unit =
val bthread = new Thread:
override def run() =
barberShop.barber()
bthread.start()
val customerThreads =
for i <- 1 to nCustomers yield
val cthread = new Thread:
override def run() =
barberShop.customer(i)
cthread.start()
cthread
bthread.join()
customerThreads.foreach(_.join())
abstract class AbstractBarberShopSolution:
// var notifyBarber: Boolean = false
// var notifyCustomer: Boolean = false
// val lockObj = Object()
// In order to be able to mock the three attributes above in the tests, we
// replace them with three private attributes and public getters and setters.
// Without overrides, this is equivalent to the definitions above.
private var _notifyBarber: Boolean = false
private var _notifyCustomer: Boolean = false
private val _lockObject: Monitor = new Monitor {}
def notifyBarber = _notifyBarber
def notifyBarber_=(v: Boolean) = _notifyBarber = v
def notifyCustomer = _notifyCustomer
def notifyCustomer_=(v: Boolean) = _notifyCustomer = v
def lockObj: Monitor = _lockObject
def hairCut(): Unit =
log("start hair cut")
Thread.sleep(1000)
log("finish hair cut")
def customer(n: Int): Unit
def barber(): Unit
/** Logs a message to the console.
*
* Overridden in the tests to avoid printing gigabytes of output.
*
* @param s
*/
def log(s: String) = println(s)
/** This is the solution we expected for the `customer` method.
*
* However, now that you have learned about `@volatile`, you can see that the
* `customer` method is not thread-safe. Can you spot a problem with this
* solution?
*
* Note that the `log` calls were added to help you understand what is going
* on. They are not part of the expected solution.
*/
trait BarberShopCustomerSolution1 extends AbstractBarberShopSolution:
override def customer(n: Int) =
lockObj.synchronized {
log(f"customer ${n} sits on the chair and waits for the barber")
notifyBarber = true
while !notifyCustomer do {}
notifyCustomer = false
log(f"customer ${n} leaves the chair")
}
/** This is the solution we expected for the `barber` method.
*
* Same question as above: can you spot a problem with this solution?
*
* @param customersNumber
* The number of customers to simulate.
*/
trait BarberShopBarberSolution1(customersNumber: Int)
extends AbstractBarberShopSolution:
override def barber() =
// while true do
// We replace the infinite loop from the instructions with a loop doing a
// limited number of iterations so that we can test the code.
for _ <- 1 to customersNumber do
log("barber waits for a customer")
while !notifyBarber do {}
notifyBarber = false
hairCut()
notifyCustomer = true
log("barber stops working")
class BarberShopSolution1(customersNumber: Int)
extends BarberShopCustomerSolution1,
BarberShopBarberSolution1(customersNumber)
/** The problem with BarberShopSolution1 is that the `notifyBarber` and
* `notifyCustomer` are accessed from multiple threads without any
* synchronization. This means that the compiler is free to reorder the
* instructions in the `customer` and `barber` methods, which can lead to
* unexpected behaviors like infinite loops!
*
* To solve this problem, the solution below redefines the two attributes as
* `@volatile`.
*
* @param customersNumber
* The number of customers to simulate.
*/
class BarberShopSolution2(customersNumber: Int)
extends BarberShopSolution1(customersNumber):
@volatile private var _notifyBarber: Boolean = false
@volatile private var _notifyCustomer: Boolean = false
override def notifyBarber = _notifyBarber
override def notifyBarber_=(v: Boolean) = _notifyBarber = v
override def notifyCustomer = _notifyCustomer
override def notifyCustomer_=(v: Boolean) = _notifyCustomer = v
/** The solution below uses a different approach to solve the problem of
* BarberShopSolution1. Instead of using `@volatile`, it uses the lock object
* to synchronize the accesses to the two attributes. Note how each access to
* the attributes is now wrapped in a `lockObj.synchronized` block.
*
* @param customersNumber
* The number of customers to simulate.
*/
class BarberShopSolution3(customersNumber: Int)
extends AbstractBarberShopSolution:
override def customer(n: Int) =
getHairCut(n)
@tailrec
private def getHairCut(n: Int): Unit =
val sited = lockObj.synchronized {
if notifyBarber then
false
else if notifyCustomer then
false
else
log(f"customer ${n} sits on the chair and waits for the barber")
notifyBarber = true
true
}
if sited then
while !lockObj.synchronized { notifyCustomer } do {}
lockObj.synchronized {
log(f"customer ${n} leaves the chair")
notifyCustomer = false
}
else
getHairCut(n)
override def barber() =
for _ <- 1 to customersNumber do
log("barber waits for a customer")
while !lockObj.synchronized { notifyBarber } do {}
hairCut()
lockObj.synchronized {
notifyCustomer = true
notifyBarber = false
}
log("barber stops working")
package midterm23
import java.util.concurrent.atomic.AtomicInteger
/** This is an example `BarberShopStudentAnswer`, like the one we made for your
* own solution and that you can find on Moodle. The solution below is boringly
* correct as it use the same implementation as the expected solution. See
* BarberShopSolution.scala for explanations and comments.
*
* @param customersNumber
* The number of customers to simulate.
*/
class BarberShopStudentAnswer333(customersNumber: Int)
extends AbstractBarberShopSolution:
/** This attribute is used to count the number of hair cuts. It is needed for
* testing because we do not want to loop forever in the `barber` method!
*/
val hairCutsCounter = AtomicInteger(customersNumber)
override def customer(n: Int) =
lockObj.synchronized {
notifyBarber = true
while !notifyCustomer do {}
notifyCustomer = false
}
override def barber() =
// while true
// We replace the infinite loop from the instructions with a loop doing a
// limited number of iterations so that we can test the code.
while hairCutsCounter.get() != 0 do
while !notifyBarber do {}
notifyBarber = false
hairCut()
hairCutsCounter.decrementAndGet() // You can ignore this line. It has been added for testing.
notifyCustomer = true
/** Version of `BarberShopStudentAnswer333` with the methods `customer`
* overridden to use the expected implementation.
*
* @param n
* The number of customers to simulate.
*/
class BarberShopStudentAnswer333WithCorrectCustomer(n: Int)
extends BarberShopStudentAnswer333(n)
with BarberShopCustomerSolution1
/** Version of `BarberShopStudentAnswer333` with the methods `barber` overridden
* to use the expected implementation.
*
* @param n
* The number of customers to simulate.
*/
class BarberShopStudentAnswer333WithCorrectBarber(n: Int)
extends BarberShopStudentAnswer333(n)
with BarberShopBarberSolution1(n)
// To grade, we ran tests with each of these classes.
import lecture1.parallel
def numContributeTo(input: Array[Int], kernel: Array[Int], output: Array[Int], i: Int): Int =
if i < 0 || i >= output.length then
0
else if (i - kernel.length < 0) then
i+1
else if (i >= input.length) then
output.length - i
else kernel.length
def sequentialConvolve(
input: Array[Int],
kernel: Array[Int],
output: Array[Int],
from: Int,
until: Int
): Unit = {
// Approach A, as described in the clarification announcement for this
// exercise, where we treat `from` and `until` as indices on `output`
// instead of `input` as in the given code.
var iOutput = from
while iOutput < until do
var iKernel = math.max(0, iOutput - input.length + 1)
while iKernel < kernel.length && iKernel <= iOutput do
// `output` is only ever written to between the indices `from` and
// `until`, the range of `iOutput`. The indices for `input` and
// `kernel` are computed accordingly.
output(iOutput) += input(iOutput - iKernel) * kernel(iKernel)
iKernel += 1
iOutput += 1
// ALTERNATE SOLUTION: Approach B, as described in the clarification
// announcement for this exercise, which is unchanged from the given
// code, i.e. we treat `from` and `until` as indices on `input`.
var iInput = from
while iInput < until do
var iKernel = 0
while iKernel < kernel.length do
output(iInput + iKernel) += input(iInput) * kernel(iKernel)
iKernel += 1
iInput += 1
}
def parallelConvolve(
input: Array[Int],
kernel: Array[Int],
output: Array[Int],
from: Int,
until: Int,
threshold: Int
): Unit =
// Approach A, as described in the clarification announcement for this
// exercise, where we treat `from` and `until` as indices on `output`
// instead of `input` as in the given code. This does not require us to
// change anything in this function. Only receives full credit if used
// together with Approach A for `sequentialConvolve`.
if (until - from) <= threshold then
sequentialConvolve(input, kernel, output, from, until)
else
val mid = from + (until - from) / 2
parallel(
parallelConvolve(input, kernel, output, from, mid, threshold),
parallelConvolve(input, kernel, output, mid, until, threshold)
)
// ALTERNATE SOLUTION: Approach B, as described in the clarification
// announcement for this exercise, where we treat `from` and `until` as
// indices on `input` as in the given code. This requires up to leave a
// gap in the parallel calls to be filled in sequentially as described
// below. Only receives full credit if used together with Approach B for
// `sequentialConvolve`.
if (until - from) <= threshold then
sequentialConvolve(input, kernel, output, from, until)
else
val mid = from + (until - from) / 2
val gap = numContributeTo(input, kernel, output, mid)
// Leave a gap large enough that accesses to `output` from the first
// parallel call will not overlap with accesses from the second. This
// gap is of size equal to the number of elements of `input` that will
// contribute to the computation of the result at the lowest index of
// `output` in the second parallel call, namely, at index `mid`.
// However, we are careful not to access indices lower than `from`.
val gapBeforeMid = Math.max(from, mid - gap + 1)
parallel(
parallelConvolve(input, kernel, output, from, gapBeforeMid, threshold),
parallelConvolve(input, kernel, output, mid, until, threshold)
)
// Fill in the gap sequentially.
sequentialConvolve(input, kernel, output, gapBeforeMid, mid)
object Original_WithOutputRace:
def sequentialConvolve(
input: Array[Int],
kernel: Array[Int],
output: Array[Int],
from: Int,
until: Int): Unit =
var iInput = from
while iInput < until do
var iKernel = 0
while iKernel < kernel.length do
output(iInput + iKernel) += input(iInput) * kernel(iKernel)
iKernel += 1
iInput += 1
def parallelConvolve(
input: Array[Int],
kernel: Array[Int],
output: Array[Int],
from: Int,
until: Int,
threshold: Int): Unit =
if (until - from) <= threshold then
sequentialConvolve(input, kernel, output, from, until)
else
val mid = from + (until - from)/2
parallel(
parallelConvolve(input, kernel, output, from, mid, threshold),
parallelConvolve(input, kernel, output, mid, until, threshold))