Skip to content

Commit

Permalink
Validate actual values instead of hash
Browse files Browse the repository at this point in the history
Forks should not be owned by anyone and the philosophers
should have consumed the expected number of meals.

Also removes the extraneous run method.
  • Loading branch information
lbulej committed Jun 7, 2024
1 parent aded856 commit f0ffa52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.renaissance.scala.stm

import scala.collection.JavaConverters._
import org.renaissance.Benchmark
import org.renaissance.Benchmark._
import org.renaissance.BenchmarkContext
import org.renaissance.BenchmarkResult
import org.renaissance.BenchmarkResult.Validators
import org.renaissance.BenchmarkResult.Assert
import org.renaissance.License

@Name("philosophers")
Expand Down Expand Up @@ -34,20 +33,28 @@ final class Philosophers extends Benchmark {
*/
private var mealCountParam: Int = _

private var expectedHash: String = _

override def setUpBeforeAll(c: BenchmarkContext) = {
override def setUpBeforeAll(c: BenchmarkContext): Unit = {
threadCountParam = c.parameter("thread_count").toPositiveInteger
mealCountParam = c.parameter("meal_count").toPositiveInteger
val expectedOutput = Array.fill(threadCountParam)(mealCountParam).toSeq.asJava;
expectedHash = Validators.computeHash(expectedOutput);
}

private def validate(forkOwners: Seq[Option[String]], mealsEaten: Seq[Int]): Unit = {
// All forks should be available, i.e., not owned by anyone.
for (i <- 0 until threadCountParam) {
Assert.assertEquals(None, forkOwners(i), s"owner of fork %i")
}

// All philosophers should have eaten the expected number of meals.
for (i <- 0 until threadCountParam) {
Assert.assertEquals(mealCountParam, mealsEaten(i), s"meals eaten by philosopher $i")
}
}

override def run(c: BenchmarkContext): BenchmarkResult = {
val mealsEaten = RealityShowPhilosophers.run(mealCountParam, threadCountParam)
val (forkOwners, mealsEaten) = RealityShowPhilosophers.run(mealCountParam, threadCountParam)

() => {
Validators.hashing(expectedHash, mealsEaten.toSeq.asJava).validate()
validate(forkOwners, mealsEaten)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.renaissance.scala.stm

import scala.annotation.tailrec
import scala.collection._
import scala.collection.mutable
import scala.concurrent.stm._

/**
Expand Down Expand Up @@ -85,29 +85,23 @@ object RealityShowPhilosophers {
}
}

def eatenMeals(philosopherCount: Int, meals: Int): Array[Int] = {
def run(mealCount: Int, philosopherCount: Int): (Seq[Option[String]], Seq[Int]) = {
val names = for (i <- 0 until philosopherCount) yield {
s"philosopher-$i"
}
val forks = Array.tabulate(names.size) { _ =>
new Fork
}
val pthreads = Array.tabulate(names.size) { i =>
new PhilosopherThread(names(i), meals, forks(i), forks((i + 1) % forks.length))
new PhilosopherThread(names(i), mealCount, forks(i), forks((i + 1) % forks.length))
}
val camera = new CameraThread(1000 / 60, forks, pthreads)
camera.start()
for (t <- pthreads) t.start()
for (t <- pthreads) t.join()
camera.join()
atomic { implicit txn =>
pthreads.map { p =>
p.mealsEaten.get(txn)
}
(forks.map(_.owner.get), pthreads.map(_.mealsEaten.get))
}
}

def run(meals: Int, philosopherCount: Int) = {
eatenMeals(philosopherCount, meals)
}
}

0 comments on commit f0ffa52

Please sign in to comment.