Skip to content

Commit f803edb

Browse files
committed
that... works?
1 parent e17fe02 commit f803edb

15 files changed

Lines changed: 11751 additions & 10 deletions

build.mill

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ trait Common extends ScalaModule with PublishModule with ScalafixModule {
5959

6060
}
6161

62-
val vecIncubatorFlag = Seq("""--add-modules=jdk.incubator.vector""")
62+
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"
65+
)
6366

6467
trait CommonJS extends ScalaJSModule {
6568
def scalaJSVersion = "1.19.0"
@@ -81,6 +84,10 @@ trait CommonTests extends TestModule.Munit {
8184
)
8285
}
8386

87+
object generated extends JavaModule {
88+
89+
}
90+
8491

8592
object vecxt extends CrossPlatform {
8693
trait Shared extends CrossPlatformScalaModule with Common {

experiments/package.mill

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ object `package` extends RootModule with ScalaModule {
1212
}
1313
def scalacOptions: T[Seq[String]] = Seq("-Xmax-inlines:10000")
1414
override def forkArgs: T[Seq[String]] = super.forkArgs() ++ build.vecIncubatorFlag
15-
override def moduleDeps: Seq[JavaModule] = Seq(build.vecxt.jvm)
15+
override def moduleDeps: Seq[JavaModule] = Seq(build.vecxt.jvm, build.generated)
16+
17+
override def mainClass = Some("testBlis")
1618
override def ivyDeps = super.ivyDeps() ++ Seq(
1719
ivy"com.lihaoyi::os-lib::0.10.4",
1820
ivy"io.github.quafadas::scautable::0.0.24",

experiments/src/blas.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
import java.lang.foreign.{Arena, MemorySegment, ValueLayout}
4+
import blas.*
5+
6+
7+
@main def testBlas() =
8+
val Layout = cblas_h.CblasColMajor()
9+
val transa = cblas_h.CblasNoTrans()
10+
11+
val m = 4 // Number of rows
12+
val n = 4 // Number of columns
13+
val lda = 4 // Leading dimension
14+
val incx = 1
15+
val incy = 1
16+
val alpha = 1.0
17+
val beta = 0.0
18+
19+
val arena = Arena.ofConfined()
20+
try {
21+
val a = arena.allocateFrom(
22+
ValueLayout.JAVA_DOUBLE,
23+
1.0, 2.0, 3.0, 4.0,
24+
1.0, 1.0, 1.0, 1.0,
25+
3.0, 4.0, 5.0, 6.0,
26+
5.0, 6.0, 7.0, 8.0
27+
)
28+
val x = arena.allocateFrom(
29+
ValueLayout.JAVA_DOUBLE,
30+
1.0, 2.0, 1.0, 1.0
31+
)
32+
val y = arena.allocate(ValueLayout.JAVA_DOUBLE, n)
33+
34+
cblas_h.cblas_dgemv(
35+
Layout, transa, m, n, alpha,
36+
a, lda, x, incx, beta, y, incy
37+
)
38+
39+
for i <- 0 until n do
40+
println(s"y$i = ${y.getAtIndex(ValueLayout.JAVA_DOUBLE, i)}")
41+
()
42+
}
43+
finally
44+
arena.close()

experiments/src/blis.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
import java.lang.foreign.{Arena, MemorySegment, ValueLayout}
4+
import blis.*
5+
6+
7+
@main def testBlis() =
8+
val Layout = blis.cblas_h.CblasColMajor
9+
val transa = blis.cblas_h.CblasNoTrans
10+
11+
val m = 4 // Number of rows
12+
val n = 4 // Number of columns
13+
val lda = 4 // Leading dimension
14+
val incx = 1
15+
val incy = 1
16+
val alpha = 1.0
17+
val beta = 0.0
18+
19+
val arena = Arena.ofConfined()
20+
try {
21+
val a = arena.allocateFrom(
22+
ValueLayout.JAVA_DOUBLE,
23+
1.0, 2.0, 3.0, 4.0,
24+
1.0, 1.0, 1.0, 1.0,
25+
3.0, 4.0, 5.0, 6.0,
26+
5.0, 6.0, 7.0, 8.0
27+
)
28+
val x = arena.allocateFrom(
29+
ValueLayout.JAVA_DOUBLE,
30+
1.0, 2.0, 1.0, 1.0
31+
)
32+
val y = arena.allocate(ValueLayout.JAVA_DOUBLE, n)
33+
34+
cblas_h.cblas_dgemv(
35+
Layout, transa, m, n, alpha,
36+
a, lda, x, incx, beta, y, incy
37+
)
38+
39+
for i <- 0 until n do
40+
println(s"y$i = ${y.getAtIndex(ValueLayout.JAVA_DOUBLE, i)}")
41+
()
42+
}
43+
finally
44+
arena.close()

experiments/src/mnist.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import vecxt.BoundsCheck
1616
//
1717

1818
@main def mnist =
19-
def traindata = CSV.resource("train_1.csv")
19+
def traindata = CSV.resource("train.csv")
2020

2121
val samplePlot = false
22-
val trainSize = 60000
22+
val trainSize = 5000
2323

2424
val labels = traindata.column["label"].map(_.toInt).toSeq.take(trainSize) // y data
2525
val others =
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Generated by jextract
2+
3+
package blas;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
public class cblas_h$shared {
16+
17+
cblas_h$shared() {
18+
// Should not be called directly
19+
}
20+
21+
public static final ValueLayout.OfBoolean C_BOOL = (ValueLayout.OfBoolean) Linker.nativeLinker().canonicalLayouts().get("bool");
22+
public static final ValueLayout.OfByte C_CHAR =(ValueLayout.OfByte)Linker.nativeLinker().canonicalLayouts().get("char");
23+
public static final ValueLayout.OfShort C_SHORT = (ValueLayout.OfShort) Linker.nativeLinker().canonicalLayouts().get("short");
24+
public static final ValueLayout.OfInt C_INT = (ValueLayout.OfInt) Linker.nativeLinker().canonicalLayouts().get("int");
25+
public static final ValueLayout.OfLong C_LONG_LONG = (ValueLayout.OfLong) Linker.nativeLinker().canonicalLayouts().get("long long");
26+
public static final ValueLayout.OfFloat C_FLOAT = (ValueLayout.OfFloat) Linker.nativeLinker().canonicalLayouts().get("float");
27+
public static final ValueLayout.OfDouble C_DOUBLE = (ValueLayout.OfDouble) Linker.nativeLinker().canonicalLayouts().get("double");
28+
public static final AddressLayout C_POINTER = ((AddressLayout) Linker.nativeLinker().canonicalLayouts().get("void*"))
29+
.withTargetLayout(MemoryLayout.sequenceLayout(java.lang.Long.MAX_VALUE, C_CHAR));
30+
public static final ValueLayout.OfLong C_LONG = (ValueLayout.OfLong) Linker.nativeLinker().canonicalLayouts().get("long");
31+
32+
static final boolean TRACE_DOWNCALLS = Boolean.getBoolean("jextract.trace.downcalls");
33+
34+
static void traceDowncall(String name, Object... args) {
35+
String traceArgs = Arrays.stream(args)
36+
.map(Object::toString)
37+
.collect(Collectors.joining(", "));
38+
System.out.printf("%s(%s)\n", name, traceArgs);
39+
}
40+
41+
static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fdesc) {
42+
try {
43+
return MethodHandles.lookup().findVirtual(fi, name, fdesc.toMethodType());
44+
} catch (ReflectiveOperationException ex) {
45+
throw new AssertionError(ex);
46+
}
47+
}
48+
49+
static MemoryLayout align(MemoryLayout layout, long align) {
50+
return switch (layout) {
51+
case PaddingLayout p -> p;
52+
case ValueLayout v -> v.withByteAlignment(align);
53+
case GroupLayout g -> {
54+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
55+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
56+
yield g instanceof StructLayout ?
57+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
58+
}
59+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
60+
};
61+
}
62+
}
63+

0 commit comments

Comments
 (0)