Skip to content

Commit 41e8101

Browse files
committed
Foreign (Function) Memory API
1 parent 1bd619c commit 41e8101

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Introducing layout reshaping:
2+
Provide an example that reshapes a hierarchical sequence layout.
3+
4+
## Using JDK 21
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java.modern.challenge</groupId>
5+
<artifactId>P160_IntroLayoutReshaping</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>22</maven.compiler.source>
11+
<maven.compiler.target>22</maven.compiler.target>
12+
</properties>
13+
<name>P160_IntroLayoutReshaping</name>
14+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package modern.challenge;
2+
3+
import java.lang.foreign.Arena;
4+
import java.lang.foreign.MemoryLayout;
5+
import java.lang.foreign.MemoryLayout.PathElement;
6+
import java.lang.foreign.MemorySegment;
7+
import java.lang.foreign.SequenceLayout;
8+
import java.lang.foreign.ValueLayout;
9+
import java.lang.invoke.VarHandle;
10+
11+
public class Main {
12+
13+
public static void main(String[] args) {
14+
15+
SequenceLayout innerSeq = MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_DOUBLE);
16+
SequenceLayout outerSeq = MemoryLayout.sequenceLayout(10, innerSeq);
17+
18+
VarHandle handle = outerSeq.varHandle(
19+
PathElement.sequenceElement(),
20+
PathElement.sequenceElement());
21+
22+
try (Arena arena = Arena.ofConfined()) {
23+
24+
MemorySegment segment = arena.allocate(outerSeq);
25+
26+
System.out.println("Outer: " + outerSeq.elementCount());
27+
System.out.println("Inner: " + innerSeq.elementCount());
28+
29+
for (int i = 0; i < outerSeq.elementCount(); i++) {
30+
for (int j = 0; j < innerSeq.elementCount(); j++) {
31+
handle.set(segment, 0L, i, j, Math.random());
32+
}
33+
}
34+
35+
for (int i = 0; i < outerSeq.elementCount(); i++) {
36+
System.out.print("\n-----" + i + "-----");
37+
for (int j = 0; j < innerSeq.elementCount(); j++) {
38+
System.out.printf("\nx = %.2f", handle.get(segment, 0L, i, j));
39+
}
40+
}
41+
}
42+
43+
// reshape
44+
SequenceLayout reshaped = outerSeq.reshape(25, 2);
45+
46+
VarHandle rhandle = reshaped.varHandle(
47+
PathElement.sequenceElement(),
48+
PathElement.sequenceElement());
49+
50+
try (Arena arena = Arena.ofConfined()) {
51+
52+
MemorySegment segment = arena.allocate(reshaped);
53+
54+
System.out.println("\n\nReshaped outerSeq: " + reshaped.elementCount());
55+
System.out.println("Reshaped innerSeq: "
56+
+ ((SequenceLayout) reshaped.select(
57+
PathElement.sequenceElement())).elementCount());
58+
59+
long outerSeqCount = reshaped.elementCount();
60+
long innerSeqCount = ((SequenceLayout) reshaped.select(
61+
PathElement.sequenceElement())).elementCount();
62+
63+
for (int i = 0; i < outerSeqCount; i++) {
64+
for (int j = 0; j < innerSeqCount; j++) {
65+
rhandle.set(segment, 0L, i, j, Math.random());
66+
}
67+
}
68+
69+
for (int i = 0; i < outerSeqCount; i++) {
70+
System.out.print("\n-----" + i + "-----");
71+
for (int j = 0; j < innerSeqCount; j++) {
72+
System.out.printf("\nx = %.2f", rhandle.get(segment, 0L, i, j));
73+
}
74+
}
75+
}
76+
}
77+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Introducing layout spreader:
2+
Provide a brief explanation and a simple example of using layout spreader (asSpreader()).
3+
4+
## Using JDK 21
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java.modern.challenge</groupId>
5+
<artifactId>P161_IntroLayoutSpreader</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>22</maven.compiler.source>
11+
<maven.compiler.target>22</maven.compiler.target>
12+
</properties>
13+
<name>P161_IntroLayoutSpreader</name>
14+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package modern.challenge;
2+
3+
import java.lang.foreign.Arena;
4+
import java.lang.foreign.MemoryLayout;
5+
import java.lang.foreign.MemoryLayout.PathElement;
6+
import java.lang.foreign.MemorySegment;
7+
import java.lang.foreign.SequenceLayout;
8+
import java.lang.foreign.ValueLayout;
9+
import java.lang.invoke.MethodHandle;
10+
import java.lang.invoke.VarHandle;
11+
12+
public class Main {
13+
14+
public static void main(String[] args) throws Throwable {
15+
16+
SequenceLayout innerSeq = MemoryLayout.sequenceLayout(5, ValueLayout.JAVA_DOUBLE);
17+
SequenceLayout outerSeq = MemoryLayout.sequenceLayout(10, innerSeq);
18+
19+
VarHandle handle = outerSeq.varHandle(
20+
PathElement.sequenceElement(),
21+
PathElement.sequenceElement());
22+
23+
try (Arena arena = Arena.ofConfined()) {
24+
25+
MemorySegment segment = arena.allocate(outerSeq);
26+
27+
System.out.println("Outer: " + outerSeq.elementCount());
28+
System.out.println("Inner: " + innerSeq.elementCount());
29+
30+
for (int i = 0; i < outerSeq.elementCount(); i++) {
31+
for (int j = 0; j < innerSeq.elementCount(); j++) {
32+
handle.set(segment, 0L, i, j, Math.random());
33+
}
34+
}
35+
36+
for (int i = 0; i < outerSeq.elementCount(); i++) {
37+
System.out.print("\n-----" + i + "-----");
38+
for (int j = 0; j < innerSeq.elementCount(); j++) {
39+
System.out.printf("\nx = %.5f", handle.get(segment, 0L, i, j));
40+
}
41+
}
42+
43+
MethodHandle mHandle = outerSeq.sliceHandle(
44+
PathElement.sequenceElement(),
45+
PathElement.sequenceElement()
46+
);
47+
48+
System.out.println();
49+
System.out.println();
50+
51+
// no spreader
52+
// MemorySegment ms = (MemorySegment) mHandle.invokeExact(segment, 7L, 3L);
53+
54+
// with spreader
55+
MemorySegment ms = (MemorySegment) mHandle
56+
.asSpreader(Long[].class, 2).invokeExact(segment, 0L, new Long[]{7L, 3L});
57+
58+
System.out.println(ms.get(ValueLayout.JAVA_DOUBLE, 0));
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)