Skip to content

Commit

Permalink
Merge pull request #32 from Chymyst/feature/no-unit-arg
Browse files Browse the repository at this point in the history
Get rid of assumed unit argument in molecules
  • Loading branch information
winitzki authored Dec 22, 2016
2 parents b5a3f5d + 96cb545 commit 2aa0782
Show file tree
Hide file tree
Showing 41 changed files with 1,157 additions and 986 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ There is an integer counter value, to which we have non-blocking access via `inc
We can also fetch the current counter value via the `get` molecule, which is blocking.
The counter is initialized to the number we specify.
```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

// Define the logic of the “non-blocking counter”.
Expand Down Expand Up @@ -238,7 +238,7 @@ The library offers some debugging facilities:
Here are the typical results:

```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

val counter = b[Int] // the name of this molecule is "counter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package code.winitzki.benchmark
import java.time.LocalDateTime

import code.winitzki.benchmark.Common._
import code.winitzki.jc.Pool
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import code.winitzki.jc.JoinRun._

object Benchmarks1 {

Expand Down Expand Up @@ -96,7 +95,7 @@ object Benchmarks1 {
}
j2.c(count)

(1 to count).foreach{ _ => j2.d() }
(1 to count).foreach{ _ => j2.d(()) }
j2.f(initialTime)
}

Expand All @@ -105,7 +104,7 @@ object Benchmarks1 {
val initialTime = LocalDateTime.now

val (d,_,f,_) = make_counter2a(count)
(1 to count).foreach{ _ => d() }
(1 to count).foreach{ _ => d(()) }
f(initialTime)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package code.winitzki.benchmark
import java.time.LocalDateTime

import code.winitzki.benchmark.Common._
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import code.winitzki.jc.Pool

object Benchmarks4 {
val differentReactions = 100
Expand Down Expand Up @@ -57,7 +56,7 @@ object Benchmarks4 {
case a2(n) => a3(n)
case a3(n) => a4(n)
case a4(n) => a5(n)
case a5(m) => if (m==0) f() else a0(m-1)
case a5(m) => if (m==0) f(()) else a0(m-1)
}
a0(count)

Expand Down Expand Up @@ -281,7 +280,7 @@ object Benchmarks4 {
// case a97(n) => a98(n)
// case a98(n) => a99(n)

case a99(m) => if (m==0) f() else a0(m-1)
case a99(m) => if (m==0) f(()) else a0(m-1)
}
a0(count)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package code.winitzki.benchmark
import java.time.LocalDateTime
import code.winitzki.benchmark.Common._
import code.winitzki.jc._
import code.winitzki.jc.JoinRun._
import code.winitzki.jc.Macros._

object Benchmarks7 {
Expand Down Expand Up @@ -54,11 +53,11 @@ object Benchmarks7 {
val initialTime = LocalDateTime.now
j8.all_done(count)
val d = make_counters8a(j8.done, numberOfCounters, count)
(1 to (count*numberOfCounters)).foreach{ _ => d() }
(1 to (count*numberOfCounters)).foreach{ _ => d(()) }
j8.f(initialTime)
}

def make_counters(done: M[Unit], counters: Int, init: Int, tp: Pool) = {
private def make_counters(done: E, counters: Int, init: Int, tp: Pool) = {
val c = m[Int]
val d = m[Unit]

Expand All @@ -71,13 +70,13 @@ object Benchmarks7 {
d
}

def make_counters8a(done: AsyName[Unit], counters: Int, init: Int): AsyName[Unit] = {
private def make_counters8a(done: AsyName[Unit], counters: Int, init: Int): AsyName[Unit] = {
object j8a extends Join {
object c extends AsyName[Int]
object d extends AsyName[Unit]

join {
case c(0) => done()
case c(0) => done(())
case c(n) and d(_) if n > 0 => c(n-1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package code.winitzki.benchmark
import java.time.LocalDateTime
import code.winitzki.benchmark.Common._
import code.winitzki.jc._
import code.winitzki.jc.JoinRun._
import code.winitzki.jc.Macros._
import scala.concurrent.duration._

object Benchmarks9 {

val numberOfCounters = 5

def make_counter_1(done: M[Unit], counters: Int, init: Int, tp: Pool): B[Unit,Unit] = {
def make_counter_1(done: M[Unit], counters: Int, init: Int, tp: Pool): EE = {
val c = m[Int]
val d = b[Unit, Unit]

site(tp)(
go { case c(0) => done() },
go { case c(0) => done(()) },
go { case c(n) + d(_, r) if n > 0 => c(n - 1); r() }
)
(1 to counters).foreach(_ => c(init))
Expand Down Expand Up @@ -47,7 +46,7 @@ object Benchmarks9 {
}


def make_ping_pong_stack(done: M[Unit], tp: Pool): B[Int,Int] = {
def make_ping_pong_stack(done: E, tp: Pool): B[Int,Int] = {
val c = m[Unit]
val d = b[Int, Int]
val e = b[Int, Int]
Expand Down Expand Up @@ -141,8 +140,8 @@ object Benchmarks9 {
object d extends SynName[Unit, Unit]

join {
case c(0) => done()
case c(n) and d(_) if n > 0 => c(n-1); d.reply()
case c(0) => done(())
case c(n) and d(_) if n > 0 => c(n-1); d.reply(())
}
}
import b9c1._
Expand Down Expand Up @@ -171,7 +170,7 @@ object Benchmarks9 {
all_done(numberOfCounters)

val d = make_counter_1_Jiansen(done, numberOfCounters, count)
(1 to (count*numberOfCounters)).foreach{ _ => d() }
(1 to (count*numberOfCounters)).foreach{ _ => d(()) }

var result = f(initialTime)
result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class AsyName[Arg](implicit owner: Join, argT:ClassTag[Arg]) extends NameBase{
* @param argT the descriptor the argument type
* @param resT the descriptor the return type
*/
class SynName[Arg, R](implicit owner: Join, argT:ClassManifest[Arg], resT:ClassManifest[R]) extends NameBase{
class SynName[Arg, R](implicit owner: Join, argT:ClassTag[Arg], resT:ClassTag[R]) extends NameBase{
// does not support subtyping

override def argTypeEqual(t:Any) :Boolean = t match {
Expand Down
11 changes: 5 additions & 6 deletions benchmark/src/main/scala/code/winitzki/benchmark/MainApp.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package code.winitzki.benchmark

import code.winitzki.benchmark.Benchmarks1._
import code.winitzki.benchmark.Benchmarks4._
import code.winitzki.benchmark.Benchmarks7._
import code.winitzki.benchmark.Benchmarks9._
import code.winitzki.jc.{FixedPool, Pool}
import code.winitzki.jc.JoinRun.{defaultSitePool, defaultReactionPool}
import Benchmarks1._
import Benchmarks4._
import Benchmarks7._
import Benchmarks9._
import code.winitzki.jc._

object MainAppConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ class JiansenFairnessSpec extends FlatSpec with Matchers with TimeLimitedTests {

join {
case getC(_) and done(arr) => getC.reply(arr)
case a0(_) and c((n, arr)) => if (n > 0) { arr(0) += 1; c((n-1,arr)); a0() } else done(arr)
case a1(_) and c((n, arr)) => if (n > 0) { arr(1) += 1; c((n-1,arr)); a1() } else done(arr)
case a2(_) and c((n, arr)) => if (n > 0) { arr(2) += 1; c((n-1,arr)); a2() } else done(arr)
case a3(_) and c((n, arr)) => if (n > 0) { arr(3) += 1; c((n-1,arr)); a3() } else done(arr)
case a0(_) and c((n, arr)) => if (n > 0) { arr(0) += 1; c((n-1,arr)); a0(()) } else done(arr)
case a1(_) and c((n, arr)) => if (n > 0) { arr(1) += 1; c((n-1,arr)); a1(()) } else done(arr)
case a2(_) and c((n, arr)) => if (n > 0) { arr(2) += 1; c((n-1,arr)); a2(()) } else done(arr)
case a3(_) and c((n, arr)) => if (n > 0) { arr(3) += 1; c((n-1,arr)); a3(()) } else done(arr)
}

}
j3.a0(); j3.a1(); j3.a2(); j3.a3()
j3.a0(())
j3.a1(())
j3.a2(())
j3.a3(())
j3.c((N, Array.fill[Int](reactions)(0)))

val result = j3.getC()
val result = j3.getC(())

// println(result.mkString(", "))

Expand Down Expand Up @@ -81,7 +84,7 @@ class JiansenFairnessSpec extends FlatSpec with Matchers with TimeLimitedTests {
Thread.sleep(200)
j4.c(cycles)

val result = j4.getC()
val result = j4.getC(())

// println(result.mkString(", "))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package code.winitzki.benchmark

import code.winitzki.jc.FixedPool
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import org.scalatest.{FlatSpec, Matchers}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package code.winitzki.benchmark

import code.winitzki.jc.FixedPool
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import org.scalatest.{FlatSpec, Matchers}
import Common._
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ val commonSettings = Defaults.coreDefaultSettings ++ Seq(
resolvers += "Typesafe releases" at "http://repo.typesafe.com/typesafe/releases",

scalacOptions ++= Seq( // https://tpolecat.github.io/2014/04/11/scalac-flags.html
// "-deprecation",
"-deprecation",
"-unchecked",
"-encoding", "UTF-8", // yes, this is 2 args
"-feature",
Expand Down
4 changes: 2 additions & 2 deletions docs/chymyst01.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ So far, we have been using a kind of chemistry-resembling pseudocode to illustra
This pseudocode was designed to prepare us for the actual syntax of `JoinRun`, which is only a little more verbose:

```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

// declare the molecule types
Expand Down Expand Up @@ -221,7 +221,7 @@ This automatically prevents race conditions with the counter: There is no possib
The code shown above will not print any output, so it is perhaps instructive to put some print statements into the reaction bodies.

```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

// declare the molecule emitters and the value types
Expand Down
2 changes: 1 addition & 1 deletion docs/chymyst02.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ go { case counter(0) + fetch(_, reply) => reply() }
Here is the complete code:

```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

import java.time.LocalDateTime.now
Expand Down
2 changes: 1 addition & 1 deletion docs/chymyst03.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ val fetch = new B[Unit, Int]("fetch")
This code is completely equivalent to the shorter code written using macros:

```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

val counter = m[Int]
Expand Down
2 changes: 1 addition & 1 deletion docs/chymyst04.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Here is the complete code for this example (see also `MapReduceSpec.scala` in th
We will apply the function `f(x) = x*x` to elements of an integer array, and then compute the sum of the resulting array of squares.

```scala
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._

object C extends App {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package code.winitzki.test

import code.winitzki.jc.FixedPool
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import org.scalatest.concurrent.TimeLimitedTests
import org.scalatest.time.{Millis, Span}
Expand All @@ -20,7 +19,7 @@ class DiningPhilosophersSpec extends FlatSpec with Matchers with TimeLimitedTest
diningPhilosophers(50)
}

def diningPhilosophers(cycles: Int) = {
private def diningPhilosophers(cycles: Int) = {

val tp = new FixedPool(8)

Expand All @@ -34,14 +33,14 @@ class DiningPhilosophersSpec extends FlatSpec with Matchers with TimeLimitedTest
val t3 = new M[Int]("Marx is thinking")
val t4 = new M[Int]("Russell is thinking")
val t5 = new M[Int]("Spinoza is thinking")
val f12 = new M[Unit]("f12")
val f23 = new M[Unit]("f23")
val f34 = new M[Unit]("f34")
val f45 = new M[Unit]("f45")
val f51 = new M[Unit]("f51")
val f12 = new E("f12")
val f23 = new E("f23")
val f34 = new E("f34")
val f45 = new E("f45")
val f51 = new E("f51")

val done = new M[Unit]("done")
val check = new B[Unit, Unit]("check")
val done = new E("done")
val check = new EE("check")

site(tp, tp) (
go { case t1(n) => rw(h1); h1(n - 1) },
Expand All @@ -62,13 +61,13 @@ class DiningPhilosophersSpec extends FlatSpec with Matchers with TimeLimitedTest
t1(cycles) + t2(cycles) + t3(cycles) + t4(cycles) + t5(cycles)
f12() + f23() + f34() + f45() + f51()

check() shouldEqual ()
check() shouldEqual (())

// stop the simulation: this should be in unit tests, not here
// not yet implemented
/*
val stop = ja[Unit]
val wait_for_stop = js[Unit,Unit]
val stop = m[Unit]
val wait_for_stop = b[Unit,Unit]
site( go { case stop(_) + wait_for_stop(_,r) => r() } )
wait_until_quiet(t1, stop)
wait_for_stop()
Expand Down
11 changes: 5 additions & 6 deletions joinrun/src/test/scala/code/winitzki/test/FairnessSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package code.winitzki.test

import code.winitzki.jc.FixedPool
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import org.scalatest.concurrent.TimeLimitedTests
import org.scalatest.time.{Millis, Span}
Expand All @@ -24,9 +23,9 @@ class FairnessSpec extends FlatSpec with Matchers with TimeLimitedTests {
val reactions = 4
val N = 1000

val c = new M[(Int, Array[Int])]("c")
val done = new M[Array[Int]]("done")
val getC = new B[Unit, Array[Int]]("getC")
val c = m[(Int, Array[Int])]
val done = m[Array[Int]]
val getC = b[Unit, Array[Int]]
val a0 = m[Unit]
val a1 = m[Unit]
val a2 = m[Unit]
Expand Down Expand Up @@ -142,7 +141,7 @@ class FairnessSpec extends FlatSpec with Matchers with TimeLimitedTests {

val tp = new FixedPool(8)

def makeRS(d1: M[Unit], d2: M[Unit]): (M[Unit],M[Unit],M[Unit]) = {
def makeRS(d1: E, d2: E): (E,E,E) = {
val a = m[Unit]
val b = m[Unit]
val c = m[Unit]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package code.winitzki.test
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

import code.winitzki.jc.FixedPool
import code.winitzki.jc.JoinRun._
import code.winitzki.jc._
import code.winitzki.jc.Macros._
import org.scalatest.{FlatSpec, Matchers}

Expand Down
Loading

0 comments on commit 2aa0782

Please sign in to comment.