Skip to content

Commit 6a5d414

Browse files
committed
More review fixes.
1 parent 79b5339 commit 6a5d414

File tree

15 files changed

+107
-144
lines changed

15 files changed

+107
-144
lines changed

cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@ import io.computenode.cyfra.core.layout.{Layout, LayoutStruct}
44
import io.computenode.cyfra.dsl.Value
55
import io.computenode.cyfra.dsl.Value.FromExpr
66
import io.computenode.cyfra.dsl.binding.GBuffer
7+
import io.computenode.cyfra.dsl.struct.GStruct
78
import izumi.reflect.Tag
89

910
import java.nio.ByteBuffer
1011

1112
trait Allocation:
1213
extension [R, T <: Value](buffer: GBuffer[T])
13-
def read(bb: ByteBuffer): Unit
14-
15-
def write(bb: ByteBuffer): Unit
16-
17-
extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL])
18-
def execute(params: Params, layout: L): RL
19-
20-
object Allocation:
21-
22-
trait InitAlloc:
23-
extension (buffers: GBuffer.type)
24-
def apply[T <: Value : Tag : FromExpr](size: Int): GBuffer[T]
25-
26-
def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GBuffer[T]
27-
28-
trait FinalizeAlloc:
29-
extension [T <: Value](buffer: GBuffer[T])
30-
def readTo(bb: ByteBuffer): Unit
14+
def read(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit
15+
16+
def write(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit
17+
18+
extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL
19+
20+
extension (buffers: GBuffer.type)
21+
def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T]
22+
23+
def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T]
24+
25+
extension (structs: GStruct.type) def apply[T <: GStruct[T]: Tag: FromExpr](buff: ByteBuffer): GStruct[T]
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package io.computenode.cyfra.core
22

3-
import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc}
3+
import io.computenode.cyfra.core.Allocation
44

55
trait CyfraRuntime:
6-
6+
77
def allocation(): Allocation
8-
9-
def initAlloc(allocation: Allocation): InitAlloc
10-
11-
def finalizeAlloc(allocation: Allocation): FinalizeAlloc
12-
13-

cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.computenode.cyfra.core
22

3-
import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc}
3+
import io.computenode.cyfra.core.Allocation
44
import io.computenode.cyfra.core.GProgram.BufferSizeSpec
55
import io.computenode.cyfra.core.layout.{Layout, LayoutStruct}
66
import io.computenode.cyfra.dsl.Value
@@ -14,7 +14,7 @@ sealed trait GBufferRegion[ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout:
1414
val initAlloc: ReqAlloc
1515

1616
object GBufferRegion:
17-
17+
1818
def allocate[Alloc <: Layout: LayoutStruct]: GBufferRegion[Alloc, Alloc] =
1919
AllocRegion(summon[LayoutStruct[Alloc]].layoutRef)
2020

@@ -31,11 +31,10 @@ object GBufferRegion:
3131
def map[NewAlloc <: Layout: LayoutStruct](f: Allocation ?=> ResAlloc => NewAlloc): GBufferRegion[ReqAlloc, NewAlloc] =
3232
MapRegion(region, (alloc: Allocation) => (resAlloc: ResAlloc) => f(using alloc)(resAlloc))
3333

34-
def runUnsafe(init: InitAlloc ?=> ReqAlloc, onDone: FinalizeAlloc ?=> ResAlloc => Unit)(using cyfraRuntime: CyfraRuntime): Unit =
34+
def runUnsafe(init: Allocation ?=> ReqAlloc, onDone: Allocation ?=> ResAlloc => Unit)(using cyfraRuntime: CyfraRuntime): Unit =
3535
val allocation = cyfraRuntime.allocation()
36-
val initAlloc = cyfraRuntime.initAlloc(allocation)
37-
init(using initAlloc)
38-
36+
init(using allocation)
37+
3938
val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]):
4039
case _: AllocRegion[?] => None
4140
case MapRegion(req, f) =>
@@ -44,5 +43,4 @@ object GBufferRegion:
4443
val bodyAlloc = steps.foldLeft[Layout](region.initAlloc): (acc, step) =>
4544
step(allocation)(acc)
4645

47-
val finalizeAlloc = cyfraRuntime.finalizeAlloc(allocation)
48-
onDone(using finalizeAlloc)(bodyAlloc.asInstanceOf[ResAlloc])
46+
onDone(using allocation)(bodyAlloc.asInstanceOf[ResAlloc])

cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,53 @@ import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.UniformStructRef
1010
import izumi.reflect.Tag
1111
import GExecution.*
1212

13-
trait GExecution[-Params, -L <: Layout, +RL <: Layout]:
13+
trait GExecution[-Params, -ExecLayout <: Layout, +ResLayout <: Layout]:
1414

15-
def flatMap[NRL <: Layout, NP <: Params, NL <: L](f: RL => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] =
15+
def flatMap[NRL <: Layout, NP <: Params, NL <: ExecLayout](f: ResLayout => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] =
1616
FlatMap(this, (p, r) => f(r))
1717

18-
def mapResult[NRL <: Layout](f: RL => NRL): GExecution[Params, L, NRL] =
18+
def map[NRL <: Layout](f: ResLayout => NRL): GExecution[Params, ExecLayout, NRL] =
1919
Map(this, f, identity, identity)
2020

21-
def contramapLayout[NL <: Layout](f: NL => L): GExecution[Params, NL, RL] =
21+
def contramap[NL <: Layout](f: NL => ExecLayout): GExecution[Params, NL, ResLayout] =
2222
Map(this, identity, f, identity)
2323

24-
def contramapParams[NP](f: NP => Params): GExecution[NP, L, RL] =
24+
def contramapParams[NP](f: NP => Params): GExecution[NP, ExecLayout, ResLayout] =
2525
Map(this, identity, identity, f)
2626

27-
def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, PL <: L, P <: GProgram[
28-
ProgramParams,
29-
ProgramLayout,
30-
]](program: P)(mapParams: PP => ProgramParams, mapLayout: PL => ProgramLayout): GExecution[PP, PL, ProgramLayout] =
31-
val adapted = program.contramapParams(mapParams).contramapLayout(mapLayout)
32-
flatMap(_ => adapted)
27+
def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, PL <: ExecLayout, P <: GProgram[ProgramParams, ProgramLayout]](
28+
program: P,
29+
)(mapParams: PP => ProgramParams, mapLayout: PL => ProgramLayout): GExecution[PP, PL, ResLayout] =
30+
val adapted = program.contramapParams(mapParams).contramap(mapLayout)
31+
flatMap(r => adapted.map(_ => r))
3332

3433
object GExecution:
3534

3635
def apply[Params, L <: Layout]() =
3736
Pure[Params, L, L]()
38-
39-
def forParams[Params, L <: Layout, RL <: Layout](
40-
f: Params => GExecution[Params, L, RL],
41-
): GExecution[Params, L, RL] =
42-
FlatMap[Params, L, RL, RL](
43-
Pure[Params, L, RL](),
44-
(params: Params, _: RL) => f(params),
45-
)
46-
37+
38+
def forParams[Params, L <: Layout, RL <: Layout](f: Params => GExecution[Params, L, RL]): GExecution[Params, L, RL] =
39+
FlatMap[Params, L, RL, RL](Pure[Params, L, RL](), (params: Params, _: RL) => f(params))
40+
4741
case class Pure[Params, L <: Layout, RL <: Layout]() extends GExecution[Params, L, RL]
48-
42+
4943
case class FlatMap[Params, L <: Layout, RL <: Layout, NRL <: Layout](
5044
execution: GExecution[Params, L, RL],
5145
f: (Params, RL) => GExecution[Params, L, NRL],
5246
) extends GExecution[Params, L, NRL]
53-
54-
47+
5548
case class Map[P, NP, L <: Layout, NL <: Layout, RL <: Layout, NRL <: Layout](
5649
execution: GExecution[P, L, RL],
5750
mapResult: RL => NRL,
5851
contramapLayout: NL => L,
5952
contramapParams: NP => P,
6053
) extends GExecution[NP, NL, NRL]:
6154

62-
override def mapResult[NNRL <: Layout](f: NRL => NNRL): GExecution[NP, NL, NNRL] =
55+
override def map[NNRL <: Layout](f: NRL => NNRL): GExecution[NP, NL, NNRL] =
6356
Map(execution, mapResult andThen f, contramapLayout, contramapParams)
64-
65-
override def contramapParams[NNP](f: NNP => NP): GExecution[NNP, NL, NRL] =
57+
58+
override def contramapParams[NNP](f: NNP => NP): GExecution[NNP, NL, NRL] =
6659
Map(execution, mapResult, contramapLayout, f andThen contramapParams)
67-
68-
override def contramapLayout[NNL <: Layout](f: NNL => NL): GExecution[NP, NNL, NRL] =
60+
61+
override def contramap[NNL <: Layout](f: NNL => NL): GExecution[NP, NNL, NRL] =
6962
Map(execution, mapResult, f andThen contramapLayout, contramapParams)

cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import io.computenode.cyfra.core.layout.Layout
66

77
import java.nio.ByteBuffer
88
import GProgram.*
9-
import io.computenode.cyfra.dsl.Value
10-
import io.computenode.cyfra.dsl.Value.FromExpr
11-
import io.computenode.cyfra.dsl.binding.{GBuffer, GUniform}
9+
import io.computenode.cyfra.dsl.{Expression, Value}
10+
import io.computenode.cyfra.dsl.Value.{FromExpr, Int32}
11+
import io.computenode.cyfra.dsl.binding.{GBinding, GBuffer, GUniform}
1212
import io.computenode.cyfra.dsl.struct.GStruct
1313
import io.computenode.cyfra.dsl.struct.GStruct.Empty
1414
import izumi.reflect.Tag
@@ -27,14 +27,14 @@ object GProgram:
2727

2828
sealed trait ProgramDispatch
2929

30-
case class DynamicDispatch[L <: Layout](buffer: GBuffer[?], offset: Int) extends ProgramDispatch
30+
case class DynamicDispatch[L <: Layout](buffer: GBinding, offset: Int) extends ProgramDispatch
3131

3232
case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch
3333

3434
private[cyfra] case class BufferSizeSpec[T <: Value: Tag: FromExpr](size: Int) extends GBuffer[T]
35-
35+
3636
private[cyfra] case class ParamUniform[T <: GStruct[T]: Tag: FromExpr](value: T) extends GUniform[T]
37-
37+
3838
private[cyfra] case class DynamicUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T]
3939

4040
trait InitProgramLayout:
@@ -45,13 +45,21 @@ object GProgram:
4545
extension (uniforms: GUniform.type)
4646
def apply[T <: GStruct[T]: Tag: FromExpr](value: T): GUniform[T] =
4747
ParamUniform[T](value)
48-
48+
4949
def apply[T <: GStruct[T]: Tag: FromExpr](): GUniform[T] =
5050
DynamicUniform[T]()
51-
51+
5252
def apply[Params, L <: Layout: LayoutStruct](
5353
layout: InitProgramLayout ?=> Params => L,
5454
dispatch: (L, Params) => ProgramDispatch,
5555
workgroupSize: WorkDimensions = (128, 1, 1),
5656
)(body: L => GIO[?]): GProgram[Params, L] =
5757
new GProgram(body, s => layout(using s), dispatch, workgroupSize)
58+
59+
case class Write(buffer: GBuffer[?], index: Int, value: Any)
60+
61+
case class Read(buffer: GBuffer[?], index: Int)
62+
63+
case class SimulationResult(invocs: List[InvocationSimResult])
64+
65+
case class InvocationSimResult(invocId: Int, instructions: List[Expression[_]], writes: List[Write], read: List[Read])

cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,3 @@ import io.computenode.cyfra.dsl.Value
44
import io.computenode.cyfra.dsl.binding.GBuffer
55

66
trait Layout
7-
8-

cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ object LayoutStruct:
3434
if !fieldTypes.forall(_ <:< TypeRepr.of[GBinding]) then
3535
report.errorAndAbort("LayoutStruct can only be derived for case classes with GBinding elements")
3636

37-
val valueTypes = fieldTypes.map:
38-
ftype => (ftype, ftype.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type")))
37+
val valueTypes = fieldTypes.map: ftype =>
38+
(ftype, ftype.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type")))
3939

4040
// summon izumi tags
4141
val typeGivens = valueTypes.map:
@@ -50,7 +50,7 @@ object LayoutStruct:
5050
case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}"),
5151
Expr.summon[FromExpr[t]] match
5252
case Some(fromExpr) => fromExpr
53-
case None => report.errorAndAbort(s"Cannot summon FromExpr for type ${tpe.show}")
53+
case None => report.errorAndAbort(s"Cannot summon FromExpr for type ${tpe.show}"),
5454
)
5555

5656
val buffers = typeGivens.zipWithIndex.map:

cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import izumi.reflect.Tag
77

88
trait GBuffer[T <: Value: FromExpr: Tag] extends GBinding:
99
def read(index: Int32): T = FromExpr.fromExpr(ReadBuffer(this, index))
10-
10+
1111
def write(index: Int32, value: T): GIO[Unit] = GIO.write(this, index, value)
1212

13-
object GBuffer
13+
object GBuffer

cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import izumi.reflect.Tag
99

1010
trait GUniform[T <: Value: Tag: FromExpr] extends GBinding:
1111
def read: T = fromExpr(ReadUniform(this))
12-
12+
1313
def write(value: T): GIO[Unit] = WriteUniform(this, value)
1414

1515
object GUniform:
16-
16+
1717
case class ParamUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T]
18-
19-
def fromParams[T <: GStruct[T]: Tag: FromExpr] = ParamUniform[T]()
18+
19+
def fromParams[T <: GStruct[T]: Tag: FromExpr] = ParamUniform[T]()

cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package io.computenode.cyfra.dsl.binding
33
import io.computenode.cyfra.dsl.{Expression, Value}
44
import izumi.reflect.Tag
55

6-
case class ReadUniform[T <: Value : Tag](uniform: GUniform[T]) extends Expression[T]
6+
case class ReadUniform[T <: Value: Tag](uniform: GUniform[T]) extends Expression[T]

0 commit comments

Comments
 (0)