Skip to content

Commit ae60030

Browse files
committed
intersting
1 parent cf1f3e3 commit ae60030

8 files changed

Lines changed: 129 additions & 9 deletions

File tree

build.mill

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ trait Common extends ScalaModule with PublishModule with ScalafixModule {
6060
}
6161

6262
val vecIncubatorFlag = Seq(
63-
"""--add-modules=jdk.incubator.vector""",
64-
"-Ddev.ludovic.netlib.blas.nativeLibPath=/opt/homebrew/Cellar/openblas/0.3.30/lib/libopenblas.dylib"
63+
"""--add-modules=jdk.incubator.vector"""
6564
)
6665

6766
trait CommonJS extends ScalaJSModule {

experiments/package.mill

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@ object `package` extends RootModule with ScalaModule {
1111
super.compileResources() ++ resources()
1212
}
1313
def scalacOptions: T[Seq[String]] = Seq("-Xmax-inlines:10000")
14-
override def forkArgs: T[Seq[String]] = super.forkArgs() ++ build.vecIncubatorFlag
14+
override def forkArgs: T[Seq[String]] = super.forkArgs() ++ build.vecIncubatorFlag ++ Seq(
15+
"-Djava.library.path=/opt/homebrew/Cellar/blis/1.1/lib"
16+
)
1517
override def moduleDeps: Seq[JavaModule] = Seq(build.vecxt.jvm, build.generated)
1618

17-
override def mainClass = Some("testBlis")
19+
override def mainClass = Some("testBlisNative")
1820
override def ivyDeps = super.ivyDeps() ++ Seq(
1921
ivy"com.lihaoyi::os-lib::0.10.4",
2022
ivy"io.github.quafadas::scautable::0.0.24",
2123
ivy"io.github.quafadas::dedav4s::0.10.0-RC2"
2224

2325
)
2426

27+
object test extends ScalaTests with TestModule.Munit {
28+
def scalaVersion = build.vecxt.jvm.scalaVersion
29+
override def ivyDeps= super.ivyDeps() ++ Agg(
30+
ivy"org.scalameta::munit::${build.V.munitVersion}"
31+
)
32+
def forkArgs = super.forkArgs() ++ build.vecIncubatorFlag
33+
}
34+
2535
}

experiments/src/Vector.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package vecxt.experiments
2+
3+
import java.lang.foreign.MemorySegment
4+
import java.lang.foreign.Arena
5+
import java.lang.foreign.ValueLayout
6+
7+
opaque type DoubleVector = MemorySegment
8+
9+
export DoubleVector.*
10+
11+
object DoubleVector {
12+
13+
extension (v: DoubleVector) {
14+
15+
inline def apply(index: Long): Double =
16+
v.getAtIndex(ValueLayout.JAVA_DOUBLE, index)
17+
18+
inline def update(index: Long, value: Double): Unit =
19+
v.setAtIndex(ValueLayout.JAVA_DOUBLE, index, value)
20+
21+
inline def length: Long =
22+
v.byteSize() / ValueLayout.JAVA_DOUBLE.byteSize()
23+
}
24+
25+
}
26+
27+
extension (dv: DoubleVector.type)
28+
inline def apply(size: Long)(using arena: Arena): DoubleVector =
29+
arena.allocate(ValueLayout.JAVA_DOUBLE, size)
30+
31+
inline def apply(data: Seq[Double])(using arena: Arena): DoubleVector =
32+
arena.allocateFrom(
33+
ValueLayout.JAVA_DOUBLE,
34+
data*
35+
)

experiments/src/blis.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import vecxt.BoundsCheck.DoBoundsCheck.yes
2727
val vec1 = NArray[Double](1.0, 2.0, 1.0, 1.0)
2828
val vec2 = NArray[Double](0.0, 0.0, 0.0, 0.0)
2929

30-
3130
val arena = Arena.ofConfined()
3231
try {
3332
val a = arena.allocateFrom(

experiments/src/blis2.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.lang.foreign.{Arena, MemorySegment, ValueLayout}
2+
import blis.*
3+
import vecxt.all.*
4+
import narr.*
5+
import vecxt.BoundsCheck.DoBoundsCheck.yes
6+
7+
@main def testBlisNative =
8+
9+
println("Panama native memory test")
10+
11+
12+
val arena = Arena.ofAuto()
13+
// Allocate native memory for 4 doubles
14+
val nativeArr = arena.allocate(ValueLayout.JAVA_DOUBLE.byteSize() * 4)
15+
// Write values to native memory
16+
var i = 0
17+
while (i < 4) {
18+
nativeArr.setAtIndex(ValueLayout.JAVA_DOUBLE, i, (i + 1).toDouble)
19+
i += 1
20+
}
21+
// Read back and print
22+
i = 0
23+
while (i < 4) {
24+
val v = nativeArr.getAtIndex(ValueLayout.JAVA_DOUBLE, i)
25+
println(s"nativeArr($i) = $v")
26+
i += 1
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package vecxt.experiments
2+
3+
4+
import narr.*
5+
import java.lang.foreign.Arena
6+
7+
import vecxt.experiments.*
8+
import vecxt.experiments.DoubleVector.*
9+
10+
class SyntaxSuite extends munit.FunSuite:
11+
12+
given arena: Arena = Arena.global()
13+
14+
test("DoubleVector apply and update"):
15+
16+
val vec = DoubleVector(5)(using arena)
17+
vec(0) = 1.0
18+
vec(1) = 2.0
19+
vec(2) = 3.0
20+
vec(3) = 4.0
21+
vec(4) = 5.0
22+
23+
assertEquals(vec(0), 1.0)
24+
assertEquals(vec(1), 2.0)
25+
assertEquals(vec(2), 3.0)
26+
assertEquals(vec(3), 4.0)
27+
assertEquals(vec(4), 5.0)
28+
29+
println(vec)
30+
31+
32+
test("DoubleVector length"):
33+
34+
val vec = DoubleVector(10)(using arena)
35+
assertEquals(vec.length, 10L)
36+
37+
test("DoubleVector from Array"):
38+
val arr = Vector(1.0, 2.0, 3.0)
39+
val vec = DoubleVector(arr)(using arena)
40+
assertEquals(vec(0), 1.0)
41+
assertEquals(vec(1), 2.0)
42+
assertEquals(vec(2), 3.0)
43+
44+
end SyntaxSuite

generated/src/blis/cblas_h.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ public class cblas_h extends cblas_h$shared {
2020

2121
static final Arena LIBRARY_ARENA = Arena.ofAuto();
2222

23-
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup("/opt/homebrew/opt/blis/lib/libblis.dylib", LIBRARY_ARENA)
24-
.or(SymbolLookup.loaderLookup())
23+
24+
static {
25+
System.loadLibrary("blis");
26+
}
27+
28+
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
2529
.or(Linker.nativeLinker().defaultLookup());
2630

2731
private static final int CblasRowMajor = (int)101L;

justfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
set shell := ["zsh", "-cu"]
12

2-
MILL := "JAVA_OPTS=\"-Ddev.ludovic.netlib.blas.nativeLibPath=/opt/homebrew/Cellar/openblas/0.3.30/lib/libopenblas.dylib\" ./millw"
3+
MILL := "./millw"
34

45
format:
56
{{MILL}} mill.scalalib.scalafmt.ScalafmtModule/reformatAll __.sources
@@ -61,7 +62,8 @@ jextractBlis:
6162
--output generated/src \
6263
--include-function dgemv \
6364
-D FORCE_OPENBLAS_COMPLEX_STRUCT \
64-
-l :/opt/homebrew/opt/blis/lib/libblis.dylib \
65+
-l blis \
6566
-t blis \
67+
--use-system-load-library \
6668
@myInclude.txt \
6769
/opt/homebrew/opt/blis/include/blis/cblas.h

0 commit comments

Comments
 (0)