Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed generic type projection use #231

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.4.7
sbt.version=1.6.2
4 changes: 1 addition & 3 deletions src/main/scala/apps/convolution.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ object convolution {
matrix: Array[Array[Float]],
weights: Array[Float]
): (Array[Float], TimeSpan[Time.ms]) = {
val f = k.as[ScalaFunction `(`
Array[Array[Float]] `,` Array[Float]
`)=>` Array[Float]]
val f = k.as[In `=` Array[Array[Float]] `,` Array[Float], Out[Array[Float]]]
f(localSize, globalSize)(matrix `,` weights)
}
}
5 changes: 2 additions & 3 deletions src/main/scala/apps/gemv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ object gemv {
val localSize = cgo17_localSize
val globalSize = GlobalSize(M)

val run = kernel.as[ScalaFunction `(`
val run = kernel.as[In `=`
Int `,` Int `,` Array[Array[Float]] `,`
Array[Float] `,` Array[Float] `,` Float `,` Float
`)=>` Array[Float]]
Array[Float] `,` Array[Float] `,` Float `,` Float, Out[Array[Float]]]
run(localSize, globalSize)(N `,` M `,` mat `,` xs `,` ys `,` alpha `,` beta)
}
}
42 changes: 11 additions & 31 deletions src/main/scala/apps/harrisCornerDetection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -252,38 +252,28 @@ object harrisCornerDetection {
val localSize = LocalSize(1)
val globalSize = GlobalSize(H)

val fSx = sobelX.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]]
`)=>` Array[Float]]
val fSx = sobelX.as[In `=` Int `,` Int `,` Array[Array[Float]], Out[Array[Float]]]
val (ix, ixt) = as2DW(fSx(localSize, globalSize)(H `,` W `,` input))

val fSy = sobelY.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]]
`)=>` Array[Float]]
val fSy = sobelY.as[In `=` Int `,` Int `,` Array[Array[Float]], Out[Array[Float]]]
val (iy, iyt) = as2DW(fSy(localSize, globalSize)(H `,` W `,` input))

val fMul = mul.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]] `,` Array[Array[Float]]
`)=>` Array[Float]]
val fMul = mul.as[In `=` Int `,` Int `,` Array[Array[Float]] `,` Array[Array[Float]], Out[Array[Float]]]
val (ixx, ixxt) = as2DW(
fMul(localSize, globalSize)(H `,` W `,` ix `,` ix))
val (ixy, ixyt) = as2DW(
fMul(localSize, globalSize)(H `,` W `,` ix `,` iy))
val (iyy, iyyt) = as2DW(
fMul(localSize, globalSize)(H `,` W `,` iy `,` iy))

val fG = gaussian.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]]
`)=>` Array[Float]]
val fG = gaussian.as[In `=` Int `,` Int `,` Array[Array[Float]], Out[Array[Float]]]
val (sxx, sxxt) = as2DW(fG(localSize, globalSize)(H `,` W `,` ixx))
val (sxy, sxyt) = as2DW(fG(localSize, globalSize)(H `,` W `,` ixy))
val (syy, syyt) = as2DW(fG(localSize, globalSize)(H `,` W `,` iyy))

val fC = coarsity.as[ScalaFunction `(`
Int `,` Int `,`
val fC = coarsity.as[In `=` Int `,` Int `,`
Array[Array[Float]] `,` Array[Array[Float]] `,` Array[Array[Float]] `,`
Float
`)=>` Array[Float]]
Float, Out[Array[Float]]]
val (k, kt) =
fC(localSize, globalSize)(H `,` W `,` sxx `,` sxy `,` syy `,` kappa)

Expand Down Expand Up @@ -325,15 +315,11 @@ object harrisCornerDetection {
val localSize = LocalSize(1)
val globalSize = GlobalSize(H)

val fSxyM = sobelXYMuls.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]]
`)=>` Array[Float]]
val fSxyM = sobelXYMuls.as[In `=` Int `,` Int `,` Array[Array[Float]], Out[Array[Float]]]
def asIs[B] = as3D[Float, B](H, W)
val (is, ist) = asIs(fSxyM(localSize, globalSize)(H `,` W `,` input))

val fGC = gaussianCoarsity.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Array[Float]]] `,` Float
`)=>` Array[Float]]
val fGC = gaussianCoarsity.as[In `=` Int `,` Int `,` Array[Array[Array[Float]]] `,` Float, Out[Array[Float]]]
val (k, kt) = fGC(localSize, globalSize)(H `,` W `,` is `,` kappa)

(k, Seq("Ixx, Ixy, Iyy" -> ist, "K" -> kt))
Expand Down Expand Up @@ -362,15 +348,11 @@ object harrisCornerDetection {
val localSize = LocalSize(1)
val globalSize = GlobalSize(H)

val fSxy = sobelXY.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]]
`)=>` Array[Float]]
val fSxy = sobelXY.as[In `=` Int `,` Int `,` Array[Array[Float]], Out[Array[Float]]]
def asIs[B] = as3D[Float, B](H, W)
val (is, ist) = asIs(fSxy(localSize, globalSize)(H `,` W `,` input))

val fMGC = mulGaussianCoarsity.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Array[Float]]] `,` Float
`)=>` Array[Float]]
val fMGC = mulGaussianCoarsity.as[In `=` Int `,` Int `,` Array[Array[Array[Float]]] `,` Float, Out[Array[Float]]]
val (k, kt) = fMGC(localSize, globalSize)(H `,` W `,` is `,` kappa)

(k, Seq("Ix, Iy" -> ist, "K" -> kt))
Expand All @@ -396,9 +378,7 @@ object harrisCornerDetection {
val localSize = LocalSize(1)
val globalSize = GlobalSize(H)

val f = sobelXYMulGaussianCoarsity.as[ScalaFunction `(`
Int `,` Int `,` Array[Array[Float]] `,` Float
`)=>` Array[Float]]
val f = sobelXYMulGaussianCoarsity.as[In `=` Int `,` Int `,` Array[Array[Float]] `,` Float, Out[Array[Float]]]
val (k, kt) = f(localSize, globalSize)(H `,` W `,` input `,` kappa)

(k, Seq("K" -> kt))
Expand Down
4 changes: 1 addition & 3 deletions src/main/scala/apps/kmeans.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ object kmeans {
val localSize = LocalSize(256)
val globalSize = GlobalSize(P)

val f = k.as[ScalaFunction `(`
Int `,` Int `,` Int `,` Array[Array[Float]] `,` Array[Array[Float]]
`)=>` Array[Int]]
val f = k.as[In `=` Int `,` Int `,` Int `,` Array[Array[Float]] `,` Array[Array[Float]], Out[Array[Int]]]
f(localSize, globalSize)(P `,` C `,` F `,` features `,` clusters)
}
}
4 changes: 1 addition & 3 deletions src/main/scala/apps/mm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ object mm {
val N = At(0).length
val M = B(0).length

val run = kernel.as[ScalaFunction `(`
Int `,` Int `,` Int `,` Array[Array[Float]] `,` Array[Array[Float]]
`)=>` Array[Float]]
val run = kernel.as[In `=` Int `,` Int `,` Int `,` Array[Array[Float]] `,` Array[Array[Float]], Out[Array[Float]]]
run(localSize, globalSize)(N `,` M `,` O `,` At `,` B)
}
}
6 changes: 3 additions & 3 deletions src/main/scala/apps/molecularDynamics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ object molecularDynamics {
val localSize = LocalSize(128)
val globalSize = GlobalSize(N)

val f = k.as[ScalaFunction `(`
val f = k.as[In `=`
Int `,` Int `,`
Array[Float] `,` Array[Array[Int]] `,`
Float `,` Float `,` Float
`)=>` Array[Float]]
Float `,` Float `,` Float,
Out[Array[Float]]]
f(localSize, globalSize)(
N `,` M `,` particles `,` neighbours `,` cutsq `,` lj1 `,` lj2
)
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/apps/mriQ.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ object mriQ {
val localSize = LocalSize(256)
val globalSize = GlobalSize(K)

val f = k.as[ScalaFunction `(`
Int `,` Array[Float] `,` Array[Float]
`)=>` Array[Float]]
val f = k.as[In `=`
Int `,` Array[Float] `,` Array[Float],
Out[Array[Float]]]
f(localSize, globalSize)(K `,` phiR `,` phiI)
}

Expand Down Expand Up @@ -214,11 +214,11 @@ object mriQ {
val localSize = LocalSize(256 / 4)
val globalSize = GlobalSize(X)

val f = k.as[ScalaFunction `(`
val f = k.as[In `=`
Int `,` Int `,`
Array[Float] `,` Array[Float] `,` Array[Float] `,`
Array[Float] `,` Array[Float] `,` Array[Float]
`)=>` Array[Float]]
Array[Float] `,` Array[Float] `,` Array[Float],
Out[Array[Float]]]
f(localSize, globalSize)(
K `,` X `,` x `,` y `,` z `,` Qr `,` Qi `,` kvalues
)
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/apps/nbody.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ object nbody {
assert(pos.length % 4 == 0)
val N = pos.length / 4

val f = k.as[ScalaFunction `(`
Int `,` Array[Float] `,` Array[Float] `,` Float `,` Float
`)=>` Array[Float]]
val f = k.as[In `=`
Int `,` Array[Float] `,` Array[Float] `,` Float `,` Float,
Out[Array[Float]]]
f(localSize, globalSize)(N `,` pos `,` vel `,` espSqr `,` deltaT)
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/apps/nearestNeighbour.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ object nearestNeighbour {
val localSize = LocalSize(128)
val globalSize = GlobalSize(N)

val f = k.as[ScalaFunction `(`
Int `,` Array[Float] `,` Float `,` Float
`)=>` Array[Float]]
val f = k.as[In `=`
Int `,` Array[Float] `,` Float `,` Float,
Out[Array[Float]]]
f(localSize, globalSize)(N `,` locations `,` lat `,` lng)
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/apps/sgemm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ object sgemm {
alpha: Float, beta: Float,
M: Int, N: Int, K: Int): (Array[Float], TimeSpan[Time.ms]) = {

val runKernel = kernel.as[ScalaFunction `(`
val runKernel = kernel.as[In `=`
Int `,` Int `,` Int `,`
Array[Array[Float]] `,`
Array[Array[Float]] `,`
Array[Array[Float]] `,`
Float `,`
Float `)=>` Array[Float]]
Float, Out[Array[Float]]]

runKernel(N `,` M `,` K `,` A `,` B `,` C `,` alpha `,` beta)
}
Expand Down
29 changes: 13 additions & 16 deletions src/main/scala/shine/OpenCL/KernelExecutor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ object KernelExecutor {
sealed case class KernelWithSizes(ktu: KernelModule,
localSize: LocalSize,
globalSize: GlobalSize) {
def as[F <: FunctionHelper](implicit ev: F#T <:< HList): F#T => (F#R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu).as[F](localSize, globalSize)
def as[T, R](implicit ev: T <:< HList): T => (R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu).as[T, R](localSize, globalSize)
}

def code: String = util.gen.opencl.kernel.asString(ktu)
Expand All @@ -31,18 +31,15 @@ object KernelExecutor {
}

sealed case class KernelNoSizes(ktu: KernelModule) {
//noinspection TypeAnnotation
def as[F <: FunctionHelper](implicit ev: F#T <:< HList): Object {
def apply(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[ms])

def withSizes(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[ms])
} = new {
def apply(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu).as[F](localSize, globalSize)
def as[T, R](implicit ev: T <:< HList): AS[T, R] = AS()

case class AS[T, R]()(implicit ev: T <:< HList) {
def apply(localSize: LocalSize, globalSize: GlobalSize): T => (R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu).as[T, R](localSize, globalSize)
}

def withSizes(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu).as[F](localSize, globalSize)
def withSizes(localSize: LocalSize, globalSize: GlobalSize): T => (R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu).as[T, R](localSize, globalSize)
}
}

Expand Down Expand Up @@ -83,9 +80,9 @@ object KernelExecutor {
val kernelF = kernel.as[ScalaFunction`(`Array[Float]`)=>`Array[Float]]
val (result, time) = kernelF(xs `;`)
*/
def as[F <: FunctionHelper](localSize: LocalSize, globalSize: GlobalSize)
(implicit ev: F#T <:< HList): F#T => (F#R, TimeSpan[Time.ms]) = {
hArgs: F#T => {
def as[T, R](localSize: LocalSize, globalSize: GlobalSize)
(implicit ev: T <:< HList): T => (R, TimeSpan[Time.ms]) = {
hArgs: T => {
val args: List[Any] = hArgs.toList
assert(kernel.inputParams.length == args.length)

Expand Down Expand Up @@ -141,7 +138,7 @@ object KernelExecutor {
kernelArgs.toArray
)

val output = castToOutputType[F#R](outputParam._2.typ, outputArg)
val output = castToOutputType[R](outputParam._2.typ, outputArg)

kernelArgs.foreach(_.dispose)
kernelJNI.dispose()
Expand Down
15 changes: 4 additions & 11 deletions src/main/scala/shine/OpenCL/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ package object OpenCL {
BuiltInFunctionCall("get_group_id", param, ContinuousRange(0, get_num_groups(param)))
}

trait FunctionHelper {
type T
type R
type F = T => R
}


type `)=>`[TT, RR] = FunctionHelper { type T = TT; type R = RR }

sealed trait HList {
def length: Int
def toList: List[Any]
Expand All @@ -85,13 +76,15 @@ package object OpenCL {

type `,`[L <: HList, N] = HCons[L, N]

type `(`[L <: ScalaFunction, N] = HCons[L, N]
type `=`[L <: In, N] = HCons[L, N]

implicit class HNilHelper[V](v1: V) {
def `,`[V2](v2: V2): HCons[HCons[HNil.type, V], V2] = HCons(HCons(HNil, v1), v2)

def `;`: HCons[HNil.type, V] = HCons(HNil, v1)
}

type ScalaFunction = HNil.type
type In = HNil.type

type Out[R] = R
}
31 changes: 14 additions & 17 deletions src/main/scala/shine/cuda/KernelExecutor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import shine.C.AST.ParamKind
import shine.DPIA.Types._
import shine.DPIA._
import shine.OpenCL
import shine.OpenCL.{FunctionHelper, GlobalSize, HList, LocalSize, NDRange, get_global_size, get_local_size, get_num_groups}
import shine.OpenCL.{GlobalSize, HList, LocalSize, NDRange, get_global_size, get_local_size, get_num_groups}
import shine.cuda.AST.Kernel
import util.Time.ms
import util.{Time, TimeSpan}
Expand All @@ -29,8 +29,8 @@ object KernelExecutor {
localSize: LocalSize,
globalSize: GlobalSize,
compilerOptions: List[String] = List.empty) {
def as[F <: FunctionHelper](implicit ev: F#T <:< HList): F#T => (F#R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu, compilerOptions).as[F](localSize, globalSize)
def as[T, R](implicit ev: T <:< HList): T => (R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu, compilerOptions).as[T, R](localSize, globalSize)
}

def benchmark(creator: KernelArgCreator, numberOfIterations: Integer, dataSizesBytes: Array[Long]) : BenchmarkResult =
Expand All @@ -43,18 +43,15 @@ object KernelExecutor {

sealed case class KernelNoSizes(ktu: KernelModule,
compilerOptions: List[String] = List.empty) {
//noinspection TypeAnnotation
def as[F <: FunctionHelper](implicit ev: F#T <:< HList): Object {
def apply(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[ms])

def withSizes(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[ms])
} = new {
def apply(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu, compilerOptions).as[F](localSize, globalSize)
def as[T, R](implicit ev: T <:< HList): AS[T, R] = AS()

case class AS[T, R]()(implicit ev: T <:< HList) {
def apply(localSize: LocalSize, globalSize: GlobalSize): T => (R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu, compilerOptions).as[T, R](localSize, globalSize)
}

def withSizes(localSize: LocalSize, globalSize: GlobalSize): F#T => (F#R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu, compilerOptions).as[F](localSize, globalSize)
def withSizes(localSize: LocalSize, globalSize: GlobalSize): T => (R, TimeSpan[Time.ms]) = {
FromKernelModule(ktu, compilerOptions).as[T, R](localSize, globalSize)
}
}

Expand Down Expand Up @@ -110,9 +107,9 @@ object KernelExecutor {
* val kernelF = kernel.as[ScalaFunction`(`Array[Float]`)=>`Array[Float]]
* val (result, time) = kernelF(xs `;`)
*/
def as[F <: FunctionHelper](localSize: LocalSize, globalSize: GlobalSize)
(implicit ev: F#T <:< HList): F#T => (F#R, TimeSpan[Time.ms]) = {
hArgs: F#T => {
def as[T, R](localSize: LocalSize, globalSize: GlobalSize)
(implicit ev: T <:< HList): T => (R, TimeSpan[Time.ms]) = {
hArgs: T => {
val args: List[Any] = hArgs.toList
assert(kernel.inputParams.length == args.length)

Expand Down Expand Up @@ -146,7 +143,7 @@ object KernelExecutor {

val dt = outputParam._2.typ
assert(dt.isInstanceOf[ArrayType] || dt.isInstanceOf[DepArrayType])
val output = asArray[F#R](getOutputType(dt), outputArg)
val output = asArray[R](getOutputType(dt), outputArg)

dispose(kernelArgs)

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/apps/asum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class asum extends test_util.TestsWithExecutor {
)(n: Int, input: Array[Float]): Array[Float] = {
import shine.OpenCL._
val k = gen.opencl.kernel.fromExpr(kernel)
val runKernel = k.as[ScalaFunction `(` Int `,` Array[Float] `)=>` Array[Float]]
val runKernel = k.as[In `=` Int `,` Array[Float], Out[Array[Float]]]
val (output, _) = runKernel(localSize, globalSize)(n `,` input)
output
}
Expand Down
Loading