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
Commits on Source (12)
Showing
with 1113 additions and 1 deletion
......@@ -21,4 +21,4 @@ test:
tags:
- cs320
script:
- sbt "runMain lecture1.javaThreads; runMain lecture1.scalaThreadWrapper; runMain lecture1.ExampleThread; runMain lecture3.intersectionWrong; runMain lecture3.intersectionCorrect; runMain lecture3.intersectionNoSideEffect; runMain lecture3.parallelGraphContraction; runMain lecture3.parallelGraphContractionCorrect; runMain midterm22.mock1; runMain midterm22.part3; test; scalafmtCheck; Test / scalafmtCheck"
- sbt "runMain lecture1.javaThreads; runMain lecture1.scalaThreadWrapper; runMain lecture1.ExampleThread; runMain lecture3.intersectionWrong; runMain lecture3.intersectionCorrect; runMain lecture3.intersectionNoSideEffect; runMain lecture3.parallelGraphContraction; runMain lecture3.parallelGraphContractionCorrect; runMain midterm22.mock1; runMain midterm22.part3; runMain ed.patternMatching; runMain lecture13.askPatternDemo; test; runMain lecture13.becomeDemo; runMain ed.futuresForTranslation; runMain concpar20final03.concpar20final03; scalafmtCheck; Test / scalafmtCheck"
package concpar20final03
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
def sequence1[A](fs: List[Future[A]]): Future[List[A]] =
fs match
case f :: fs =>
for
x <- f
xs <- sequence1(fs)
yield x :: xs
case Nil =>
Future.successful(Nil)
def sequence2[A](fs: List[Future[A]]): Future[List[A]] =
fs match
case f :: fs =>
f.flatMap(x => sequence2(fs).map(xs => x :: xs))
case Nil =>
Future.successful(Nil)
def sequence3[A](fs: List[Future[A]]): Future[List[A]] =
fs match
case f :: fs =>
f.transformWith {
case Failure(e) => Future.failed(e)
case Success(value) =>
sequence3(fs).transform {
case res: Failure[List[A]] => res
case Success(values) => Success(value :: values)
}
}
case Nil =>
Future.successful(Nil)
def sequence4[A](fs: List[Future[A]]): Future[List[A]] =
fs match
case f :: fs =>
val fsFuture: Future[List[A]] = sequence4(fs)
f.zip(fsFuture).map((fValue, fsValue) => fValue :: fsValue)
case Nil =>
Future.successful(Nil)
@main def concpar20final03 =
val xs = List(Future { 1 }, Future { 2 }, Future { 3 })
val ys = List(Future { 1 }, Future.failed(Error("BOOM")), Future { 3 })
println(Await.result(sequence1(xs), Duration.Inf))
// println(Await.result(sequence1(xs), Duration.Inf)) // Throws exception
println(Await.result(sequence2(xs), Duration.Inf))
// println(Await.result(sequence2(ys), Duration.Inf)) // Throws exception
println(Await.result(sequence3(xs), Duration.Inf))
// println(Await.result(sequence3(ys), Duration.Inf)) // Throws exception
println(Await.result(sequence4(xs), Duration.Inf))
// println(Await.result(sequence4(ys), Duration.Inf)) // Throws exception
package concpar21final01
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
case class Grade(sciper: Int, grade: Double)
trait Problem1:
/** Retrieve the list of student grades, sorted such that maximum grades
* appear at the head of the list.
*/
def leaderboard(): Future[List[Grade]] =
getScipers()
.flatMap { scipers =>
Future.sequence(scipers.map(getGrade))
}
.map(_.flatten.sortBy(_.grade).reverse)
/** Retrieve a student's grade using GitLab's API. The result is wrapped in an
* option, where `Future(None)` indicates either:
* - the student is not registered to the class
* - the student did not push his/her solution to GitLab
*/
def getGrade(sciper: Int): Future[Option[Grade]]
/** Retrieve the list of enrolled students from IS-academia
*/
def getScipers(): Future[List[Int]]
package concpar21final01
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.Random
class Problem1MockData extends Problem1:
def getGrade(sciper: Int): Future[Option[Grade]] =
Future {
// In an actual implementation, this is where we would make a call to
// the GitLab APIs. This mock returns a random grade after a short delay.
Thread.sleep(15) // GitLab is pretty fast today...
val rand = new Random(sciper)
val grade = rand.nextInt(6).toDouble + rand.nextDouble()
if sciper < 100000 || sciper > 999999 || sciper % 10 == 0 then None
else Some(Grade(sciper, grade))
}
/** Retrieve the list of enrolled students from IS-academia
*/
def getScipers(): Future[List[Int]] =
Future {
Thread.sleep(100)
List( // A fake list of SCIPER numbers
301425, 207372, 320658, 300217, 224523, 301068, 331020, 331095, 320270,
320742, 299310, 300974, 322202, 343357, 302632, 343366, 320229, 269364,
320004, 321830, 219188, 300834, 320992, 299237, 298016, 300397, 269857,
300492, 300481, 279254, 320967, 300443, 300329, 300305, 331158, 310402,
279067, 300682, 259825, 351616, 310869, 301215, 299481, 269375, 351249,
310866, 351141, 301530, 361378, 351661, 351524, 311081, 331137, 332319,
301045, 300393, 300308, 310889, 310064, 310841, 351333, 310382, 333887,
333837, 320832, 321397, 351691, 269125, 312732, 351546, 301783, 351698,
310775, 331388, 311139, 301992, 301578, 361760, 351174, 310298, 300666,
259778, 301554, 301278, 301669, 321372, 311347, 321129, 351490, 321189,
301336, 341560, 331220, 331129, 333927, 279186, 310596, 299135, 279226,
310507, 269049, 300309, 341524, 351143, 300785, 310612, 320338, 259980,
269952, 310397, 320246, 310959, 301454, 301835, 301802, 301649, 301170,
301908, 351708, 321046, 361490, 311070, 351830, 311054, 311912, 301913,
361232, 301030, 351723, 311472, 311166, 321057, 310793, 269462, 311948,
321693, 321056, 361765, 301453, 321626, 341490, 320892, 269871, 269580,
320199, 320908, 320830, 269071, 380542, 253768, 311204, 269127, 351073,
341327, 301792, 299789, 361424, 301525, 311637, 321423, 279111, 330126,
310371, 259888, 269525, 299585, 300147, 341402, 330067, 311796, 279037,
248517, 301436, 269965, 259963, 320720, 248583, 259709, 361204, 341500,
311803, 299981, 311832, 301088, 259649, 279183, 341760, 311844, 279079,
390997, 311917, 390999, 361122, 301208, 311538, 272943, 361570, 390959)
}
package ed
// To demonstrate different ways of pattern matching, let's consider the
// following example case class and instance:
case class Person(name: String, age: Int)
val ada = Person("Ada", 36)
// Run using `sbt "runMain ed.patternMatching"`.
@main def patternMatching =
// There are several ways to pattern match on the `ada` instance:
// 1. Pattern matching on the case class constructor using `Person(name, age)`.
// If the pattern matches, the value of `n` is bound `ada.name` field, and
// `a` is bound to the `ada.age` field.
ada match
case Person(n, a) => println(f"$n is $a years old")
// 2. We can also check only that `ada` is of type `Person` without binding its
// fields by using a `:` pattern. The `:` pattern is used to check if a value is
// of a certain type.
ada match
case p: Person => println(f"${p.name} is ${p.age} years old")
// 3. If we want to both bind the fields and bind a value to the whole instance,
// we can use an `@` pattern.
ada match
case p @ Person(n, a) => println(f"${p.name} is ${a} years old")
package ed
import concurrent.{Await, Future}
import concurrent.duration.Duration
import concurrent.ExecutionContext.Implicits.global
// Pro Tip: you can print the code generated by the Scala compiler after the
// translation of for-comprehensions by running:
//
// scala -Xprint:firstTransform src/main/scala/ed/40056.scala
@main def futuresForTranslation =
val f1 = Future { 451 }
val f2 = Future { 1984 }
val f3 = for v1 <- f1; v2 <- f2 yield v1 + v2
println(Await.result(f3, Duration.Inf))
val f4 = Future { 451 }
val f5 = Future { 1984 }
val f6 = f4.flatMap(v4 => f5.map(v5 => v4 + v5))
println(Await.result(f6, Duration.Inf))
package lecture13
import concurrent.{ExecutionContext, Future, Promise}
import concurrent.duration.DurationInt
import akka.actor.{Actor, ActorContext, ActorRef, ActorSystem, Props}
import akka.event.LoggingReceive
import akka.pattern.pipe
import akka.testkit.{ImplicitSender, TestKit}
import akka.util.Timeout
/** This demonstrates a simplified implementation of the ask pattern.
*
* @param promise
* the promise to be completed when a message is received
*/
class AskMiniActor(promise: Promise[Any]) extends Actor:
def receive = LoggingReceive {
case msg => promise.success(msg)
}
extension (receiver: ActorRef)
def ?(msg: Any)(using
// In this simplified implementation, we don't use the timeout.
timeout: Timeout,
// This only used for logging purposes (and to get the actor system in the
// real implementation), but is not used otherwise in the implementation.
sender: ActorRef,
// In the real implementation, the actor system is retrieved differently,
// but here we pass it as an additional implicit parameter for simplicity.
context: ActorContext
): Future[Any] =
context.system.log.debug(
s"Create mini actor to ask $msg from $sender to $receiver"
)
// Create a `Promise` that will be completed when a message is received.
val promise = Promise[Any]()
// Create a mini actor that will complete the `Promise` when it receives a
// message.
val miniActor = context.system.actorOf(Props(AskMiniActor(promise)))
// Send the message to the mini actor, *using the mini actor as the sender*.
// This part is important as it is the mini actor that needs to receive the
// response.
receiver.tell(msg, miniActor)
// Using the `Promise` from the Scala standard library, the corresponding
// `Future` can be retrieved with the method `future`.
promise.future
object Person:
enum Protocol:
case GetName
case GetAge
enum Response:
case Name(name: String)
case Age(age: Int)
case UnknownMessage
class Person(name: String, age: Int) extends Actor:
import Person.Protocol.*
import Person.Response.*
def receive = LoggingReceive {
case GetName => sender() ! Name(name)
case GetAge => sender() ! Age(age)
case _ => sender() ! UnknownMessage
}
object PersonsDatabase:
enum Protocol:
case CreatePerson(name: String, age: Int)
case GetPersonNames(ids: List[Int])
enum Response:
case PersonCreated(id: Int)
case PersonNames(names: List[String])
class PersonsDatabase extends Actor:
import Person.Protocol.*
import Person.Response.*
import PersonsDatabase.Protocol.*
import PersonsDatabase.Response.*
var persons: Map[Int, ActorRef] = Map.empty
var maxId = 0
given ExecutionContext = context.system.dispatcher
given Timeout = Timeout(200.millis)
def receive = LoggingReceive {
case CreatePerson(name, age) =>
val personRef = context.actorOf(Props(Person(name, age)))
persons = persons + (maxId -> personRef)
sender() ! PersonCreated(maxId)
maxId += 1
case GetPersonNames(ids) =>
// We ask every person for their name using the ask pattern. This
// gives us a list of `Future`s that will each be completed with a
// `Name` message.
val rawResponsesFutures: List[Future[Any]] =
ids.map(id => (persons(id) ? GetName))
// We first map each `Future[Any]` to a `Future[Name]` using the
// `mapTo` method. Then, we map each `Future[Name]` to a
// `Future[String]` using the `map` method.
val namesFutures: List[Future[String]] =
rawResponsesFutures.map(_.mapTo[Name].map(_.name))
// We use the `Future.sequence` method to convert a
// `List[Future[String]]` to a `Future[List[String]]`. The resulting
// future will be completed once all the futures in the input list
// are completed.
val futureOfNames: Future[List[String]] = Future.sequence(namesFutures)
// Finally, map the `Future[List[String]]` to a `Future[PersonNames]` and
// pipe it to the sender of the `GetPersonNames` message.
futureOfNames.map(PersonNames.apply).pipeTo(sender())
}
@main def askPatternDemo() =
new TestKit(ActorSystem("DebugSystem")) with ImplicitSender:
import Person.Protocol.*
import Person.Response.*
import PersonsDatabase.Protocol.*
import PersonsDatabase.Response.*
try
val personsDb = system.actorOf(Props(PersonsDatabase()))
personsDb ! CreatePerson("Anita", 20)
expectMsg(PersonCreated(0))
personsDb ! CreatePerson("Berta", 30)
expectMsg(PersonCreated(1))
personsDb ! CreatePerson("Cecilia", 40)
expectMsg(PersonCreated(2))
personsDb ! GetPersonNames(List(0, 1, 2))
expectMsg(PersonNames(List("Anita", "Berta", "Cecilia")))
finally shutdown(system)
package lecture13
import akka.actor.{Actor, ActorContext, ActorRef, ActorSystem, Props}
import akka.event.LoggingReceive
import akka.testkit.{ImplicitSender, TestKit}
class CounterRight extends Actor:
def counter(n: Int): Receive = {
case "increment" => context.become(counter(n + 1), discardOld = false)
// ^^^^^^^^^^^^^^^^^^
// This is needed.
case "get" => sender() ! n
case "decrement" => if n != 0 then context.unbecome()
}
def receive = counter(0)
class CounterWrong extends Actor:
def counter(n: Int): Receive = {
case "increment" => context.become(counter(n + 1))
case "get" => sender() ! n
case "decrement" => if n != 0 then context.unbecome()
}
def receive = counter(0)
@main def becomeDemo() =
new TestKit(ActorSystem("DebugSystem")) with ImplicitSender:
try
val counter = system.actorOf(Props(CounterRight()), "counter")
counter ! "increment"
counter ! "get"
expectMsg(1)
counter ! "increment"
counter ! "get"
expectMsg(2)
counter ! "decrement"
counter ! "get"
expectMsg(1)
// This is wrong if we use CounterWrong because it doesn't set the
// discardOld parameter to false. Therefore, it will not remember the
// previous state and reset the behavior to the initial one, which is
// `counter(0)`.
counter ! "decrement"
counter ! "get"
expectMsg(0)
finally shutdown(system)
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))
import lecture1.parallel
import collection.concurrent.TrieMap
import collection.parallel.CollectionConverters.*
// Example run of `groupBy`:
case class Course(semester: String, id: String)
val cs210 = Course("BA3", "CS210")
val cs250 = Course("BA3", "CS250")
val cs206 = Course("BA4", "CS206")
val courses = Vector(cs210, cs250, cs206)
assertEquals(
courses.groupBy(_.semester),
Map(
"BA3" -> Vector(cs210, cs250),
"BA4" -> Vector(cs206)
)
)
/** Asymptotic run time: O(1) */
def appendAtKey[K, V](m: Map[K, Vector[V]], k: K, v: V): Map[K, Vector[V]] =
val prev = m.getOrElse(k, Vector.empty)
m.updated(k, prev :+ v)
// Example runs of `appendAtKey`:
assertEquals(
appendAtKey(Map(), 0, 1),
Map(0 -> Vector(1))
)
assertEquals(
appendAtKey(Map("BA3" -> Vector(cs210)), "BA3", cs250),
Map("BA3" -> List(cs210, cs250))
)
/** Asymptotic run time: O(1) */
def mergeMaps[K, V](a: Map[K, Vector[V]], b: Map[K, Vector[V]]) =
var res = a
for (k, vs) <- b do // O(1) iterations (we assume the number of keys to be O(1))
val prev = res.getOrElse(k, Vector.empty) // O(1) lookup
res = res.updated(k, prev ++ vs) // O(1) update, O(1) concatenation
res
// Example run of `mergeMaps`:
val m1 = Map(1 -> Vector(5, 7))
val m2 = Map(1 -> Vector(6), 2 -> Vector(3))
assertEquals(
mergeMaps(m1, m2),
Map(
1 -> Vector(5, 7, 6),
2 -> Vector(3)
)
)
def parGroupBy1[K, V](vs: Vector[V], key: V => K): Map[K, Vector[V]] =
if vs.size == 1 then
Map(key(vs.head) -> Vector(vs.head))
else
val mid = vs.size / 2
val (m1, m2) = parallel(
parGroupBy1(vs.slice(0, mid), key),
parGroupBy1(vs.slice(mid, vs.size), key)
)
mergeMaps(m1, m2)
assertEquals(
parGroupBy1(courses, c => c.semester),
Map(
"BA3" -> List(cs210, cs250),
"BA4" -> List(cs206)
)
)
assertEquals(
parGroupBy1(courses, c => c.semester),
Map(
"BA3" -> List(cs210, cs250),
"BA4" -> List(cs206)
)
)
def parGroupBy2[K, V](vs: Vector[V], key: V => K): Map[K, Vector[V]] =
if vs.size == 1 then
Map(key(vs.head) -> Vector(vs.head))
else
val (m1, m2) = parallel(
parGroupBy2(Vector(vs.head), key),
parGroupBy2(vs.tail, key)
)
mergeMaps(m1, m2)
assertEquals(
parGroupBy2(courses, c => c.semester),
Map(
"BA3" -> List(cs210, cs250),
"BA4" -> List(cs206)
)
)
assertEquals(
parGroupBy2(courses, c => c.semester),
Map(
"BA3" -> List(cs210, cs250),
"BA4" -> List(cs206)
)
)
def parGroupBy3[K, V](vs: Vector[V], key: V => K): Map[K, Vector[V]] =
val lock = Object()
var res = Map[K, Vector[V]]()
for v <- vs.par do
lock.synchronized { res = appendAtKey(res, key(v), v) }
res
var values3 = Set[Map[String, Vector[Course]]]()
for _ <- 1 to 100000 do
values3 += parGroupBy3(courses, _.semester)
assertEquals(values3.size, 2)
values3
def parGroupBy4[K, V](vs: Vector[V], key: V => K): Map[K, Vector[V]] =
val res = TrieMap[K, Vector[V]]()
for v <- vs.par do
val k = key(v)
val prev = res.getOrElse(k, Vector.empty)
res.update(k, prev :+ v)
res.toMap
var values4 = Set[Map[String, Vector[Course]]]()
for _ <- 1 to 100000 do
values4 += parGroupBy4(courses, _.semester)
assertEquals(values4.size, 4)
values4
def parGroupBySolution[K, V](vs: Vector[V], key: V => K): Map[K, Vector[V]] =
vs.par.aggregate(Map.empty)(
(m, v) => appendAtKey(m, key(v), v),
mergeMaps
)
var valuesSol = Set[Map[String, Vector[Course]]]()
for _ <- 1 to 100000 do
valuesSol += parGroupBySolution(courses, _.semester)
assertEquals(valuesSol.size, 1)
valuesSol
def assertEquals(a: Any, b: Any) = assert(a == b)
package concpar21final01
import scala.concurrent.duration.*
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
class Problem1Suite extends munit.FunSuite:
test(
"Retrieves grades at the end of the exam (everyone pushed something) (10pts)"
) {
class Problem1Done extends Problem1:
override def getGrade(sciper: Int): Future[Option[Grade]] =
Future {
Thread.sleep(100)
Some(Grade(sciper, sciper))
}
override def getScipers(): Future[List[Int]] =
Future {
Thread.sleep(100)
List(1, 2, 3, 4)
}
val expected: List[Grade] =
List(Grade(1, 1.0), Grade(2, 2.0), Grade(3, 3.0), Grade(4, 4.0))
(new Problem1Done).leaderboard().map { grades =>
assertEquals(grades.toSet, expected.toSet)
}
}
test("Retrieves grades mid exam (some students didn't push yet) (10pts)") {
class Problem1Partial extends Problem1:
override def getGrade(sciper: Int): Future[Option[Grade]] =
Future {
Thread.sleep(100)
if sciper % 2 == 0 then None
else Some(Grade(sciper, sciper))
}
override def getScipers(): Future[List[Int]] =
Future {
Thread.sleep(100)
List(1, 2, 3, 4)
}
val expected: List[Grade] =
List(Grade(1, 1.0), Grade(3, 3.0))
(new Problem1Partial).leaderboard().map { grades =>
assertEquals(grades.toSet, expected.toSet)
}
}
test("The output list is sorted by grade (10pts)") {
(new Problem1MockData).leaderboard().map { grades =>
assert(grades.size >= 176)
assert(grades.zipWithIndex.forall { case (g, i) =>
grades.drop(i).forall(x => g.grade >= x.grade)
})
}
}
test("GitLab API calls are done in parallel (2pts)") {
var inParallel: Boolean = false
class Problem1Par extends Problem1MockData:
var in: Boolean = false
override def getGrade(sciper: Int): Future[Option[Grade]] =
Future {
if in then inParallel = true
in = true
val out = super.getGrade(sciper)
in = false
concurrent.Await.result(out, Duration(10, SECONDS))
}
(new Problem1Par).leaderboard().map { grades =>
assert(grades.size >= 176)
assert(inParallel)
}
}
test("The IS-academia API is called exactly once (2pts)") {
var called: Int = 0
class Problem1Once extends Problem1MockData:
override def getScipers(): Future[List[Int]] =
called += 1
super.getScipers()
(new Problem1Once).leaderboard().map { grades =>
assert(grades.size >= 176)
assert(called == 1)
}
}
package midterm23
import instrumentation.*
import java.util.concurrent.atomic.AtomicInteger
class BarberShopSolutionTest extends munit.FunSuite:
val implementations =
Map[String, (Int, Scheduler) => ScheduledBarberShopSolution](
"BarberShopSolution1" -> (new BarberShopSolution1(_)
with ScheduledBarberShopSolution(_)),
"BarberShopSolution2" -> (new BarberShopSolution2(_)
with ScheduledBarberShopSolution(_)),
"BarberShopSolution3" -> (new BarberShopSolution3(_)
with ScheduledBarberShopSolution(_))
)
for (name, makeShop) <- implementations do
for nCustomers <- 1 to 3 do
test(f"$name with $nCustomers customer(s)") {
testBarberShop(nCustomers, makeShop)
}
package midterm23
import instrumentation.{MockedMonitor, Monitor, Scheduler}
import java.util.concurrent.ConcurrentLinkedQueue
import scala.jdk.CollectionConverters.IterableHasAsScala
import scala.annotation.tailrec
trait ScheduledBarberShopSolution(scheduler0: Scheduler)
extends AbstractBarberShopSolution:
enum Event:
case HairCut
case CustomerLeaves
val trace = ConcurrentLinkedQueue[Event]()
override def notifyBarber = scheduler0.exec { super.notifyBarber }(
"",
Some(res => f"read notifyBarber == $res")
)
override def notifyBarber_=(v: Boolean) =
scheduler0.exec { super.notifyBarber = v }(
f"write notifyBarber = $v"
)
override def notifyCustomer = scheduler0.exec { super.notifyCustomer }(
"",
Some(res => f"read notifyCustomer == $res")
)
override def notifyCustomer_=(v: Boolean) =
scheduler0.exec { super.notifyCustomer = v }(
f"write notifyCustomer = $v"
)
override def hairCut() =
log("hair cut")
trace.add(Event.HairCut)
def customerTrace(n: Int): Unit =
this.customer(n)
log("customer leaves")
trace.add(Event.CustomerLeaves)
private val _lockObj = new MockedMonitor:
override def scheduler: Scheduler = scheduler0
override def lockObj: Monitor = _lockObj
override def log(s: String): Unit = scheduler0.log(s)
def nHaircuts: Int =
trace.asScala.filter(_ == Event.HairCut).size
def customerLeftEarly(): Boolean =
@tailrec
def loop(it: Iterable[Event], n: Int): Boolean =
if it.isEmpty then false
else
val newN = it.head match
case Event.HairCut => n + 1
case Event.CustomerLeaves => n - 1
if newN < 0 then true
else loop(it.tail, newN)
loop(trace.asScala, 0)
package midterm23
import instrumentation.{Scheduler, TestHelper}
/** Tests a barber shop implementation.
*
* @param nCustomers
* The number of customers to simulate.
* @param makeShop
* A function that creates a barber shop given the number of customers and a
* scheduler.
* @param checkEarlyCustomer
* If true, checks that no customer left early, i.e. that there is always a
* number of terminated customer threads equal or less than to the number of
* haircuts done.
*/
def testBarberShop(
nCustomers: Int,
makeShop: (Int, Scheduler) => ScheduledBarberShopSolution,
checkEarlyCustomer: Boolean = true
) =
TestHelper.testManySchedules(
nCustomers + 1,
scheduler =>
val barberShop = makeShop(nCustomers, scheduler)
(
(() => barberShop.barber())
:: (for i <- 1 to nCustomers
yield () => barberShop.customerTrace(i)).toList,
results =>
if barberShop.nHaircuts != nCustomers then
(false, f"Unexpected number of hair cuts: ${barberShop.nHaircuts}")
else if checkEarlyCustomer && barberShop.customerLeftEarly() then
(false, f"A customer left early")
else
(true, "")
)
)