Skip to content

Commit

Permalink
Bound check experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Nov 6, 2023
1 parent 3c93c7a commit 72f6505
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 167 deletions.
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ lazy val core = crossProject(
NativePlatform
).crossType(CrossType.Full)
.settings(
description := "Dyanmic extensions for slash",
libraryDependencies ++= Seq("ai.dragonfly" %%% "slash" % "0.1")
)
.jvmSettings(
)
Expand Down
27 changes: 15 additions & 12 deletions core/shared/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

package vexct
import vexct.Limits.Limit
import vexct.Retentions.Retention
package vecxt
import vecxt.Limits.Limit
import vecxt.Retentions.Retention

import scala.util.chaining.*
import vecxt.BoundsCheck


enum LossCalc:
case Agg, Occ
Expand Down Expand Up @@ -56,7 +58,7 @@ end extension

extension (vec: Array[Double])

def idx(index: Array[Boolean]) =
def idxBoolean(index: Array[Boolean]) =
val trues = index.countTrue
val newVec = new Array[Double](trues)
var j = 0
Expand All @@ -67,7 +69,7 @@ extension (vec: Array[Double])
j += 1
end for
newVec
end idx
end idxBoolean

def increments: Array[Double] =
val out = new Array[Double](vec.length)
Expand Down Expand Up @@ -99,15 +101,16 @@ extension (vec: Array[Double])
sum
end sum

def cumsum =
inline def cumsum =
var i = 1
while i < vec.length do
vec(i) = vec(i - 1) + vec(i)
i = i + 1
end while
end cumsum

def -(vec2: Array[Double]) =
inline def -(vec2: Array[Double])(using inline boundsCheck : BoundsCheck) =
dimCheck(vec, vec2)
val out = new Array[Double](vec.length)
var i = 0
while i < vec.length do
Expand All @@ -117,15 +120,15 @@ extension (vec: Array[Double])
out
end -

def -=(vec2: Array[Double]): Unit =
inline def -=(vec2: Array[Double]): Unit =
var i = 0
while i < vec.length do
vec(i) = vec(i) - vec2(i)
i = i + 1
end while
end -=

def +(vec2: Array[Double]) =
inline def +(vec2: Array[Double]) =
val out = new Array[Double](vec.length)
var i = 0
while i < vec.length do
Expand All @@ -135,15 +138,15 @@ extension (vec: Array[Double])
out
end +

def +=(vec2: Array[Double]): Unit =
inline def +=(vec2: Array[Double]): Unit =
var i = 0
while i < vec.length do
vec(i) = vec(i) + vec2(i)
i = i + 1
end while
end +=

def *=(d: Double) =
inline def *=(d: Double) =
var i = 0
while i < vec.length do
vec(i) = vec(i) * d
Expand All @@ -152,7 +155,7 @@ extension (vec: Array[Double])
vec
end *=

def *(d: Double) =
inline def *(d: Double) =
val out = new Array[Double](vec.length)
var i = 0
while i < vec.length do
Expand Down
16 changes: 16 additions & 0 deletions core/shared/src/main/scala/vecxt/bounds.check.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package vecxt

// opaque type BoundsCheck = Boolean

// object BoundsCheck:
// inline def apply(inline d: Boolean): BoundsCheck = d
// end BoundsCheck

// extension (inline bc: BoundsCheck) inline def value: Boolean = bc.self

type BoundsCheck = Boolean

object BoundsCheck:
inline given yes : BoundsCheck = true
inline given no : BoundsCheck = false
end BoundsCheck
7 changes: 4 additions & 3 deletions core/shared/src/main/scala/vecxt/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package vecxt

object dimCheck:
inline def apply[A, B](a: Array[A], b : Array[B]) =
if a.length != b.length then throw UnsupportedVectorDimension(a.length, b.length)
inline def apply[A, B](a: Array[A], b : Array[B]) (using inline doCheck: BoundsCheck)=
inline if doCheck then
if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)

case class UnsupportedVectorDimension(givenDimension:Int, requiredDimension:Int) extends Exception(
case class VectorDimensionMismatch(givenDimension:Int, requiredDimension:Int) extends Exception(
s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ."
)
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/vecxt/limit.rpt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package vexct
package vecxt

import vexct.Retentions.*
import vecxt.Retentions.*

object Limits:
opaque type Limit = Double
Expand Down
11 changes: 11 additions & 0 deletions core/shared/src/main/scala/vecxt/main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package vecxt


@main def checkBytecode =
val a = Array[Double](1,2,3)
val a1 = Array[Double](1,2,3)
// val b = Array[Boolean](true, false, true)
// val c = Array[Boolean](false, true, true)

import vecxt.BoundsCheck.yes
a - a1
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/vecxt/retention.rpt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package vexct
package vecxt

object Retentions:
opaque type Retention = Double
Expand Down
5 changes: 3 additions & 2 deletions tests/shared/src/test/scala/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/

package vexct
package vecxt

import scala.util.chaining.*

class ArrayExtensionSuite extends munit.FunSuite:
import vecxt.BoundsCheck.yes

lazy val v_fill = Array.tabulate(5)(i => i.toDouble)

Expand Down Expand Up @@ -142,7 +143,7 @@ class ArrayExtensionSuite extends munit.FunSuite:
test("Array indexing") {
val v1 = Array[Double](1.0, 2.0, 3.0)
val vIdx = Array[Boolean](true, false, true)
val afterIndex = v1.idx(vIdx)
val afterIndex = v1.idxBoolean(vIdx)

assertEquals(afterIndex.length, 2)
assertEqualsDouble(afterIndex.head, 1, 0.0001)
Expand Down
34 changes: 34 additions & 0 deletions tests/shared/src/test/scala/boundsCheck.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2023 quafadas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vecxt

import scala.util.chaining.*

class BoundsCheckSuite extends munit.FunSuite:


lazy val v_fill = Array.tabulate(5)(i => i.toDouble)

test("Bounds check") {
intercept[vecxt.VectorDimensionMismatch](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.yes))
}

test("no bound check") {
intercept[java.lang.ArrayIndexOutOfBoundsException](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.no))
}

end BoundsCheckSuite
62 changes: 0 additions & 62 deletions tests/shared/src/test/scala/dynamic.scala

This file was deleted.

82 changes: 0 additions & 82 deletions tests/shared/src/test/scala/index.scala

This file was deleted.

2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/rpt.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package vexct
package vecxt

import Retentions.*
import Limits.*
Expand Down

0 comments on commit 72f6505

Please sign in to comment.