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

Fox docs #20

Merged
merged 5 commits into from
Dec 15, 2024
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
10 changes: 9 additions & 1 deletion build.mill
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mill.scalalib.publish.Scope.Test

import $ivy.`com.github.lolgab::mill-crossplatform::0.2.4`
import $ivy.`io.github.quafadas:millSite_mill0.12_2.13:0.0.36`
import $ivy.`io.github.quafadas:millSite_mill0.12_2.13:0.0.38`
import $ivy.`de.tototec::de.tobiasroeser.mill.vcs.version::0.4.0`
import $ivy.`com.lihaoyi::mill-contrib-jmh:`
import $ivy.`com.goyeau::mill-scalafix::0.4.2`
Expand Down Expand Up @@ -178,6 +178,12 @@ object jsSite extends SiteJSModule {
override def moduleDeps = Seq(vecxt.js, vecxtensions.js)
override def scalaVersion = vecxt.js.scalaVersion
override def scalaJSVersion = vecxt.js.scalaJSVersion

override def scalaJsCompilerVersion: String = "3.6.2"


// override def allScalacOptions: T[Seq[String]] = super.allScalacOptions`() ++ Seq("-experimental", "-language:experimental.namedTuples")
override def scalacOptions: T[Seq[String]] = super.scalacOptions() ++ Seq("-experimental", "-language:experimental.namedTuples")
override def moduleKind = ModuleKind.ESModule
override def ivyDeps = super.ivyDeps() ++ Agg(
ivy"org.scala-js::scalajs-dom::2.8.0",
Expand Down Expand Up @@ -206,4 +212,6 @@ object site extends SiteModule {
override def moduleDeps = Seq(vecxt.jvm)
override def scalaDocOptions = super.scalaDocOptions

override def scalacOptions: T[Seq[String]] = super.scalacOptions() ++ Seq("-experimental", "-language:experimental.namedTuples")

}
2 changes: 2 additions & 0 deletions jsSite/src/BenchmarkPlots.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import scala.NamedTuple.AnyNamedTuple
import scala.annotation.meta.field
import upickle.default.*
import NamedTupleReadWriter.given
import scala.annotation.experimental

@experimental()
object BenchmarkPlots:
def addScalarBenchmark: String =
val thePlot = BenchmarkPlotElements.schema ++
Expand Down
4 changes: 2 additions & 2 deletions jsSite/src/PlotElements.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package vecxt.plot

import scala.NamedTuple.*
// import scala.NamedTuple.*
import scala.annotation.meta.field
import upickle.default.*
import NamedTupleReadWriter.given
import scala.collection.immutable.Stream.Empty

object BenchmarkPlotElements:
final val schema = (`$schema` = "https://vega.github.io/schema/vega-lite/v5.json")
final val schema: ($schema: String) = (`$schema` = "https://vega.github.io/schema/vega-lite/v5.json")

val fakeData = (
data = (
Expand Down
2 changes: 2 additions & 0 deletions jsSite/src/facade.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import org.scalajs.dom.Element
import scala.util.Random
import org.scalajs.dom.Element
import org.scalajs.dom.XMLHttpRequest
import scala.annotation.experimental

@experimental()
object showJsDocs:
def apply(path: String, node: Element, width: Int = 50) =
val child = dom.document.createElement("div")
Expand Down
1 change: 1 addition & 0 deletions site/docs/_docs/benchmarks/sum.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ And the function left in vexct over time (against regressions)
```scala mdoc:js sc:nocompile
import vecxt.plot.*
import vecxt.facades.*

showJsDocs.fromSpec(BenchmarkPlots.sumBenchmarkOverTime, node)
```

Expand Down
28 changes: 27 additions & 1 deletion site/docs/_docs/examples.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Vector Examples
---
# Vector Examples

Some basic exampeles.
Some basic examples with doubles.

```scala mdoc
import vecxt.all.*
Expand Down Expand Up @@ -33,3 +33,29 @@ cosineSimilarity(v1, v2)
(v1(v1 <= 2)).printArr

```
And Ints. Note that the API here is more limited at the moment.

```scala mdoc:reset
import vecxt.all.*
import narr.*
import vecxt.BoundsCheck.DoBoundsCheck.yes

val v1 = NArray(1, 2, 3)
val v2 = NArray(4, 5, 6)


v1.dot(v2)

(v1 + v2).printArr

(v1 - v2).printArr

(v1 > 2).printArr
(v1 >= 2).printArr

(v1 < 2).printArr
(v1 <= 2).printArr

(v1(v1 <= 2)).printArr

```
28 changes: 28 additions & 0 deletions vecxt/js-native/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import vecxt.BoundsCheck.BoundsCheck
import scala.math.Ordering

import narr.*
import scala.reflect.ClassTag

object JsNativeBooleanArrays:

Expand Down Expand Up @@ -129,6 +130,33 @@ object JsNativeDoubleArrays:
res
end -

inline def +(other: NArray[Int])(using inline boundsCheck: BoundsCheck): NArray[Int] =
dimCheck(vec, other)

val n = vec.length
val res = NArray.fill(n)(0)

var i = 0
while i < n do
res(i) = vec(i) + other(i)
i = i + 1
end while
res
end +

inline def dot(other: NArray[Int])(using inline boundsCheck: BoundsCheck): Int =
dimCheck(vec, other)
val n = vec.length
var sum = 0

var i = 0
while i < n do
sum += vec(i) * other(i)
i = i + 1
end while
sum
end dot

inline def <(num: Int): NArray[Boolean] =
logicalIdx((a, b) => a < b, num)

Expand Down
46 changes: 29 additions & 17 deletions vecxt/js/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import scala.util.chaining.*
import vecxt.BoundsCheck.BoundsCheck

import narr.*
import scala.reflect.ClassTag
import scala.scalajs.js.annotation.JSBracketAccess
import vecxt.JsNativeBooleanArrays.trues

object arrayUtil:
extension [A](d: Array[A]) def printArr: String = d.mkString("[", ",", "]")
Expand Down Expand Up @@ -52,12 +55,12 @@ object arrays:

extension [A](v: js.Array[A]) inline def fill(a: A): Unit = v.asInstanceOf[JsArrayFacade].fill(a)
extension (vec: js.Array[Boolean])
inline def countTrue: Int =
var sum = 0
for i <- 0 until vec.length do if vec(i) then sum = sum + 1
end for
sum
end countTrue
// inline def trues: Int =
// var sum = 0
// for i <- 0 until vec.length do if vec(i) then sum = sum + 1
// end for
// sum
// end trues

inline def &&(thatIdx: js.Array[Boolean]): js.Array[Boolean] =
val result = new js.Array[Boolean](vec.length)
Expand All @@ -72,28 +75,37 @@ object arrays:
end for
result
end ||

// def copy: Array[Boolean] =
// val copyOfThisVector: Array[Boolean] = new Array[Boolean](vec.length)
// var i = 0
// while i < vec.length do
// copyOfThisVector(i) = vec(i)
// i = i + 1
// end while
// copyOfThisVector
// end copy
end extension

extension (vec: NArray[Double])
inline def update(idx: Int, d: Double)(using inline boundsCheck: BoundsCheck.BoundsCheck): Unit =
indexCheck(vec, idx)
vec(idx) = d
end update

end extension

extension (vec: NArray[Int])

def apply(index: js.Array[Boolean]): NArray[Int] =
val truely = index.trues
val newVec = NArray.ofSize[Int](truely)
var j = 0
for i <- 0 until index.length do
// println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ")
if index(i) then
newVec(j) = vec(i)
j += 1
end for
newVec
end apply
end extension

extension (vec: NArray[Double])

inline def apply(index: js.Array[Boolean])(using inline boundsCheck: BoundsCheck.BoundsCheck) =
dimCheck(vec, index)
val trues = index.countTrue
val trues = index.trues
val newVec = Float64Array(trues)
var j = 0
for i <- 0 until index.length do
Expand Down
71 changes: 62 additions & 9 deletions vecxt/jvm/src/arrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import jdk.incubator.vector.ByteVector
import jdk.incubator.vector.DoubleVector
import jdk.incubator.vector.VectorOperators
import jdk.incubator.vector.IntVector
import scala.reflect.ClassTag

object arrays:

Expand Down Expand Up @@ -79,7 +80,6 @@ object arrays:
out
end any

// TODO this may be sub-optimal - we want to move the accumulator out of the hot loop.
inline def trues: Int =
var i = 0
var sum = 0
Expand Down Expand Up @@ -132,6 +132,7 @@ object arrays:
end extension

extension (vec: Array[Int])

inline def =:=(num: Int): Array[Boolean] =
logicalIdx(VectorOperators.EQ, num)

Expand Down Expand Up @@ -239,34 +240,83 @@ object arrays:
temp
end sum

inline def -(vec2: Array[Int])(using inline boundsCheck: BoundsCheck): Array[Int] =
inline def dot(vec2: Array[Int])(using inline boundsCheck: BoundsCheck): Int =
dimCheck(vec, vec2)
val newVec = Array.ofDim[Int](vec.length)
var i = 0
var acc = IntVector.zero(spi)

while i < spi.loopBound(vec.length) do
acc = IntVector
.fromArray(spi, vec, i)
.mul(IntVector.fromArray(spi, vec2, i))
.add(acc)

i += spil
end while

var temp = acc.reduceLanes(VectorOperators.ADD)

while i < vec.length do
temp += vec(i) * vec2(i)
i += 1
end while
temp
end dot

inline def -(vec2: Array[Int])(using inline boundsCheck: BoundsCheck): Array[Int] =
dimCheck(vec, vec2)
vec.clone.tap(_ -= vec2)
end -

inline def -=(vec2: Array[Int])(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0

while i < spi.loopBound(vec.length) do
IntVector
.fromArray(spi, vec, i)
.sub(IntVector.fromArray(spi, vec2, i))
.intoArray(newVec, i)
.intoArray(vec, i)
i += spil
end while

while i < vec.length do
newVec(i) = vec(i) - vec2(i)
vec(i) = vec(i) - vec2(i)
i += 1
end while
newVec
end -
end -=

end extension
inline def +(vec2: Array[Int])(using inline boundsCheck: BoundsCheck): Array[Int] =
dimCheck(vec, vec2)
vec.clone.tap(_ += vec2)
end +

extension (vec: Array[Double])
inline def +=(vec2: Array[Int])(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0

while i < spi.loopBound(vec.length) do
IntVector
.fromArray(spi, vec, i)
.add(IntVector.fromArray(spi, vec2, i))
.intoArray(vec, i)
i += spil
end while

while i < vec.length do
vec(i) = vec(i) + vec2(i)
i += 1
end while
end +=

end extension

extension [@specialized(Double, Int) A](vec: Array[A])(using ClassTag[A])
inline def apply(index: Array[Boolean])(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, index)
val trues = index.trues
val newVec: Array[Double] = new Array[Double](trues)
val newVec: Array[A] = new Array[A](trues)
var j = 0
for i <- 0 until index.length do
// println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ")
Expand All @@ -276,6 +326,9 @@ object arrays:
end for
newVec
end apply
end extension

extension (vec: Array[Double])

/** Apparently, left packing is hard problem in SIMD land.
* https://stackoverflow.com/questions/79025873/selecting-values-from-java-simd-doublevector
Expand Down
Loading
Loading